Skip to content

Commit

Permalink
implementing createPgDbApi
Browse files Browse the repository at this point in the history
  • Loading branch information
JeromeBu committed Jun 28, 2024
1 parent f1eb3a1 commit 3e6b988
Show file tree
Hide file tree
Showing 6 changed files with 540 additions and 222 deletions.
17 changes: 13 additions & 4 deletions api/src/core/adapters/dbApi/createGitDbApi.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
import type { DbApi, Db } from "../../ports/DbApi";
import { gitSsh } from "../../../tools/gitSsh";
import { Deferred } from "evt/tools/Deferred";
import { type CompiledData, compiledDataPrivateToPublic } from "../../ports/CompileData";
import * as fs from "fs";
import { join as pathJoin } from "path";
import type { ReturnType } from "tsafe";
import type { DbApi, Db } from "../../ports/DbApi";
import { gitSsh } from "../../../tools/gitSsh";
import { type CompiledData, compiledDataPrivateToPublic } from "../../ports/CompileData";
import type { Db, DbApi } from "../../ports/DbApi";

export const compiledDataBranch = "compiled-data";
const compiledDataPrivateJsonRelativeFilePath = "compiledData_private.json";
Expand Down Expand Up @@ -177,3 +174,15 @@ export function createGitDbApi(params: GitDbApiParams): Db.DbApiAndInitializeCac
initializeDbApiCache
};
}

type Original = { salut: string };

type OnlyOriginal = Original & { [K in keyof Original]: Original[K] } & {};

const o: Original = { salut: "salut" };
const x = { salut: "salut", truc: "truc" };

const b: OnlyOriginal = { ...x };
const b2: OnlyOriginal = { ...o };

console.log({ b, o, b2 });
111 changes: 111 additions & 0 deletions api/src/core/adapters/dbApi/createPgDbApi.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
import { Kysely } from "kysely";
import type { Db, DbApi } from "../../ports/DbApi";
import { Database } from "./kysely/kysely.database";
import { createPgDialect } from "./kysely/kysely.dialect";

export type PgConfig = {
dbUrl: string;
};

export function createPgDbApi(params: PgConfig): {
dbApi: DbApi;
initializeDbApiCache: () => Promise<void>;
} {
const db = new Kysely<Database>({ dialect: createPgDialect(params.dbUrl) });

const dbApi: DbApi = {
"fetchCompiledData": () => {
throw new Error("Not implemented");
},
"fetchDb": async () => {
const agentRows: Db.AgentRow[] = await db
.selectFrom("agents")
.selectAll()
.execute()
.then(rows =>
rows.map(row => ({
...row,
about: row.about ?? undefined
}))
);

const softwareRows: Db.SoftwareRow[] = await db
.selectFrom("softwares")
.selectAll()
.execute()
.then(rows =>
rows.map(row => ({
...row,
dereferencing: row.dereferencing ?? undefined,
parentSoftwareWikidataId: row.parentSoftwareWikidataId ?? undefined,
externalId: row.externalId ?? undefined,
externalDataOrigin: row.externalDataOrigin ?? undefined,
comptoirDuLibreId: row.comptoirDuLibreId ?? undefined,
catalogNumeriqueGouvFrId: row.catalogNumeriqueGouvFrId ?? undefined,
generalInfoMd: row.generalInfoMd ?? undefined,
logoUrl: row.logoUrl ?? undefined
}))
);

const softwareReferentRows: Db.SoftwareReferentRow[] = await db
.selectFrom("software_referents as r")
.innerJoin("agents as a", "r.agentId", "a.id")
.select(["softwareId", "isExpert", "serviceUrl", "useCaseDescription", "a.email as agentEmail"])
.execute()
.then(rows => rows.map(row => ({ ...row, serviceUrl: row.serviceUrl ?? undefined })));

const softwareUserRows: Db.SoftwareUserRow[] = await db
.selectFrom("software_users as u")
.innerJoin("agents as a", "u.agentId", "a.id")
.select(["softwareId", "a.email as agentEmail", "useCaseDescription", "os", "version", "serviceUrl"])
.execute()
.then(rows =>
rows.map(row => ({
...row,
os: row.os ?? undefined,
serviceUrl: row.serviceUrl ?? undefined
}))
);

const instanceRows: Db.InstanceRow[] = await db
.selectFrom("instances")
.selectAll()
.execute()
.then(rows =>
rows.map(row => ({
...row,
publicUrl: row.publicUrl ?? undefined
}))
);

return {
agentRows,
softwareRows,
softwareReferentRows,
softwareUserRows,
instanceRows
};
},
"updateDb": async ({ commitMessage, newDb }) => {
throw new Error("Not implemented");
},
"updateCompiledData": async ({ newCompiledData, commitMessage }) => {
throw new Error("Not implemented");
}
};

const initializeDbApiCache = async () => {
const start = Date.now();

console.log("Starting dbApi cache initialization...");

// TODO

console.log(`dbApi cache initialization done in ${Date.now() - start}ms`);
};

return {
dbApi,
initializeDbApiCache
};
}
Empty file.
214 changes: 0 additions & 214 deletions api/src/core/adapters/dbApi/postgresDbApi.ts

This file was deleted.

2 changes: 1 addition & 1 deletion api/src/core/usecases/readWriteSillData/thunks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -800,7 +800,7 @@ const privateThunks = {

try {
await Promise.all([
dbApi.updateDb({ m, commitMessage }),
dbApi.updateDb({ newDb, commitMessage }),
dbApi.updateCompiledData({ newCompiledData, commitMessage })
]);
} catch (error) {
Expand Down
Loading

0 comments on commit 3e6b988

Please sign in to comment.