Skip to content

Commit

Permalink
Merge pull request #19 from alexfauquette/explor-tabs
Browse files Browse the repository at this point in the history
  • Loading branch information
alexfauquette authored Dec 13, 2023
2 parents 2b6afd3 + 0a29a9b commit 122c61c
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 29 deletions.
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;
}
}

0 comments on commit 122c61c

Please sign in to comment.