zenstack-kit 0.1.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 (93) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +313 -0
  3. package/dist/cli/app.d.ts +12 -0
  4. package/dist/cli/app.d.ts.map +1 -0
  5. package/dist/cli/app.js +253 -0
  6. package/dist/cli/commands.d.ts +70 -0
  7. package/dist/cli/commands.d.ts.map +1 -0
  8. package/dist/cli/commands.js +308 -0
  9. package/dist/cli/index.d.ts +12 -0
  10. package/dist/cli/index.d.ts.map +1 -0
  11. package/dist/cli/index.js +12 -0
  12. package/dist/cli/prompt-provider.d.ts +10 -0
  13. package/dist/cli/prompt-provider.d.ts.map +1 -0
  14. package/dist/cli/prompt-provider.js +41 -0
  15. package/dist/cli/prompts.d.ts +27 -0
  16. package/dist/cli/prompts.d.ts.map +1 -0
  17. package/dist/cli/prompts.js +133 -0
  18. package/dist/cli.d.ts +12 -0
  19. package/dist/cli.d.ts.map +1 -0
  20. package/dist/cli.js +240 -0
  21. package/dist/config/index.d.ts +96 -0
  22. package/dist/config/index.d.ts.map +1 -0
  23. package/dist/config/index.js +48 -0
  24. package/dist/config/loader.d.ts +11 -0
  25. package/dist/config/loader.d.ts.map +1 -0
  26. package/dist/config/loader.js +44 -0
  27. package/dist/config-loader.d.ts +6 -0
  28. package/dist/config-loader.d.ts.map +1 -0
  29. package/dist/config-loader.js +36 -0
  30. package/dist/config.d.ts +62 -0
  31. package/dist/config.d.ts.map +1 -0
  32. package/dist/config.js +44 -0
  33. package/dist/index.d.ts +19 -0
  34. package/dist/index.d.ts.map +1 -0
  35. package/dist/index.js +23 -0
  36. package/dist/init-prompts.d.ts +13 -0
  37. package/dist/init-prompts.d.ts.map +1 -0
  38. package/dist/init-prompts.js +64 -0
  39. package/dist/introspect.d.ts +54 -0
  40. package/dist/introspect.d.ts.map +1 -0
  41. package/dist/introspect.js +75 -0
  42. package/dist/kysely-adapter.d.ts +49 -0
  43. package/dist/kysely-adapter.d.ts.map +1 -0
  44. package/dist/kysely-adapter.js +74 -0
  45. package/dist/migrate-apply.d.ts +18 -0
  46. package/dist/migrate-apply.d.ts.map +1 -0
  47. package/dist/migrate-apply.js +61 -0
  48. package/dist/migrate.d.ts +108 -0
  49. package/dist/migrate.d.ts.map +1 -0
  50. package/dist/migrate.js +127 -0
  51. package/dist/migrations/apply.d.ts +18 -0
  52. package/dist/migrations/apply.d.ts.map +1 -0
  53. package/dist/migrations/apply.js +61 -0
  54. package/dist/migrations/diff.d.ts +161 -0
  55. package/dist/migrations/diff.d.ts.map +1 -0
  56. package/dist/migrations/diff.js +620 -0
  57. package/dist/migrations/prisma.d.ts +193 -0
  58. package/dist/migrations/prisma.d.ts.map +1 -0
  59. package/dist/migrations/prisma.js +929 -0
  60. package/dist/migrations.d.ts +161 -0
  61. package/dist/migrations.d.ts.map +1 -0
  62. package/dist/migrations.js +620 -0
  63. package/dist/prisma-migrations.d.ts +160 -0
  64. package/dist/prisma-migrations.d.ts.map +1 -0
  65. package/dist/prisma-migrations.js +789 -0
  66. package/dist/prompts.d.ts +10 -0
  67. package/dist/prompts.d.ts.map +1 -0
  68. package/dist/prompts.js +41 -0
  69. package/dist/pull.d.ts +23 -0
  70. package/dist/pull.d.ts.map +1 -0
  71. package/dist/pull.js +424 -0
  72. package/dist/schema/introspect.d.ts +54 -0
  73. package/dist/schema/introspect.d.ts.map +1 -0
  74. package/dist/schema/introspect.js +75 -0
  75. package/dist/schema/pull.d.ts +23 -0
  76. package/dist/schema/pull.d.ts.map +1 -0
  77. package/dist/schema/pull.js +424 -0
  78. package/dist/schema/snapshot.d.ts +46 -0
  79. package/dist/schema/snapshot.d.ts.map +1 -0
  80. package/dist/schema/snapshot.js +278 -0
  81. package/dist/schema-snapshot.d.ts +45 -0
  82. package/dist/schema-snapshot.d.ts.map +1 -0
  83. package/dist/schema-snapshot.js +265 -0
  84. package/dist/sql/compiler.d.ts +74 -0
  85. package/dist/sql/compiler.d.ts.map +1 -0
  86. package/dist/sql/compiler.js +270 -0
  87. package/dist/sql/kysely-adapter.d.ts +49 -0
  88. package/dist/sql/kysely-adapter.d.ts.map +1 -0
  89. package/dist/sql/kysely-adapter.js +74 -0
  90. package/dist/sql-compiler.d.ts +74 -0
  91. package/dist/sql-compiler.d.ts.map +1 -0
  92. package/dist/sql-compiler.js +243 -0
  93. package/package.json +81 -0
