kysely-schema 0.1.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.
@@ -0,0 +1,20 @@
1
+
2
+ > kysely-schema@0.1.0 build C:\Codes\node\2026\github\kysely-schema\packages\core
3
+ > tsup
4
+
5
+ CLI Building entry: src/index.ts
6
+ CLI Using tsconfig: tsconfig.json
7
+ CLI tsup v8.5.1
8
+ CLI Using tsup config: C:\Codes\node\2026\github\kysely-schema\packages\core\tsup.config.ts
9
+ CLI Target: es2022
10
+ CLI Cleaning output folder
11
+ ESM Build start
12
+ CJS Build start
13
+ CJS dist\index.js 18.21 KB
14
+ CJS ⚡️ Build success in 41ms
15
+ ESM dist\index.mjs 16.80 KB
16
+ ESM ⚡️ Build success in 42ms
17
+ DTS Build start
18
+ DTS ⚡️ Build success in 655ms
19
+ DTS dist\index.d.mts 7.09 KB
20
+ DTS dist\index.d.ts 7.09 KB
@@ -0,0 +1,207 @@
1
+ type ReferentialAction = 'cascade' | 'set null' | 'restrict' | 'no action';
2
+ type ColumnType = 'serial' | 'integer' | 'bigint' | 'decimal' | 'text' | 'varchar' | 'timestamp' | 'date' | 'time' | 'boolean' | 'json' | 'jsonb' | 'binary' | 'uuid';
3
+ interface ForeignKeyReference {
4
+ table: string;
5
+ column: string;
6
+ }
7
+ interface ColumnDefinition {
8
+ type: ColumnType;
9
+ primaryKey?: boolean;
10
+ notNull?: boolean;
11
+ nullable?: boolean;
12
+ unique?: boolean;
13
+ default?: string | number | boolean;
14
+ references?: ForeignKeyReference;
15
+ onDelete?: ReferentialAction;
16
+ onUpdate?: ReferentialAction;
17
+ index?: boolean;
18
+ check?: string;
19
+ length?: number;
20
+ precision?: number;
21
+ scale?: number;
22
+ }
23
+ interface IndexDefinition {
24
+ name?: string;
25
+ columns: string[];
26
+ unique?: boolean;
27
+ }
28
+ interface TableDefinition {
29
+ columns: Record<string, ColumnDefinition>;
30
+ indexes: IndexDefinition[];
31
+ checks?: string[];
32
+ }
33
+ interface SchemaDefinition {
34
+ tables: Record<string, TableDefinition>;
35
+ }
36
+ interface MigrationFile {
37
+ filename: string;
38
+ content: string;
39
+ }
40
+ type DiffOperationType = 'addTable' | 'dropTable' | 'addColumn' | 'dropColumn' | 'alterColumn' | 'addIndex' | 'dropIndex';
41
+ interface BaseDiffOperation {
42
+ type: DiffOperationType;
43
+ description: string;
44
+ }
45
+ interface AddTableOperation extends BaseDiffOperation {
46
+ type: 'addTable';
47
+ tableName: string;
48
+ table: TableDefinition;
49
+ }
50
+ interface DropTableOperation extends BaseDiffOperation {
51
+ type: 'dropTable';
52
+ tableName: string;
53
+ }
54
+ interface AddColumnOperation extends BaseDiffOperation {
55
+ type: 'addColumn';
56
+ tableName: string;
57
+ columnName: string;
58
+ column: ColumnDefinition;
59
+ }
60
+ interface DropColumnOperation extends BaseDiffOperation {
61
+ type: 'dropColumn';
62
+ tableName: string;
63
+ columnName: string;
64
+ }
65
+ interface AlterColumnOperation extends BaseDiffOperation {
66
+ type: 'alterColumn';
67
+ tableName: string;
68
+ columnName: string;
69
+ oldColumn: ColumnDefinition;
70
+ newColumn: ColumnDefinition;
71
+ }
72
+ interface AddIndexOperation extends BaseDiffOperation {
73
+ type: 'addIndex';
74
+ tableName: string;
75
+ index: IndexDefinition;
76
+ }
77
+ interface DropIndexOperation extends BaseDiffOperation {
78
+ type: 'dropIndex';
79
+ tableName: string;
80
+ indexName: string;
81
+ }
82
+ type DiffOperation = AddTableOperation | DropTableOperation | AddColumnOperation | DropColumnOperation | AlterColumnOperation | AddIndexOperation | DropIndexOperation;
83
+ interface SchemaSnapshot {
84
+ version: string;
85
+ timestamp: string;
86
+ schema: SchemaDefinition;
87
+ }
88
+ interface KyselySchemaConfig {
89
+ schemaPath: string;
90
+ migrationsDir: string;
91
+ generatedDir: string;
92
+ snapshotDir: string;
93
+ }
94
+
95
+ declare class ColumnBuilder {
96
+ private def;
97
+ constructor(init: Partial<ColumnDefinition> & {
98
+ type: ColumnType;
99
+ });
100
+ primaryKey(): this;
101
+ notNull(): this;
102
+ nullable(): this;
103
+ unique(): this;
104
+ default(value: string | number | boolean): this;
105
+ references(table: string, col: string): this;
106
+ onDelete(action: ReferentialAction): this;
107
+ onUpdate(action: ReferentialAction): this;
108
+ index(): this;
109
+ check(expression: string): this;
110
+ /** @internal Return the raw column definition. */
111
+ build(): ColumnDefinition;
112
+ }
113
+ declare const column: {
114
+ serial: () => ColumnBuilder;
115
+ integer: () => ColumnBuilder;
116
+ bigint: () => ColumnBuilder;
117
+ decimal: (precision?: number, scale?: number) => ColumnBuilder;
118
+ text: () => ColumnBuilder;
119
+ varchar: (length?: number) => ColumnBuilder;
120
+ timestamp: () => ColumnBuilder;
121
+ date: () => ColumnBuilder;
122
+ time: () => ColumnBuilder;
123
+ boolean: () => ColumnBuilder;
124
+ json: () => ColumnBuilder;
125
+ jsonb: () => ColumnBuilder;
126
+ binary: () => ColumnBuilder;
127
+ uuid: () => ColumnBuilder;
128
+ };
129
+ declare function table(columns: Record<string, ColumnBuilder>): TableDefinition;
130
+ declare function defineSchema(tables: Record<string, TableDefinition>): SchemaDefinition;
131
+
132
+ interface ValidationError {
133
+ table: string;
134
+ column?: string;
135
+ message: string;
136
+ }
137
+ /**
138
+ * Validate a schema definition and return a list of problems.
139
+ * Returns an empty array if the schema is valid.
140
+ */
141
+ declare function validateSchema(schema: SchemaDefinition): ValidationError[];
142
+
143
+ /**
144
+ * Generates Kysely migration files from a SchemaDefinition.
145
+ */
146
+ declare class MigrationGenerator {
147
+ /**
148
+ * Generate a full CREATE-TABLE migration for the entire schema.
149
+ */
150
+ generate(schema: SchemaDefinition, migrationName: string): MigrationFile;
151
+ private generateUpFunction;
152
+ private generateCreateTable;
153
+ private generateColumnDefinition;
154
+ private buildModifiers;
155
+ private generateDownFunction;
156
+ private generateIndexes;
157
+ private mapColumnType;
158
+ private formatDefaultValue;
159
+ private generateTimestamp;
160
+ private sanitize;
161
+ }
162
+
163
+ /**
164
+ * Generates a TypeScript `Database` interface from a SchemaDefinition.
165
+ */
166
+ declare class TypeGenerator {
167
+ generate(schema: SchemaDefinition): string;
168
+ private mapToTypeScript;
169
+ private baseTypeMap;
170
+ private toPascalCase;
171
+ }
172
+
173
+ /**
174
+ * Default schema template written by `kysely-schema init`.
175
+ */
176
+ declare const schemaTemplate = "import { defineSchema, table, column } from 'kysely-schema';\n\nexport default defineSchema({\n // Define your tables here. Example:\n //\n // user: table({\n // id: column.serial().primaryKey(),\n // email: column.text().notNull().unique(),\n // name: column.text().nullable(),\n // createdAt: column.timestamp().default('now()').notNull(),\n // }),\n});\n";
177
+ /**
178
+ * Default config file template.
179
+ */
180
+ declare const configTemplate = "import type { KyselySchemaConfig } from 'kysely-schema';\n\nconst config: KyselySchemaConfig = {\n schemaPath: './schema/index.ts',\n migrationsDir: './migrations',\n generatedDir: './generated',\n snapshotDir: './.kysely-schema',\n};\n\nexport default config;\n";
181
+
182
+ /**
183
+ * Compares two schema snapshots and returns a list of diff operations
184
+ * needed to migrate from `previous` to `current`.
185
+ */
186
+ declare class SchemaDiffer {
187
+ diff(previous: SchemaDefinition, current: SchemaDefinition): DiffOperation[];
188
+ /**
189
+ * Generate an ALTER-TABLE migration string from a list of diff operations.
190
+ */
191
+ generateAlterMigration(ops: DiffOperation[]): {
192
+ up: string;
193
+ down: string;
194
+ };
195
+ private columnsEqual;
196
+ }
197
+
198
+ /**
199
+ * Labels used when printing diffs to the console.
200
+ */
201
+ declare const operationLabels: Record<DiffOperation['type'], string>;
202
+ /**
203
+ * Return a human-readable summary for a diff operation.
204
+ */
205
+ declare function describeOperation(op: DiffOperation): string;
206
+
207
+ export { type AddColumnOperation, type AddIndexOperation, type AddTableOperation, type AlterColumnOperation, ColumnBuilder, type ColumnDefinition, type ColumnType, type DiffOperation, type DiffOperationType, type DropColumnOperation, type DropIndexOperation, type DropTableOperation, type ForeignKeyReference, type IndexDefinition, type KyselySchemaConfig, type MigrationFile, MigrationGenerator, type ReferentialAction, type SchemaDefinition, SchemaDiffer, type SchemaSnapshot, type TableDefinition, TypeGenerator, type ValidationError, column, configTemplate, defineSchema, describeOperation, operationLabels, schemaTemplate, table, validateSchema };
@@ -0,0 +1,207 @@
1
+ type ReferentialAction = 'cascade' | 'set null' | 'restrict' | 'no action';
2
+ type ColumnType = 'serial' | 'integer' | 'bigint' | 'decimal' | 'text' | 'varchar' | 'timestamp' | 'date' | 'time' | 'boolean' | 'json' | 'jsonb' | 'binary' | 'uuid';
3
+ interface ForeignKeyReference {
4
+ table: string;
5
+ column: string;
6
+ }
7
+ interface ColumnDefinition {
8
+ type: ColumnType;
9
+ primaryKey?: boolean;
10
+ notNull?: boolean;
11
+ nullable?: boolean;
12
+ unique?: boolean;
13
+ default?: string | number | boolean;
14
+ references?: ForeignKeyReference;
15
+ onDelete?: ReferentialAction;
16
+ onUpdate?: ReferentialAction;
17
+ index?: boolean;
18
+ check?: string;
19
+ length?: number;
20
+ precision?: number;
21
+ scale?: number;
22
+ }
23
+ interface IndexDefinition {
24
+ name?: string;
25
+ columns: string[];
26
+ unique?: boolean;
27
+ }
28
+ interface TableDefinition {
29
+ columns: Record<string, ColumnDefinition>;
30
+ indexes: IndexDefinition[];
31
+ checks?: string[];
32
+ }
33
+ interface SchemaDefinition {
34
+ tables: Record<string, TableDefinition>;
35
+ }
36
+ interface MigrationFile {
37
+ filename: string;
38
+ content: string;
39
+ }
40
+ type DiffOperationType = 'addTable' | 'dropTable' | 'addColumn' | 'dropColumn' | 'alterColumn' | 'addIndex' | 'dropIndex';
41
+ interface BaseDiffOperation {
42
+ type: DiffOperationType;
43
+ description: string;
44
+ }
45
+ interface AddTableOperation extends BaseDiffOperation {
46
+ type: 'addTable';
47
+ tableName: string;
48
+ table: TableDefinition;
49
+ }
50
+ interface DropTableOperation extends BaseDiffOperation {
51
+ type: 'dropTable';
52
+ tableName: string;
53
+ }
54
+ interface AddColumnOperation extends BaseDiffOperation {
55
+ type: 'addColumn';
56
+ tableName: string;
57
+ columnName: string;
58
+ column: ColumnDefinition;
59
+ }
60
+ interface DropColumnOperation extends BaseDiffOperation {
61
+ type: 'dropColumn';
62
+ tableName: string;
63
+ columnName: string;
64
+ }
65
+ interface AlterColumnOperation extends BaseDiffOperation {
66
+ type: 'alterColumn';
67
+ tableName: string;
68
+ columnName: string;
69
+ oldColumn: ColumnDefinition;
70
+ newColumn: ColumnDefinition;
71
+ }
72
+ interface AddIndexOperation extends BaseDiffOperation {
73
+ type: 'addIndex';
74
+ tableName: string;
75
+ index: IndexDefinition;
76
+ }
77
+ interface DropIndexOperation extends BaseDiffOperation {
78
+ type: 'dropIndex';
79
+ tableName: string;
80
+ indexName: string;
81
+ }
82
+ type DiffOperation = AddTableOperation | DropTableOperation | AddColumnOperation | DropColumnOperation | AlterColumnOperation | AddIndexOperation | DropIndexOperation;
83
+ interface SchemaSnapshot {
84
+ version: string;
85
+ timestamp: string;
86
+ schema: SchemaDefinition;
87
+ }
88
+ interface KyselySchemaConfig {
89
+ schemaPath: string;
90
+ migrationsDir: string;
91
+ generatedDir: string;
92
+ snapshotDir: string;
93
+ }
94
+
95
+ declare class ColumnBuilder {
96
+ private def;
97
+ constructor(init: Partial<ColumnDefinition> & {
98
+ type: ColumnType;
99
+ });
100
+ primaryKey(): this;
101
+ notNull(): this;
102
+ nullable(): this;
103
+ unique(): this;
104
+ default(value: string | number | boolean): this;
105
+ references(table: string, col: string): this;
106
+ onDelete(action: ReferentialAction): this;
107
+ onUpdate(action: ReferentialAction): this;
108
+ index(): this;
109
+ check(expression: string): this;
110
+ /** @internal Return the raw column definition. */
111
+ build(): ColumnDefinition;
112
+ }
113
+ declare const column: {
114
+ serial: () => ColumnBuilder;
115
+ integer: () => ColumnBuilder;
116
+ bigint: () => ColumnBuilder;
117
+ decimal: (precision?: number, scale?: number) => ColumnBuilder;
118
+ text: () => ColumnBuilder;
119
+ varchar: (length?: number) => ColumnBuilder;
120
+ timestamp: () => ColumnBuilder;
121
+ date: () => ColumnBuilder;
122
+ time: () => ColumnBuilder;
123
+ boolean: () => ColumnBuilder;
124
+ json: () => ColumnBuilder;
125
+ jsonb: () => ColumnBuilder;
126
+ binary: () => ColumnBuilder;
127
+ uuid: () => ColumnBuilder;
128
+ };
129
+ declare function table(columns: Record<string, ColumnBuilder>): TableDefinition;
130
+ declare function defineSchema(tables: Record<string, TableDefinition>): SchemaDefinition;
131
+
132
+ interface ValidationError {
133
+ table: string;
134
+ column?: string;
135
+ message: string;
136
+ }
137
+ /**
138
+ * Validate a schema definition and return a list of problems.
139
+ * Returns an empty array if the schema is valid.
140
+ */
141
+ declare function validateSchema(schema: SchemaDefinition): ValidationError[];
142
+
143
+ /**
144
+ * Generates Kysely migration files from a SchemaDefinition.
145
+ */
146
+ declare class MigrationGenerator {
147
+ /**
148
+ * Generate a full CREATE-TABLE migration for the entire schema.
149
+ */
150
+ generate(schema: SchemaDefinition, migrationName: string): MigrationFile;
151
+ private generateUpFunction;
152
+ private generateCreateTable;
153
+ private generateColumnDefinition;
154
+ private buildModifiers;
155
+ private generateDownFunction;
156
+ private generateIndexes;
157
+ private mapColumnType;
158
+ private formatDefaultValue;
159
+ private generateTimestamp;
160
+ private sanitize;
161
+ }
162
+
163
+ /**
164
+ * Generates a TypeScript `Database` interface from a SchemaDefinition.
165
+ */
166
+ declare class TypeGenerator {
167
+ generate(schema: SchemaDefinition): string;
168
+ private mapToTypeScript;
169
+ private baseTypeMap;
170
+ private toPascalCase;
171
+ }
172
+
173
+ /**
174
+ * Default schema template written by `kysely-schema init`.
175
+ */
176
+ declare const schemaTemplate = "import { defineSchema, table, column } from 'kysely-schema';\n\nexport default defineSchema({\n // Define your tables here. Example:\n //\n // user: table({\n // id: column.serial().primaryKey(),\n // email: column.text().notNull().unique(),\n // name: column.text().nullable(),\n // createdAt: column.timestamp().default('now()').notNull(),\n // }),\n});\n";
177
+ /**
178
+ * Default config file template.
179
+ */
180
+ declare const configTemplate = "import type { KyselySchemaConfig } from 'kysely-schema';\n\nconst config: KyselySchemaConfig = {\n schemaPath: './schema/index.ts',\n migrationsDir: './migrations',\n generatedDir: './generated',\n snapshotDir: './.kysely-schema',\n};\n\nexport default config;\n";
181
+
182
+ /**
183
+ * Compares two schema snapshots and returns a list of diff operations
184
+ * needed to migrate from `previous` to `current`.
185
+ */
186
+ declare class SchemaDiffer {
187
+ diff(previous: SchemaDefinition, current: SchemaDefinition): DiffOperation[];
188
+ /**
189
+ * Generate an ALTER-TABLE migration string from a list of diff operations.
190
+ */
191
+ generateAlterMigration(ops: DiffOperation[]): {
192
+ up: string;
193
+ down: string;
194
+ };
195
+ private columnsEqual;
196
+ }
197
+
198
+ /**
199
+ * Labels used when printing diffs to the console.
200
+ */
201
+ declare const operationLabels: Record<DiffOperation['type'], string>;
202
+ /**
203
+ * Return a human-readable summary for a diff operation.
204
+ */
205
+ declare function describeOperation(op: DiffOperation): string;
206
+
207
+ export { type AddColumnOperation, type AddIndexOperation, type AddTableOperation, type AlterColumnOperation, ColumnBuilder, type ColumnDefinition, type ColumnType, type DiffOperation, type DiffOperationType, type DropColumnOperation, type DropIndexOperation, type DropTableOperation, type ForeignKeyReference, type IndexDefinition, type KyselySchemaConfig, type MigrationFile, MigrationGenerator, type ReferentialAction, type SchemaDefinition, SchemaDiffer, type SchemaSnapshot, type TableDefinition, TypeGenerator, type ValidationError, column, configTemplate, defineSchema, describeOperation, operationLabels, schemaTemplate, table, validateSchema };