From 77a6b6e7f5e8b78d58494825a0b5e34242b8e142 Mon Sep 17 00:00:00 2001 From: dswbx Date: Thu, 5 Dec 2024 10:37:05 +0100 Subject: [PATCH] =?UTF-8?q?fix=20repository's=20findOne=20to=20allow=20off?= =?UTF-8?q?set=20and=20sort=20=E2=80=93=20fixes=20module=20manager's=20con?= =?UTF-8?q?fig=20retrieval?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/__test__/ModuleManager.spec.ts | 58 +++++++++++++++++++++-- app/src/core/utils/typebox/index.ts | 4 +- app/src/data/entities/query/Repository.ts | 17 +++---- app/src/modules/ModuleManager.ts | 13 ++++- 4 files changed, 73 insertions(+), 19 deletions(-) diff --git a/app/__test__/ModuleManager.spec.ts b/app/__test__/ModuleManager.spec.ts index 38e1670..26b2b9a 100644 --- a/app/__test__/ModuleManager.spec.ts +++ b/app/__test__/ModuleManager.spec.ts @@ -1,6 +1,7 @@ import { describe, expect, test } from "bun:test"; import { mark, stripMark } from "../src/core/utils"; -import { ModuleManager } from "../src/modules/ModuleManager"; +import { entity, text } from "../src/data"; +import { ModuleManager, getDefaultConfig } from "../src/modules/ModuleManager"; import { CURRENT_VERSION, TABLE_NAME } from "../src/modules/migrations"; import { getDummyConnection } from "./helper"; @@ -29,7 +30,19 @@ describe("ModuleManager", async () => { const mm = new ModuleManager(c.dummyConnection); await mm.build(); const version = mm.version(); - const json = mm.configs(); + const configs = mm.configs(); + const json = stripMark({ + ...configs, + data: { + ...configs.data, + basepath: "/api/data2", + entities: { + test: entity("test", { + content: text() + }).toJSON() + } + } + }); //const { version, ...json } = mm.toJSON() as any; const c2 = getDummyConnection(); @@ -37,13 +50,48 @@ describe("ModuleManager", async () => { const mm2 = new ModuleManager(c2.dummyConnection, { initial: { version, ...json } }); await mm2.syncConfigTable(); await db - .updateTable(TABLE_NAME) - .set({ json: JSON.stringify(json), version: CURRENT_VERSION }) + .insertInto(TABLE_NAME) + .values({ type: "config", json: JSON.stringify(json), version: CURRENT_VERSION }) .execute(); await mm2.build(); - expect(json).toEqual(mm2.configs()); + expect(json).toEqual(stripMark(mm2.configs())); + }); + + test("s3.1: (fetch) config given, table exists, version matches", async () => { + const configs = getDefaultConfig(); + const json = { + ...configs, + data: { + ...configs.data, + basepath: "/api/data2", + entities: { + test: entity("test", { + content: text() + }).toJSON() + } + } + }; + //const { version, ...json } = mm.toJSON() as any; + + const { dummyConnection } = getDummyConnection(); + const db = dummyConnection.kysely; + const mm2 = new ModuleManager(dummyConnection); + await mm2.syncConfigTable(); + // assume an initial version + await db.insertInto(TABLE_NAME).values({ type: "config", json: null, version: 1 }).execute(); + await db + .insertInto(TABLE_NAME) + .values({ type: "config", json: JSON.stringify(json), version: CURRENT_VERSION }) + .execute(); + + await mm2.build(); + + expect(stripMark(json)).toEqual(stripMark(mm2.configs())); + expect(mm2.configs().data.entities.test).toBeDefined(); + expect(mm2.configs().data.entities.test.fields.content).toBeDefined(); + expect(mm2.get("data").toJSON().entities.test.fields.content).toBeDefined(); }); test("s4: config given, table exists, version outdated, migrate", async () => { diff --git a/app/src/core/utils/typebox/index.ts b/app/src/core/utils/typebox/index.ts index 2b923c1..2e08d7a 100644 --- a/app/src/core/utils/typebox/index.ts +++ b/app/src/core/utils/typebox/index.ts @@ -72,10 +72,10 @@ export class TypeInvalidError extends Error { } } -export function stripMark(obj: any) { +export function stripMark(obj: O) { const newObj = cloneDeep(obj); mark(newObj, false); - return newObj; + return newObj as O; } export function mark(obj: any, validated = true) { diff --git a/app/src/data/entities/query/Repository.ts b/app/src/data/entities/query/Repository.ts index f296adf..8156869 100644 --- a/app/src/data/entities/query/Repository.ts +++ b/app/src/data/entities/query/Repository.ts @@ -162,8 +162,7 @@ export class Repository implements EmitsEve protected async performQuery(qb: RepositoryQB): Promise { const entity = this.entity; const compiled = qb.compile(); - /*const { sql, parameters } = qb.compile(); - console.log("many", sql, parameters);*/ + //console.log("performQuery", compiled.sql, compiled.parameters); const start = performance.now(); const selector = (as = "count") => this.conn.fn.countAll().as(as); @@ -263,6 +262,7 @@ export class Repository implements EmitsEve qb = qb.orderBy(aliased(options.sort.by), options.sort.dir); } + //console.log("options", { _options, options, exclude_options }); return { qb, options }; } @@ -286,14 +286,11 @@ export class Repository implements EmitsEve where: RepoQuery["where"], _options?: Partial> ): Promise> { - const { qb, options } = this.buildQuery( - { - ..._options, - where, - limit: 1 - }, - ["offset", "sort"] - ); + const { qb, options } = this.buildQuery({ + ..._options, + where, + limit: 1 + }); return this.single(qb, options) as any; } diff --git a/app/src/modules/ModuleManager.ts b/app/src/modules/ModuleManager.ts index b365fdd..5da0dc2 100644 --- a/app/src/modules/ModuleManager.ts +++ b/app/src/modules/ModuleManager.ts @@ -2,7 +2,15 @@ import { Diff } from "@sinclair/typebox/value"; import { Guard } from "auth"; import { BkndError, DebugLogger, Exception, isDebug } from "core"; import { EventManager } from "core/events"; -import { Default, type Static, StringEnum, Type, objectEach, transformObject } from "core/utils"; +import { + Default, + type Static, + StringEnum, + Type, + objectEach, + stripMark, + transformObject +} from "core/utils"; import { type Connection, EntityManager, @@ -130,7 +138,7 @@ export class ModuleManager { if ("version" in options.initial) { const { version, ...initialConfig } = options.initial; this._version = version; - initial = initialConfig; + initial = stripMark(initialConfig); this._booted_with = "provided"; } else { @@ -393,6 +401,7 @@ export class ModuleManager { this.logger.context("build").log("version", this.version()); this.logger.log("booted with", this._booted_with); + // @todo: check this, because you could start without an initial config if (this.version() !== CURRENT_VERSION) { await this.syncConfigTable(); }