package/dist/cli.js ADDED
@@ -0,0 +1,240 @@
1
+ #!/usr/bin/env node
2
+ /**
3
+ * zenstack-kit CLI - Database tooling for ZenStack schemas
4
+ *
5
+ * Commands:
6
+ * migrate:generate Generate a new SQL migration
7
+ * migrate:apply Apply pending migrations
8
+ * init Initialize snapshot from existing schema
9
+ * pull Introspect database and generate schema
10
+ */
11
+ import { Command } from "commander";
12
+ import chalk from "chalk";
13
+ import { loadConfig } from "./config-loader.js";
14
+ import { getPromptProvider } from "./prompts.js";
15
+ import { pullSchema } from "./pull.js";
16
+ import { createPrismaMigration, applyPrismaMigrations, hasPrismaSchemaChanges, hasSnapshot, scanMigrationFolders, writeMigrationLog, initializeSnapshot, createInitialMigration, } from "./prisma-migrations.js";
17
+ import { promptSnapshotExists, promptFreshInit } from "./init-prompts.js";
18
+ const program = new Command();
19
+ program
20
+ .name("zenstack-kit")
21
+ .description("Drizzle-kit like CLI tooling for ZenStack schemas with Kysely support")
22
+ .version("0.1.0");
23
+ program
24
+ .command("migrate:generate")
25
+ .description("Generate a new SQL migration file")
26
+ .option("-n, --name <name>", "Migration name")
27
+ .option("-s, --schema <path>", "Path to ZenStack schema")
28
+ .option("-m, --migrations <path>", "Migrations directory")
29
+ .option("--dialect <dialect>", "Database dialect (sqlite, postgres, mysql)")
30
+ .action(async (options) => {
31
+ console.log(chalk.blue("Generating migration..."));
32
+ try {
33
+ const config = await loadConfig(process.cwd());
34
+ const schemaPath = options.schema ?? config?.schema ?? "./schema.zmodel";
35
+ const outputPath = options.migrations ?? config?.migrations?.migrationsFolder ?? "./prisma/migrations";
36
+ const dialect = (options.dialect ?? config?.dialect ?? "sqlite");
37
+ // Check for changes first
38
+ const hasChanges = await hasPrismaSchemaChanges({
39
+ schemaPath,
40
+ outputPath,
41
+ });
42
+ if (!hasChanges) {
43
+ console.log(chalk.yellow("No schema changes detected"));
44
+ return;
45
+ }
46
+ // Get migration name
47
+ const name = options.name ?? (await promptForMigrationName("migration"));
48
+ // TODO: Add rename prompts and destructive change confirmation
49
+ // For now, we generate without prompts
50
+ const migration = await createPrismaMigration({
51
+ name,
52
+ schemaPath,
53
+ outputPath,
54
+ dialect,
55
+ });
56
+ if (!migration) {
57
+ console.log(chalk.yellow("No schema changes detected"));
58
+ return;
59
+ }
60
+ console.log(chalk.green(`✓ Migration created: ${migration.folderName}/migration.sql`));
61
+ console.log(chalk.gray(` Path: ${migration.folderPath}`));
62
+ }
63
+ catch (error) {
64
+ console.error(chalk.red("Error creating migration:"), error);
65
+ process.exit(1);
66
+ }
67
+ });
68
+ program
69
+ .command("migrate:apply")
70
+ .description("Apply pending SQL migrations")
71
+ .option("-m, --migrations <path>", "Migrations directory")
72
+ .option("--dialect <dialect>", "Database dialect (sqlite, postgres, mysql)")
73
+ .option("--url <url>", "Database connection URL")
74
+ .option("--table <name>", "Migrations table name (default: _prisma_migrations)")
75
+ .option("--schema <name>", "Migrations schema (PostgreSQL only, default: public)")
76
+ .action(async (options) => {
77
+ console.log(chalk.blue("Applying migrations..."));
78
+ try {
79
+ const config = await loadConfig(process.cwd());
80
+ const outputPath = options.migrations ?? config?.migrations?.migrationsFolder ?? "./prisma/migrations";
81
+ const dialect = (options.dialect ?? config?.dialect ?? "sqlite");
82
+ const connectionUrl = options.url ?? config?.dbCredentials?.url;
83
+ const migrationsTable = options.table ?? config?.migrations?.migrationsTable ?? "_prisma_migrations";
84
+ const migrationsSchema = options.schema ?? config?.migrations?.migrationsSchema ?? "public";
85
+ if (dialect !== "sqlite" && !connectionUrl) {
86
+ throw new Error("Database connection URL is required for non-sqlite dialects");
87
+ }
88
+ const databasePath = dialect === "sqlite" ? resolveSqlitePath(connectionUrl) : undefined;
89
+ const result = await applyPrismaMigrations({
90
+ migrationsFolder: outputPath,
91
+ dialect,
92
+ connectionUrl,
93
+ databasePath,
94
+ migrationsTable,
95
+ migrationsSchema,
96
+ });
97
+ if (result.applied.length === 0 && !result.failed) {
98
+ console.log(chalk.yellow("No pending migrations"));
99
+ if (result.alreadyApplied.length > 0) {
100
+ console.log(chalk.gray(` ${result.alreadyApplied.length} migration(s) already applied`));
101
+ }
102
+ return;
103
+ }
104
+ for (const item of result.applied) {
105
+ console.log(chalk.green(`✓ Applied: ${item.migrationName} (${item.duration}ms)`));
106
+ }
107
+ if (result.failed) {
108
+ console.log(chalk.red(`✗ Failed: ${result.failed.migrationName}`));
109
+ console.log(chalk.red(` Error: ${result.failed.error}`));
110
+ process.exit(1);
111
+ }
112
+ }
113
+ catch (error) {
114
+ console.error(chalk.red("Error applying migrations:"), error);
115
+ process.exit(1);
116
+ }
117
+ });
118
+ program
119
+ .command("init")
120
+ .description("Initialize snapshot from existing schema (baseline for migrations)")
121
+ .option("-s, --schema <path>", "Path to ZenStack schema")
122
+ .option("-m, --migrations <path>", "Migrations directory")
123
+ .option("--dialect <dialect>", "Database dialect (sqlite, postgres, mysql)")
124
+ .action(async (options) => {
125
+ try {
126
+ const config = await loadConfig(process.cwd());
127
+ const schemaPath = options.schema ?? config?.schema ?? "./schema.zmodel";
128
+ const outputPath = options.migrations ?? config?.migrations?.migrationsFolder ?? "./prisma/migrations";
129
+ const dialect = (options.dialect ?? config?.dialect ?? "sqlite");
130
+ // Scan current state
131
+ const snapshotExists = await hasSnapshot(outputPath);
132
+ const existingMigrations = await scanMigrationFolders(outputPath);
133
+ // CASE A: Snapshot already exists
134
+ if (snapshotExists) {
135
+ const choice = await promptSnapshotExists();
136
+ if (choice === "skip") {
137
+ console.log(chalk.yellow("Skipped - no changes made"));
138
+ return;
139
+ }
140
+ // Reinitialize: overwrite snapshot, rebuild migration log
141
+ console.log(chalk.blue("Reinitializing..."));
142
+ const result = await initializeSnapshot({ schemaPath, outputPath });
143
+ // Rebuild migration log from existing folders
144
+ const migrations = await scanMigrationFolders(outputPath);
145
+ await writeMigrationLog(outputPath, migrations);
146
+ console.log(chalk.green(`✓ Snapshot recreated: ${result.snapshotPath}`));
147
+ console.log(chalk.green(`✓ Migration log rebuilt with ${migrations.length} migration(s)`));
148
+ console.log(chalk.gray(` ${result.tableCount} table(s) captured`));
149
+ return;
150
+ }
151
+ // CASE B: No snapshot but migrations exist (takeover mode)
152
+ if (existingMigrations.length > 0) {
153
+ console.log(chalk.blue("Initializing snapshot..."));
154
+ console.log(chalk.yellow(`Found ${existingMigrations.length} existing migration(s) without snapshot.`));
155
+ console.log(chalk.gray(" Taking over from existing migrations..."));
156
+ for (const migration of existingMigrations) {
157
+ console.log(chalk.gray(` ✓ ${migration.name} (${migration.checksum.slice(0, 8)}...)`));
158
+ }
159
+ const result = await initializeSnapshot({ schemaPath, outputPath });
160
+ await writeMigrationLog(outputPath, existingMigrations);
161
+ console.log(chalk.green(`✓ Snapshot created: ${result.snapshotPath}`));
162
+ console.log(chalk.green(`✓ Migration log created with ${existingMigrations.length} migration(s)`));
163
+ console.log(chalk.gray(` ${result.tableCount} table(s) captured`));
164
+ return;
165
+ }
166
+ // CASE C: Fresh init (no snapshot, no migrations)
167
+ const choice = await promptFreshInit();
168
+ if (choice === "baseline") {
169
+ // Baseline only: create snapshot, empty migration log
170
+ console.log(chalk.blue("Creating baseline snapshot..."));
171
+ const result = await initializeSnapshot({ schemaPath, outputPath });
172
+ await writeMigrationLog(outputPath, []);
173
+ console.log(chalk.green(`✓ Snapshot created: ${result.snapshotPath}`));
174
+ console.log(chalk.green(`✓ Empty migration log created`));
175
+ console.log(chalk.gray(` ${result.tableCount} table(s) captured`));
176
+ console.log(chalk.gray(" Baselined. Future changes will generate migrations."));
177
+ return;
178
+ }
179
+ // Create initial migration
180
+ console.log(chalk.blue("Creating initial migration..."));
181
+ const migration = await createInitialMigration({
182
+ name: "init",
183
+ schemaPath,
184
+ outputPath,
185
+ dialect,
186
+ });
187
+ console.log(chalk.green(`✓ Initial migration created: ${migration.folderName}/migration.sql`));
188
+ console.log(chalk.gray(` Path: ${migration.folderPath}`));
189
+ console.log(chalk.gray(" Run migrate:apply to set up the database."));
190
+ }
191
+ catch (error) {
192
+ console.error(chalk.red("Error initializing:"), error);
193
+ process.exit(1);
194
+ }
195
+ });
196
+ program
197
+ .command("pull")
198
+ .description("Introspect database and generate ZenStack schema")
199
+ .option("-o, --output <path>", "Output path for schema", "./schema.zmodel")
200
+ .option("--dialect <dialect>", "Database dialect (sqlite, postgres, mysql)")
201
+ .option("--url <url>", "Database connection URL")
202
+ .action(async (options) => {
203
+ console.log(chalk.blue("Pulling schema from database..."));
204
+ try {
205
+ const config = await loadConfig(process.cwd());
206
+ const dialect = (options.dialect ?? config?.dialect ?? "sqlite");
207
+ const connectionUrl = options.url ?? config?.dbCredentials?.url;
208
+ if (dialect !== "sqlite" && !connectionUrl) {
209
+ throw new Error("Database connection URL is required for non-sqlite dialects");
210
+ }
211
+ const databasePath = dialect === "sqlite" ? resolveSqlitePath(connectionUrl) : undefined;
212
+ const result = await pullSchema({
213
+ dialect,
214
+ connectionUrl,
215
+ databasePath,
216
+ outputPath: options.output,
217
+ });
218
+ console.log(chalk.green(`✓ Schema generated: ${result.outputPath}`));
219
+ console.log(chalk.gray(` ${result.tableCount} table(s) introspected`));
220
+ }
221
+ catch (error) {
222
+ console.error(chalk.red("Error pulling schema:"), error);
223
+ process.exit(1);
224
+ }
225
+ });
226
+ program.parse();
227
+ async function promptForMigrationName(defaultName) {
228
+ const prompt = getPromptProvider();
229
+ const answer = await prompt.question(`Migration name (${defaultName}): `);
230
+ const trimmed = answer.trim();
231
+ return trimmed.length > 0 ? trimmed : defaultName;
232
+ }
233
+ function resolveSqlitePath(url) {
234
+ if (!url)
235
+ return undefined;
236
+ if (url.startsWith("file:")) {
237
+ return url.slice("file:".length);
238
+ }
239
+ return url;
240
+ }
@@ -0,0 +1,96 @@
1
+ /**
2
+ * Configuration utilities for zenstack-kit
3
+ *
4
+ * Provides a type-safe way to define configuration similar to drizzle-kit's config.
5
+ */
6
+ /** Base configuration shared across all dialects */
7
+ interface BaseConfig {
8
+ /** Path to ZenStack schema file */
9
+ schema: string;
10
+ /** Output directory for generated files */
11
+ out?: string;
12
+ /** Migration settings */
13
+ migrations?: {
14
+ /** Directory for migration files (default: ./prisma/migrations) */
15
+ migrationsFolder?: string;
16
+ /** Table name for migration metadata (default: _prisma_migrations) */
17
+ migrationsTable?: string;
18
+ };
19
+ /** Code generation settings */
20
+ codegen?: {
21
+ /** Use camelCase for column names */
22
+ camelCase?: boolean;
23
+ /** Generate index file */
24
+ generateIndex?: boolean;
25
+ };
26
+ /** Enable verbose logging */
27
+ verbose?: boolean;
28
+ /** Enable strict mode */
29
+ strict?: boolean;
30
+ }
31
+ /** SQLite-specific configuration */
32
+ interface SqliteConfig extends BaseConfig {
33
+ dialect?: "sqlite";
34
+ /** Database credentials for SQLite */
35
+ dbCredentials?: {
36
+ /** Path to SQLite database file */
37
+ file?: string;
38
+ };
39
+ }
40
+ /** PostgreSQL-specific configuration */
41
+ interface PostgresConfig extends BaseConfig {
42
+ dialect: "postgres";
43
+ /** Database credentials for PostgreSQL */
44
+ dbCredentials?: {
45
+ /** Connection URL */
46
+ url?: string;
47
+ /** Or individual connection parameters */
48
+ host?: string;
49
+ port?: number;
50
+ user?: string;
51
+ password?: string;
52
+ database?: string;
53
+ };
54
+ /** Migration settings with PostgreSQL-specific options */
55
+ migrations?: BaseConfig["migrations"] & {
56
+ /** Database schema for migrations table (PostgreSQL only, default: public) */
57
+ migrationsSchema?: string;
58
+ };
59
+ }
60
+ /** MySQL-specific configuration */
61
+ interface MysqlConfig extends BaseConfig {
62
+ dialect: "mysql";
63
+ /** Database credentials for MySQL */
64
+ dbCredentials?: {
65
+ /** Connection URL */
66
+ url?: string;
67
+ /** Or individual connection parameters */
68
+ host?: string;
69
+ port?: number;
70
+ user?: string;
71
+ password?: string;
72
+ database?: string;
73
+ };
74
+ }
75
+ export type ZenStackKitConfig = SqliteConfig | PostgresConfig | MysqlConfig;
76
+ /**
77
+ * Define zenstack-kit configuration
78
+ *
79
+ * @example
80
+ * ```ts
81
+ * // zenstack-kit.config.ts
82
+ * import { defineConfig } from "zenstack-kit";
83
+ *
84
+ * export default defineConfig({
85
+ * schema: "./prisma/schema.zmodel",
86
+ * out: "./src/db",
87
+ * dialect: "postgres",
88
+ * dbCredentials: {
89
+ * url: process.env.DATABASE_URL,
90
+ * },
91
+ * });
92
+ * ```
93
+ */
94
+ export declare function defineConfig<T extends ZenStackKitConfig>(config: T): T;
95
+ export {};
96
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/config/index.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,oDAAoD;AACpD,UAAU,UAAU;IAClB,mCAAmC;IACnC,MAAM,EAAE,MAAM,CAAC;IACf,2CAA2C;IAC3C,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,yBAAyB;IACzB,UAAU,CAAC,EAAE;QACX,mEAAmE;QACnE,gBAAgB,CAAC,EAAE,MAAM,CAAC;QAC1B,sEAAsE;QACtE,eAAe,CAAC,EAAE,MAAM,CAAC;KAC1B,CAAC;IACF,+BAA+B;IAC/B,OAAO,CAAC,EAAE;QACR,qCAAqC;QACrC,SAAS,CAAC,EAAE,OAAO,CAAC;QACpB,0BAA0B;QAC1B,aAAa,CAAC,EAAE,OAAO,CAAC;KACzB,CAAC;IACF,6BAA6B;IAC7B,OAAO,CAAC,EAAE,OAAO,CAAC;IAClB,yBAAyB;IACzB,MAAM,CAAC,EAAE,OAAO,CAAC;CAClB;AAED,oCAAoC;AACpC,UAAU,YAAa,SAAQ,UAAU;IACvC,OAAO,CAAC,EAAE,QAAQ,CAAC;IACnB,sCAAsC;IACtC,aAAa,CA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uration similar to drizzle-kit's config.
5
+ */
6
+ /**
7
+ * Define zenstack-kit configuration
8
+ *
9
+ * @example
10
+ * ```ts
11
+ * // zenstack-kit.config.ts
12
+ * import { defineConfig } from "zenstack-kit";
13
+ *
14
+ * export default defineConfig({
15
+ * schema: "./prisma/schema.zmodel",
16
+ * out: "./src/db",
17
+ * dialect: "postgres",
18
+ * dbCredentials: {
19
+ * url: process.env.DATABASE_URL,
20
+ * },
21
+ * });
22
+ * ```
23
+ */
24
+ export function defineConfig(config) {
25
+ const dialect = config.dialect ?? "sqlite";
26
+ const baseMigrations = {
27
+ migrationsFolder: "./prisma/migrations",
28
+ migrationsTable: "_prisma_migrations",
29
+ ...config.migrations,
30
+ };
31
+ // Add migrationsSchema only for PostgreSQL
32
+ const migrations = dialect === "postgres"
33
+ ? { migrationsSchema: "public", ...baseMigrations }
34
+ : baseMigrations;
35
+ return {
36
+ dialect,
37
+ verbose: false,
38
+ strict: false,
39
+ ...config,
40
+ out: config.out ?? "./generated",
41
+ migrations,
42
+ codegen: {
43
+ camelCase: true,
44
+ generateIndex: true,
45
+ ...config.codegen,
46
+ },
47
+ };
48
+ }
@@ -0,0 +1,11 @@
1
+ /**
2
+ * Configuration loader for zenstack-kit
3
+ */
4
+ import type { ZenStackKitConfig } from "./index.js";
5
+ export interface LoadedConfig {
6
+ config: ZenStackKitConfig;
7
+ configPath: string;
8
+ configDir: string;
9
+ }
10
+ export declare function loadConfig(cwd: string): Promise<LoadedConfig | null>;
11
+ //# sourceMappingURL=loader.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"loader.d.ts","sourceRoot":"","sources":["../../src/config/loader.ts"],"names":[],"mappings":"AAAA;;GAEG;AAIH,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,YAAY,CAAC;AAUpD,MAAM,WAAW,YAAY;IAC3B,MAAM,EAAE,iBAAiB,CAAC;IAC1B,UAAU,EAAE,MAAM,CAAC;IACnB,SAAS,EAAE,MAAM,CAAC;CACnB;AAED,wBAAsB,UAAU,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,YAAY,GAAG,IAAI,CAAC,CA4B1E"}
@@ -0,0 +1,44 @@
1
+ /**
2
+ * Configuration loader for zenstack-kit
3
+ */
4
+ import * as fs from "fs";
5
+ import * as path from "path";
6
+ import { createRequire } from "module";
7
+ const CONFIG_FILES = [
8
+ "zenstack-kit.config.ts",
9
+ "zenstack-kit.config.js",
10
+ "zenstack-kit.config.mjs",
11
+ "zenstack-kit.config.cjs",
12
+ ];
13
+ export async function loadConfig(cwd) {
14
+ const configPath = CONFIG_FILES.map((file) => path.join(cwd, file)).find((file) => fs.existsSync(file));
15
+ if (!configPath) {
16
+ return null;
17
+ }
18
+ const ext = path.extname(configPath);
19
+ let config;
20
+ if (ext === ".cjs") {
21
+ const require = createRequire(import.meta.url);
22
+ const loaded = require(configPath);
23
+ config = (loaded.default ?? loaded);
24
+ }
25
+ else if (ext === ".js" || ext === ".mjs") {
26
+ const loaded = await import(pathToFileUrl(configPath));
27
+ config = (loaded.default ?? loaded);
28
+ }
29
+ else {
30
+ const { default: jiti } = await import("jiti");
31
+ const loader = jiti(import.meta.url, { interopDefault: true });
32
+ const loaded = loader(configPath);
33
+ config = (loaded.default ?? loaded);
34
+ }
35
+ return {
36
+ config,
37
+ configPath,
38
+ configDir: path.dirname(configPath),
39
+ };
40
+ }
41
+ function pathToFileUrl(filePath) {
42
+ const resolved = path.resolve(filePath);
43
+ return new URL(`file://${resolved}`).href;
44
+ }
@@ -0,0 +1,6 @@
1
+ /**
2
+ * Configuration loader for zenstack-kit
3
+ */
4
+ import type { ZenStackKitConfig } from "./config.js";
5
+ export declare function loadConfig(cwd: string): Promise<ZenStackKitConfig | null>;
6
+ //# sourceMappingURL=config-loader.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"config-loader.d.ts","sourceRoot":"","sources":["../src/config-loader.ts"],"names":[],"mappings":"AAAA;;GAEG;AAIH,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,aAAa,CAAC;AAUrD,wBAAsB,UAAU,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,iBAAiB,GAAG,IAAI,CAAC,CAuB/E"}
@@ -0,0 +1,36 @@
1
+ /**
2
+ * Configuration loader for zenstack-kit
3
+ */
4
+ import * as fs from "fs";
5
+ import * as path from "path";
6
+ import { createRequire } from "module";
7
+ const CONFIG_FILES = [
8
+ "zenstack-kit.config.ts",
9
+ "zenstack-kit.config.js",
10
+ "zenstack-kit.config.mjs",
11
+ "zenstack-kit.config.cjs",
12
+ ];
13
+ export async function loadConfig(cwd) {
14
+ const configPath = CONFIG_FILES.map((file) => path.join(cwd, file)).find((file) => fs.existsSync(file));
15
+ if (!configPath) {
16
+ return null;
17
+ }
18
+ const ext = path.extname(configPath);
19
+ if (ext === ".cjs") {
20
+ const require = createRequire(import.meta.url);
21
+ const loaded = require(configPath);
22
+ return (loaded.default ?? loaded);
23
+ }
24
+ if (ext === ".js" || ext === ".mjs") {
25
+ const loaded = await import(pathToFileUrl(configPath));
26
+ return (loaded.default ?? loaded);
27
+ }
28
+ const { default: jiti } = await import("jiti");
29
+ const loader = jiti(import.meta.url, { interopDefault: true });
30
+ const loaded = loader(configPath);
31
+ return (loaded.default ?? loaded);
32
+ }
33
+ function pathToFileUrl(filePath) {
34
+ const resolved = path.resolve(filePath);
35
+ return new URL(`file://${resolved}`).href;
36
+ }
@@ -0,0 +1,62 @@
1
+ /**
2
+ * Configuration utilities for zenstack-kit
3
+ *
4
+ * Provides a type-safe way to define configuration similar to drizzle-kit's config.
5
+ */
6
+ export interface ZenStackKitConfig {
7
+ /** Path to ZenStack schema file */
8
+ schema: string;
9
+ /** Output directory for generated files */
10
+ out: string;
11
+ /** Database dialect */
12
+ dialect?: "sqlite" | "postgres" | "mysql";
13
+ /** Database connection string */
14
+ dbCredentials?: {
15
+ url?: string;
16
+ host?: string;
17
+ port?: number;
18
+ user?: string;
19
+ password?: string;
20
+ database?: string;
21
+ };
22
+ /** Migration settings */
23
+ migrations?: {
24
+ /** Directory for migration files (default: ./prisma/migrations) */
25
+ migrationsFolder?: string;
26
+ /** Table name for migration metadata (default: _prisma_migrations) */
27
+ migrationsTable?: string;
28
+ /** Database schema for migrations table (PostgreSQL only, default: public) */
29
+ migrationsSchema?: string;
30
+ };
31
+ /** Code generation settings */
32
+ codegen?: {
33
+ /** Use camelCase for column names */
34
+ camelCase?: boolean;
35
+ /** Generate index file */
36
+ generateIndex?: boolean;
37
+ };
38
+ /** Enable verbose logging */
39
+ verbose?: boolean;
40
+ /** Enable strict mode */
41
+ strict?: boolean;
42
+ }
43
+ /**
44
+ * Define zenstack-kit configuration
45
+ *
46
+ * @example
47
+ * ```ts
48
+ * // zenstack-kit.config.ts
49
+ * import { defineConfig } from "zenstack-kit";
50
+ *
51
+ * export default defineConfig({
52
+ * schema: "./prisma/schema.zmodel",
53
+ * out: "./src/db",
54
+ * dialect: "postgres",
55
+ * dbCredentials: {
56
+ * url: process.env.DATABASE_URL,
57
+ * },
58
+ * });
59
+ * ```
60
+ */
61
+ export declare function defineConfig(config: ZenStackKitConfig): ZenStackKitConfig;
62
+ //# sourceMappingURL=config.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"config.d.ts","sourceRoot":"","sources":["../src/config.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,MAAM,WAAW,iBAAiB;IAChC,mCAAmC;IACnC,MAAM,EAAE,MAAM,CAAC;IACf,2CAA2C;IAC3C,GAAG,EAAE,MAAM,CAAC;IACZ,uBAAuB;IACvB,OAAO,CAAC,EAAE,QAAQ,GAAG,UAAU,GAAG,OAAO,CAAC;IAC1C,iCAAiC;IACjC,aAAa,CAAC,EAAE;QACd,GAAG,CAAC,EAAE,MAAM,CAAC;QACb,IAAI,CAAC,EAAE,MAAM,CAAC;QACd,IAAI,CAAC,EAAE,MAAM,CAAC;QACd,IAAI,CAAC,EAAE,MAAM,CAAC;QACd,QAAQ,CAAC,EAAE,MAAM,CAAC;QAClB,QAAQ,CAAC,EAAE,MAAM,CAAC;KACnB,CAAC;IACF,yBAAyB;IACzB,UAAU,CAAC,EAAE;QACX,mEAAmE;QACnE,gBAAgB,CAAC,EAAE,MAAM,CAAC;QAC1B,sEAAsE;QACtE,eAAe,CAAC,EAAE,MAAM,CAAC;QACzB,8EAA8E;QAC9E,gBAAgB,CAAC,EAAE,MAAM,CAAC;KAC3B,CAAC;IACF,+BAA+B;IAC/B,OAAO,CAAC,EAAE;QACR,qCAAqC;QACrC,SAAS,CAAC,EAAE,OAAO,CAAC;QACpB,0BAA0B;QAC1B,aAAa,CAAC,EAAE,OAAO,CAAC;KACzB,CAAC;IACF,6BAA6B;IAC7B,OAAO,CAAC,EAAE,OAAO,CAAC;IAClB,yBAAyB;IACzB,MAAM,CAAC,EAAE,OAAO,CAAC;CAClB;AAED;;;;;;;;;;;;;;;;;GAiBG;AACH,wBAAgB,YAAY,CAAC,MAAM,EAAE,iBAAiB,GAAG,iBAAiB,CAoBzE"}
package/dist/config.js ADDED
@@ -0,0 +1,44 @@
1
+ /**
2
+ * Configuration utilities for zenstack-kit
3
+ *
4
+ * Provides a type-safe way to define configuration similar to drizzle-kit's config.
5
+ */
6
+ /**
7
+ * Define zenstack-kit configuration
8
+ *
9
+ * @example
10
+ * ```ts
11
+ * // zenstack-kit.config.ts
12
+ * import { defineConfig } from "zenstack-kit";
13
+ *
14
+ * export default defineConfig({
15
+ * schema: "./prisma/schema.zmodel",
16
+ * out: "./src/db",
17
+ * dialect: "postgres",
18
+ * dbCredentials: {
19
+ * url: process.env.DATABASE_URL,
20
+ * },
21
+ * });
22
+ * ```
23
+ */
24
+ export function defineConfig(config) {
25
+ return {
26
+ // Default values
27
+ dialect: "sqlite",
28
+ verbose: false,
29
+ strict: false,
30
+ ...config,
31
+ out: config.out ?? "./generated",
32
+ migrations: {
33
+ migrationsFolder: "./prisma/migrations",
34
+ migrationsTable: "_prisma_migrations",
35
+ migrationsSchema: "public",
36
+ ...config.migrations,
37
+ },
38
+ codegen: {
39
+ camelCase: true,
40
+ generateIndex: true,
41
+ ...config.codegen,
42
+ },
43
+ };
44
+ }
@@ -0,0 +1,19 @@
1
+ /**
2
+ * zenstack-kit - Drizzle-kit like CLI tooling for ZenStack schemas with Kysely support
3
+ *
4
+ * This package provides database migration and introspection utilities for ZenStack V3
5
+ * schemas, generating Kysely-compatible type definitions and migration files.
6
+ *
7
+ * @packageDocumentation
8
+ */
9
+ export { introspectSchema, type SchemaInfo, type ModelInfo, type FieldInfo } from "./schema/introspect.js";
10
+ export { createMigration, getSchemaDiff, hasSchemaChanges, initSnapshot, type MigrationOptions, type Migration, type InitSnapshotOptions, type InitSnapshotResult, } from "./migrations/diff.js";
11
+ export { applyMigrations, type ApplyMigrationsOptions } from "./migrations/apply.js";
12
+ export { setPromptProvider, type PromptProvider } from "./cli/prompt-provider.js";
13
+ export { createPrismaMigration, applyPrismaMigrations, previewPrismaMigrations, hasPrismaSchemaChanges, createInitialMigration, initializeSnapshot, hasSnapshot, scanMigrationFolders, readMigrationLog, writeMigrationLog, appendToMigrationLog, getMigrationLogPath, calculateChecksum, detectPotentialRenames, type PrismaMigrationOptions, type PrismaMigration, type ApplyPrismaMigrationsOptions, type ApplyPrismaMigrationsResult, type PreviewPrismaMigrationsResult, type CreateInitialMigrationOptions, type MigrationLogEntry, type PotentialTableRename, type PotentialColumnRename, type PotentialRenames, } from "./migrations/prisma.js";
14
+ export { migrate, type MigrateOptions, type MigrateResult } from "./migrate.js";
15
+ export { defineConfig, type ZenStackKitConfig } from "./config/index.js";
16
+ export { type RenameChoice } from "./cli/prompts.js";
17
+ export { createKyselyAdapter, type KyselyAdapter, type KyselyDialect, } from "./sql/kysely-adapter.js";
18
+ export { pullSchema, type PullOptions, type PullResult } from "./schema/pull.js";
19
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;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U,EAAE,MAAM,kBAAkB,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1,23 @@
1
+ /**
2
+ * zenstack-kit - Drizzle-kit like CLI tooling for ZenStack schemas with Kysely support
3
+ *
4
+ * This package provides database migration and introspection utilities for ZenStack V3
5
+ * schemas, generating Kysely-compatible type definitions and migration files.
6
+ *
7
+ * @packageDocumentation
8
+ */
9
+ // Core functionality
10
+ export { introspectSchema } from "./schema/introspect.js";
11
+ export { createMigration, getSchemaDiff, hasSchemaChanges, initSnapshot, } from "./migrations/diff.js";
12
+ export { applyMigrations } from "./migrations/apply.js";
13
+ export { setPromptProvider } from "./cli/prompt-provider.js";
14
+ // Prisma-compatible migrations (default)
15
+ export { createPrismaMigration, applyPrismaMigrations, previewPrismaMigrations, hasPrismaSchemaChanges, createInitialMigration, initializeSnapshot, hasSnapshot, scanMigrationFolders, readMigrationLog, writeMigrationLog, appendToMigrationLog, getMigrationLogPath, calculateChecksum, detectPotentialRenames, } from "./migrations/prisma.js";
16
+ // High-level programmatic API
17
+ export { migrate } from "./migrate.js";
18
+ // CLI utilities
19
+ export { defineConfig } from "./config/index.js";
20
+ // Kysely integration
21
+ export { createKyselyAdapter, } from "./sql/kysely-adapter.js";
22
+ // Database pull (introspection)
23
+ export { pullSchema } from "./schema/pull.js";