@syncular/typegen 0.0.1 → 0.0.2-127
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/README.md +51 -0
- package/dist/generate.d.ts.map +1 -1
- package/dist/generate.js +12 -9
- package/dist/generate.js.map +1 -1
- package/dist/index.js +5 -5
- package/dist/introspect-sqlite.d.ts.map +1 -1
- package/dist/introspect-sqlite.js +10 -2
- package/dist/introspect-sqlite.js.map +1 -1
- package/dist/introspect.js +2 -2
- package/dist/map-types.d.ts +5 -5
- package/dist/map-types.d.ts.map +1 -1
- package/dist/map-types.js +19 -22
- package/dist/map-types.js.map +1 -1
- package/dist/render.d.ts +3 -1
- package/dist/render.d.ts.map +1 -1
- package/dist/render.js +24 -5
- package/dist/render.js.map +1 -1
- package/dist/types.d.ts +33 -5
- package/dist/types.d.ts.map +1 -1
- package/package.json +27 -4
- package/src/generate.ts +18 -17
- package/src/introspect-sqlite.ts +11 -2
- package/src/map-types.ts +20 -23
- package/src/render.ts +48 -7
- package/src/types.ts +45 -5
package/README.md
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
# @syncular/typegen
|
|
2
|
+
|
|
3
|
+
Generate TypeScript database types from your migrations.
|
|
4
|
+
|
|
5
|
+
Supports SQLite and Postgres introspection with column-level codec type overrides via `columnCodecs`.
|
|
6
|
+
|
|
7
|
+
## Install
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm install @syncular/typegen
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Usage
|
|
14
|
+
|
|
15
|
+
```ts
|
|
16
|
+
import { codecs } from '@syncular/core';
|
|
17
|
+
import { generateTypes } from '@syncular/typegen';
|
|
18
|
+
import { migrations } from './migrations';
|
|
19
|
+
|
|
20
|
+
await generateTypes({
|
|
21
|
+
migrations,
|
|
22
|
+
output: './src/db.generated.ts',
|
|
23
|
+
dialect: 'postgres',
|
|
24
|
+
columnCodecs: (col) => {
|
|
25
|
+
if (col.table === 'events' && col.column === 'payload') {
|
|
26
|
+
return codecs.stringJson({
|
|
27
|
+
import: { name: 'EventPayload', from: './domain' },
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
if (
|
|
31
|
+
col.table === 'events' &&
|
|
32
|
+
col.column === 'is_active' &&
|
|
33
|
+
col.sqlType?.toLowerCase().includes('int')
|
|
34
|
+
) {
|
|
35
|
+
return codecs.numberBoolean();
|
|
36
|
+
}
|
|
37
|
+
return undefined;
|
|
38
|
+
},
|
|
39
|
+
});
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
## Documentation
|
|
43
|
+
|
|
44
|
+
- Typegen (in migrations guide): https://syncular.dev/docs/build/migrations#type-generation-with-synculartypegen
|
|
45
|
+
|
|
46
|
+
## Links
|
|
47
|
+
|
|
48
|
+
- GitHub: https://github.com/syncular/syncular
|
|
49
|
+
- Issues: https://github.com/syncular/syncular/issues
|
|
50
|
+
|
|
51
|
+
> Status: Alpha. APIs and storage layouts may change between releases.
|
package/dist/generate.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"generate.d.ts","sourceRoot":"","sources":["../src/generate.ts"],"names":[],"mappings":"AAAA;;GAEG;AAOH,OAAO,KAAK,
|
|
1
|
+
{"version":3,"file":"generate.d.ts","sourceRoot":"","sources":["../src/generate.ts"],"names":[],"mappings":"AAAA;;GAEG;AAOH,OAAO,KAAK,EAEV,oBAAoB,EACpB,mBAAmB,EAGpB,MAAM,SAAS,CAAC;AA4CjB;;;;;;;;;;;;;;GAcG;AACH,wBAAsB,aAAa,CAAC,EAAE,EACpC,OAAO,EAAE,oBAAoB,CAAC,EAAE,CAAC,GAChC,OAAO,CAAC,mBAAmB,CAAC,CAmD9B"}
|
package/dist/generate.js
CHANGED
|
@@ -3,21 +3,21 @@
|
|
|
3
3
|
*/
|
|
4
4
|
import { mkdir, writeFile } from 'node:fs/promises';
|
|
5
5
|
import { dirname } from 'node:path';
|
|
6
|
-
import { introspectAllVersions, introspectCurrentSchema } from './introspect';
|
|
7
|
-
import { resolveColumnType } from './map-types';
|
|
8
|
-
import { renderTypes } from './render';
|
|
6
|
+
import { introspectAllVersions, introspectCurrentSchema } from './introspect.js';
|
|
7
|
+
import { resolveColumnType } from './map-types.js';
|
|
8
|
+
import { renderTypes } from './render.js';
|
|
9
9
|
/**
|
|
10
10
|
* Apply type mapping to all columns in the schemas.
|
|
11
11
|
* Returns the schemas with tsType filled in and any custom imports collected.
|
|
12
12
|
*/
|
|
13
|
-
function applyTypeMappings(schemas, dialect,
|
|
13
|
+
function applyTypeMappings(schemas, dialect, columnCodecs) {
|
|
14
14
|
const allImports = [];
|
|
15
15
|
const mapped = schemas.map((schema) => ({
|
|
16
16
|
...schema,
|
|
17
17
|
tables: schema.tables.map((table) => ({
|
|
18
18
|
...table,
|
|
19
19
|
columns: table.columns.map((col) => {
|
|
20
|
-
const
|
|
20
|
+
const columnInfo = {
|
|
21
21
|
table: table.name,
|
|
22
22
|
column: col.name,
|
|
23
23
|
sqlType: col.sqlType,
|
|
@@ -25,7 +25,9 @@ function applyTypeMappings(schemas, dialect, resolveType) {
|
|
|
25
25
|
isPrimaryKey: col.isPrimaryKey,
|
|
26
26
|
hasDefault: col.hasDefault,
|
|
27
27
|
dialect,
|
|
28
|
-
}
|
|
28
|
+
};
|
|
29
|
+
const codec = columnCodecs?.(columnInfo);
|
|
30
|
+
const resolved = resolveColumnType(columnInfo, codec?.ts);
|
|
29
31
|
allImports.push(...resolved.imports);
|
|
30
32
|
return {
|
|
31
33
|
...col,
|
|
@@ -52,7 +54,7 @@ function applyTypeMappings(schemas, dialect, resolveType) {
|
|
|
52
54
|
* ```
|
|
53
55
|
*/
|
|
54
56
|
export async function generateTypes(options) {
|
|
55
|
-
const { migrations, output, extendsSyncClientDb, includeVersionHistory, tables, dialect = 'sqlite',
|
|
57
|
+
const { migrations, output, extendsSyncClientDb, syncularImportType, includeVersionHistory, tables, dialect = 'sqlite', columnCodecs, } = options;
|
|
56
58
|
// Introspect schemas (raw SQL types, no TS mapping yet)
|
|
57
59
|
let rawSchemas;
|
|
58
60
|
if (includeVersionHistory) {
|
|
@@ -62,12 +64,13 @@ export async function generateTypes(options) {
|
|
|
62
64
|
const current = await introspectCurrentSchema(migrations, dialect, tables);
|
|
63
65
|
rawSchemas = [current];
|
|
64
66
|
}
|
|
65
|
-
// Apply type mapping (default +
|
|
66
|
-
const { schemas, customImports } = applyTypeMappings(rawSchemas, dialect,
|
|
67
|
+
// Apply type mapping (default + column codec overrides)
|
|
68
|
+
const { schemas, customImports } = applyTypeMappings(rawSchemas, dialect, columnCodecs);
|
|
67
69
|
// Render TypeScript code
|
|
68
70
|
const code = renderTypes({
|
|
69
71
|
schemas,
|
|
70
72
|
extendsSyncClientDb,
|
|
73
|
+
syncularImportType,
|
|
71
74
|
includeVersionHistory,
|
|
72
75
|
customImports,
|
|
73
76
|
});
|
package/dist/generate.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"generate.js","sourceRoot":"","sources":["../src/generate.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,EAAE,KAAK,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAC;AACpD,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AACpC,OAAO,EAAE,qBAAqB,EAAE,uBAAuB,EAAE,MAAM,cAAc,CAAC;AAC9E,OAAO,EAAE,iBAAiB,EAAE,MAAM,aAAa,CAAC;AAChD,OAAO,EAAE,WAAW,EAAE,MAAM,UAAU,CAAC;AASvC;;;GAGG;AACH,SAAS,iBAAiB,CACxB,OAA0B,EAC1B,OAAuB,EACvB,
|
|
1
|
+
{"version":3,"file":"generate.js","sourceRoot":"","sources":["../src/generate.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,EAAE,KAAK,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAC;AACpD,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AAC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M,CAAC,MAAM,IAAI,CAAC;QAC5C,IAAI;KACL,CAAC;AAAA,CACH"}
|
package/dist/index.js
CHANGED
|
@@ -6,9 +6,9 @@
|
|
|
6
6
|
* 2. Introspecting the resulting schema
|
|
7
7
|
* 3. Generating TypeScript interfaces
|
|
8
8
|
*/
|
|
9
|
-
export * from './generate';
|
|
10
|
-
export * from './introspect';
|
|
11
|
-
export * from './map-types';
|
|
12
|
-
export * from './render';
|
|
13
|
-
export * from './types';
|
|
9
|
+
export * from './generate.js';
|
|
10
|
+
export * from './introspect.js';
|
|
11
|
+
export * from './map-types.js';
|
|
12
|
+
export * from './render.js';
|
|
13
|
+
export * from './types.js';
|
|
14
14
|
//# sourceMappingURL=index.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"introspect-sqlite.d.ts","sourceRoot":"","sources":["../src/introspect-sqlite.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,sBAAsB,CAAC;AAE9D,OAAO,KAAK,EAAe,eAAe,EAAE,MAAM,SAAS,CAAC;
|
|
1
|
+
{"version":3,"file":"introspect-sqlite.d.ts","sourceRoot":"","sources":["../src/introspect-sqlite.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,sBAAsB,CAAC;AAE9D,OAAO,KAAK,EAAe,eAAe,EAAE,MAAM,SAAS,CAAC;AAmH5D,wBAAsB,2BAA2B,CAAC,EAAE,GAAG,OAAO,EAC5D,UAAU,EAAE,iBAAiB,CAAC,EAAE,CAAC,EACjC,YAAY,CAAC,EAAE,MAAM,EAAE,GACtB,OAAO,CAAC,eAAe,EAAE,CAAC,CAa5B;AAED,wBAAsB,6BAA6B,CAAC,EAAE,GAAG,OAAO,EAC9D,UAAU,EAAE,iBAAiB,CAAC,EAAE,CAAC,EACjC,YAAY,CAAC,EAAE,MAAM,EAAE,GACtB,OAAO,CAAC,eAAe,CAAC,CAM1B"}
|
|
@@ -13,7 +13,15 @@ async function createSqliteDb() {
|
|
|
13
13
|
const { default: Database } = await import('better-sqlite3');
|
|
14
14
|
return new Database(':memory:');
|
|
15
15
|
}
|
|
16
|
-
function createKysely(sqliteDb) {
|
|
16
|
+
async function createKysely(sqliteDb) {
|
|
17
|
+
if (isBun) {
|
|
18
|
+
const { BunSqliteDialect } = await import('kysely-bun-sqlite');
|
|
19
|
+
return new Kysely({
|
|
20
|
+
dialect: new BunSqliteDialect({
|
|
21
|
+
database: sqliteDb,
|
|
22
|
+
}),
|
|
23
|
+
});
|
|
24
|
+
}
|
|
17
25
|
return new Kysely({
|
|
18
26
|
dialect: new SqliteDialect({
|
|
19
27
|
database: sqliteDb,
|
|
@@ -52,7 +60,7 @@ function getAllTables(sqliteDb) {
|
|
|
52
60
|
async function introspectAtVersion(migrations, targetVersion, filterTables) {
|
|
53
61
|
const sqliteDb = await createSqliteDb();
|
|
54
62
|
try {
|
|
55
|
-
const db = createKysely(sqliteDb);
|
|
63
|
+
const db = await createKysely(sqliteDb);
|
|
56
64
|
for (const migration of migrations.migrations) {
|
|
57
65
|
if (migration.version > targetVersion)
|
|
58
66
|
break;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"introspect-sqlite.js","sourceRoot":"","sources":["../src/introspect-sqlite.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAGH,OAAO,EAAE,MAAM,EAAE,aAAa,EAAE,MAAM,QAAQ,CAAC;AAkB/C,MAAM,KAAK,GAAG,OAAO,UAAU,CAAC,GAAG,KAAK,WAAW,CAAC;AAEpD,KAAK,UAAU,cAAc,GAAsB;IACjD,IAAI,KAAK,EAAE,CAAC;QACV,MAAM,EAAE,QAAQ,EAAE,GAAG,MAAM,MAAM,CAAC,YAAY,CAAC,CAAC;QAChD,OAAO,IAAI,QAAQ,CAAC,UAAU,CAAC,CAAC;IAClC,CAAC;IACD,MAAM,EAAE,OAAO,EAAE,QAAQ,EAAE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, introspectPostgresCurrentSchema, } from './introspect-postgres';
|
|
5
|
-
import { introspectSqliteAllVersions, introspectSqliteCurrentSchema, } from './introspect-sqlite';
|
|
4
|
+
import { introspectPostgresAllVersions, introspectPostgresCurrentSchema, } from './introspect-postgres.js';
|
|
5
|
+
import { introspectSqliteAllVersions, introspectSqliteCurrentSchema, } from './introspect-sqlite.js';
|
|
6
6
|
export async function introspectAllVersions(migrations, dialect, filterTables) {
|
|
7
7
|
if (dialect === 'postgres') {
|
|
8
8
|
return introspectPostgresAllVersions(migrations, filterTables);
|
package/dist/map-types.d.ts
CHANGED
|
@@ -2,9 +2,9 @@
|
|
|
2
2
|
* @syncular/typegen - Type mapping
|
|
3
3
|
*
|
|
4
4
|
* Maps SQL types to TypeScript types for each dialect,
|
|
5
|
-
* with support for
|
|
5
|
+
* with support for column codec type overrides.
|
|
6
6
|
*/
|
|
7
|
-
import type { ColumnInfo,
|
|
7
|
+
import type { ColumnInfo, TypeOverride } from './types';
|
|
8
8
|
export interface ResolvedType {
|
|
9
9
|
tsType: string;
|
|
10
10
|
imports: Array<{
|
|
@@ -13,8 +13,8 @@ export interface ResolvedType {
|
|
|
13
13
|
}>;
|
|
14
14
|
}
|
|
15
15
|
/**
|
|
16
|
-
* Resolve the TypeScript type for a column, applying
|
|
17
|
-
* and falling back to the default dialect mapping.
|
|
16
|
+
* Resolve the TypeScript type for a column, applying an optional override
|
|
17
|
+
* first and falling back to the default dialect mapping.
|
|
18
18
|
*/
|
|
19
|
-
export declare function resolveColumnType(col: ColumnInfo,
|
|
19
|
+
export declare function resolveColumnType(col: ColumnInfo, override?: TypeOverride): ResolvedType;
|
|
20
20
|
//# sourceMappingURL=map-types.d.ts.map
|
package/dist/map-types.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"map-types.d.ts","sourceRoot":"","sources":["../src/map-types.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,KAAK,EAAE,UAAU,
|
|
1
|
+
{"version":3,"file":"map-types.d.ts","sourceRoot":"","sources":["../src/map-types.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,KAAK,EAAE,UAAU,EAAkB,YAAY,EAAE,MAAM,SAAS,CAAC;AAExE,MAAM,WAAW,YAAY;IAC3B,MAAM,EAAE,MAAM,CAAC;IACf,OAAO,EAAE,KAAK,CAAC;QAAE,IAAI,EAAE,MAAM,CAAC;QAAC,IAAI,EAAE,MAAM,CAAA;KAAE,CAAC,CAAC;CAChD;AA6ID;;;GAGG;AACH,wBAAgB,iBAAiB,CAC/B,GAAG,EAAE,UAAU,EACf,QAAQ,CAAC,EAAE,YAAY,GACtB,YAAY,CA4Bd"}
|
package/dist/map-types.js
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
* @syncular/typegen - Type mapping
|
|
3
3
|
*
|
|
4
4
|
* Maps SQL types to TypeScript types for each dialect,
|
|
5
|
-
* with support for
|
|
5
|
+
* with support for column codec type overrides.
|
|
6
6
|
*/
|
|
7
7
|
function mapSqliteType(sqlType) {
|
|
8
8
|
const upper = sqlType.toUpperCase();
|
|
@@ -40,10 +40,11 @@ function mapPostgresType(sqlType) {
|
|
|
40
40
|
if (lower === 'float4' ||
|
|
41
41
|
lower === 'float8' ||
|
|
42
42
|
lower === 'real' ||
|
|
43
|
-
lower === 'double precision'
|
|
44
|
-
lower === 'numeric' ||
|
|
45
|
-
lower === 'decimal')
|
|
43
|
+
lower === 'double precision')
|
|
46
44
|
return 'number';
|
|
45
|
+
// Exact numeric types are string by default to match common pg driver behavior.
|
|
46
|
+
if (lower === 'numeric' || lower === 'decimal')
|
|
47
|
+
return 'string';
|
|
47
48
|
// Boolean
|
|
48
49
|
if (lower === 'bool' || lower === 'boolean')
|
|
49
50
|
return 'boolean';
|
|
@@ -117,31 +118,27 @@ function defaultMapper(dialect) {
|
|
|
117
118
|
return dialect === 'postgres' ? mapPostgresType : mapSqliteType;
|
|
118
119
|
}
|
|
119
120
|
/**
|
|
120
|
-
* Resolve the TypeScript type for a column, applying
|
|
121
|
-
* and falling back to the default dialect mapping.
|
|
121
|
+
* Resolve the TypeScript type for a column, applying an optional override
|
|
122
|
+
* first and falling back to the default dialect mapping.
|
|
122
123
|
*/
|
|
123
|
-
export function resolveColumnType(col,
|
|
124
|
+
export function resolveColumnType(col, override) {
|
|
124
125
|
const imports = [];
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
if (override !== undefined) {
|
|
129
|
-
if (typeof override === 'string') {
|
|
130
|
-
const baseType = override;
|
|
131
|
-
return {
|
|
132
|
-
tsType: col.nullable ? `${baseType} | null` : baseType,
|
|
133
|
-
imports,
|
|
134
|
-
};
|
|
135
|
-
}
|
|
136
|
-
if (override.import) {
|
|
137
|
-
imports.push(override.import);
|
|
138
|
-
}
|
|
139
|
-
const baseType = override.type;
|
|
126
|
+
if (override !== undefined) {
|
|
127
|
+
if (typeof override === 'string') {
|
|
128
|
+
const baseType = override;
|
|
140
129
|
return {
|
|
141
130
|
tsType: col.nullable ? `${baseType} | null` : baseType,
|
|
142
131
|
imports,
|
|
143
132
|
};
|
|
144
133
|
}
|
|
134
|
+
if (override.import) {
|
|
135
|
+
imports.push(override.import);
|
|
136
|
+
}
|
|
137
|
+
const baseType = override.type;
|
|
138
|
+
return {
|
|
139
|
+
tsType: col.nullable ? `${baseType} | null` : baseType,
|
|
140
|
+
imports,
|
|
141
|
+
};
|
|
145
142
|
}
|
|
146
143
|
// Default mapping
|
|
147
144
|
const mapper = defaultMapper(col.dialect);
|
package/dist/map-types.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"map-types.js","sourceRoot":"","sources":["../src/map-types.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AASH,SAAS,aAAa,CAAC,OAAe,EAAU;IAC9C,MAAM,KAAK,GAAG,OAAO,CAAC,WAAW,EAAE,CAAC;IAEpC,IAAI,KAAK,CAAC,QAAQ,CAAC,KAAK,CAAC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,CAAC;YAC1B,OAAO;gBACL,MAAM,EAAE,GAAG,CAAC,QAAQ,CAAC,CAAC,CAAC,GAAG,QAAQ,SAAS,CAAC,CAAC,CAAC,QAAQ;gBACtD,OAAO;aACR,CAAC;QACJ,CAAC;QACD,IAAI,QAAQ,CAAC,MAAM,EAAE,CAAC;YACpB,OAAO,CAAC,IAAI,CAAC,QAAQ,CAAC,MAAM,CAAC,CAAC;QAChC,CAAC;QACD,MAAM,QAAQ,GAAG,QAAQ,CAAC,IAAI,CAAC;QAC/B,OAAO;YACL,MAAM,EAAE,GAAG,CAAC,QAAQ,CAAC,CAAC,CAAC,GAAG,QAAQ,SAAS,CAAC,CAAC,CAAC,QAAQ;YACtD,OAAO;SACR,CAAC;IACJ,CAAC;IAED,kBAAkB;IAClB,MAAM,MAAM,GAAG,aAAa,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC;IAC1C,MAAM,QAAQ,GAAG,MAAM,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC;IACrC,OAAO;QACL,MAAM,EAAE,GAAG,CAAC,QAAQ,CAAC,CAAC,CAAC,GAAG,QAAQ,SAAS,CAAC,CAAC,CAAC,QAAQ;QACtD,OAAO;KACR,CAAC;AAAA,CACH"}
|
package/dist/render.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @syncular/typegen - TypeScript code generation
|
|
3
3
|
*/
|
|
4
|
-
import type { VersionedSchema } from './types';
|
|
4
|
+
import type { SyncularImportType, VersionedSchema } from './types';
|
|
5
5
|
/**
|
|
6
6
|
* Options for rendering types.
|
|
7
7
|
*/
|
|
@@ -10,6 +10,8 @@ export interface RenderOptions {
|
|
|
10
10
|
schemas: VersionedSchema[];
|
|
11
11
|
/** Whether to extend SyncClientDb */
|
|
12
12
|
extendsSyncClientDb?: boolean;
|
|
13
|
+
/** Controls package import style for SyncClientDb (default: 'scoped') */
|
|
14
|
+
syncularImportType?: SyncularImportType;
|
|
13
15
|
/** Generate versioned interfaces */
|
|
14
16
|
includeVersionHistory?: boolean;
|
|
15
17
|
/** Custom imports collected from resolver results */
|
package/dist/render.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"render.d.ts","sourceRoot":"","sources":["../src/render.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,KAAK,
|
|
1
|
+
{"version":3,"file":"render.d.ts","sourceRoot":"","sources":["../src/render.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,KAAK,EAEV,kBAAkB,EAElB,eAAe,EAChB,MAAM,SAAS,CAAC;AAiDjB;;GAEG;AACH,MAAM,WAAW,aAAa;IAC5B,oDAAoD;IACpD,OAAO,EAAE,eAAe,EAAE,CAAC;IAC3B,qCAAqC;IACrC,mBAAmB,CAAC,EAAE,OAAO,CAAC;IAC9B,yEAAyE;IACzE,kBAAkB,CAAC,EAAE,kBAAkB,CAAC;IACxC,oCAAoC;IACpC,qBAAqB,CAAC,EAAE,OAAO,CAAC;IAChC,qDAAqD;IACrD,aAAa,CAAC,EAAE,KAAK,CAAC;QAAE,IAAI,EAAE,MAAM,CAAC;QAAC,IAAI,EAAE,MAAM,CAAA;KAAE,CAAC,CAAC;CACvD;AAkBD;;GAEG;AACH,wBAAgB,WAAW,CAAC,OAAO,EAAE,aAAa,GAAG,MAAM,CAuH1D"}
|
package/dist/render.js
CHANGED
|
@@ -14,9 +14,10 @@ function toPascalCase(str) {
|
|
|
14
14
|
* Render a single column definition.
|
|
15
15
|
*/
|
|
16
16
|
function renderColumn(column) {
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
17
|
+
const tsType = column.hasDefault
|
|
18
|
+
? `Generated<${column.tsType}>`
|
|
19
|
+
: column.tsType;
|
|
20
|
+
return ` ${column.name}: ${tsType};`;
|
|
20
21
|
}
|
|
21
22
|
/**
|
|
22
23
|
* Render a table interface.
|
|
@@ -35,11 +36,24 @@ function renderDbInterface(schema, interfaceName, extendsType) {
|
|
|
35
36
|
.join('\n');
|
|
36
37
|
return `export interface ${interfaceName}${extendsClause} {\n${tableEntries}\n}`;
|
|
37
38
|
}
|
|
39
|
+
function resolveSyncClientImportPath(importType) {
|
|
40
|
+
if (importType === 'umbrella') {
|
|
41
|
+
return 'syncular/client';
|
|
42
|
+
}
|
|
43
|
+
if (importType === 'scoped') {
|
|
44
|
+
return '@syncular/client';
|
|
45
|
+
}
|
|
46
|
+
const clientImportPath = importType.client.trim();
|
|
47
|
+
if (clientImportPath.length === 0) {
|
|
48
|
+
throw new Error('syncularImportType.client must be a non-empty package import path');
|
|
49
|
+
}
|
|
50
|
+
return clientImportPath;
|
|
51
|
+
}
|
|
38
52
|
/**
|
|
39
53
|
* Render complete TypeScript type definitions.
|
|
40
54
|
*/
|
|
41
55
|
export function renderTypes(options) {
|
|
42
|
-
const { schemas, extendsSyncClientDb, includeVersionHistory, customImports } = options;
|
|
56
|
+
const { schemas, extendsSyncClientDb, syncularImportType = 'scoped', includeVersionHistory, customImports, } = options;
|
|
43
57
|
const lines = [];
|
|
44
58
|
// Header
|
|
45
59
|
lines.push('/**');
|
|
@@ -49,7 +63,12 @@ export function renderTypes(options) {
|
|
|
49
63
|
lines.push('');
|
|
50
64
|
// Import SyncClientDb if extending
|
|
51
65
|
if (extendsSyncClientDb) {
|
|
52
|
-
lines.push(
|
|
66
|
+
lines.push(`import type { SyncClientDb } from '${resolveSyncClientImportPath(syncularImportType)}';`);
|
|
67
|
+
lines.push('');
|
|
68
|
+
}
|
|
69
|
+
const usesGenerated = schemas.some((schema) => schema.tables.some((table) => table.columns.some((column) => column.hasDefault)));
|
|
70
|
+
if (usesGenerated) {
|
|
71
|
+
lines.push("import type { Generated } from 'kysely';");
|
|
53
72
|
lines.push('');
|
|
54
73
|
}
|
|
55
74
|
// Render custom imports from resolver
|
package/dist/render.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"render.js","sourceRoot":"","sources":["../src/render.ts"],"names":[],"mapp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I,CAAC,CAAC,OAAO,CAAC,MAAM,KAAK,CAAC,CAAC,OAAO,CAAC,MAAM;QAAE,OAAO,KAAK,CAAC;IAExD,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,CAAC,CAAC,OAAO,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE,CAAC;QAC1C,MAAM,IAAI,GAAG,CAAC,CAAC,OAAO,CAAC,CAAC,CAAE,CAAC;QAC3B,MAAM,IAAI,GAAG,CAAC,CAAC,OAAO,CAAC,CAAC,CAAE,CAAC;QAE3B,IACE,IAAI,CAAC,IAAI,KAAK,IAAI,CAAC,IAAI;YACvB,IAAI,CAAC,MAAM,KAAK,IAAI,CAAC,MAAM;YAC3B,IAAI,CAAC,QAAQ,KAAK,IAAI,CAAC,QAAQ;YAC/B,IAAI,CAAC,UAAU,KAAK,IAAI,CAAC,UAAU,EACnC,CAAC;YACD,OAAO,KAAK,CAAC;QACf,CAAC;IACH,CAAC;IAED,OAAO,IAAI,CAAC;AAAA,CACb"}
|
package/dist/types.d.ts
CHANGED
|
@@ -3,8 +3,12 @@
|
|
|
3
3
|
*/
|
|
4
4
|
import type { DefinedMigrations } from '@syncular/migrations';
|
|
5
5
|
export type TypegenDialect = 'sqlite' | 'postgres';
|
|
6
|
+
export type SyncularImportType = 'scoped' | 'umbrella' | {
|
|
7
|
+
client: string;
|
|
8
|
+
[packageName: string]: string;
|
|
9
|
+
};
|
|
6
10
|
/**
|
|
7
|
-
* Column information
|
|
11
|
+
* Column information for a schema column.
|
|
8
12
|
*/
|
|
9
13
|
export interface ColumnInfo {
|
|
10
14
|
table: string;
|
|
@@ -26,9 +30,23 @@ export type TypeOverride = string | {
|
|
|
26
30
|
};
|
|
27
31
|
};
|
|
28
32
|
/**
|
|
29
|
-
*
|
|
33
|
+
* Column codec definition shared across runtime and typegen.
|
|
34
|
+
* Typegen only consumes the `ts` field, but a full codec object can be
|
|
35
|
+
* provided as a single source of truth.
|
|
36
|
+
*/
|
|
37
|
+
export interface ColumnCodec<App, Db> {
|
|
38
|
+
ts: TypeOverride;
|
|
39
|
+
toDb(value: App): Db;
|
|
40
|
+
fromDb(value: Db): App;
|
|
41
|
+
dialects?: Partial<Record<TypegenDialect, {
|
|
42
|
+
toDb?(value: App): Db;
|
|
43
|
+
fromDb?(value: Db): App;
|
|
44
|
+
}>>;
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Column codec resolver for a column.
|
|
30
48
|
*/
|
|
31
|
-
export type
|
|
49
|
+
export type ColumnCodecResolver = (column: ColumnInfo) => ColumnCodec<unknown, unknown> | undefined;
|
|
32
50
|
/**
|
|
33
51
|
* Parsed table schema.
|
|
34
52
|
*/
|
|
@@ -66,12 +84,22 @@ export interface GenerateTypesOptions<DB = unknown> {
|
|
|
66
84
|
dialect?: TypegenDialect;
|
|
67
85
|
/** Whether to extend SyncClientDb interface (adds sync infrastructure types) */
|
|
68
86
|
extendsSyncClientDb?: boolean;
|
|
87
|
+
/**
|
|
88
|
+
* Controls how syncular package imports are rendered in generated output.
|
|
89
|
+
* - 'scoped' (default): '@syncular/client'
|
|
90
|
+
* - 'umbrella': 'syncular/client'
|
|
91
|
+
* - object: explicit package mapping (must include `client`)
|
|
92
|
+
*/
|
|
93
|
+
syncularImportType?: SyncularImportType;
|
|
69
94
|
/** Generate versioned interfaces (ClientDbV1, ClientDbV2, etc.) */
|
|
70
95
|
includeVersionHistory?: boolean;
|
|
71
96
|
/** Only generate types for these tables (default: all tables) */
|
|
72
97
|
tables?: string[];
|
|
73
|
-
/**
|
|
74
|
-
|
|
98
|
+
/**
|
|
99
|
+
* Optional column codec resolver for per-column type overrides.
|
|
100
|
+
* Receives full column metadata (table, column, sqlType, dialect, etc.).
|
|
101
|
+
*/
|
|
102
|
+
columnCodecs?: ColumnCodecResolver;
|
|
75
103
|
}
|
|
76
104
|
/**
|
|
77
105
|
* Result of type generation.
|
package/dist/types.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,sBAAsB,CAAC;AAE9D,MAAM,MAAM,cAAc,GAAG,QAAQ,GAAG,UAAU,CAAC;AAEnD;;GAEG;AACH,MAAM,WAAW,UAAU;IACzB,KAAK,EAAE,MAAM,CAAC;IACd,MAAM,EAAE,MAAM,CAAC;IACf,OAAO,EAAE,MAAM,CAAC;IAChB,QAAQ,EAAE,OAAO,CAAC;IAClB,YAAY,EAAE,OAAO,CAAC;IACtB,UAAU,EAAE,OAAO,CAAC;IACpB,OAAO,EAAE,cAAc,CAAC;CACzB;AAED;;GAEG;AACH,MAAM,MAAM,YAAY,GACpB,MAAM,GACN;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,MAAM,CAAC,EAAE;QAAE,IAAI,EAAE,MAAM,CAAC;QAAC,IAAI,EAAE,MAAM,CAAA;KAAE,CAAA;CAAE,CAAC;AAE9D
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,sBAAsB,CAAC;AAE9D,MAAM,MAAM,cAAc,GAAG,QAAQ,GAAG,UAAU,CAAC;AAEnD,MAAM,MAAM,kBAAkB,GAC1B,QAAQ,GACR,UAAU,GACV;IACE,MAAM,EAAE,MAAM,CAAC;IACf,CAAC,WAAW,EAAE,MAAM,GAAG,MAAM,CAAC;CAC/B,CAAC;AAEN;;GAEG;AACH,MAAM,WAAW,UAAU;IACzB,KAAK,EAAE,MAAM,CAAC;IACd,MAAM,EAAE,MAAM,CAAC;IACf,OAAO,EAAE,MAAM,CAAC;IAChB,QAAQ,EAAE,OAAO,CAAC;IAClB,YAAY,EAAE,OAAO,CAAC;IACtB,UAAU,EAAE,OAAO,CAAC;IACpB,OAAO,EAAE,cAAc,CAAC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IAChD,iDAAiD;IACjD,UAAU,EAAE,iBAAiB,CAAC,EAAE,CAAC,CAAC;IAClC,2CAA2C;IAC3C,MAAM,EAAE,MAAM,CAAC;IACf,oEAAoE;IACpE,OAAO,CAAC,EAAE,cAAc,CAAC;IACzB,gFAAgF;IAChF,mBAAmB,CAAC,EAAE,OAAO,CAAC;IAC9B;;;;;OAKG;IACH,kBAAkB,CAAC,EAAE,kBAAkB,CAAC;IACxC,mEAAmE;IACnE,qBAAqB,CAAC,EAAE,OAAO,CAAC;IAChC,iEAAiE;IACjE,MAAM,CAAC,EAAE,MAAM,EAAE,CAAC;IAClB;;;OAGG;IACH,YAAY,CAAC,EAAE,mBAAmB,CAAC;CACpC;AAED;;GAEG;AACH,MAAM,WAAW,mBAAmB;IAClC,iCAAiC;IACjC,UAAU,EAAE,MAAM,CAAC;IACnB,6BAA6B;IAC7B,cAAc,EAAE,MAAM,CAAC;IACvB,iCAAiC;IACjC,UAAU,EAAE,MAAM,CAAC;IACnB,gCAAgC;IAChC,IAAI,EAAE,MAAM,CAAC;CACd"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,27 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@syncular/typegen",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.2-127",
|
|
4
|
+
"description": "TypeScript type generator for Syncular schemas",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"author": "Benjamin Kniffler",
|
|
7
|
+
"homepage": "https://syncular.dev",
|
|
8
|
+
"repository": {
|
|
9
|
+
"type": "git",
|
|
10
|
+
"url": "https://github.com/syncular/syncular.git",
|
|
11
|
+
"directory": "packages/typegen"
|
|
12
|
+
},
|
|
13
|
+
"bugs": {
|
|
14
|
+
"url": "https://github.com/syncular/syncular/issues"
|
|
15
|
+
},
|
|
16
|
+
"keywords": [
|
|
17
|
+
"sync",
|
|
18
|
+
"offline-first",
|
|
19
|
+
"realtime",
|
|
20
|
+
"database",
|
|
21
|
+
"typescript",
|
|
22
|
+
"codegen",
|
|
23
|
+
"typescript"
|
|
24
|
+
],
|
|
4
25
|
"private": false,
|
|
5
26
|
"publishConfig": {
|
|
6
27
|
"access": "public"
|
|
@@ -18,19 +39,21 @@
|
|
|
18
39
|
"scripts": {
|
|
19
40
|
"test": "bun test --pass-with-no-tests",
|
|
20
41
|
"tsgo": "tsgo --noEmit",
|
|
21
|
-
"build": "
|
|
42
|
+
"build": "tsgo",
|
|
43
|
+
"release": "bunx syncular-publish"
|
|
22
44
|
},
|
|
23
45
|
"dependencies": {
|
|
24
46
|
"@electric-sql/pglite": "^0.3.15",
|
|
25
|
-
"@syncular/migrations": "
|
|
47
|
+
"@syncular/migrations": "0.0.2-127",
|
|
26
48
|
"better-sqlite3": "^12.6.2",
|
|
49
|
+
"kysely-bun-sqlite": "^0.4.0",
|
|
27
50
|
"kysely-pglite-dialect": "^1.2.0"
|
|
28
51
|
},
|
|
29
52
|
"peerDependencies": {
|
|
30
53
|
"kysely": "^0.28.0"
|
|
31
54
|
},
|
|
32
55
|
"devDependencies": {
|
|
33
|
-
"@syncular/config": "
|
|
56
|
+
"@syncular/config": "0.0.0",
|
|
34
57
|
"@types/better-sqlite3": "^7.6.13",
|
|
35
58
|
"kysely": "*"
|
|
36
59
|
},
|
package/src/generate.ts
CHANGED
|
@@ -8,9 +8,9 @@ import { introspectAllVersions, introspectCurrentSchema } from './introspect';
|
|
|
8
8
|
import { resolveColumnType } from './map-types';
|
|
9
9
|
import { renderTypes } from './render';
|
|
10
10
|
import type {
|
|
11
|
+
ColumnCodecResolver,
|
|
11
12
|
GenerateTypesOptions,
|
|
12
13
|
GenerateTypesResult,
|
|
13
|
-
ResolveTypeFn,
|
|
14
14
|
TypegenDialect,
|
|
15
15
|
VersionedSchema,
|
|
16
16
|
} from './types';
|
|
@@ -22,7 +22,7 @@ import type {
|
|
|
22
22
|
function applyTypeMappings(
|
|
23
23
|
schemas: VersionedSchema[],
|
|
24
24
|
dialect: TypegenDialect,
|
|
25
|
-
|
|
25
|
+
columnCodecs?: ColumnCodecResolver
|
|
26
26
|
): {
|
|
27
27
|
schemas: VersionedSchema[];
|
|
28
28
|
customImports: Array<{ name: string; from: string }>;
|
|
@@ -34,18 +34,17 @@ function applyTypeMappings(
|
|
|
34
34
|
tables: schema.tables.map((table) => ({
|
|
35
35
|
...table,
|
|
36
36
|
columns: table.columns.map((col) => {
|
|
37
|
-
const
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
);
|
|
37
|
+
const columnInfo = {
|
|
38
|
+
table: table.name,
|
|
39
|
+
column: col.name,
|
|
40
|
+
sqlType: col.sqlType,
|
|
41
|
+
nullable: col.nullable,
|
|
42
|
+
isPrimaryKey: col.isPrimaryKey,
|
|
43
|
+
hasDefault: col.hasDefault,
|
|
44
|
+
dialect,
|
|
45
|
+
} as const;
|
|
46
|
+
const codec = columnCodecs?.(columnInfo);
|
|
47
|
+
const resolved = resolveColumnType(columnInfo, codec?.ts);
|
|
49
48
|
allImports.push(...resolved.imports);
|
|
50
49
|
return {
|
|
51
50
|
...col,
|
|
@@ -80,10 +79,11 @@ export async function generateTypes<DB>(
|
|
|
80
79
|
migrations,
|
|
81
80
|
output,
|
|
82
81
|
extendsSyncClientDb,
|
|
82
|
+
syncularImportType,
|
|
83
83
|
includeVersionHistory,
|
|
84
84
|
tables,
|
|
85
85
|
dialect = 'sqlite',
|
|
86
|
-
|
|
86
|
+
columnCodecs,
|
|
87
87
|
} = options;
|
|
88
88
|
|
|
89
89
|
// Introspect schemas (raw SQL types, no TS mapping yet)
|
|
@@ -95,17 +95,18 @@ export async function generateTypes<DB>(
|
|
|
95
95
|
rawSchemas = [current];
|
|
96
96
|
}
|
|
97
97
|
|
|
98
|
-
// Apply type mapping (default +
|
|
98
|
+
// Apply type mapping (default + column codec overrides)
|
|
99
99
|
const { schemas, customImports } = applyTypeMappings(
|
|
100
100
|
rawSchemas,
|
|
101
101
|
dialect,
|
|
102
|
-
|
|
102
|
+
columnCodecs
|
|
103
103
|
);
|
|
104
104
|
|
|
105
105
|
// Render TypeScript code
|
|
106
106
|
const code = renderTypes({
|
|
107
107
|
schemas,
|
|
108
108
|
extendsSyncClientDb,
|
|
109
|
+
syncularImportType,
|
|
109
110
|
includeVersionHistory,
|
|
110
111
|
customImports,
|
|
111
112
|
});
|
package/src/introspect-sqlite.ts
CHANGED
|
@@ -34,7 +34,16 @@ async function createSqliteDb(): Promise<SqliteDb> {
|
|
|
34
34
|
return new Database(':memory:');
|
|
35
35
|
}
|
|
36
36
|
|
|
37
|
-
function createKysely<DB>(sqliteDb: SqliteDb): Kysely<DB
|
|
37
|
+
async function createKysely<DB>(sqliteDb: SqliteDb): Promise<Kysely<DB>> {
|
|
38
|
+
if (isBun) {
|
|
39
|
+
const { BunSqliteDialect } = await import('kysely-bun-sqlite');
|
|
40
|
+
return new Kysely<DB>({
|
|
41
|
+
dialect: new BunSqliteDialect({
|
|
42
|
+
database: sqliteDb as never,
|
|
43
|
+
}),
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
|
|
38
47
|
return new Kysely<DB>({
|
|
39
48
|
dialect: new SqliteDialect({
|
|
40
49
|
database: sqliteDb as never,
|
|
@@ -85,7 +94,7 @@ async function introspectAtVersion<DB = unknown>(
|
|
|
85
94
|
const sqliteDb = await createSqliteDb();
|
|
86
95
|
|
|
87
96
|
try {
|
|
88
|
-
const db = createKysely<DB>(sqliteDb);
|
|
97
|
+
const db = await createKysely<DB>(sqliteDb);
|
|
89
98
|
|
|
90
99
|
for (const migration of migrations.migrations) {
|
|
91
100
|
if (migration.version > targetVersion) break;
|
package/src/map-types.ts
CHANGED
|
@@ -2,10 +2,10 @@
|
|
|
2
2
|
* @syncular/typegen - Type mapping
|
|
3
3
|
*
|
|
4
4
|
* Maps SQL types to TypeScript types for each dialect,
|
|
5
|
-
* with support for
|
|
5
|
+
* with support for column codec type overrides.
|
|
6
6
|
*/
|
|
7
7
|
|
|
8
|
-
import type { ColumnInfo,
|
|
8
|
+
import type { ColumnInfo, TypegenDialect, TypeOverride } from './types';
|
|
9
9
|
|
|
10
10
|
export interface ResolvedType {
|
|
11
11
|
tsType: string;
|
|
@@ -56,12 +56,13 @@ function mapPostgresType(sqlType: string): string {
|
|
|
56
56
|
lower === 'float4' ||
|
|
57
57
|
lower === 'float8' ||
|
|
58
58
|
lower === 'real' ||
|
|
59
|
-
lower === 'double precision'
|
|
60
|
-
lower === 'numeric' ||
|
|
61
|
-
lower === 'decimal'
|
|
59
|
+
lower === 'double precision'
|
|
62
60
|
)
|
|
63
61
|
return 'number';
|
|
64
62
|
|
|
63
|
+
// Exact numeric types are string by default to match common pg driver behavior.
|
|
64
|
+
if (lower === 'numeric' || lower === 'decimal') return 'string';
|
|
65
|
+
|
|
65
66
|
// Boolean
|
|
66
67
|
if (lower === 'bool' || lower === 'boolean') return 'boolean';
|
|
67
68
|
|
|
@@ -151,35 +152,31 @@ function defaultMapper(dialect: TypegenDialect): (sqlType: string) => string {
|
|
|
151
152
|
}
|
|
152
153
|
|
|
153
154
|
/**
|
|
154
|
-
* Resolve the TypeScript type for a column, applying
|
|
155
|
-
* and falling back to the default dialect mapping.
|
|
155
|
+
* Resolve the TypeScript type for a column, applying an optional override
|
|
156
|
+
* first and falling back to the default dialect mapping.
|
|
156
157
|
*/
|
|
157
158
|
export function resolveColumnType(
|
|
158
159
|
col: ColumnInfo,
|
|
159
|
-
|
|
160
|
+
override?: TypeOverride
|
|
160
161
|
): ResolvedType {
|
|
161
162
|
const imports: Array<{ name: string; from: string }> = [];
|
|
162
163
|
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
if (override !== undefined) {
|
|
167
|
-
if (typeof override === 'string') {
|
|
168
|
-
const baseType = override;
|
|
169
|
-
return {
|
|
170
|
-
tsType: col.nullable ? `${baseType} | null` : baseType,
|
|
171
|
-
imports,
|
|
172
|
-
};
|
|
173
|
-
}
|
|
174
|
-
if (override.import) {
|
|
175
|
-
imports.push(override.import);
|
|
176
|
-
}
|
|
177
|
-
const baseType = override.type;
|
|
164
|
+
if (override !== undefined) {
|
|
165
|
+
if (typeof override === 'string') {
|
|
166
|
+
const baseType = override;
|
|
178
167
|
return {
|
|
179
168
|
tsType: col.nullable ? `${baseType} | null` : baseType,
|
|
180
169
|
imports,
|
|
181
170
|
};
|
|
182
171
|
}
|
|
172
|
+
if (override.import) {
|
|
173
|
+
imports.push(override.import);
|
|
174
|
+
}
|
|
175
|
+
const baseType = override.type;
|
|
176
|
+
return {
|
|
177
|
+
tsType: col.nullable ? `${baseType} | null` : baseType,
|
|
178
|
+
imports,
|
|
179
|
+
};
|
|
183
180
|
}
|
|
184
181
|
|
|
185
182
|
// Default mapping
|
package/src/render.ts
CHANGED
|
@@ -2,7 +2,12 @@
|
|
|
2
2
|
* @syncular/typegen - TypeScript code generation
|
|
3
3
|
*/
|
|
4
4
|
|
|
5
|
-
import type {
|
|
5
|
+
import type {
|
|
6
|
+
ColumnSchema,
|
|
7
|
+
SyncularImportType,
|
|
8
|
+
TableSchema,
|
|
9
|
+
VersionedSchema,
|
|
10
|
+
} from './types';
|
|
6
11
|
|
|
7
12
|
/**
|
|
8
13
|
* Convert a snake_case table/column name to PascalCase.
|
|
@@ -18,9 +23,10 @@ function toPascalCase(str: string): string {
|
|
|
18
23
|
* Render a single column definition.
|
|
19
24
|
*/
|
|
20
25
|
function renderColumn(column: ColumnSchema): string {
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
26
|
+
const tsType = column.hasDefault
|
|
27
|
+
? `Generated<${column.tsType}>`
|
|
28
|
+
: column.tsType;
|
|
29
|
+
return ` ${column.name}: ${tsType};`;
|
|
24
30
|
}
|
|
25
31
|
|
|
26
32
|
/**
|
|
@@ -58,18 +64,41 @@ export interface RenderOptions {
|
|
|
58
64
|
schemas: VersionedSchema[];
|
|
59
65
|
/** Whether to extend SyncClientDb */
|
|
60
66
|
extendsSyncClientDb?: boolean;
|
|
67
|
+
/** Controls package import style for SyncClientDb (default: 'scoped') */
|
|
68
|
+
syncularImportType?: SyncularImportType;
|
|
61
69
|
/** Generate versioned interfaces */
|
|
62
70
|
includeVersionHistory?: boolean;
|
|
63
71
|
/** Custom imports collected from resolver results */
|
|
64
72
|
customImports?: Array<{ name: string; from: string }>;
|
|
65
73
|
}
|
|
66
74
|
|
|
75
|
+
function resolveSyncClientImportPath(importType: SyncularImportType): string {
|
|
76
|
+
if (importType === 'umbrella') {
|
|
77
|
+
return 'syncular/client';
|
|
78
|
+
}
|
|
79
|
+
if (importType === 'scoped') {
|
|
80
|
+
return '@syncular/client';
|
|
81
|
+
}
|
|
82
|
+
const clientImportPath = importType.client.trim();
|
|
83
|
+
if (clientImportPath.length === 0) {
|
|
84
|
+
throw new Error(
|
|
85
|
+
'syncularImportType.client must be a non-empty package import path'
|
|
86
|
+
);
|
|
87
|
+
}
|
|
88
|
+
return clientImportPath;
|
|
89
|
+
}
|
|
90
|
+
|
|
67
91
|
/**
|
|
68
92
|
* Render complete TypeScript type definitions.
|
|
69
93
|
*/
|
|
70
94
|
export function renderTypes(options: RenderOptions): string {
|
|
71
|
-
const {
|
|
72
|
-
|
|
95
|
+
const {
|
|
96
|
+
schemas,
|
|
97
|
+
extendsSyncClientDb,
|
|
98
|
+
syncularImportType = 'scoped',
|
|
99
|
+
includeVersionHistory,
|
|
100
|
+
customImports,
|
|
101
|
+
} = options;
|
|
73
102
|
const lines: string[] = [];
|
|
74
103
|
|
|
75
104
|
// Header
|
|
@@ -81,7 +110,19 @@ export function renderTypes(options: RenderOptions): string {
|
|
|
81
110
|
|
|
82
111
|
// Import SyncClientDb if extending
|
|
83
112
|
if (extendsSyncClientDb) {
|
|
84
|
-
lines.push(
|
|
113
|
+
lines.push(
|
|
114
|
+
`import type { SyncClientDb } from '${resolveSyncClientImportPath(syncularImportType)}';`
|
|
115
|
+
);
|
|
116
|
+
lines.push('');
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
const usesGenerated = schemas.some((schema) =>
|
|
120
|
+
schema.tables.some((table) =>
|
|
121
|
+
table.columns.some((column) => column.hasDefault)
|
|
122
|
+
)
|
|
123
|
+
);
|
|
124
|
+
if (usesGenerated) {
|
|
125
|
+
lines.push("import type { Generated } from 'kysely';");
|
|
85
126
|
lines.push('');
|
|
86
127
|
}
|
|
87
128
|
|
package/src/types.ts
CHANGED
|
@@ -6,8 +6,16 @@ import type { DefinedMigrations } from '@syncular/migrations';
|
|
|
6
6
|
|
|
7
7
|
export type TypegenDialect = 'sqlite' | 'postgres';
|
|
8
8
|
|
|
9
|
+
export type SyncularImportType =
|
|
10
|
+
| 'scoped'
|
|
11
|
+
| 'umbrella'
|
|
12
|
+
| {
|
|
13
|
+
client: string;
|
|
14
|
+
[packageName: string]: string;
|
|
15
|
+
};
|
|
16
|
+
|
|
9
17
|
/**
|
|
10
|
-
* Column information
|
|
18
|
+
* Column information for a schema column.
|
|
11
19
|
*/
|
|
12
20
|
export interface ColumnInfo {
|
|
13
21
|
table: string;
|
|
@@ -27,9 +35,31 @@ export type TypeOverride =
|
|
|
27
35
|
| { type: string; import?: { name: string; from: string } };
|
|
28
36
|
|
|
29
37
|
/**
|
|
30
|
-
*
|
|
38
|
+
* Column codec definition shared across runtime and typegen.
|
|
39
|
+
* Typegen only consumes the `ts` field, but a full codec object can be
|
|
40
|
+
* provided as a single source of truth.
|
|
41
|
+
*/
|
|
42
|
+
export interface ColumnCodec<App, Db> {
|
|
43
|
+
ts: TypeOverride;
|
|
44
|
+
toDb(value: App): Db;
|
|
45
|
+
fromDb(value: Db): App;
|
|
46
|
+
dialects?: Partial<
|
|
47
|
+
Record<
|
|
48
|
+
TypegenDialect,
|
|
49
|
+
{
|
|
50
|
+
toDb?(value: App): Db;
|
|
51
|
+
fromDb?(value: Db): App;
|
|
52
|
+
}
|
|
53
|
+
>
|
|
54
|
+
>;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Column codec resolver for a column.
|
|
31
59
|
*/
|
|
32
|
-
export type
|
|
60
|
+
export type ColumnCodecResolver = (
|
|
61
|
+
column: ColumnInfo
|
|
62
|
+
) => ColumnCodec<unknown, unknown> | undefined;
|
|
33
63
|
|
|
34
64
|
/**
|
|
35
65
|
* Parsed table schema.
|
|
@@ -71,12 +101,22 @@ export interface GenerateTypesOptions<DB = unknown> {
|
|
|
71
101
|
dialect?: TypegenDialect;
|
|
72
102
|
/** Whether to extend SyncClientDb interface (adds sync infrastructure types) */
|
|
73
103
|
extendsSyncClientDb?: boolean;
|
|
104
|
+
/**
|
|
105
|
+
* Controls how syncular package imports are rendered in generated output.
|
|
106
|
+
* - 'scoped' (default): '@syncular/client'
|
|
107
|
+
* - 'umbrella': 'syncular/client'
|
|
108
|
+
* - object: explicit package mapping (must include `client`)
|
|
109
|
+
*/
|
|
110
|
+
syncularImportType?: SyncularImportType;
|
|
74
111
|
/** Generate versioned interfaces (ClientDbV1, ClientDbV2, etc.) */
|
|
75
112
|
includeVersionHistory?: boolean;
|
|
76
113
|
/** Only generate types for these tables (default: all tables) */
|
|
77
114
|
tables?: string[];
|
|
78
|
-
/**
|
|
79
|
-
|
|
115
|
+
/**
|
|
116
|
+
* Optional column codec resolver for per-column type overrides.
|
|
117
|
+
* Receives full column metadata (table, column, sqlType, dialect, etc.).
|
|
118
|
+
*/
|
|
119
|
+
columnCodecs?: ColumnCodecResolver;
|
|
80
120
|
}
|
|
81
121
|
|
|
82
122
|
/**
|