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
@@ -0,0 +1,243 @@
1
+ /**
2
+ * SQL Compiler - Generates raw SQL from schema operations using Kysely's compile()
3
+ *
4
+ * Uses Kysely with DummyDriver to compile schema operations to dialect-specific SQL
5
+ * without requiring a database connection.
6
+ */
7
+ import { Kysely, DummyDriver, SqliteAdapter, SqliteIntrospector, SqliteQueryCompiler, PostgresAdapter, PostgresIntrospector, PostgresQueryCompiler, MysqlAdapter, MysqlIntrospector, MysqlQueryCompiler, sql, } from "kysely";
8
+ /**
9
+ * Create a Kysely instance configured for SQL compilation only (no actual DB connection)
10
+ */
11
+ function createCompilerDb(dialect) {
12
+ if (dialect === "sqlite") {
13
+ return new Kysely({
14
+ dialect: {
15
+ createAdapter: () => new SqliteAdapter(),
16
+ createDriver: () => new DummyDriver(),
17
+ createIntrospector: (db) => new SqliteIntrospector(db),
18
+ createQueryCompiler: () => new SqliteQueryCompiler(),
19
+ },
20
+ });
21
+ }
22
+ else if (dialect === "postgres") {
23
+ return new Kysely({
24
+ dialect: {
25
+ createAdapter: () => new PostgresAdapter(),
26
+ createDriver: () => new DummyDriver(),
27
+ createIntrospector: (db) => new PostgresIntrospector(db),
28
+ createQueryCompiler: () => new PostgresQueryCompiler(),
29
+ },
30
+ });
31
+ }
32
+ else {
33
+ return new Kysely({
34
+ dialect: {
35
+ createAdapter: () => new MysqlAdapter(),
36
+ createDriver: () => new DummyDriver(),
37
+ createIntrospector: (db) => new MysqlIntrospector(db),
38
+ createQueryCompiler: () => new MysqlQueryCompiler(),
39
+ },
40
+ });
41
+ }
42
+ }
43
+ /**
44
+ * Compile a CREATE TABLE statement to SQL
45
+ */
46
+ export function compileCreateTable(model, options) {
47
+ const db = createCompilerDb(options.dialect);
48
+ let builder = db.schema.createTable(model.name);
49
+ for (const column of model.columns) {
50
+ const columnType = mapColumnType(column.type, options.dialect);
51
+ builder = builder.addColumn(column.name, sql.raw(columnType), (cb) => {
52
+ if (column.notNull) {
53
+ cb = cb.notNull();
54
+ }
55
+ if (column.default !== undefined) {
56
+ cb = cb.defaultTo(sql.raw(formatDefault(column.default, options.dialect)));
57
+ }
58
+ return cb;
59
+ });
60
+ }
61
+ // Add primary key constraint
62
+ if (model.primaryKey) {
63
+ builder = builder.addPrimaryKeyConstraint(model.primaryKey.name, model.primaryKey.columns);
64
+ }
65
+ // Add unique constraints
66
+ for (const unique of model.uniqueConstraints) {
67
+ builder = builder.addUniqueConstraint(unique.name, unique.columns);
68
+ }
69
+ // Add foreign key constraints
70
+ for (const fk of model.foreignKeys) {
71
+ builder = builder.addForeignKeyConstraint(fk.name, fk.columns, fk.referencedTable, fk.referencedColumns);
72
+ }
73
+ return builder.compile().sql + ";";
74
+ }
75
+ /**
76
+ * Compile a DROP TABLE statement to SQL
77
+ */
78
+ export function compileDropTable(tableName, options) {
79
+ const db = createCompilerDb(options.dialect);
80
+ return db.schema.dropTable(tableName).ifExists().compile().sql + ";";
81
+ }
82
+ /**
83
+ * Compile an ADD COLUMN statement to SQL
84
+ */
85
+ export function compileAddColumn(tableName, column, options) {
86
+ const db = createCompilerDb(options.dialect);
87
+ const columnType = mapColumnType(column.type, options.dialect);
88
+ return (db.schema
89
+ .alterTable(tableName)
90
+ .addColumn(column.name, sql.raw(columnType), (cb) => {
91
+ if (column.notNull) {
92
+ cb = cb.notNull();
93
+ }
94
+ if (column.default !== undefined) {
95
+ cb = cb.defaultTo(sql.raw(formatDefault(column.default, options.dialect)));
96
+ }
97
+ return cb;
98
+ })
99
+ .compile().sql + ";");
100
+ }
101
+ /**
102
+ * Compile a DROP COLUMN statement to SQL
103
+ */
104
+ export function compileDropColumn(tableName, columnName, options) {
105
+ const db = createCompilerDb(options.dialect);
106
+ return db.schema.alterTable(tableName).dropColumn(columnName).compile().sql + ";";
107
+ }
108
+ /**
109
+ * Compile a RENAME TABLE statement to SQL
110
+ */
111
+ export function compileRenameTable(fromName, toName, options) {
112
+ const db = createCompilerDb(options.dialect);
113
+ return db.schema.alterTable(fromName).renameTo(toName).compile().sql + ";";
114
+ }
115
+ /**
116
+ * Compile a RENAME COLUMN statement to SQL
117
+ */
118
+ export function compileRenameColumn(tableName, fromName, toName, options) {
119
+ const db = createCompilerDb(options.dialect);
120
+ return (db.schema.alterTable(tableName).renameColumn(fromName, toName).compile().sql + ";");
121
+ }
122
+ /**
123
+ * Compile a CREATE INDEX statement to SQL
124
+ */
125
+ export function compileCreateIndex(tableName, indexName, columns, options) {
126
+ const db = createCompilerDb(options.dialect);
127
+ let builder = db.schema.createIndex(indexName).on(tableName);
128
+ for (const col of columns) {
129
+ builder = builder.column(col);
130
+ }
131
+ return builder.compile().sql + ";";
132
+ }
133
+ /**
134
+ * Compile a DROP INDEX statement to SQL
135
+ */
136
+ export function compileDropIndex(indexName, options) {
137
+ const db = createCompilerDb(options.dialect);
138
+ return db.schema.dropIndex(indexName).compile().sql + ";";
139
+ }
140
+ /**
141
+ * Compile an ADD CONSTRAINT (unique) statement to SQL
142
+ */
143
+ export function compileAddUniqueConstraint(tableName, constraintName, columns, options) {
144
+ const db = createCompilerDb(options.dialect);
145
+ return (db.schema
146
+ .alterTable(tableName)
147
+ .addUniqueConstraint(constraintName, columns)
148
+ .compile().sql + ";");
149
+ }
150
+ /**
151
+ * Compile a DROP CONSTRAINT statement to SQL
152
+ */
153
+ export function compileDropConstraint(tableName, constraintName, options) {
154
+ const db = createCompilerDb(options.dialect);
155
+ return (db.schema.alterTable(tableName).dropConstraint(constraintName).compile().sql + ";");
156
+ }
157
+ /**
158
+ * Compile an ADD FOREIGN KEY CONSTRAINT statement to SQL
159
+ */
160
+ export function compileAddForeignKeyConstraint(tableName, constraintName, columns, referencedTable, referencedColumns, options) {
161
+ const db = createCompilerDb(options.dialect);
162
+ return (db.schema
163
+ .alterTable(tableName)
164
+ .addForeignKeyConstraint(constraintName, columns, referencedTable, referencedColumns)
165
+ .compile().sql + ";");
166
+ }
167
+ /**
168
+ * Compile an ADD PRIMARY KEY CONSTRAINT statement to SQL
169
+ */
170
+ export function compileAddPrimaryKeyConstraint(tableName, constraintName, columns, options) {
171
+ const db = createCompilerDb(options.dialect);
172
+ return (db.schema
173
+ .alterTable(tableName)
174
+ .addPrimaryKeyConstraint(constraintName, columns)
175
+ .compile().sql + ";");
176
+ }
177
+ /**
178
+ * Compile ALTER COLUMN statements for type/nullability/default changes
179
+ */
180
+ export function compileAlterColumn(tableName, columnName, changes, options) {
181
+ const db = createCompilerDb(options.dialect);
182
+ const statements = [];
183
+ if (changes.setType) {
184
+ const columnType = mapColumnType(changes.setType, options.dialect);
185
+ statements.push(db.schema
186
+ .alterTable(tableName)
187
+ .alterColumn(columnName, (ac) => ac.setDataType(columnType))
188
+ .compile().sql + ";");
189
+ }
190
+ if (changes.setNotNull) {
191
+ statements.push(db.schema
192
+ .alterTable(tableName)
193
+ .alterColumn(columnName, (ac) => ac.setNotNull())
194
+ .compile().sql + ";");
195
+ }
196
+ if (changes.dropNotNull) {
197
+ statements.push(db.schema
198
+ .alterTable(tableName)
199
+ .alterColumn(columnName, (ac) => ac.dropNotNull())
200
+ .compile().sql + ";");
201
+ }
202
+ if (changes.setDefault !== undefined) {
203
+ statements.push(db.schema
204
+ .alterTable(tableName)
205
+ .alterColumn(columnName, (ac) => ac.setDefault(sql.raw(formatDefault(changes.setDefault, options.dialect))))
206
+ .compile().sql + ";");
207
+ }
208
+ if (changes.dropDefault) {
209
+ statements.push(db.schema
210
+ .alterTable(tableName)
211
+ .alterColumn(columnName, (ac) => ac.dropDefault())
212
+ .compile().sql + ";");
213
+ }
214
+ return statements;
215
+ }
216
+ /**
217
+ * Map our internal type names to dialect-specific SQL types
218
+ */
219
+ function mapColumnType(type, dialect) {
220
+ // Most types are already SQL types from our snapshot, just return as-is
221
+ // The Kysely compiler will handle dialect-specific adjustments
222
+ return type;
223
+ }
224
+ /**
225
+ * Format a default value for SQL
226
+ */
227
+ function formatDefault(value, dialect) {
228
+ if (typeof value === "string") {
229
+ // Check if it's a function call like now() or autoincrement()
230
+ if (/^\w+\([^)]*\)$/.test(value)) {
231
+ return value;
232
+ }
233
+ // Escape string values
234
+ return `'${value.replace(/'/g, "''")}'`;
235
+ }
236
+ if (typeof value === "boolean") {
237
+ if (dialect === "sqlite") {
238
+ return value ? "1" : "0";
239
+ }
240
+ return value ? "true" : "false";
241
+ }
242
+ return String(value);
243
+ }
package/package.json ADDED
@@ -0,0 +1,81 @@
1
+ {
2
+ "name": "zenstack-kit",
3
+ "version": "0.1.1",
4
+ "description": "Drizzle-kit like CLI tooling for ZenStack schemas with Kysely support",
5
+ "type": "module",
6
+ "main": "./dist/index.js",
7
+ "module": "./dist/index.js",
8
+ "types": "./dist/index.d.ts",
9
+ "exports": {
10
+ ".": {
11
+ "import": "./dist/index.js",
12
+ "types": "./dist/index.d.ts"
13
+ }
14
+ },
15
+ "bin": {
16
+ "zenstack-kit": "./dist/cli/index.js"
17
+ },
18
+ "files": [
19
+ "dist",
20
+ "README.md",
21
+ "LICENSE"
22
+ ],
23
+ "scripts": {
24
+ "build": "tsc -p tsconfig.build.json",
25
+ "prepublishOnly": "pnpm run build",
26
+ "test": "vitest run",
27
+ "test:watch": "vitest",
28
+ "typecheck": "tsc --noEmit"
29
+ },
30
+ "keywords": [
31
+ "zenstack",
32
+ "kysely",
33
+ "drizzle-kit",
34
+ "typescript",
35
+ "orm",
36
+ "migrations",
37
+ "database",
38
+ "cli",
39
+ "schema"
40
+ ],
41
+ "author": "",
42
+ "license": "MIT",
43
+ "repository": {
44
+ "type": "git",
45
+ "url": "git+https://github.com/olup/zenstack-kit.git"
46
+ },
47
+ "bugs": {
48
+ "url": "https://github.com/olup/zenstack-kit/issues"
49
+ },
50
+ "homepage": "https://github.com/olup/zenstack-kit#readme",
51
+ "engines": {
52
+ "node": ">=18"
53
+ },
54
+ "peerDependencies": {
55
+ "@zenstackhq/orm": ">=3.0.0",
56
+ "kysely": ">=0.27.0"
57
+ },
58
+ "dependencies": {
59
+ "@zenstackhq/language": "^3.1.0",
60
+ "chalk": "^5.3.0",
61
+ "ink": "^6.6.0",
62
+ "ink-select-input": "^6.2.0",
63
+ "jiti": "^2.4.2",
64
+ "react": "^19.2.3"
65
+ },
66
+ "devDependencies": {
67
+ "@testcontainers/postgresql": "^10.18.0",
68
+ "@types/node": "^22.0.0",
69
+ "@types/pg": "^8.11.10",
70
+ "@types/react": "^19.2.7",
71
+ "@zenstackhq/cli": "^3.1.0",
72
+ "@zenstackhq/orm": "^3.1.0",
73
+ "better-sqlite3": "^12.5.0",
74
+ "kysely": "^0.28.9",
75
+ "pg": "^8.13.1",
76
+ "prisma": "^7.2.0",
77
+ "tsx": "^4.21.0",
78
+ "typescript": "^5.9.3",
79
+ "vitest": "^4.0.16"
80
+ }
81
+ }