@seekdb/prisma-adapter 1.2.0 → 1.4.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/dist/cli.d.ts ADDED
@@ -0,0 +1,12 @@
1
+ #!/usr/bin/env node
2
+ /**
3
+ * CLI for Prisma migrations on seekdb Embedded.
4
+ * Aligns with Prisma CLI: migrate deploy (apply), migrate dev (generate + apply).
5
+ * Run from project root (where prisma.config.ts / prisma/migrations live).
6
+ *
7
+ * Usage:
8
+ * seekdb-prisma-migrate migrate deploy
9
+ * seekdb-prisma-migrate migrate dev [--name <migration_name>]
10
+ */
11
+ export {};
12
+ //# sourceMappingURL=cli.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"cli.d.ts","sourceRoot":"","sources":["../src/cli.ts"],"names":[],"mappings":";AACA;;;;;;;;GAQG"}
package/dist/cli.js ADDED
@@ -0,0 +1,74 @@
1
+ #!/usr/bin/env node
2
+ /**
3
+ * CLI for Prisma migrations on seekdb Embedded.
4
+ * Aligns with Prisma CLI: migrate deploy (apply), migrate dev (generate + apply).
5
+ * Run from project root (where prisma.config.ts / prisma/migrations live).
6
+ *
7
+ * Usage:
8
+ * seekdb-prisma-migrate migrate deploy
9
+ * seekdb-prisma-migrate migrate dev [--name <migration_name>]
10
+ */
11
+ import { createRequire } from "module";
12
+ try {
13
+ createRequire(import.meta.url)("dotenv/config");
14
+ }
15
+ catch {
16
+ /* dotenv optional */
17
+ }
18
+ import { generateMigrations, applyMigrations } from "./index.js";
19
+ const cwd = process.cwd();
20
+ function getArg(name) {
21
+ const argv = process.argv;
22
+ const i = argv.indexOf(name);
23
+ if (i !== -1 && argv[i + 1])
24
+ return argv[i + 1];
25
+ return undefined;
26
+ }
27
+ async function runDeploy() {
28
+ const loadSeekdb = new Function("return import('seekdb')");
29
+ const { SeekdbClient } = await loadSeekdb();
30
+ const client = new SeekdbClient({
31
+ path: process.env.SEEKDB_PATH ?? "./seekdb.db",
32
+ database: process.env.SEEKDB_DATABASE ?? "test",
33
+ });
34
+ await applyMigrations(client, { cwd });
35
+ await client.close();
36
+ console.log("Migrations applied.");
37
+ }
38
+ function runDev() {
39
+ const name = getArg("--name") ??
40
+ process.env.MIGRATION_NAME ??
41
+ "schema_update";
42
+ const result = generateMigrations({
43
+ cwd,
44
+ incrementalName: name.replace(/[^a-zA-Z0-9_]/g, "_"),
45
+ shadowDatabaseUrl: process.env.SHADOW_DATABASE_URL,
46
+ });
47
+ if (result.created)
48
+ console.log(`Created migration: ${result.created}`);
49
+ else if (result.hasInit && !result.hasSchemaChanges && process.env.SHADOW_DATABASE_URL)
50
+ console.log("No schema changes detected.");
51
+ else if (result.hasInit)
52
+ console.log("Migrations exist. Set SHADOW_DATABASE_URL and run again to add schema changes.");
53
+ }
54
+ async function main() {
55
+ const argv = process.argv.slice(2);
56
+ const sub = argv[0];
57
+ const cmd = argv[1];
58
+ if (sub === "migrate" && cmd === "deploy") {
59
+ await runDeploy();
60
+ return;
61
+ }
62
+ if (sub === "migrate" && cmd === "dev") {
63
+ runDev();
64
+ await runDeploy();
65
+ return;
66
+ }
67
+ console.error(`Usage: seekdb-prisma-migrate migrate deploy | migrate dev [--name <name>]`);
68
+ process.exit(1);
69
+ }
70
+ main().catch((err) => {
71
+ console.error(err);
72
+ process.exit(1);
73
+ });
74
+ //# sourceMappingURL=cli.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"cli.js","sourceRoot":"","sources":["../src/cli.ts"],"names":[],"mappings":";AACA;;;;;;;;GAQG;AAEH,OAAO,EAAE,aAAa,EAAE,MAAM,QAAQ,CAAC;AACvC,IAAI,CAAC;IACH,aAAa,CAAC,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,eAAe,CAAC,CAAC;AAClD,CAAC;AAAC,MAAM,CAAC;IACP,qBAAqB;AACvB,CAAC;AAED,OAAO,EAAE,kBAAkB,EAAE,eAAe,EAAE,MAAM,YAAY,CAAC;AAEjE,MAAM,GA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sB,MAAM,CAAC,OAAO,EAAE,CAAC,CAAC;SACnE,IAAI,MAAM,CAAC,OAAO,IAAI,CAAC,MAAM,CAAC,gBAAgB,IAAI,OAAO,CAAC,GAAG,CAAC,mBAAmB;QACpF,OAAO,CAAC,GAAG,CAAC,6BAA6B,CAAC,CAAC;SACxC,IAAI,MAAM,CAAC,OAAO;QACrB,OAAO,CAAC,GAAG,CAAC,gFAAgF,CAAC,CAAC;AAClG,CAAC;AAED,KAAK,UAAU,IAAI;IACjB,MAAM,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;IACnC,MAAM,GAAG,GAAG,IAAI,CAAC,CAAC,CAAC,CAAC;IACpB,MAAM,GAAG,GAAG,IAAI,CAAC,CAAC,CAAC,CAAC;IAEpB,IAAI,GAAG,KAAK,SAAS,IAAI,GAAG,KAAK,QAAQ,EAAE,CAAC;QAC1C,MAAM,SAAS,EAAE,CAAC;QAClB,OAAO;IACT,CAAC;IACD,IAAI,GAAG,KAAK,SAAS,IAAI,GAAG,KAAK,KAAK,EAAE,CAAC;QACvC,MAAM,EAAE,CAAC;QACT,MAAM,SAAS,EAAE,CAAC;QAClB,OAAO;IACT,CAAC;IAED,OAAO,CAAC,KAAK,CAAC,2EAA2E,CAAC,CAAC;IAC3F,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;AAClB,CAAC;AAED,IAAI,EAAE,CAAC,KAAK,CAAC,CAAC,GAAG,EAAE,EAAE;IACnB,OAAO,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;IACnB,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;AAClB,CAAC,CAAC,CAAC"}
@@ -0,0 +1,31 @@
1
+ /**
2
+ * Resolve Prisma config (prisma.config.ts / prisma.config.js) for schema and migrations paths.
3
+ * Aligns with Prisma config reference: paths relative to config file location.
4
+ */
5
+ export interface ResolvedPrismaConfig {
6
+ /** Schema path (relative to configDir). */
7
+ schemaPath: string;
8
+ /** Migrations directory path (relative to configDir). */
9
+ migrationsPath: string;
10
+ /** Directory containing the config file (for resolving relative paths). */
11
+ configDir: string;
12
+ /** Config file path that was used, or null if defaults. */
13
+ configFile: string | null;
14
+ }
15
+ /**
16
+ * Resolve Prisma config from project root (cwd). Uses prisma.config.* or .config/prisma.*.
17
+ * For .ts/.mts/.cts, parses file content (best-effort). For .js/.mjs/.cjs, loads via import.
18
+ * Paths in the config are relative to the config file directory (Prisma behavior).
19
+ *
20
+ * @example
21
+ * ```ts
22
+ * const config = await resolvePrismaConfig(process.cwd());
23
+ * generateMigrations({ cwd: config.configDir, schemaPath: config.schemaPath, migrationsDir: config.migrationsPath, ... });
24
+ * ```
25
+ */
26
+ export declare function resolvePrismaConfig(cwd: string): Promise<ResolvedPrismaConfig>;
27
+ /**
28
+ * Sync version: only parses file content (no import). Use when async import is not needed.
29
+ */
30
+ export declare function resolvePrismaConfigSync(cwd: string): ResolvedPrismaConfig;
31
+ //# sourceMappingURL=config.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"config.d.ts","sourceRoot":"","sources":["../src/config.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAqBH,MAAM,WAAW,oBAAoB;IACnC,2CAA2C;IAC3C,UAAU,EAAE,MAAM,CAAC;IACnB,yDAAyD;IACzD,cAAc,EAAE,MAAM,CAAC;IACvB,2EAA2E;IAC3E,SAAS,EAAE,MAAM,CAAC;IAClB,2DAA2D;IAC3D,UAAU,EAAE,MAAM,GAAG,IAAI,CAAC;CAC3B;AAyDD;;;;;;;;;;GAUG;AACH,wBAAsB,mBAAmB,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,oBAAoB,CAAC,CA2CpF;AAED;;GAEG;AACH,wBAAgB,uBAAuB,CAAC,GAAG,EAAE,MAAM,GAAG,oBAAoB,CA6BzE"}
package/dist/config.js ADDED
@@ -0,0 +1,160 @@
1
+ /**
2
+ * Resolve Prisma config (prisma.config.ts / prisma.config.js) for schema and migrations paths.
3
+ * Aligns with Prisma config reference: paths relative to config file location.
4
+ */
5
+ import { existsSync, readFileSync } from "fs";
6
+ import { join, dirname } from "path";
7
+ import { pathToFileURL } from "url";
8
+ const CONFIG_NAMES = [
9
+ "prisma.config.ts",
10
+ "prisma.config.cts",
11
+ "prisma.config.mts",
12
+ "prisma.config.js",
13
+ "prisma.config.cjs",
14
+ "prisma.config.mjs",
15
+ ];
16
+ const CONFIG_DIR_NAMES = [".config/prisma.ts", ".config/prisma.cts", ".config/prisma.mts", ".config/prisma.js", ".config/prisma.cjs", ".config/prisma.mjs"];
17
+ /** Defaults matching Prisma CLI when no config is found. */
18
+ const DEFAULT_SCHEMA = "prisma/schema.prisma";
19
+ const DEFAULT_MIGRATIONS = "prisma/migrations";
20
+ /**
21
+ * Extract schema and migrations.path from config file content (best-effort for .ts).
22
+ */
23
+ function parseConfigContent(content) {
24
+ let schemaPath;
25
+ let migrationsPath;
26
+ // schema: "prisma/schema.prisma" or schema: 'prisma/schema.prisma'
27
+ const schemaMatch = content.match(/schema\s*:\s*['"]([^'"]+)['"]/);
28
+ if (schemaMatch)
29
+ schemaPath = schemaMatch[1].trim();
30
+ // migrations: { path: "prisma/migrations" } (allow newlines)
31
+ const migrationsBlock = content.match(/migrations\s*:\s*\{([^}]*)\}/s);
32
+ if (migrationsBlock) {
33
+ const pathMatch = migrationsBlock[1].match(/path\s*:\s*['"]([^'"]+)['"]/);
34
+ if (pathMatch)
35
+ migrationsPath = pathMatch[1].trim();
36
+ }
37
+ return { schemaPath, migrationsPath };
38
+ }
39
+ /**
40
+ * Try to load config via dynamic import (.js/.mjs/.cjs only).
41
+ */
42
+ async function loadConfigModule(configPath) {
43
+ const ext = configPath.replace(/^.*\./, "");
44
+ if (ext !== "js" && ext !== "mjs" && ext !== "cjs")
45
+ return null;
46
+ try {
47
+ const url = pathToFileURL(configPath).href;
48
+ const mod = await import(url);
49
+ const config = mod?.default ?? mod;
50
+ if (!config || typeof config !== "object")
51
+ return null;
52
+ return {
53
+ schemaPath: config.schema,
54
+ migrationsPath: config.migrations?.path,
55
+ };
56
+ }
57
+ catch {
58
+ return null;
59
+ }
60
+ }
61
+ function findConfigFile(cwd) {
62
+ for (const name of CONFIG_NAMES) {
63
+ const p = join(cwd, name);
64
+ if (existsSync(p))
65
+ return { path: p, dir: cwd };
66
+ }
67
+ for (const name of CONFIG_DIR_NAMES) {
68
+ const p = join(cwd, name);
69
+ if (existsSync(p))
70
+ return { path: p, dir: dirname(p) };
71
+ }
72
+ return null;
73
+ }
74
+ /**
75
+ * Resolve Prisma config from project root (cwd). Uses prisma.config.* or .config/prisma.*.
76
+ * For .ts/.mts/.cts, parses file content (best-effort). For .js/.mjs/.cjs, loads via import.
77
+ * Paths in the config are relative to the config file directory (Prisma behavior).
78
+ *
79
+ * @example
80
+ * ```ts
81
+ * const config = await resolvePrismaConfig(process.cwd());
82
+ * generateMigrations({ cwd: config.configDir, schemaPath: config.schemaPath, migrationsDir: config.migrationsPath, ... });
83
+ * ```
84
+ */
85
+ export async function resolvePrismaConfig(cwd) {
86
+ const found = findConfigFile(cwd);
87
+ if (!found) {
88
+ return {
89
+ schemaPath: DEFAULT_SCHEMA,
90
+ migrationsPath: DEFAULT_MIGRATIONS,
91
+ configDir: cwd,
92
+ configFile: null,
93
+ };
94
+ }
95
+ const { path: configPath, dir: configDir } = found;
96
+ const ext = configPath.replace(/^.*\./, "");
97
+ if (ext === "js" || ext === "mjs" || ext === "cjs") {
98
+ const loaded = await loadConfigModule(configPath);
99
+ if (loaded?.schemaPath != null || loaded?.migrationsPath != null) {
100
+ return {
101
+ schemaPath: loaded.schemaPath ?? DEFAULT_SCHEMA,
102
+ migrationsPath: loaded.migrationsPath ?? DEFAULT_MIGRATIONS,
103
+ configDir,
104
+ configFile: configPath,
105
+ };
106
+ }
107
+ }
108
+ try {
109
+ const content = readFileSync(configPath, "utf-8");
110
+ const parsed = parseConfigContent(content);
111
+ return {
112
+ schemaPath: parsed.schemaPath ?? DEFAULT_SCHEMA,
113
+ migrationsPath: parsed.migrationsPath ?? DEFAULT_MIGRATIONS,
114
+ configDir,
115
+ configFile: configPath,
116
+ };
117
+ }
118
+ catch {
119
+ return {
120
+ schemaPath: DEFAULT_SCHEMA,
121
+ migrationsPath: DEFAULT_MIGRATIONS,
122
+ configDir,
123
+ configFile: configPath,
124
+ };
125
+ }
126
+ }
127
+ /**
128
+ * Sync version: only parses file content (no import). Use when async import is not needed.
129
+ */
130
+ export function resolvePrismaConfigSync(cwd) {
131
+ const found = findConfigFile(cwd);
132
+ if (!found) {
133
+ return {
134
+ schemaPath: DEFAULT_SCHEMA,
135
+ migrationsPath: DEFAULT_MIGRATIONS,
136
+ configDir: cwd,
137
+ configFile: null,
138
+ };
139
+ }
140
+ const { path: configPath, dir: configDir } = found;
141
+ try {
142
+ const content = readFileSync(configPath, "utf-8");
143
+ const parsed = parseConfigContent(content);
144
+ return {
145
+ schemaPath: parsed.schemaPath ?? DEFAULT_SCHEMA,
146
+ migrationsPath: parsed.migrationsPath ?? DEFAULT_MIGRATIONS,
147
+ configDir,
148
+ configFile: configPath,
149
+ };
150
+ }
151
+ catch {
152
+ return {
153
+ schemaPath: DEFAULT_SCHEMA,
154
+ migrationsPath: DEFAULT_MIGRATIONS,
155
+ configDir,
156
+ configFile: configPath,
157
+ };
158
+ }
159
+ }
160
+ //# sourceMappingURL=config.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"config.js","sourceRoot":"","sources":["../src/config.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,EAAE,UAAU,EAAE,YAAY,EAAE,MAAM,IAAI,CAAC;AAC9C,OAAO,EAAE,IAAI,EAAE,OAAO,EAAE,MAAM,MAAM,CAAC;AACrC,OAAO,EAAE,aAAa,EAAE,MAAM,KAAK,CAAC;AAEpC,MAAM,YAAY,GAAG;IACnB,kBAAkB;IAClB,mBAAmB;IACnB,mBAAmB;IACnB,kBAAkB;IAClB,mBAAmB;IACnB,mBAAmB;CACpB,CA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SACjB,CAAC;IACJ,CAAC;IAED,MAAM,EAAE,IAAI,EAAE,UAAU,EAAE,GAAG,EAAE,SAAS,EAAE,GAAG,KAAK,CAAC;IACnD,IAAI,CAAC;QACH,MAAM,OAAO,GAAG,YAAY,CAAC,UAAU,EAAE,OAAO,CAAC,CAAC;QAClD,MAAM,MAAM,GAAG,kBAAkB,CAAC,OAAO,CAAC,CAAC;QAC3C,OAAO;YACL,UAAU,EAAE,MAAM,CAAC,UAAU,IAAI,cAAc;YAC/C,cAAc,EAAE,MAAM,CAAC,cAAc,IAAI,kBAAkB;YAC3D,SAAS;YACT,UAAU,EAAE,UAAU;SACvB,CAAC;IACJ,CAAC;IAAC,MAAM,CAAC;QACP,OAAO;YACL,UAAU,EAAE,cAAc;YAC1B,cAAc,EAAE,kBAAkB;YAClC,SAAS;YACT,UAAU,EAAE,UAAU;SACvB,CAAC;IACJ,CAAC;AACH,CAAC"}
@@ -0,0 +1,53 @@
1
+ /**
2
+ * Generate Prisma migration files via `prisma migrate diff` (no DB connection).
3
+ * Aligns with Prisma migrate: from-empty for initial, from-migrations for incremental.
4
+ * Requires Prisma CLI (prisma package) in the project.
5
+ * When schemaPath/migrationsDir are omitted, resolves from prisma.config.* (see resolvePrismaConfig).
6
+ */
7
+ export interface GenerateMigrationsOptions {
8
+ /** Project root (cwd for prisma CLI). When schemaPath/migrationsDir omitted, config is resolved from here. */
9
+ cwd: string;
10
+ /** Path to schema file (relative to cwd or absolute). Omit to use prisma.config.*. */
11
+ schemaPath?: string;
12
+ /** Path to prisma/migrations directory (relative to cwd or absolute). Omit to use prisma.config.*. */
13
+ migrationsDir?: string;
14
+ /** Name for incremental migration (default "schema_update"). Only used when creating a new migration. */
15
+ incrementalName?: string;
16
+ /**
17
+ * When set, enables generating an incremental migration (diff from existing migrations to schema).
18
+ * When unset and init already exists, no new migration is created (caller can prompt to set this).
19
+ */
20
+ shadowDatabaseUrl?: string;
21
+ }
22
+ export interface GenerateMigrationsResult {
23
+ /** Migration name created (e.g. "0_init" or "20240101120000_add_email"), or null if none. */
24
+ created: string | null;
25
+ /** Whether the initial migration (0_init) already existed before this run. */
26
+ hasInit: boolean;
27
+ /** Whether schema had changes (only meaningful when shadowDatabaseUrl was set and no new migration was written). */
28
+ hasSchemaChanges: boolean;
29
+ }
30
+ /**
31
+ * Generate migration files under migrationsDir using Prisma migrate diff.
32
+ * - If no initial migration: creates 0_init from empty → schema.
33
+ * - If initial exists and shadowDatabaseUrl is set: creates timestamp_incrementalName from migrations → schema when there are changes.
34
+ * - Otherwise returns without creating a migration.
35
+ *
36
+ * @example
37
+ * ```ts
38
+ * import path from "path";
39
+ * import { generateMigrations } from "@seekdb/prisma-adapter";
40
+ *
41
+ * const result = generateMigrations({
42
+ * cwd: process.cwd(),
43
+ * schemaPath: "prisma/schema.prisma",
44
+ * migrationsDir: path.join(process.cwd(), "prisma", "migrations"),
45
+ * incrementalName: "add_email",
46
+ * shadowDatabaseUrl: process.env.SHADOW_DATABASE_URL,
47
+ * });
48
+ * if (result.created) console.log("Created migration:", result.created);
49
+ * else if (!result.hasInit) console.log("Set SHADOW_DATABASE_URL to add schema changes.");
50
+ * ```
51
+ */
52
+ export declare function generateMigrations(options: GenerateMigrationsOptions): GenerateMigrationsResult;
53
+ //# sourceMappingURL=generateMigrations.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"generateMigrations.d.ts","sourceRoot":"","sources":["../src/generateMigrations.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAgBH,MAAM,WAAW,yBAAyB;IACxC,8GAA8G;IAC9G,GAAG,EAAE,MAAM,CAAC;IACZ,sFAAsF;IACtF,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,sGAAsG;IACtG,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB,yGAAyG;IACzG,eAAe,CAAC,EAAE,MAAM,CAAC;IACzB;;;OAGG;IACH,iBAAiB,CAAC,EAAE,MAAM,CAAC;CAC5B;AAED,MAAM,WAAW,wBAAwB;IACvC,6FAA6F;IAC7F,OAAO,EAAE,MAAM,GAAG,IAAI,CAAC;IACvB,8EAA8E;IAC9E,OAAO,EAAE,OAAO,CAAC;IACjB,oHAAoH;IACpH,gBAAgB,EAAE,OAAO,CAAC;CAC3B;AAoCD;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,wBAAgB,kBAAkB,CAChC,OAAO,EAAE,yBAAyB,GACjC,wBAAwB,CAwD1B"}
@@ -0,0 +1,106 @@
1
+ /**
2
+ * Generate Prisma migration files via `prisma migrate diff` (no DB connection).
3
+ * Aligns with Prisma migrate: from-empty for initial, from-migrations for incremental.
4
+ * Requires Prisma CLI (prisma package) in the project.
5
+ * When schemaPath/migrationsDir are omitted, resolves from prisma.config.* (see resolvePrismaConfig).
6
+ */
7
+ import { execSync } from "child_process";
8
+ import { existsSync, mkdirSync, readFileSync, unlinkSync, writeFileSync, } from "fs";
9
+ import { join } from "path";
10
+ import { tmpdir } from "os";
11
+ import { resolvePrismaConfigSync } from "./config.js";
12
+ const INIT_MIGRATION = "0_init";
13
+ function ensureMigrationsDir(dir) {
14
+ if (!existsSync(dir)) {
15
+ mkdirSync(dir, { recursive: true });
16
+ }
17
+ }
18
+ function writeMigration(dir, name, rawSql) {
19
+ const migrationDir = join(dir, name);
20
+ mkdirSync(migrationDir, { recursive: true });
21
+ const content = rawSql.trim() || "-- no changes\n";
22
+ writeFileSync(join(migrationDir, "migration.sql"), content);
23
+ }
24
+ function hasMeaningfulSql(sql) {
25
+ return sql.replace(/^--.*$/gm, "").trim().length > 0;
26
+ }
27
+ function runDiff(cwd, schemaPath, fromArg) {
28
+ const tmpFile = join(tmpdir(), `prisma-migrate-diff-${Date.now()}.sql`);
29
+ try {
30
+ execSync(`npx prisma migrate diff ${fromArg} --to-schema=${schemaPath} --script -o "${tmpFile}"`, { cwd, stdio: "pipe" });
31
+ return readFileSync(tmpFile, "utf-8");
32
+ }
33
+ finally {
34
+ try {
35
+ unlinkSync(tmpFile);
36
+ }
37
+ catch {
38
+ // ignore
39
+ }
40
+ }
41
+ }
42
+ /**
43
+ * Generate migration files under migrationsDir using Prisma migrate diff.
44
+ * - If no initial migration: creates 0_init from empty → schema.
45
+ * - If initial exists and shadowDatabaseUrl is set: creates timestamp_incrementalName from migrations → schema when there are changes.
46
+ * - Otherwise returns without creating a migration.
47
+ *
48
+ * @example
49
+ * ```ts
50
+ * import path from "path";
51
+ * import { generateMigrations } from "@seekdb/prisma-adapter";
52
+ *
53
+ * const result = generateMigrations({
54
+ * cwd: process.cwd(),
55
+ * schemaPath: "prisma/schema.prisma",
56
+ * migrationsDir: path.join(process.cwd(), "prisma", "migrations"),
57
+ * incrementalName: "add_email",
58
+ * shadowDatabaseUrl: process.env.SHADOW_DATABASE_URL,
59
+ * });
60
+ * if (result.created) console.log("Created migration:", result.created);
61
+ * else if (!result.hasInit) console.log("Set SHADOW_DATABASE_URL to add schema changes.");
62
+ * ```
63
+ */
64
+ export function generateMigrations(options) {
65
+ const { cwd, incrementalName = "schema_update", shadowDatabaseUrl, } = options;
66
+ const resolved = options.schemaPath != null && options.migrationsDir != null
67
+ ? {
68
+ schemaPath: options.schemaPath,
69
+ migrationsPath: options.migrationsDir,
70
+ configDir: cwd,
71
+ }
72
+ : resolvePrismaConfigSync(cwd);
73
+ const schemaPath = resolved.schemaPath;
74
+ const migrationsDir = resolved.migrationsPath;
75
+ const effectiveCwd = resolved.configDir;
76
+ const isAbsolute = migrationsDir.startsWith("/") ||
77
+ (process.platform === "win32" && /^[A-Za-z]:/.test(migrationsDir));
78
+ const resolvedMigrationsDir = isAbsolute
79
+ ? migrationsDir
80
+ : join(effectiveCwd, migrationsDir);
81
+ ensureMigrationsDir(resolvedMigrationsDir);
82
+ const initPath = join(resolvedMigrationsDir, INIT_MIGRATION);
83
+ const hasInit = existsSync(initPath);
84
+ if (!hasInit) {
85
+ const sql = runDiff(effectiveCwd, schemaPath, "--from-empty");
86
+ writeMigration(resolvedMigrationsDir, INIT_MIGRATION, sql);
87
+ return { created: INIT_MIGRATION, hasInit: false, hasSchemaChanges: true };
88
+ }
89
+ if (shadowDatabaseUrl) {
90
+ const sql = runDiff(effectiveCwd, schemaPath, `--from-migrations=${resolvedMigrationsDir}`);
91
+ const hasSchemaChanges = hasMeaningfulSql(sql);
92
+ if (hasSchemaChanges) {
93
+ const timestamp = new Date()
94
+ .toISOString()
95
+ .replace(/[-:T]/g, "")
96
+ .slice(0, 14);
97
+ const safeName = incrementalName.replace(/[^a-zA-Z0-9_]/g, "_");
98
+ const name = `${timestamp}_${safeName}`;
99
+ writeMigration(resolvedMigrationsDir, name, sql);
100
+ return { created: name, hasInit: true, hasSchemaChanges: true };
101
+ }
102
+ return { created: null, hasInit: true, hasSchemaChanges: false };
103
+ }
104
+ return { created: null, hasInit: true, hasSchemaChanges: false };
105
+ }
106
+ //# sourceMappingURL=generateMigrations.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"generateMigrations.js","sourceRoot":"","sources":["../src/generateMigrations.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AACzC,OAAO,EACL,UAAU,EACV,SAAS,EACT,YAAY,EACZ,UAAU,EACV,aAAa,GACd,MAAM,IAAI,CAAC;AACZ,OAAO,EAAE,IAAI,EAAE,MAAM,MAAM,CAAC;AAC5B,OAAO,EAAE,MAAM,EAAE,MAAM,IAAI,CAAC;AAC5B,OAAO,EAAE,uBAAuB,EAAE,MAAM,aAAa,CAAC;AAEtD,MAAM,cAAc,GAAG,QAAQ,CAAC;AA2BhC,SAAS,mBAAmB,CAAC,GAAW;IACtC,IAAI,CAAC,UAAU,CAAC,GAAG,CAAC,EAAE,CAAC;QACrB,SAAS,CAAC,GAAG,EAAE,EAAE,SAAS,EAAE,IAAI,EAAE,CAAC,CAAC;IACtC,CAAC;AACH,CAAC;AAED,SAAS,cAAc,CAAC,GAAW,EAAE,IAAY,EAAE,MAAc;IAC/D,MAAM,YAAY,GAAG,IAAI,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,GAAG,IAAI,IAAI,EAAE;iBACzB,WAAW,EAAE;iBACb,OAAO,CAAC,QAAQ,EAAE,EAAE,CAAC;iBACrB,KAAK,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC;YAChB,MAAM,QAAQ,GAAG,eAAe,CAAC,OAAO,CAAC,gBAAgB,EAAE,GAAG,CAAC,CAAC;YAChE,MAAM,IAAI,GAAG,GAAG,SAAS,IAAI,QAAQ,EAAE,CAAC;YACxC,cAAc,CAAC,qBAAqB,EAAE,IAAI,EAAE,GAAG,CAAC,CAAC;YACjD,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,OAAO,EAAE,IAAI,EAAE,gBAAgB,EAAE,IAAI,EAAE,CAAC;QAClE,CAAC;QACD,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,OAAO,EAAE,IAAI,EAAE,gBAAgB,EAAE,KAAK,EAAE,CAAC;IACnE,CAAC;IAED,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,OAAO,EAAE,IAAI,EAAE,gBAAgB,EAAE,KAAK,EAAE,CAAC;AACnE,CAAC"}
@@ -0,0 +1,30 @@
1
+ /**
2
+ * Apply Prisma migrations to seekdb Embedded at runtime.
3
+ * For Prisma v7 when CLI cannot connect (no adapter in prisma.config).
4
+ * When migrationsDir is omitted, pass cwd to resolve from prisma.config.*.
5
+ */
6
+ import type { SeekdbClientLike } from "./adapter.js";
7
+ export interface ApplyMigrationsOptions {
8
+ /** Path to prisma/migrations directory (absolute or relative to cwd). Omit if using cwd + config. */
9
+ migrationsDir?: string;
10
+ /** Project root; used to resolve migrations path from prisma.config.* when migrationsDir is omitted. */
11
+ cwd?: string;
12
+ }
13
+ /**
14
+ * Apply pending migrations from a Prisma migrations directory.
15
+ * Creates `_prisma_migrations` to track applied migrations; runs each migration.sql in sorted order.
16
+ *
17
+ * @example
18
+ * ```ts
19
+ * import { SeekdbClient } from "seekdb";
20
+ * import { applyMigrations } from "@seekdb/prisma-adapter";
21
+ * import path from "path";
22
+ *
23
+ * const client = new SeekdbClient({ path: "./seekdb.db", database: "test" });
24
+ * await applyMigrations(client, {
25
+ * migrationsDir: path.join(process.cwd(), "prisma", "migrations"),
26
+ * });
27
+ * ```
28
+ */
29
+ export declare function applyMigrations(client: SeekdbClientLike, options: ApplyMigrationsOptions): Promise<void>;
30
+ //# sourceMappingURL=migrate.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"migrate.d.ts","sourceRoot":"","sources":["../src/migrate.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAIH,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,cAAc,CAAC;AAKrD,MAAM,WAAW,sBAAsB;IACrC,qGAAqG;IACrG,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB,wGAAwG;IACxG,GAAG,CAAC,EAAE,MAAM,CAAC;CACd;AA2DD;;;;;;;;;;;;;;;GAeG;AACH,wBAAsB,eAAe,CACnC,MAAM,EAAE,gBAAgB,EACxB,OAAO,EAAE,sBAAsB,GAC9B,OAAO,CAAC,IAAI,CAAC,CA8Bf"}
@@ -0,0 +1,101 @@
1
+ /**
2
+ * Apply Prisma migrations to seekdb Embedded at runtime.
3
+ * For Prisma v7 when CLI cannot connect (no adapter in prisma.config).
4
+ * When migrationsDir is omitted, pass cwd to resolve from prisma.config.*.
5
+ */
6
+ import { readdirSync, readFileSync } from "fs";
7
+ import { join } from "path";
8
+ import { resolvePrismaConfigSync } from "./config.js";
9
+ const MIGRATIONS_TABLE = "_prisma_migrations";
10
+ async function ensureMigrationsTable(client) {
11
+ await client.execute(`
12
+ CREATE TABLE IF NOT EXISTS \`${MIGRATIONS_TABLE}\` (
13
+ \`id\` VARCHAR(36) NOT NULL,
14
+ \`migration_name\` VARCHAR(255) NOT NULL,
15
+ \`finished_at\` DATETIME(3) NULL,
16
+ PRIMARY KEY (\`id\`)
17
+ )
18
+ `);
19
+ }
20
+ async function getAppliedMigrations(client) {
21
+ try {
22
+ const result = await client.execute(`SELECT \`migration_name\` FROM \`${MIGRATIONS_TABLE}\``);
23
+ const rows = (result ?? []);
24
+ return new Set(rows.map((r) => r.migration_name ?? "").filter(Boolean));
25
+ }
26
+ catch {
27
+ return new Set();
28
+ }
29
+ }
30
+ async function recordMigration(client, migrationName) {
31
+ const id = crypto.randomUUID();
32
+ const now = new Date().toISOString().slice(0, 23).replace("T", " ");
33
+ const escaped = migrationName.replace(/'/g, "''");
34
+ await client.execute(`INSERT INTO \`${MIGRATIONS_TABLE}\` (\`id\`, \`migration_name\`, \`finished_at\`) VALUES ('${id}', '${escaped}', '${now}')`);
35
+ }
36
+ function parseStatements(sql) {
37
+ return sql
38
+ .replace(/^--.*$/gm, "")
39
+ .trim()
40
+ .split(";")
41
+ .map((s) => s.trim())
42
+ .filter(Boolean);
43
+ }
44
+ function getMigrationDirs(migrationsDir) {
45
+ try {
46
+ return readdirSync(migrationsDir, { withFileTypes: true })
47
+ .filter((d) => d.isDirectory())
48
+ .map((d) => d.name)
49
+ .sort();
50
+ }
51
+ catch {
52
+ return [];
53
+ }
54
+ }
55
+ /**
56
+ * Apply pending migrations from a Prisma migrations directory.
57
+ * Creates `_prisma_migrations` to track applied migrations; runs each migration.sql in sorted order.
58
+ *
59
+ * @example
60
+ * ```ts
61
+ * import { SeekdbClient } from "seekdb";
62
+ * import { applyMigrations } from "@seekdb/prisma-adapter";
63
+ * import path from "path";
64
+ *
65
+ * const client = new SeekdbClient({ path: "./seekdb.db", database: "test" });
66
+ * await applyMigrations(client, {
67
+ * migrationsDir: path.join(process.cwd(), "prisma", "migrations"),
68
+ * });
69
+ * ```
70
+ */
71
+ export async function applyMigrations(client, options) {
72
+ let migrationsDir = options.migrationsDir;
73
+ if (migrationsDir == null) {
74
+ const cwd = options.cwd ?? process.cwd();
75
+ const config = resolvePrismaConfigSync(cwd);
76
+ migrationsDir = join(config.configDir, config.migrationsPath);
77
+ }
78
+ await ensureMigrationsTable(client);
79
+ const applied = await getAppliedMigrations(client);
80
+ const dirs = getMigrationDirs(migrationsDir);
81
+ for (const name of dirs) {
82
+ if (applied.has(name))
83
+ continue;
84
+ const sqlPath = join(migrationsDir, name, "migration.sql");
85
+ let sql;
86
+ try {
87
+ sql = readFileSync(sqlPath, "utf-8");
88
+ }
89
+ catch {
90
+ continue;
91
+ }
92
+ const statements = parseStatements(sql);
93
+ if (statements.length === 0)
94
+ continue;
95
+ for (const stmt of statements) {
96
+ await client.execute(stmt + ";");
97
+ }
98
+ await recordMigration(client, name);
99
+ }
100
+ }
101
+ //# sourceMappingURL=migrate.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"migrate.js","sourceRoot":"","sources":["../src/migrate.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EAAE,WAAW,EAAE,YAAY,EAAE,MAAM,IAAI,CAAC;AAC/C,OAAO,EAAE,IAAI,EAAE,MAAM,MAAM,CAAC;AAE5B,OAAO,EAAE,uBAAuB,EAAE,MAAM,aAAa,CAAC;AAEtD,MAAM,gBAAgB,GAAG,oBAAoB,CAAC;AAS9C,KAAK,UAAU,qBAAqB,CAAC,MAAwB;IAC3D,MAAM,MAAM,CAAC,OAAO,CAAC;mCACY,gBAAgB;;;;;;GAMhD,CAAC,CAAC;AACL,CAAC;AAED,KAAK,UAAU,oBAAoB,CACjC,MAAwB;IAExB,IAAI,CAAC;QACH,MAAM,MAAM,GAAG,MAAM,MAAM,CAAC,OAAO,CACjC,oCAAoC,gBAAgB,IAAI,CACzD,CAAC;QACF,MAAM,IAAI,GAAG,CAAC,MAAM,IAAI,EAAE,CAAkC,CAAC;QAC7D,OAAO,IAAI,GAAG,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,cAAc,IAAI,EAAE,CAAC,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC,CAAC;IAC1E,CAAC;IAAC,MAAM,CAAC;QACP,OAAO,IAAI,GAAG,EAAE,CAAC;IACnB,CAAC;AACH,CAAC;AAED,KAAK,UAA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a,IAAI,IAAI,EAAE,CAAC;QAC1B,MAAM,GAAG,GAAG,OAAO,CAAC,GAAG,IAAI,OAAO,CAAC,GAAG,EAAE,CAAC;QACzC,MAAM,MAAM,GAAG,uBAAuB,CAAC,GAAG,CAAC,CAAC;QAC5C,aAAa,GAAG,IAAI,CAAC,MAAM,CAAC,SAAS,EAAE,MAAM,CAAC,cAAc,CAAC,CAAC;IAChE,CAAC;IACD,MAAM,qBAAqB,CAAC,MAAM,CAAC,CAAC;IACpC,MAAM,OAAO,GAAG,MAAM,oBAAoB,CAAC,MAAM,CAAC,CAAC;IACnD,MAAM,IAAI,GAAG,gBAAgB,CAAC,aAAa,CAAC,CAAC;IAE7C,KAAK,MAAM,IAAI,IAAI,IAAI,EAAE,CAAC;QACxB,IAAI,OAAO,CAAC,GAAG,CAAC,IAAI,CAAC;YAAE,SAAS;QAEhC,MAAM,OAAO,GAAG,IAAI,CAAC,aAAa,EAAE,IAAI,EAAE,eAAe,CAAC,CAAC;QAC3D,IAAI,GAAW,CAAC;QAChB,IAAI,CAAC;YACH,GAAG,GAAG,YAAY,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC;QACvC,CAAC;QAAC,MAAM,CAAC;YACP,SAAS;QACX,CAAC;QAED,MAAM,UAAU,GAAG,eAAe,CAAC,GAAG,CAAC,CAAC;QACxC,IAAI,UAAU,CAAC,MAAM,KAAK,CAAC;YAAE,SAAS;QAEtC,KAAK,MAAM,IAAI,IAAI,UAAU,EAAE,CAAC;YAC9B,MAAM,MAAM,CAAC,OAAO,CAAC,IAAI,GAAG,GAAG,CAAC,CAAC;QACnC,CAAC;QACD,MAAM,eAAe,CAAC,MAAM,EAAE,IAAI,CAAC,CAAC;IACtC,CAAC;AACH,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@seekdb/prisma-adapter",
3
- "version": "1.2.0",
3
+ "version": "1.4.0",
4
4
  "description": "Prisma driver adapter for seekdb Embedded — use Prisma with seekdb Embedded",
5
5
  "keywords": [
6
6
  "seekdb",
@@ -24,6 +24,12 @@
24
24
  "dist",
25
25
  "README.md"
26
26
  ],
27
+ "scripts": {
28
+ "build": "tsc",
29
+ "clean": "rm -rf dist",
30
+ "typecheck": "tsc --noEmit",
31
+ "test": "vitest run"
32
+ },
27
33
  "dependencies": {
28
34
  "@prisma/driver-adapter-utils": "^6.19.2"
29
35
  },
@@ -32,9 +38,9 @@
32
38
  "seekdb": ">=1.2.0"
33
39
  },
34
40
  "devDependencies": {
41
+ "seekdb": "1.4.0",
35
42
  "typescript": "^5.0.0",
36
- "vitest": "^2.1.9",
37
- "seekdb": "1.2.0"
43
+ "vitest": "^2.1.9"
38
44
  },
39
45
  "engines": {
40
46
  "node": ">=18"
@@ -52,10 +58,5 @@
52
58
  "registry": "https://registry.npmjs.org",
53
59
  "access": "public"
54
60
  },
55
- "scripts": {
56
- "build": "tsc",
57
- "clean": "rm -rf dist",
58
- "typecheck": "tsc --noEmit",
59
- "test": "vitest run"
60
- }
61
- }
61
+ "gitHead": "65fb0cb350507622a547ba4a3786f41c6f82e027"
62
+ }