keryx 0.41.0 → 0.41.1

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.
Files changed (2) hide show
  1. package/initializers/db.ts +54 -11
  2. package/package.json +1 -1
@@ -1,6 +1,6 @@
1
1
  import fs from "node:fs";
2
2
  import { unlink } from "node:fs/promises";
3
- import { $ } from "bun";
3
+ import { $, Glob } from "bun";
4
4
  import { type Config as DrizzleMigrateConfig } from "drizzle-kit";
5
5
  import { DefaultLogger, type LogWriter, sql } from "drizzle-orm";
6
6
  import { drizzle } from "drizzle-orm/node-postgres";
@@ -18,6 +18,29 @@ import {
18
18
 
19
19
  const namespace = "db";
20
20
 
21
+ const SCHEMA_DIR = "schema";
22
+
23
+ /**
24
+ * Check whether the project has any drizzle schema files to generate migrations from.
25
+ * A freshly-scaffolded project (or one whose last schema file was deleted) has an
26
+ * empty `schema/` directory, which `drizzle-kit generate` treats as a fatal error.
27
+ *
28
+ * @param schemaDir - Absolute path to the project's `schema/` directory. A missing
29
+ * directory counts as empty rather than an error.
30
+ * @returns `true` when at least one non-dotfile `.ts`/`.tsx`/`.js` file exists in the tree.
31
+ */
32
+ async function hasSchemaFiles(schemaDir: string) {
33
+ if (!fs.existsSync(schemaDir)) return false;
34
+
35
+ const glob = new Glob("**/*.{ts,tsx,js,mjs,cjs}");
36
+ for await (const file of glob.scan(schemaDir)) {
37
+ if (path.basename(file).startsWith(".")) continue;
38
+ return true;
39
+ }
40
+
41
+ return false;
42
+ }
43
+
21
44
  declare module "keryx" {
22
45
  export interface API {
23
46
  [namespace]: Awaited<ReturnType<DB["initialize"]>>;
@@ -107,32 +130,52 @@ export class DB extends Initializer {
107
130
  /**
108
131
  * Generate migrations for the database schema.
109
132
  * Learn more @ https://orm.drizzle.team/kit-docs/overview
133
+ *
134
+ * An empty `schema/` directory is a legitimate state for a new project, so this
135
+ * is a no-op (rather than an error) when there is nothing to generate from.
136
+ *
137
+ * @throws {TypedError} With `ErrorType.SERVER_INITIALIZATION` if `drizzle-kit generate` fails.
110
138
  */
111
139
  async generateMigrations() {
140
+ const schemaDir = path.join(api.rootDir, SCHEMA_DIR);
141
+ const migrationsDir = path.join(api.rootDir, "drizzle");
142
+
143
+ if (!(await hasSchemaFiles(schemaDir))) {
144
+ logger.info(
145
+ `no schema files found in ${SCHEMA_DIR}/ - nothing to generate`,
146
+ );
147
+ return;
148
+ }
149
+
150
+ // Paths stay relative — drizzle-kit concatenates `out` onto "./" when reading
151
+ // existing snapshots, so an absolute path breaks on any non-empty migrations
152
+ // folder. The child process is run with cwd set to the project root instead.
112
153
  const migrationConfig: DrizzleMigrateConfig = {
113
154
  dialect: "postgresql" as const,
114
- schema: path.join("schema", "*"),
155
+ schema: path.join(SCHEMA_DIR, "*"),
115
156
  dbCredentials: {
116
157
  url: config.database.connectionString,
117
158
  },
118
- out: path.join("drizzle"),
159
+ out: "drizzle",
119
160
  };
120
161
 
121
162
  const fileContent = `export default ${JSON.stringify(migrationConfig, null, 2)}`;
122
- const tmpfilePath = path.join(api.rootDir, "drizzle", "config.tmp.ts");
163
+ const tmpfilePath = path.join(migrationsDir, "config.tmp.ts");
123
164
 
124
165
  try {
125
166
  await Bun.write(tmpfilePath, fileContent);
167
+ // .nothrow() so a failing drizzle-kit surfaces as a TypedError with its
168
+ // stderr, rather than a bare ShellError from the tagged template.
126
169
  const { exitCode, stdout, stderr } =
127
- await $`bun drizzle-kit generate --config ${tmpfilePath}`;
170
+ await $`bun drizzle-kit generate --config ${tmpfilePath}`
171
+ .cwd(api.rootDir)
172
+ .nothrow();
128
173
  logger.trace(stdout.toString());
129
174
  if (exitCode !== 0) {
130
- {
131
- throw new TypedError({
132
- message: `Failed to generate migrations: ${stderr.toString()}`,
133
- type: ErrorType.SERVER_INITIALIZATION,
134
- });
135
- }
175
+ throw new TypedError({
176
+ message: `Failed to generate migrations: ${stderr.toString()}`,
177
+ type: ErrorType.SERVER_INITIALIZATION,
178
+ });
136
179
  }
137
180
  } finally {
138
181
  const filePointer = Bun.file(tmpfilePath);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "keryx",
3
- "version": "0.41.0",
3
+ "version": "0.41.1",
4
4
  "module": "index.ts",
5
5
  "type": "module",
6
6
  "license": "MIT",