keryx 0.42.7 → 0.43.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.ts +1 -1
- package/initializers/db.ts +32 -1
- package/package.json +1 -1
- package/util/glob.ts +54 -0
package/index.ts
CHANGED
|
@@ -68,7 +68,7 @@ export type {
|
|
|
68
68
|
export { buildProgram } from "./util/cli";
|
|
69
69
|
export { deepMerge, deepMergeDefaults, loadFromEnvIfSet } from "./util/config";
|
|
70
70
|
export { getValidTypes } from "./util/generate";
|
|
71
|
-
export { globLoader } from "./util/glob";
|
|
71
|
+
export { globLoader, globModuleExports } from "./util/glob";
|
|
72
72
|
export { type PaginatedResult, paginate } from "./util/pagination";
|
|
73
73
|
export { safeCompare } from "./util/safeCompare";
|
|
74
74
|
export type { JSONSchema } from "./util/swaggerSchemaGenerator";
|
package/initializers/db.ts
CHANGED
|
@@ -2,9 +2,10 @@ import fs from "node:fs";
|
|
|
2
2
|
import { unlink } from "node:fs/promises";
|
|
3
3
|
import { $, Glob } from "bun";
|
|
4
4
|
import { type Config as DrizzleMigrateConfig } from "drizzle-kit";
|
|
5
|
-
import { DefaultLogger, type LogWriter, sql } from "drizzle-orm";
|
|
5
|
+
import { DefaultLogger, is, type LogWriter, sql } from "drizzle-orm";
|
|
6
6
|
import { drizzle } from "drizzle-orm/node-postgres";
|
|
7
7
|
import { migrate } from "drizzle-orm/node-postgres/migrator";
|
|
8
|
+
import { PgTable } from "drizzle-orm/pg-core";
|
|
8
9
|
import path from "path";
|
|
9
10
|
import { Pool } from "pg";
|
|
10
11
|
import { api, logger } from "../api";
|
|
@@ -15,6 +16,7 @@ import {
|
|
|
15
16
|
formatConnectionStringForLogging,
|
|
16
17
|
throwConnectionError,
|
|
17
18
|
} from "../util/connectionString";
|
|
19
|
+
import { globModuleExports } from "../util/glob";
|
|
18
20
|
|
|
19
21
|
const namespace = "db";
|
|
20
22
|
|
|
@@ -41,6 +43,29 @@ async function hasSchemaFiles(schemaDir: string) {
|
|
|
41
43
|
return false;
|
|
42
44
|
}
|
|
43
45
|
|
|
46
|
+
/**
|
|
47
|
+
* Build a registry of the project's Drizzle tables so the framework and plugins can
|
|
48
|
+
* enumerate the database at runtime. Drizzle itself only ever sees the tables an action
|
|
49
|
+
* imports directly, which leaves generic tooling — an admin dashboard, a schema
|
|
50
|
+
* explorer — with nothing to introspect.
|
|
51
|
+
*
|
|
52
|
+
* Every `.ts` file under the directory is imported and its exports filtered down to
|
|
53
|
+
* Drizzle tables. Keys are the *export* names (`users`), which may differ from the SQL
|
|
54
|
+
* table names (`"users"`); use `getTableName()` when the SQL name is what you need.
|
|
55
|
+
*
|
|
56
|
+
* @param schemaDir - Absolute path to the project's `schema/` directory. A missing
|
|
57
|
+
* directory yields an empty registry, so `schema/` remains an optional convention and
|
|
58
|
+
* schema-less projects are unaffected.
|
|
59
|
+
* @returns Map of export name to Drizzle table, ready to hand to `db.select().from()`
|
|
60
|
+
* or `getTableConfig()`.
|
|
61
|
+
* @throws {TypedError} With `ErrorType.SERVER_INITIALIZATION` if a schema file fails to import.
|
|
62
|
+
*/
|
|
63
|
+
export async function loadSchema(schemaDir: string) {
|
|
64
|
+
return globModuleExports<PgTable>(schemaDir, (value): value is PgTable =>
|
|
65
|
+
is(value, PgTable),
|
|
66
|
+
);
|
|
67
|
+
}
|
|
68
|
+
|
|
44
69
|
declare module "keryx" {
|
|
45
70
|
export interface API {
|
|
46
71
|
[namespace]: Awaited<ReturnType<DB["initialize"]>>;
|
|
@@ -53,6 +78,10 @@ export class DB extends Initializer {
|
|
|
53
78
|
}
|
|
54
79
|
|
|
55
80
|
async initialize() {
|
|
81
|
+
const schema = await loadSchema(path.join(api.rootDir, SCHEMA_DIR));
|
|
82
|
+
const tableCount = Object.keys(schema).length;
|
|
83
|
+
logger.debug(`loaded ${tableCount} table(s) from ${SCHEMA_DIR}/`);
|
|
84
|
+
|
|
56
85
|
const dbContainer = {} as {
|
|
57
86
|
db: ReturnType<typeof drizzle>;
|
|
58
87
|
pool: InstanceType<typeof Pool>;
|
|
@@ -61,6 +90,7 @@ export class DB extends Initializer {
|
|
|
61
90
|
{
|
|
62
91
|
generateMigrations: this.generateMigrations,
|
|
63
92
|
clearDatabase: this.clearDatabase,
|
|
93
|
+
schema,
|
|
64
94
|
},
|
|
65
95
|
dbContainer,
|
|
66
96
|
);
|
|
@@ -79,6 +109,7 @@ export class DB extends Initializer {
|
|
|
79
109
|
}
|
|
80
110
|
|
|
81
111
|
api.db.db = drizzle(api.db.pool, {
|
|
112
|
+
schema: api.db.schema,
|
|
82
113
|
logger: new DefaultLogger({ writer: new DrizzleLogger() }),
|
|
83
114
|
});
|
|
84
115
|
|
package/package.json
CHANGED
package/util/glob.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
1
2
|
import { Glob } from "bun";
|
|
2
3
|
import path from "path";
|
|
3
4
|
import { api } from "../api";
|
|
@@ -45,3 +46,56 @@ export async function globLoader<T>(searchDir: string) {
|
|
|
45
46
|
|
|
46
47
|
return results;
|
|
47
48
|
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Auto-discover exported *values* from `.ts`/`.tsx` files in a directory, keyed by export name.
|
|
52
|
+
*
|
|
53
|
+
* Where {@link globLoader} instantiates every exported class, this returns the exports
|
|
54
|
+
* themselves. Use it for modules that export plain objects rather than classes — Drizzle
|
|
55
|
+
* table definitions, for example, which cannot be constructed with `new`.
|
|
56
|
+
*
|
|
57
|
+
* @param searchDir - Absolute path, or a path resolved from `api.rootDir`, to scan. A missing
|
|
58
|
+
* directory yields an empty registry instead of throwing, so optional project conventions
|
|
59
|
+
* (like `schema/`) stay optional.
|
|
60
|
+
* @param predicate - Type guard selecting which exports to keep; rejected exports are skipped,
|
|
61
|
+
* which lets schema files freely export helpers, types, and constants alongside tables.
|
|
62
|
+
* @returns Object mapping export name to value. When two files export the same name, whichever
|
|
63
|
+
* the glob visits last wins.
|
|
64
|
+
* @throws {TypedError} With `ErrorType.SERVER_INITIALIZATION` if a file fails to import.
|
|
65
|
+
*/
|
|
66
|
+
export async function globModuleExports<T>(
|
|
67
|
+
searchDir: string,
|
|
68
|
+
predicate: (value: unknown) => value is T,
|
|
69
|
+
) {
|
|
70
|
+
const results: Record<string, T> = {};
|
|
71
|
+
const dir = path.isAbsolute(searchDir)
|
|
72
|
+
? searchDir
|
|
73
|
+
: path.join(api.rootDir, searchDir);
|
|
74
|
+
|
|
75
|
+
if (!fs.existsSync(dir)) return results;
|
|
76
|
+
|
|
77
|
+
const glob = new Glob("**/*.{ts,tsx}");
|
|
78
|
+
|
|
79
|
+
for await (const file of glob.scan(dir)) {
|
|
80
|
+
if (file.startsWith(".")) continue;
|
|
81
|
+
|
|
82
|
+
const fullPath = path.join(dir, file);
|
|
83
|
+
let modules: Record<string, unknown>;
|
|
84
|
+
|
|
85
|
+
try {
|
|
86
|
+
modules = (await import(fullPath)) as Record<string, unknown>;
|
|
87
|
+
} catch (error) {
|
|
88
|
+
throw new TypedError({
|
|
89
|
+
message: `Error loading from ${dir} - ${file} - ${error}`,
|
|
90
|
+
type: ErrorType.SERVER_INITIALIZATION,
|
|
91
|
+
cause: error,
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
for (const [name, value] of Object.entries(modules)) {
|
|
96
|
+
if (predicate(value)) results[name] = value;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
return results;
|
|
101
|
+
}
|