Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

explore tabs #19

Merged
merged 1 commit into from
Dec 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions app/tables/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { getTable, listTables } from "@/repository/database";
import Link from "next/link";

export default async function Dossiers({
searchParams,
}: {
searchParams: { table: string };
}) {
const tables = await listTables();
const dossier = await getTable(searchParams.table ?? "");
// TODO: match old website url ":id/dossiers/:id"
return (
<div style={{ display: "flex" }}>
<ul>
{tables.map((tableName: string) => (
<li key={tableName}>
<a href={`/tables/?table=${tableName}`}>{tableName}</a>
</li>
))}
</ul>
<div>
<p>{searchParams.table}</p>
<pre>{JSON.stringify(dossier, null, 2)}</pre>
</div>
</div>
);
}
73 changes: 44 additions & 29 deletions repository/database.ts
Original file line number Diff line number Diff line change
@@ -1,39 +1,54 @@
import knex from 'knex';
import config from './knexfile';
import knex from "knex";
import config from "./knexfile";

const db = knex(config.development);

export async function listTables() {
try {
const result = await db.raw("SELECT table_name FROM information_schema.tables WHERE table_schema='public'");
return result.rows.map((row: any) => row.table_name);
} catch (error) {
console.error('Error listing tables:', error);
throw error;
}
try {
const result = await db.raw(
"SELECT table_name FROM information_schema.tables WHERE table_schema='public'"
);
return result.rows.map((row: any) => row.table_name).sort();
} catch (error) {
console.error("Error listing tables:", error);
throw error;
}
}

interface DossierRow {
uid: string;
xsiType: string;
legislature: string;
senatChemin: string;
titre: string;
titreChemin: string;
theme: string | null;
codeProcedure: string;
libelleProcedure: string;
causeFusionDossier: string | null;
dossierAbsorbantRefUid: string | null;
organeRefUid: string | null;
uid: string;
xsiType: string;
legislature: string;
senatChemin: string;
titre: string;
titreChemin: string;
theme: string | null;
codeProcedure: string;
libelleProcedure: string;
causeFusionDossier: string | null;
dossierAbsorbantRefUid: string | null;
organeRefUid: string | null;
}

export async function getDossiers(limit = 10): Promise<DossierRow[]> {
try {
const rows = await db.select('*').from('Dossier').limit(limit);
return rows;
} catch (error) {
console.error('Error fetching rows from Dossier:', error);
throw error;
}
}
try {
const rows = await db.select("*").from("Dossier").limit(limit);
return rows;
} catch (error) {
console.error("Error fetching rows from Dossier:", error);
throw error;
}
}

export async function getTable(
table: string,
limit = 10
): Promise<DossierRow[]> {
try {
const rows = await db.select("*").from(table).limit(limit);
return rows;
} catch (error) {
console.error("Error fetching rows from Dossier:", error);
throw error;
}
}