@syncular/typegen 0.0.1-100

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,151 @@
1
+ /**
2
+ * @syncular/typegen - SQLite schema introspection
3
+ *
4
+ * Works with both better-sqlite3 (Node.js) and bun:sqlite (Bun runtime).
5
+ */
6
+
7
+ import type { DefinedMigrations } from '@syncular/migrations';
8
+ import { Kysely, SqliteDialect } from 'kysely';
9
+ import type { TableSchema, VersionedSchema } from './types';
10
+
11
+ interface SqliteColumnInfo {
12
+ cid: number;
13
+ name: string;
14
+ type: string;
15
+ notnull: 0 | 1;
16
+ dflt_value: string | null;
17
+ pk: 0 | 1;
18
+ }
19
+
20
+ /** Minimal interface shared by better-sqlite3 and bun:sqlite */
21
+ interface SqliteDb {
22
+ prepare(sql: string): { all(...params: unknown[]): unknown[] };
23
+ close(): void;
24
+ }
25
+
26
+ const isBun = typeof globalThis.Bun !== 'undefined';
27
+
28
+ async function createSqliteDb(): Promise<SqliteDb> {
29
+ if (isBun) {
30
+ const { Database } = await import('bun:sqlite');
31
+ return new Database(':memory:');
32
+ }
33
+ const { default: Database } = await import('better-sqlite3');
34
+ return new Database(':memory:');
35
+ }
36
+
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
+
47
+ return new Kysely<DB>({
48
+ dialect: new SqliteDialect({
49
+ database: sqliteDb as never,
50
+ }),
51
+ });
52
+ }
53
+
54
+ function introspectTable(sqliteDb: SqliteDb, tableName: string): TableSchema {
55
+ const columns = sqliteDb
56
+ .prepare(`PRAGMA table_info("${tableName}")`)
57
+ .all() as SqliteColumnInfo[];
58
+
59
+ return {
60
+ name: tableName,
61
+ columns: columns.map((col) => {
62
+ const nullable = col.notnull === 0 && col.pk === 0;
63
+ const hasDefault = col.dflt_value !== null;
64
+ return {
65
+ name: col.name,
66
+ sqlType: col.type,
67
+ tsType: '', // resolved later by map-types
68
+ nullable,
69
+ isPrimaryKey: col.pk === 1,
70
+ hasDefault,
71
+ };
72
+ }),
73
+ };
74
+ }
75
+
76
+ function getAllTables(sqliteDb: SqliteDb): string[] {
77
+ const rows = sqliteDb
78
+ .prepare(
79
+ `SELECT name FROM sqlite_master
80
+ WHERE type='table'
81
+ AND name NOT LIKE 'sqlite_%'
82
+ ORDER BY name`
83
+ )
84
+ .all() as { name: string }[];
85
+
86
+ return rows.map((r) => r.name);
87
+ }
88
+
89
+ async function introspectAtVersion<DB = unknown>(
90
+ migrations: DefinedMigrations<DB>,
91
+ targetVersion: number,
92
+ filterTables?: string[]
93
+ ): Promise<VersionedSchema> {
94
+ const sqliteDb = await createSqliteDb();
95
+
96
+ try {
97
+ const db = await createKysely<DB>(sqliteDb);
98
+
99
+ for (const migration of migrations.migrations) {
100
+ if (migration.version > targetVersion) break;
101
+ await migration.fn(db);
102
+ }
103
+
104
+ let tableNames = getAllTables(sqliteDb);
105
+
106
+ if (filterTables && filterTables.length > 0) {
107
+ const filterSet = new Set(filterTables);
108
+ tableNames = tableNames.filter((t) => filterSet.has(t));
109
+ }
110
+
111
+ const tables = tableNames.map((name) => introspectTable(sqliteDb, name));
112
+
113
+ await db.destroy();
114
+
115
+ return {
116
+ version: targetVersion,
117
+ tables,
118
+ };
119
+ } finally {
120
+ sqliteDb.close();
121
+ }
122
+ }
123
+
124
+ export async function introspectSqliteAllVersions<DB = unknown>(
125
+ migrations: DefinedMigrations<DB>,
126
+ filterTables?: string[]
127
+ ): Promise<VersionedSchema[]> {
128
+ const schemas: VersionedSchema[] = [];
129
+
130
+ for (const migration of migrations.migrations) {
131
+ const schema = await introspectAtVersion(
132
+ migrations,
133
+ migration.version,
134
+ filterTables
135
+ );
136
+ schemas.push(schema);
137
+ }
138
+
139
+ return schemas;
140
+ }
141
+
142
+ export async function introspectSqliteCurrentSchema<DB = unknown>(
143
+ migrations: DefinedMigrations<DB>,
144
+ filterTables?: string[]
145
+ ): Promise<VersionedSchema> {
146
+ return introspectAtVersion(
147
+ migrations,
148
+ migrations.currentVersion,
149
+ filterTables
150
+ );
151
+ }
@@ -0,0 +1,36 @@
1
+ /**
2
+ * @syncular/typegen - Schema introspection dispatcher
3
+ */
4
+
5
+ import type { DefinedMigrations } from '@syncular/migrations';
6
+ import {
7
+ introspectPostgresAllVersions,
8
+ introspectPostgresCurrentSchema,
9
+ } from './introspect-postgres';
10
+ import {
11
+ introspectSqliteAllVersions,
12
+ introspectSqliteCurrentSchema,
13
+ } from './introspect-sqlite';
14
+ import type { TypegenDialect, VersionedSchema } from './types';
15
+
16
+ export async function introspectAllVersions<DB = unknown>(
17
+ migrations: DefinedMigrations<DB>,
18
+ dialect: TypegenDialect,
19
+ filterTables?: string[]
20
+ ): Promise<VersionedSchema[]> {
21
+ if (dialect === 'postgres') {
22
+ return introspectPostgresAllVersions(migrations, filterTables);
23
+ }
24
+ return introspectSqliteAllVersions(migrations, filterTables);
25
+ }
26
+
27
+ export async function introspectCurrentSchema<DB = unknown>(
28
+ migrations: DefinedMigrations<DB>,
29
+ dialect: TypegenDialect,
30
+ filterTables?: string[]
31
+ ): Promise<VersionedSchema> {
32
+ if (dialect === 'postgres') {
33
+ return introspectPostgresCurrentSchema(migrations, filterTables);
34
+ }
35
+ return introspectSqliteCurrentSchema(migrations, filterTables);
36
+ }
@@ -0,0 +1,193 @@
1
+ /**
2
+ * @syncular/typegen - Type mapping
3
+ *
4
+ * Maps SQL types to TypeScript types for each dialect,
5
+ * with support for user-provided resolver overrides.
6
+ */
7
+
8
+ import type { ColumnInfo, ResolveTypeFn, TypegenDialect } from './types';
9
+
10
+ export interface ResolvedType {
11
+ tsType: string;
12
+ imports: Array<{ name: string; from: string }>;
13
+ }
14
+
15
+ function mapSqliteType(sqlType: string): string {
16
+ const upper = sqlType.toUpperCase();
17
+
18
+ if (upper.includes('INT')) return 'number';
19
+ if (
20
+ upper.includes('REAL') ||
21
+ upper.includes('FLOAT') ||
22
+ upper.includes('DOUBLE')
23
+ )
24
+ return 'number';
25
+ if (upper.includes('BLOB')) return 'Uint8Array';
26
+ if (upper.includes('BOOL')) return 'number';
27
+ // TEXT, VARCHAR, CHAR, etc.
28
+ return 'string';
29
+ }
30
+
31
+ function mapPostgresType(sqlType: string): string {
32
+ const lower = sqlType.toLowerCase().replace(/\s+/g, ' ').trim();
33
+
34
+ // Array types — strip trailing [] and map the element type
35
+ if (lower.endsWith('[]')) {
36
+ const element = mapPostgresType(lower.slice(0, -2));
37
+ return `${element}[]`;
38
+ }
39
+
40
+ // Integer types
41
+ if (
42
+ lower === 'int2' ||
43
+ lower === 'int4' ||
44
+ lower === 'integer' ||
45
+ lower === 'smallint' ||
46
+ lower === 'serial'
47
+ )
48
+ return 'number';
49
+
50
+ // 64-bit integers — not safe in JS
51
+ if (lower === 'int8' || lower === 'bigint' || lower === 'bigserial')
52
+ return 'string';
53
+
54
+ // Floating-point / numeric
55
+ if (
56
+ lower === 'float4' ||
57
+ lower === 'float8' ||
58
+ lower === 'real' ||
59
+ lower === 'double precision'
60
+ )
61
+ return 'number';
62
+
63
+ // Exact numeric types are string by default to match common pg driver behavior.
64
+ if (lower === 'numeric' || lower === 'decimal') return 'string';
65
+
66
+ // Boolean
67
+ if (lower === 'bool' || lower === 'boolean') return 'boolean';
68
+
69
+ // JSON
70
+ if (lower === 'json' || lower === 'jsonb') return 'unknown';
71
+
72
+ // Date/time
73
+ if (
74
+ lower === 'timestamp' ||
75
+ lower === 'timestamptz' ||
76
+ lower === 'timestamp with time zone' ||
77
+ lower === 'timestamp without time zone' ||
78
+ lower === 'date' ||
79
+ lower === 'time' ||
80
+ lower === 'timetz' ||
81
+ lower === 'time with time zone' ||
82
+ lower === 'time without time zone'
83
+ )
84
+ return 'string';
85
+
86
+ // Binary
87
+ if (lower === 'bytea') return 'Uint8Array';
88
+
89
+ // Text types
90
+ if (
91
+ lower === 'uuid' ||
92
+ lower === 'text' ||
93
+ lower === 'varchar' ||
94
+ lower === 'char' ||
95
+ lower === 'citext' ||
96
+ lower.startsWith('character varying') ||
97
+ lower.startsWith('character(') ||
98
+ lower.startsWith('varchar(') ||
99
+ lower.startsWith('char(')
100
+ )
101
+ return 'string';
102
+
103
+ // Interval
104
+ if (lower === 'interval') return 'string';
105
+
106
+ // Network types
107
+ if (lower === 'inet' || lower === 'cidr' || lower === 'macaddr')
108
+ return 'string';
109
+
110
+ // Geometric types
111
+ if (
112
+ lower === 'point' ||
113
+ lower === 'line' ||
114
+ lower === 'box' ||
115
+ lower === 'path' ||
116
+ lower === 'polygon' ||
117
+ lower === 'circle' ||
118
+ lower === 'lseg'
119
+ )
120
+ return 'string';
121
+
122
+ // Range types
123
+ if (
124
+ lower === 'int4range' ||
125
+ lower === 'int8range' ||
126
+ lower === 'tsrange' ||
127
+ lower === 'tstzrange' ||
128
+ lower === 'daterange' ||
129
+ lower === 'numrange'
130
+ )
131
+ return 'string';
132
+
133
+ // Full-text search
134
+ if (lower === 'tsvector' || lower === 'tsquery') return 'string';
135
+
136
+ // Other
137
+ if (lower === 'xml') return 'string';
138
+ if (lower === 'money') return 'string';
139
+ if (
140
+ lower === 'bit' ||
141
+ lower === 'varbit' ||
142
+ lower.startsWith('bit(') ||
143
+ lower.startsWith('bit varying')
144
+ )
145
+ return 'string';
146
+
147
+ return 'string';
148
+ }
149
+
150
+ function defaultMapper(dialect: TypegenDialect): (sqlType: string) => string {
151
+ return dialect === 'postgres' ? mapPostgresType : mapSqliteType;
152
+ }
153
+
154
+ /**
155
+ * Resolve the TypeScript type for a column, applying the user resolver first
156
+ * and falling back to the default dialect mapping.
157
+ */
158
+ export function resolveColumnType(
159
+ col: ColumnInfo,
160
+ userResolver?: ResolveTypeFn
161
+ ): ResolvedType {
162
+ const imports: Array<{ name: string; from: string }> = [];
163
+
164
+ // Try user resolver first
165
+ if (userResolver) {
166
+ const override = userResolver(col);
167
+ if (override !== undefined) {
168
+ if (typeof override === 'string') {
169
+ const baseType = override;
170
+ return {
171
+ tsType: col.nullable ? `${baseType} | null` : baseType,
172
+ imports,
173
+ };
174
+ }
175
+ if (override.import) {
176
+ imports.push(override.import);
177
+ }
178
+ const baseType = override.type;
179
+ return {
180
+ tsType: col.nullable ? `${baseType} | null` : baseType,
181
+ imports,
182
+ };
183
+ }
184
+ }
185
+
186
+ // Default mapping
187
+ const mapper = defaultMapper(col.dialect);
188
+ const baseType = mapper(col.sqlType);
189
+ return {
190
+ tsType: col.nullable ? `${baseType} | null` : baseType,
191
+ imports,
192
+ };
193
+ }
package/src/render.ts ADDED
@@ -0,0 +1,206 @@
1
+ /**
2
+ * @syncular/typegen - TypeScript code generation
3
+ */
4
+
5
+ import type { ColumnSchema, TableSchema, VersionedSchema } from './types';
6
+
7
+ /**
8
+ * Convert a snake_case table/column name to PascalCase.
9
+ */
10
+ function toPascalCase(str: string): string {
11
+ return str
12
+ .split(/[_-]/)
13
+ .map((word) => word.charAt(0).toUpperCase() + word.slice(1).toLowerCase())
14
+ .join('');
15
+ }
16
+
17
+ /**
18
+ * Render a single column definition.
19
+ */
20
+ function renderColumn(column: ColumnSchema): string {
21
+ const tsType = column.hasDefault
22
+ ? `Generated<${column.tsType}>`
23
+ : column.tsType;
24
+ return ` ${column.name}: ${tsType};`;
25
+ }
26
+
27
+ /**
28
+ * Render a table interface.
29
+ */
30
+ function renderTableInterface(
31
+ table: TableSchema,
32
+ interfaceName: string
33
+ ): string {
34
+ const columns = table.columns.map(renderColumn).join('\n');
35
+ return `export interface ${interfaceName} {\n${columns}\n}`;
36
+ }
37
+
38
+ /**
39
+ * Render a database interface containing all tables.
40
+ */
41
+ function renderDbInterface(
42
+ schema: VersionedSchema,
43
+ interfaceName: string,
44
+ extendsType?: string
45
+ ): string {
46
+ const extendsClause = extendsType ? ` extends ${extendsType}` : '';
47
+ const tableEntries = schema.tables
48
+ .map((t) => ` ${t.name}: ${toPascalCase(t.name)}Table;`)
49
+ .join('\n');
50
+
51
+ return `export interface ${interfaceName}${extendsClause} {\n${tableEntries}\n}`;
52
+ }
53
+
54
+ /**
55
+ * Options for rendering types.
56
+ */
57
+ export interface RenderOptions {
58
+ /** Schemas at each version (for version history) */
59
+ schemas: VersionedSchema[];
60
+ /** Whether to extend SyncClientDb */
61
+ extendsSyncClientDb?: boolean;
62
+ /** Generate versioned interfaces */
63
+ includeVersionHistory?: boolean;
64
+ /** Custom imports collected from resolver results */
65
+ customImports?: Array<{ name: string; from: string }>;
66
+ }
67
+
68
+ /**
69
+ * Render complete TypeScript type definitions.
70
+ */
71
+ export function renderTypes(options: RenderOptions): string {
72
+ const { schemas, extendsSyncClientDb, includeVersionHistory, customImports } =
73
+ options;
74
+ const lines: string[] = [];
75
+
76
+ // Header
77
+ lines.push('/**');
78
+ lines.push(' * Auto-generated database types from migrations.');
79
+ lines.push(' * DO NOT EDIT - regenerate with @syncular/typegen');
80
+ lines.push(' */');
81
+ lines.push('');
82
+
83
+ // Import SyncClientDb if extending
84
+ if (extendsSyncClientDb) {
85
+ lines.push("import type { SyncClientDb } from '@syncular/client';");
86
+ lines.push('');
87
+ }
88
+
89
+ const usesGenerated = schemas.some((schema) =>
90
+ schema.tables.some((table) =>
91
+ table.columns.some((column) => column.hasDefault)
92
+ )
93
+ );
94
+ if (usesGenerated) {
95
+ lines.push("import type { Generated } from 'kysely';");
96
+ lines.push('');
97
+ }
98
+
99
+ // Render custom imports from resolver
100
+ if (customImports && customImports.length > 0) {
101
+ // Group imports by source module
102
+ const byModule = new Map<string, Set<string>>();
103
+ for (const imp of customImports) {
104
+ let names = byModule.get(imp.from);
105
+ if (!names) {
106
+ names = new Set();
107
+ byModule.set(imp.from, names);
108
+ }
109
+ names.add(imp.name);
110
+ }
111
+ for (const [from, names] of byModule) {
112
+ const sorted = [...names].sort();
113
+ lines.push(`import type { ${sorted.join(', ')} } from '${from}';`);
114
+ }
115
+ lines.push('');
116
+ }
117
+
118
+ // Get the latest schema
119
+ const latestSchema = schemas[schemas.length - 1];
120
+ if (!latestSchema) {
121
+ lines.push('// No migrations defined');
122
+ return lines.join('\n');
123
+ }
124
+
125
+ // Generate table interfaces for latest version
126
+ for (const table of latestSchema.tables) {
127
+ lines.push(renderTableInterface(table, `${toPascalCase(table.name)}Table`));
128
+ lines.push('');
129
+ }
130
+
131
+ // Generate versioned DB interfaces if requested
132
+ if (includeVersionHistory && schemas.length > 0) {
133
+ for (const schema of schemas) {
134
+ // For each version, generate table interfaces with version suffix
135
+ // if they differ from the latest
136
+ const versionSuffix = `V${schema.version}`;
137
+
138
+ // Generate versioned table interfaces
139
+ for (const table of schema.tables) {
140
+ const latestTable = latestSchema.tables.find(
141
+ (t) => t.name === table.name
142
+ );
143
+
144
+ // Only generate versioned interface if different from latest
145
+ if (latestTable && !tablesEqual(table, latestTable)) {
146
+ lines.push(
147
+ renderTableInterface(
148
+ table,
149
+ `${toPascalCase(table.name)}Table${versionSuffix}`
150
+ )
151
+ );
152
+ lines.push('');
153
+ }
154
+ }
155
+
156
+ // Generate versioned DB interface
157
+ const tableEntries = schema.tables
158
+ .map((t) => {
159
+ const latestTable = latestSchema.tables.find(
160
+ (lt) => lt.name === t.name
161
+ );
162
+ const useVersioned = latestTable && !tablesEqual(t, latestTable);
163
+ const typeName = useVersioned
164
+ ? `${toPascalCase(t.name)}Table${versionSuffix}`
165
+ : `${toPascalCase(t.name)}Table`;
166
+ return ` ${t.name}: ${typeName};`;
167
+ })
168
+ .join('\n');
169
+
170
+ lines.push(`export interface ClientDb${versionSuffix} {`);
171
+ lines.push(tableEntries);
172
+ lines.push('}');
173
+ lines.push('');
174
+ }
175
+ }
176
+
177
+ // Generate main DB interface (latest version)
178
+ const extendsType = extendsSyncClientDb ? 'SyncClientDb' : undefined;
179
+ lines.push(renderDbInterface(latestSchema, 'ClientDb', extendsType));
180
+ lines.push('');
181
+
182
+ return lines.join('\n');
183
+ }
184
+
185
+ /**
186
+ * Check if two table schemas are equal.
187
+ */
188
+ function tablesEqual(a: TableSchema, b: TableSchema): boolean {
189
+ if (a.columns.length !== b.columns.length) return false;
190
+
191
+ for (let i = 0; i < a.columns.length; i++) {
192
+ const colA = a.columns[i]!;
193
+ const colB = b.columns[i]!;
194
+
195
+ if (
196
+ colA.name !== colB.name ||
197
+ colA.tsType !== colB.tsType ||
198
+ colA.nullable !== colB.nullable ||
199
+ colA.hasDefault !== colB.hasDefault
200
+ ) {
201
+ return false;
202
+ }
203
+ }
204
+
205
+ return true;
206
+ }
package/src/types.ts ADDED
@@ -0,0 +1,94 @@
1
+ /**
2
+ * @syncular/typegen - Type definitions
3
+ */
4
+
5
+ import type { DefinedMigrations } from '@syncular/migrations';
6
+
7
+ export type TypegenDialect = 'sqlite' | 'postgres';
8
+
9
+ /**
10
+ * Column information passed to the resolver function.
11
+ */
12
+ export interface ColumnInfo {
13
+ table: string;
14
+ column: string;
15
+ sqlType: string;
16
+ nullable: boolean;
17
+ isPrimaryKey: boolean;
18
+ hasDefault: boolean;
19
+ dialect: TypegenDialect;
20
+ }
21
+
22
+ /**
23
+ * Return type from a resolver function.
24
+ */
25
+ export type TypeOverride =
26
+ | string
27
+ | { type: string; import?: { name: string; from: string } };
28
+
29
+ /**
30
+ * User-provided function to override default type mapping.
31
+ */
32
+ export type ResolveTypeFn = (col: ColumnInfo) => TypeOverride | undefined;
33
+
34
+ /**
35
+ * Parsed table schema.
36
+ */
37
+ export interface TableSchema {
38
+ name: string;
39
+ columns: ColumnSchema[];
40
+ }
41
+
42
+ /**
43
+ * Parsed column schema.
44
+ */
45
+ export interface ColumnSchema {
46
+ name: string;
47
+ sqlType: string;
48
+ tsType: string;
49
+ nullable: boolean;
50
+ isPrimaryKey: boolean;
51
+ hasDefault: boolean;
52
+ }
53
+
54
+ /**
55
+ * Schema snapshot at a specific version.
56
+ */
57
+ export interface VersionedSchema {
58
+ version: number;
59
+ tables: TableSchema[];
60
+ }
61
+
62
+ /**
63
+ * Options for generateTypes().
64
+ */
65
+ export interface GenerateTypesOptions<DB = unknown> {
66
+ /** Defined migrations from defineMigrations() */
67
+ migrations: DefinedMigrations<DB>;
68
+ /** Output file path for generated types */
69
+ output: string;
70
+ /** Database dialect to use for introspection (default: 'sqlite') */
71
+ dialect?: TypegenDialect;
72
+ /** Whether to extend SyncClientDb interface (adds sync infrastructure types) */
73
+ extendsSyncClientDb?: boolean;
74
+ /** Generate versioned interfaces (ClientDbV1, ClientDbV2, etc.) */
75
+ includeVersionHistory?: boolean;
76
+ /** Only generate types for these tables (default: all tables) */
77
+ tables?: string[];
78
+ /** Custom type resolver for overriding default type mapping */
79
+ resolveType?: ResolveTypeFn;
80
+ }
81
+
82
+ /**
83
+ * Result of type generation.
84
+ */
85
+ export interface GenerateTypesResult {
86
+ /** Path to the generated file */
87
+ outputPath: string;
88
+ /** Current schema version */
89
+ currentVersion: number;
90
+ /** Number of tables generated */
91
+ tableCount: number;
92
+ /** Generated TypeScript code */
93
+ code: string;
94
+ }