@danielfgray/pg-sourcerer 0.2.1 → 0.2.2
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/dist/cli.js +3 -4
- package/dist/cli.js.map +1 -1
- package/dist/config.d.ts +1 -1
- package/dist/config.d.ts.map +1 -1
- package/dist/config.js +1 -1
- package/dist/config.js.map +1 -1
- package/dist/errors.d.ts +14 -1
- package/dist/errors.d.ts.map +1 -1
- package/dist/errors.js +2 -0
- package/dist/errors.js.map +1 -1
- package/dist/generate.d.ts +5 -9
- package/dist/generate.d.ts.map +1 -1
- package/dist/generate.js +27 -29
- package/dist/generate.js.map +1 -1
- package/dist/index.d.ts +19 -12
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +25 -13
- package/dist/index.js.map +1 -1
- package/dist/init.d.ts.map +1 -1
- package/dist/init.js +39 -9
- package/dist/init.js.map +1 -1
- package/dist/ir/extensions/queries.d.ts +264 -0
- package/dist/ir/extensions/queries.d.ts.map +1 -0
- package/dist/ir/extensions/queries.js +153 -0
- package/dist/ir/extensions/queries.js.map +1 -0
- package/dist/ir/extensions/schema-builder.d.ts +61 -0
- package/dist/ir/extensions/schema-builder.d.ts.map +1 -0
- package/dist/ir/extensions/schema-builder.js +5 -0
- package/dist/ir/extensions/schema-builder.js.map +1 -0
- package/dist/lib/conjure.d.ts +66 -0
- package/dist/lib/conjure.d.ts.map +1 -1
- package/dist/lib/conjure.js +127 -29
- package/dist/lib/conjure.js.map +1 -1
- package/dist/lib/hex.d.ts +10 -3
- package/dist/lib/hex.d.ts.map +1 -1
- package/dist/lib/hex.js +18 -8
- package/dist/lib/hex.js.map +1 -1
- package/dist/plugins/arktype.d.ts +27 -14
- package/dist/plugins/arktype.d.ts.map +1 -1
- package/dist/plugins/arktype.js +166 -130
- package/dist/plugins/arktype.js.map +1 -1
- package/dist/plugins/effect.d.ts +53 -0
- package/dist/plugins/effect.d.ts.map +1 -0
- package/dist/plugins/effect.js +1074 -0
- package/dist/plugins/effect.js.map +1 -0
- package/dist/plugins/http-elysia.d.ts +32 -0
- package/dist/plugins/http-elysia.d.ts.map +1 -0
- package/dist/plugins/http-elysia.js +613 -0
- package/dist/plugins/http-elysia.js.map +1 -0
- package/dist/plugins/http-express.d.ts +36 -0
- package/dist/plugins/http-express.d.ts.map +1 -0
- package/dist/plugins/http-express.js +388 -0
- package/dist/plugins/http-express.js.map +1 -0
- package/dist/plugins/http-hono.d.ts +36 -0
- package/dist/plugins/http-hono.d.ts.map +1 -0
- package/dist/plugins/http-hono.js +453 -0
- package/dist/plugins/http-hono.js.map +1 -0
- package/dist/plugins/http-orpc.d.ts +55 -0
- package/dist/plugins/http-orpc.d.ts.map +1 -0
- package/dist/plugins/http-orpc.js +370 -0
- package/dist/plugins/http-orpc.js.map +1 -0
- package/dist/plugins/http-trpc.d.ts +59 -0
- package/dist/plugins/http-trpc.d.ts.map +1 -0
- package/dist/plugins/http-trpc.js +392 -0
- package/dist/plugins/http-trpc.js.map +1 -0
- package/dist/plugins/kysely/queries.d.ts +92 -0
- package/dist/plugins/kysely/queries.d.ts.map +1 -0
- package/dist/plugins/kysely/queries.js +1169 -0
- package/dist/plugins/kysely/queries.js.map +1 -0
- package/dist/plugins/kysely/shared.d.ts +59 -0
- package/dist/plugins/kysely/shared.d.ts.map +1 -0
- package/dist/plugins/kysely/shared.js +247 -0
- package/dist/plugins/kysely/shared.js.map +1 -0
- package/dist/plugins/kysely/types.d.ts +22 -0
- package/dist/plugins/kysely/types.d.ts.map +1 -0
- package/dist/plugins/kysely/types.js +428 -0
- package/dist/plugins/kysely/types.js.map +1 -0
- package/dist/plugins/kysely.d.ts +72 -0
- package/dist/plugins/kysely.d.ts.map +1 -0
- package/dist/plugins/kysely.js +906 -0
- package/dist/plugins/kysely.js.map +1 -0
- package/dist/plugins/sql-queries.d.ts +55 -11
- package/dist/plugins/sql-queries.d.ts.map +1 -1
- package/dist/plugins/sql-queries.js +467 -218
- package/dist/plugins/sql-queries.js.map +1 -1
- package/dist/plugins/types.d.ts +20 -14
- package/dist/plugins/types.d.ts.map +1 -1
- package/dist/plugins/types.js +90 -112
- package/dist/plugins/types.js.map +1 -1
- package/dist/plugins/valibot.d.ts +45 -0
- package/dist/plugins/valibot.d.ts.map +1 -0
- package/dist/plugins/valibot.js +422 -0
- package/dist/plugins/valibot.js.map +1 -0
- package/dist/plugins/zod.d.ts +27 -14
- package/dist/plugins/zod.d.ts.map +1 -1
- package/dist/plugins/zod.js +231 -166
- package/dist/plugins/zod.js.map +1 -1
- package/dist/services/artifact-store.d.ts +11 -1
- package/dist/services/artifact-store.d.ts.map +1 -1
- package/dist/services/artifact-store.js +9 -0
- package/dist/services/artifact-store.js.map +1 -1
- package/dist/services/core-providers.d.ts +15 -0
- package/dist/services/core-providers.d.ts.map +1 -0
- package/dist/services/core-providers.js +23 -0
- package/dist/services/core-providers.js.map +1 -0
- package/dist/services/emissions.d.ts +14 -0
- package/dist/services/emissions.d.ts.map +1 -1
- package/dist/services/emissions.js +86 -47
- package/dist/services/emissions.js.map +1 -1
- package/dist/services/execution.d.ts +35 -0
- package/dist/services/execution.d.ts.map +1 -0
- package/dist/services/execution.js +86 -0
- package/dist/services/execution.js.map +1 -0
- package/dist/services/file-builder.d.ts +4 -0
- package/dist/services/file-builder.d.ts.map +1 -1
- package/dist/services/file-builder.js.map +1 -1
- package/dist/services/inflection.d.ts +2 -2
- package/dist/services/inflection.d.ts.map +1 -1
- package/dist/services/inflection.js +4 -4
- package/dist/services/inflection.js.map +1 -1
- package/dist/services/ir-builder.d.ts.map +1 -1
- package/dist/services/ir-builder.js +10 -3
- package/dist/services/ir-builder.js.map +1 -1
- package/dist/services/pg-types.d.ts +31 -0
- package/dist/services/pg-types.d.ts.map +1 -1
- package/dist/services/pg-types.js +24 -0
- package/dist/services/pg-types.js.map +1 -1
- package/dist/services/plugin-runner.d.ts +27 -37
- package/dist/services/plugin-runner.d.ts.map +1 -1
- package/dist/services/plugin-runner.js +73 -171
- package/dist/services/plugin-runner.js.map +1 -1
- package/dist/services/plugin.d.ts +349 -217
- package/dist/services/plugin.d.ts.map +1 -1
- package/dist/services/plugin.js +182 -130
- package/dist/services/plugin.js.map +1 -1
- package/dist/services/resolution.d.ts +38 -0
- package/dist/services/resolution.d.ts.map +1 -0
- package/dist/services/resolution.js +242 -0
- package/dist/services/resolution.js.map +1 -0
- package/dist/services/service-registry.d.ts +74 -0
- package/dist/services/service-registry.d.ts.map +1 -0
- package/dist/services/service-registry.js +61 -0
- package/dist/services/service-registry.js.map +1 -0
- package/dist/services/symbols.d.ts +59 -0
- package/dist/services/symbols.d.ts.map +1 -1
- package/dist/services/symbols.js +16 -0
- package/dist/services/symbols.js.map +1 -1
- package/dist/testing.d.ts +4 -25
- package/dist/testing.d.ts.map +1 -1
- package/dist/testing.js +2 -23
- package/dist/testing.js.map +1 -1
- package/package.json +1 -1
- package/dist/plugins/effect-model.d.ts +0 -17
- package/dist/plugins/effect-model.d.ts.map +0 -1
- package/dist/plugins/effect-model.js +0 -409
- package/dist/plugins/effect-model.js.map +0 -1
- package/dist/plugins/kysely-queries.d.ts +0 -66
- package/dist/plugins/kysely-queries.d.ts.map +0 -1
- package/dist/plugins/kysely-queries.js +0 -951
- package/dist/plugins/kysely-queries.js.map +0 -1
- package/dist/plugins/kysely-types.d.ts +0 -35
- package/dist/plugins/kysely-types.d.ts.map +0 -1
- package/dist/plugins/kysely-types.js +0 -601
- package/dist/plugins/kysely-types.js.map +0 -1
|
@@ -0,0 +1,428 @@
|
|
|
1
|
+
// @ts-nocheck - TODO: migrate to new plugin API
|
|
2
|
+
/**
|
|
3
|
+
* Kysely Types Generation
|
|
4
|
+
*
|
|
5
|
+
* Generates table interfaces with Generated<T>, ColumnType<S, I, U>, and DB interface.
|
|
6
|
+
* Uses RLS permissions to determine Generated<T> wrapping.
|
|
7
|
+
*/
|
|
8
|
+
import { Option, pipe } from "effect";
|
|
9
|
+
import { findEnumByPgName, findCompositeByPgName, PgTypeOid } from "../../services/pg-types.js";
|
|
10
|
+
import { getEnumEntities, getTableEntities, getCompositeEntities } from "../../ir/semantic-ir.js";
|
|
11
|
+
import { conjure } from "../../lib/conjure.js";
|
|
12
|
+
import { isEnumType, getPgTypeName } from "../../lib/field-utils.js";
|
|
13
|
+
import { getExtensionTypeMapping } from "../../services/pg-types.js";
|
|
14
|
+
import { SCALAR_TYPES, COMPLEX_TYPES, GENERATED_TYPE_DEF, ARRAY_TYPE_DEF, ARRAY_TYPE_IMPL_DEF, } from "./shared.js";
|
|
15
|
+
const { ts, exp } = conjure;
|
|
16
|
+
/** Default config values */
|
|
17
|
+
export const defaultTypesConfig = {
|
|
18
|
+
outputFile: "db.ts",
|
|
19
|
+
runtimeEnums: false,
|
|
20
|
+
typeOnlyImports: true,
|
|
21
|
+
};
|
|
22
|
+
// ============================================================================
|
|
23
|
+
// Type Resolution
|
|
24
|
+
// ============================================================================
|
|
25
|
+
/** Build TypeHintFieldMatch from field and context */
|
|
26
|
+
const buildFieldMatch = (field, ctx) => ({
|
|
27
|
+
schema: ctx.schemaName,
|
|
28
|
+
table: ctx.tableName,
|
|
29
|
+
column: field.columnName,
|
|
30
|
+
pgType: field.isArray && field.elementTypeName
|
|
31
|
+
? field.elementTypeName
|
|
32
|
+
: (field.pgAttribute.getType()?.typname ?? ""),
|
|
33
|
+
});
|
|
34
|
+
/**
|
|
35
|
+
* Resolve a field to its Kysely type.
|
|
36
|
+
* Handles: TypeHints, enums, complex types, scalar types, extensions.
|
|
37
|
+
*/
|
|
38
|
+
function resolveFieldType(field, ctx) {
|
|
39
|
+
const pgType = field.pgAttribute.getType();
|
|
40
|
+
const typeName = pgType?.typname ?? "";
|
|
41
|
+
// 1. Check TypeHints first (highest priority)
|
|
42
|
+
const fieldMatch = buildFieldMatch(field, ctx);
|
|
43
|
+
const tsTypeHint = ctx.typeHints.getHint(fieldMatch, "tsType");
|
|
44
|
+
if (Option.isSome(tsTypeHint)) {
|
|
45
|
+
const hintTypeName = tsTypeHint.value;
|
|
46
|
+
const importPath = ctx.typeHints.getHint(fieldMatch, "import");
|
|
47
|
+
return {
|
|
48
|
+
selectType: ts.ref(hintTypeName),
|
|
49
|
+
needsColumnType: false,
|
|
50
|
+
externalImport: pipe(importPath, Option.map(path => ({ name: hintTypeName, from: path })), Option.getOrUndefined),
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
// 2. Check if it's an enum
|
|
54
|
+
if (isEnumType(field)) {
|
|
55
|
+
const enumName = getPgTypeName(field);
|
|
56
|
+
if (enumName) {
|
|
57
|
+
const enumDef = findEnumByPgName(ctx.enums, enumName);
|
|
58
|
+
if (Option.isSome(enumDef)) {
|
|
59
|
+
return {
|
|
60
|
+
selectType: ts.ref(enumDef.value.name),
|
|
61
|
+
needsColumnType: false,
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
// 3. Check if it's a composite type
|
|
67
|
+
if (pgType?.typtype === "c") {
|
|
68
|
+
const compositeName = getPgTypeName(field);
|
|
69
|
+
if (compositeName) {
|
|
70
|
+
const compositeDef = findCompositeByPgName(ctx.composites, compositeName);
|
|
71
|
+
if (Option.isSome(compositeDef)) {
|
|
72
|
+
return {
|
|
73
|
+
selectType: ts.ref(compositeDef.value.name),
|
|
74
|
+
needsColumnType: false,
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
// 4. Check complex types (need ColumnType wrapper)
|
|
80
|
+
const complexType = COMPLEX_TYPES[typeName];
|
|
81
|
+
if (complexType) {
|
|
82
|
+
return {
|
|
83
|
+
selectType: complexType.select(),
|
|
84
|
+
insertType: complexType.insert(),
|
|
85
|
+
updateType: complexType.update(),
|
|
86
|
+
needsColumnType: true,
|
|
87
|
+
externalImport: complexType.import,
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
// 5. Check simple scalar types
|
|
91
|
+
const scalarBuilder = SCALAR_TYPES[typeName];
|
|
92
|
+
if (scalarBuilder) {
|
|
93
|
+
return {
|
|
94
|
+
selectType: scalarBuilder(),
|
|
95
|
+
needsColumnType: false,
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
// 6. Check extension types (citext, etc.)
|
|
99
|
+
if (pgType) {
|
|
100
|
+
const extType = getExtensionTypeMapping(typeName, String(pgType.typnamespace), ctx.extensions);
|
|
101
|
+
if (Option.isSome(extType)) {
|
|
102
|
+
return {
|
|
103
|
+
selectType: ts.fromString(extType.value),
|
|
104
|
+
needsColumnType: false,
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
// 7. Default to string (PostgreSQL sends unknown types as strings)
|
|
109
|
+
return {
|
|
110
|
+
selectType: ts.string(),
|
|
111
|
+
needsColumnType: false,
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* Build the final field type with array/nullable/Generated wrappers.
|
|
116
|
+
*/
|
|
117
|
+
function buildFieldType(field, kyselyType, needsGenerated) {
|
|
118
|
+
let baseType;
|
|
119
|
+
// If complex type, wrap in ColumnType<S, I, U>
|
|
120
|
+
if (kyselyType.needsColumnType && kyselyType.insertType && kyselyType.updateType) {
|
|
121
|
+
baseType = ts.ref("ColumnType", [kyselyType.selectType, kyselyType.insertType, kyselyType.updateType]);
|
|
122
|
+
}
|
|
123
|
+
else {
|
|
124
|
+
baseType = kyselyType.selectType;
|
|
125
|
+
}
|
|
126
|
+
// Wrap in array if needed
|
|
127
|
+
if (field.isArray) {
|
|
128
|
+
// For simple types, use T[]
|
|
129
|
+
// For complex types, use ArrayType<T>
|
|
130
|
+
if (kyselyType.needsColumnType) {
|
|
131
|
+
baseType = ts.ref("ArrayType", [baseType]);
|
|
132
|
+
}
|
|
133
|
+
else {
|
|
134
|
+
baseType = ts.array(baseType);
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
// Wrap in nullable if needed
|
|
138
|
+
if (field.nullable) {
|
|
139
|
+
baseType = ts.nullable(baseType);
|
|
140
|
+
}
|
|
141
|
+
// Wrap in Generated<T> if field has default and is not insertable
|
|
142
|
+
if (needsGenerated) {
|
|
143
|
+
baseType = ts.ref("Generated", [baseType]);
|
|
144
|
+
}
|
|
145
|
+
return baseType;
|
|
146
|
+
}
|
|
147
|
+
/**
|
|
148
|
+
* Determine if a field should be wrapped in Generated<T>.
|
|
149
|
+
*
|
|
150
|
+
* A field is Generated when:
|
|
151
|
+
* - It has a default value, AND
|
|
152
|
+
* - It's not required on insert (either auto-increment or RLS blocks insert)
|
|
153
|
+
*/
|
|
154
|
+
function isGeneratedField(field) {
|
|
155
|
+
if (!field.hasDefault)
|
|
156
|
+
return false;
|
|
157
|
+
// If field can't be inserted via RLS, it's generated
|
|
158
|
+
if (!field.permissions.canInsert)
|
|
159
|
+
return true;
|
|
160
|
+
// Check for serial/identity columns (auto-incrementing)
|
|
161
|
+
const pgType = field.pgAttribute.getType();
|
|
162
|
+
if (!pgType)
|
|
163
|
+
return false;
|
|
164
|
+
// Check for identity columns (GENERATED ALWAYS AS IDENTITY)
|
|
165
|
+
if (field.isIdentity)
|
|
166
|
+
return true;
|
|
167
|
+
// Check for generated columns (GENERATED ALWAYS AS ...)
|
|
168
|
+
if (field.isGenerated)
|
|
169
|
+
return true;
|
|
170
|
+
// serial types have specific OIDs that resolve to int4/int8
|
|
171
|
+
// but the attribute itself may have attidentity or atthasdef
|
|
172
|
+
// For now, use a heuristic: if it's an integer with a default, likely generated
|
|
173
|
+
const typeOid = Number(pgType._id);
|
|
174
|
+
const isIntegerType = typeOid === PgTypeOid.Int2 || typeOid === PgTypeOid.Int4 || typeOid === PgTypeOid.Int8;
|
|
175
|
+
// If it's an integer primary key with a default, it's likely auto-increment
|
|
176
|
+
// This is a simplification - proper detection would check sequences
|
|
177
|
+
if (isIntegerType && field.hasDefault) {
|
|
178
|
+
// Check if it's a primary key (best effort)
|
|
179
|
+
const constraints = field.pgAttribute.getClass()?.getConstraints() ?? [];
|
|
180
|
+
const isPrimaryKey = constraints.some(c => c.contype === "p" && c.conkey?.includes(field.pgAttribute.attnum));
|
|
181
|
+
if (isPrimaryKey)
|
|
182
|
+
return true;
|
|
183
|
+
}
|
|
184
|
+
return false;
|
|
185
|
+
}
|
|
186
|
+
// ============================================================================
|
|
187
|
+
// Statement Generators
|
|
188
|
+
// ============================================================================
|
|
189
|
+
/**
|
|
190
|
+
* Generate enum type alias: `export type Status = "active" | "inactive"`
|
|
191
|
+
*/
|
|
192
|
+
function generateEnumStatement(enumEntity) {
|
|
193
|
+
return exp.typeAlias(enumEntity.name, { capability: "types:kysely", entity: enumEntity.name }, ts.union(...enumEntity.values.map(v => ts.literal(v))));
|
|
194
|
+
}
|
|
195
|
+
/**
|
|
196
|
+
* Generate table interface with all column types.
|
|
197
|
+
*/
|
|
198
|
+
function generateTableInterface(entity, ctx) {
|
|
199
|
+
const properties = [];
|
|
200
|
+
// Use the row shape for column definitions
|
|
201
|
+
for (const field of entity.shapes.row.fields) {
|
|
202
|
+
// Skip fields that aren't selectable via RLS
|
|
203
|
+
if (!field.permissions.canSelect)
|
|
204
|
+
continue;
|
|
205
|
+
const kyselyType = resolveFieldType(field, ctx);
|
|
206
|
+
const needsGenerated = isGeneratedField(field);
|
|
207
|
+
const fieldType = buildFieldType(field, kyselyType, needsGenerated);
|
|
208
|
+
properties.push({
|
|
209
|
+
name: field.name,
|
|
210
|
+
type: fieldType,
|
|
211
|
+
});
|
|
212
|
+
}
|
|
213
|
+
return exp.interface(entity.name, { capability: "types:kysely", entity: entity.name }, properties);
|
|
214
|
+
}
|
|
215
|
+
/**
|
|
216
|
+
* Generate composite type interface.
|
|
217
|
+
*
|
|
218
|
+
* Composite types are simpler than tables - they just have fields,
|
|
219
|
+
* no shapes, no Generated wrapping (composites can't have defaults).
|
|
220
|
+
*/
|
|
221
|
+
function generateCompositeInterface(composite, ctx) {
|
|
222
|
+
const properties = [];
|
|
223
|
+
for (const field of composite.fields) {
|
|
224
|
+
const kyselyType = resolveFieldType(field, ctx);
|
|
225
|
+
// Composites don't have Generated wrapping - they're just data structures
|
|
226
|
+
const fieldType = buildFieldType(field, kyselyType, false);
|
|
227
|
+
properties.push({
|
|
228
|
+
name: field.name,
|
|
229
|
+
type: fieldType,
|
|
230
|
+
});
|
|
231
|
+
}
|
|
232
|
+
return exp.interface(composite.name, { capability: "types:kysely", entity: composite.name }, properties);
|
|
233
|
+
}
|
|
234
|
+
/**
|
|
235
|
+
* Generate DB interface: `export interface DB { 'schema.table': Table }`
|
|
236
|
+
*/
|
|
237
|
+
function generateDBInterface(entities, defaultSchemas) {
|
|
238
|
+
const properties = [];
|
|
239
|
+
for (const entity of entities) {
|
|
240
|
+
// Skip entities that aren't selectable
|
|
241
|
+
if (!entity.permissions.canSelect)
|
|
242
|
+
continue;
|
|
243
|
+
// Use schema-qualified key if not in default schema
|
|
244
|
+
const key = defaultSchemas.includes(entity.schemaName)
|
|
245
|
+
? entity.pgName
|
|
246
|
+
: `${entity.schemaName}.${entity.pgName}`;
|
|
247
|
+
properties.push({
|
|
248
|
+
name: key,
|
|
249
|
+
type: ts.ref(entity.name),
|
|
250
|
+
});
|
|
251
|
+
}
|
|
252
|
+
// Sort by key for stable output
|
|
253
|
+
properties.sort((a, b) => a.name.localeCompare(b.name));
|
|
254
|
+
return exp.interface("DB", { capability: "types:kysely", entity: "DB" }, properties);
|
|
255
|
+
}
|
|
256
|
+
function collectImports(entities, composites, ctx) {
|
|
257
|
+
const kyselyImports = new Set();
|
|
258
|
+
const externalImports = new Map();
|
|
259
|
+
const usedEnums = new Set();
|
|
260
|
+
let needsJsonTypes = false;
|
|
261
|
+
let needsArrayType = false;
|
|
262
|
+
let needsGenerated = false;
|
|
263
|
+
// Helper to process a single field
|
|
264
|
+
const processField = (field, schemaName, tableName, checkGenerated) => {
|
|
265
|
+
const kyselyType = resolveFieldType(field, {
|
|
266
|
+
...ctx,
|
|
267
|
+
schemaName,
|
|
268
|
+
tableName,
|
|
269
|
+
});
|
|
270
|
+
// Check for ColumnType usage
|
|
271
|
+
if (kyselyType.needsColumnType) {
|
|
272
|
+
kyselyImports.add("ColumnType");
|
|
273
|
+
}
|
|
274
|
+
// Check for external imports
|
|
275
|
+
if (kyselyType.externalImport) {
|
|
276
|
+
const { name, from } = kyselyType.externalImport;
|
|
277
|
+
if (!externalImports.has(from)) {
|
|
278
|
+
externalImports.set(from, new Set());
|
|
279
|
+
}
|
|
280
|
+
externalImports.get(from).add(name);
|
|
281
|
+
}
|
|
282
|
+
// Check for enum usage
|
|
283
|
+
if (isEnumType(field)) {
|
|
284
|
+
const enumName = getPgTypeName(field);
|
|
285
|
+
if (enumName) {
|
|
286
|
+
const enumDef = findEnumByPgName(ctx.enums, enumName);
|
|
287
|
+
if (Option.isSome(enumDef)) {
|
|
288
|
+
usedEnums.add(enumDef.value.name);
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
// Check for JSON types
|
|
293
|
+
const pgType = field.pgAttribute.getType();
|
|
294
|
+
if (pgType?.typname === "json" || pgType?.typname === "jsonb") {
|
|
295
|
+
needsJsonTypes = true;
|
|
296
|
+
}
|
|
297
|
+
// Check for complex array types
|
|
298
|
+
if (field.isArray && kyselyType.needsColumnType) {
|
|
299
|
+
needsArrayType = true;
|
|
300
|
+
}
|
|
301
|
+
// Check for Generated (only for table fields)
|
|
302
|
+
if (checkGenerated && isGeneratedField(field)) {
|
|
303
|
+
needsGenerated = true;
|
|
304
|
+
}
|
|
305
|
+
};
|
|
306
|
+
// Process table entity fields
|
|
307
|
+
for (const entity of entities) {
|
|
308
|
+
if (!entity.permissions.canSelect)
|
|
309
|
+
continue;
|
|
310
|
+
for (const field of entity.shapes.row.fields) {
|
|
311
|
+
if (!field.permissions.canSelect)
|
|
312
|
+
continue;
|
|
313
|
+
processField(field, entity.schemaName, entity.pgName, true);
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
// Process composite entity fields
|
|
317
|
+
for (const composite of composites) {
|
|
318
|
+
for (const field of composite.fields) {
|
|
319
|
+
processField(field, composite.schemaName, composite.pgName, false);
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
return {
|
|
323
|
+
kyselyImports,
|
|
324
|
+
externalImports,
|
|
325
|
+
usedEnums,
|
|
326
|
+
needsJsonTypes,
|
|
327
|
+
needsArrayType,
|
|
328
|
+
needsGenerated,
|
|
329
|
+
};
|
|
330
|
+
}
|
|
331
|
+
// ============================================================================
|
|
332
|
+
// Core Generation Function
|
|
333
|
+
// ============================================================================
|
|
334
|
+
/**
|
|
335
|
+
* Generate Kysely types from the IR.
|
|
336
|
+
*
|
|
337
|
+
* @param ctx - Plugin context for file emission and services
|
|
338
|
+
* @param config - Types configuration (partial, defaults applied)
|
|
339
|
+
*/
|
|
340
|
+
export function generateKyselyTypes(ctx, config = {}) {
|
|
341
|
+
// Apply defaults
|
|
342
|
+
const resolvedConfig = {
|
|
343
|
+
...defaultTypesConfig,
|
|
344
|
+
...config,
|
|
345
|
+
};
|
|
346
|
+
const { ir, typeHints } = ctx;
|
|
347
|
+
const enumEntities = getEnumEntities(ir);
|
|
348
|
+
const compositeEntities = getCompositeEntities(ir).filter(e => e.tags.omit !== true);
|
|
349
|
+
const tableEntities = getTableEntities(ir).filter(e => e.tags.omit !== true);
|
|
350
|
+
const defaultSchemas = ir.schemas;
|
|
351
|
+
// Build field context
|
|
352
|
+
const fieldCtx = {
|
|
353
|
+
schemaName: "", // Will be set per-entity
|
|
354
|
+
tableName: "",
|
|
355
|
+
enums: enumEntities,
|
|
356
|
+
composites: compositeEntities,
|
|
357
|
+
extensions: ir.extensions,
|
|
358
|
+
typeHints,
|
|
359
|
+
defaultSchemas,
|
|
360
|
+
};
|
|
361
|
+
// Collect what imports we need
|
|
362
|
+
const imports = collectImports(tableEntities, compositeEntities, fieldCtx);
|
|
363
|
+
// Build statements
|
|
364
|
+
const statements = [];
|
|
365
|
+
// Generate enum types
|
|
366
|
+
for (const enumEntity of enumEntities) {
|
|
367
|
+
if (enumEntity.tags.omit === true)
|
|
368
|
+
continue;
|
|
369
|
+
statements.push(generateEnumStatement(enumEntity));
|
|
370
|
+
}
|
|
371
|
+
// Generate composite type interfaces
|
|
372
|
+
for (const composite of compositeEntities) {
|
|
373
|
+
statements.push(generateCompositeInterface(composite, {
|
|
374
|
+
...fieldCtx,
|
|
375
|
+
schemaName: composite.schemaName,
|
|
376
|
+
tableName: composite.pgName,
|
|
377
|
+
}));
|
|
378
|
+
}
|
|
379
|
+
// Generate table interfaces
|
|
380
|
+
for (const entity of tableEntities) {
|
|
381
|
+
statements.push(generateTableInterface(entity, {
|
|
382
|
+
...fieldCtx,
|
|
383
|
+
schemaName: entity.schemaName,
|
|
384
|
+
tableName: entity.pgName,
|
|
385
|
+
}));
|
|
386
|
+
}
|
|
387
|
+
// Generate DB interface
|
|
388
|
+
statements.push(generateDBInterface(tableEntities, defaultSchemas));
|
|
389
|
+
// Build the file
|
|
390
|
+
const file = ctx.file(resolvedConfig.outputFile);
|
|
391
|
+
// Add Kysely imports
|
|
392
|
+
if (imports.kyselyImports.size > 0) {
|
|
393
|
+
file.import({
|
|
394
|
+
kind: "package",
|
|
395
|
+
types: [...imports.kyselyImports],
|
|
396
|
+
from: "kysely",
|
|
397
|
+
});
|
|
398
|
+
}
|
|
399
|
+
// Add external imports
|
|
400
|
+
for (const [from, names] of imports.externalImports) {
|
|
401
|
+
file.import({
|
|
402
|
+
kind: "relative",
|
|
403
|
+
types: [...names],
|
|
404
|
+
from,
|
|
405
|
+
});
|
|
406
|
+
}
|
|
407
|
+
// Build header with helper types
|
|
408
|
+
const helperTypes = [];
|
|
409
|
+
if (imports.needsGenerated) {
|
|
410
|
+
helperTypes.push(`export type Generated<T> = ${GENERATED_TYPE_DEF};`);
|
|
411
|
+
}
|
|
412
|
+
if (imports.needsArrayType) {
|
|
413
|
+
helperTypes.push(`export type ArrayType<T> = ${ARRAY_TYPE_DEF};`);
|
|
414
|
+
helperTypes.push(`export type ArrayTypeImpl<T> = ${ARRAY_TYPE_IMPL_DEF};`);
|
|
415
|
+
}
|
|
416
|
+
if (imports.needsJsonTypes) {
|
|
417
|
+
helperTypes.push(`export type JsonPrimitive = boolean | number | string | null;`);
|
|
418
|
+
helperTypes.push(`export type JsonObject = { [x: string]: JsonValue | undefined };`);
|
|
419
|
+
helperTypes.push(`export type JsonArray = JsonValue[];`);
|
|
420
|
+
helperTypes.push(`export type JsonValue = JsonArray | JsonObject | JsonPrimitive;`);
|
|
421
|
+
}
|
|
422
|
+
if (helperTypes.length > 0) {
|
|
423
|
+
file.header(helperTypes.join("\n\n"));
|
|
424
|
+
}
|
|
425
|
+
// Add the main content
|
|
426
|
+
file.ast(conjure.symbolProgram(...statements)).emit();
|
|
427
|
+
}
|
|
428
|
+
//# sourceMappingURL=types.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../../src/plugins/kysely/types.ts"],"names":[],"mappings":"AAAA,gDAAgD;AAChD;;;;;GAKG;AACH,OAAO,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,QAAQ,CAAA;AAGrC,OAAO,EAAE,gBAAgB,EAAE,qBAAqB,EAAE,SAAS,EAAE,MAAM,4BAA4B,CAAA;AAQ/F,OAAO,EAAE,eAAe,EAAE,gBAAgB,EAAE,oBAAoB,EAAE,MAAM,yBAAyB,CAAA;AACjG,OAAO,EAAE,OAAO,EAAE,MAAM,sBAAsB,CAAA;AAG9C,OAAO,EAAE,UAAU,EAAE,aAAa,EAAE,MAAM,0BAA0B,CAAA;AACpE,OAAO,EAAE,uBAAuB,EAAE,MAAM,4BAA4B,CAAA;AACpE,OAAO,EACL,YAAY,EACZ,aAAa,EACb,kBAAkB,EAClB,cAAc,EACd,mBAAmB,GACpB,MAAM,aAAa,CAAA;AAEpB,MAAM,EAAE,EAAE,EAAE,GAAG,EAAE,GAAG,OAAO,CAAA;AAkB3B,4BAA4B;AAC5B,MAAM,CAAC,MAAM,kBAAkB,GAAsB;IACnD,UAAU,E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IAED,IAAI,OAAO,CAAC,cAAc,EAAE,CAAC;QAC3B,WAAW,CAAC,IAAI,CAAC,+DAA+D,CAAC,CAAA;QACjF,WAAW,CAAC,IAAI,CAAC,kEAAkE,CAAC,CAAA;QACpF,WAAW,CAAC,IAAI,CAAC,sCAAsC,CAAC,CAAA;QACxD,WAAW,CAAC,IAAI,CAAC,iEAAiE,CAAC,CAAA;IACrF,CAAC;IAED,IAAI,WAAW,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;QAC3B,IAAI,CAAC,MAAM,CAAC,WAAW,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,CAAA;IACvC,CAAC;IAED,uBAAuB;IACvB,IAAI,CAAC,GAAG,CAAC,OAAO,CAAC,aAAa,CAAC,GAAG,UAAU,CAAC,CAAC,CAAC,IAAI,EAAE,CAAA;AACvD,CAAC"}
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import { definePlugin } from "../services/plugin.js";
|
|
2
|
+
/**
|
|
3
|
+
* Configuration for the kysely plugin
|
|
4
|
+
*/
|
|
5
|
+
export interface KyselyConfig {
|
|
6
|
+
/** Output directory for generated files */
|
|
7
|
+
readonly outputDir?: string;
|
|
8
|
+
/** DB interface type name */
|
|
9
|
+
readonly dbTypeName?: string;
|
|
10
|
+
/** Use camelCase for field names (true) or keep original (false) */
|
|
11
|
+
readonly camelCase?: boolean;
|
|
12
|
+
/** Use runtime enums instead of string literal unions */
|
|
13
|
+
readonly runtimeEnums?: boolean;
|
|
14
|
+
/** Use type-only imports (recommended) */
|
|
15
|
+
readonly typeOnlyImports?: boolean;
|
|
16
|
+
/** Whether to generate query functions (default: true) */
|
|
17
|
+
readonly generateQueries?: boolean;
|
|
18
|
+
/**
|
|
19
|
+
* Path to import DB type from (relative to query files).
|
|
20
|
+
* Defaults to "./${dbTypeName}.js" since both are in the same outputDir.
|
|
21
|
+
*/
|
|
22
|
+
readonly dbTypesPath?: string;
|
|
23
|
+
/**
|
|
24
|
+
* Whether to call .execute() / .executeTakeFirst() on queries.
|
|
25
|
+
* When true (default), methods return Promise<Row> or Promise<Row[]>.
|
|
26
|
+
*/
|
|
27
|
+
readonly executeQueries?: boolean;
|
|
28
|
+
/**
|
|
29
|
+
* Whether to generate listMany() method for unfiltered table scans.
|
|
30
|
+
* Disabled by default since unfiltered scans don't use indexes.
|
|
31
|
+
*/
|
|
32
|
+
readonly generateListMany?: boolean;
|
|
33
|
+
/** Whether to generate function wrappers for stored functions. */
|
|
34
|
+
readonly generateFunctions?: boolean;
|
|
35
|
+
/** Output file name for scalar function wrappers (relative to outputDir). */
|
|
36
|
+
readonly functionsFile?: string;
|
|
37
|
+
/**
|
|
38
|
+
* Custom export name function for CRUD/lookup methods.
|
|
39
|
+
* @default (_entityName, methodName) => camelCase(methodName)
|
|
40
|
+
*/
|
|
41
|
+
readonly exportName?: ExportNameFn;
|
|
42
|
+
/**
|
|
43
|
+
* Export style for generated query functions.
|
|
44
|
+
* - "flat": Individual exports (e.g., `export const findById = ...`)
|
|
45
|
+
* - "namespace": Single object export (e.g., `export const User = { findById: ... }`)
|
|
46
|
+
*/
|
|
47
|
+
readonly exportStyle?: "flat" | "namespace";
|
|
48
|
+
/**
|
|
49
|
+
* Use explicit column lists instead of .selectAll().
|
|
50
|
+
* When true, generates .select(['col1', 'col2']) which excludes omitted fields at runtime.
|
|
51
|
+
*/
|
|
52
|
+
readonly explicitColumns?: boolean;
|
|
53
|
+
/**
|
|
54
|
+
* Whether to pass db as first parameter to each function.
|
|
55
|
+
* When true (default), functions take `db: Kysely<DB>` as first arg.
|
|
56
|
+
*/
|
|
57
|
+
readonly dbAsParameter?: boolean;
|
|
58
|
+
/**
|
|
59
|
+
* Header content to prepend to each generated query file.
|
|
60
|
+
* Use this to provide imports when dbAsParameter is false.
|
|
61
|
+
*/
|
|
62
|
+
readonly header?: string;
|
|
63
|
+
/** Default limit for listMany queries. */
|
|
64
|
+
readonly defaultLimit?: number;
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Function to generate export names for CRUD/lookup methods.
|
|
68
|
+
*/
|
|
69
|
+
type ExportNameFn = (entityName: string, methodName: string) => string;
|
|
70
|
+
export declare function kysely(config: KyselyConfig): ReturnType<typeof definePlugin>;
|
|
71
|
+
export {};
|
|
72
|
+
//# sourceMappingURL=kysely.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"kysely.d.ts","sourceRoot":"","sources":["../../src/plugins/kysely.ts"],"names":[],"mappings":"AAwBA,OAAO,EAAE,YAAY,EAAsB,MAAM,uBAAuB,CAAC;AAgCzE;;GAEG;AACH,MAAM,WAAW,YAAY;IAC3B,2CAA2C;IAC3C,QAAQ,CAAC,SAAS,CAAC,EAAE,MAAM,CAAC;IAC5B,6BAA6B;IAC7B,QAAQ,CAAC,UAAU,CAAC,EAAE,MAAM,CAAC;IAC7B,oEAAoE;IACpE,QAAQ,CAAC,SAAS,CAAC,EAAE,OAAO,CAAC;IAC7B,yDAAyD;IACzD,QAAQ,CAAC,YAAY,CAAC,EAAE,OAAO,CAAC;IAChC,0CAA0C;IAC1C,QAAQ,CAAC,eAAe,CAAC,EAAE,OAAO,CAAC;IACnC,0DAA0D;IAC1D,QAAQ,CAAC,eAAe,CAAC,EAAE,OAAO,CAAC;IACnC;;;OAGG;IACH,QAAQ,CAAC,WAAW,CAAC,EAAE,MAAM,CAAC;IAC9B;;;OAGG;IACH,QAAQ,CAAC,cAAc,CAAC,EAAE,OAAO,CAAC;IAClC;;;OAGG;IACH,QAAQ,CAAC,gBAAgB,CAAC,EAAE,OAAO,CAAC;IACpC,kEAAkE;IAClE,QAAQ,CAAC,iBAAiB,CAAC,EAAE,OAAO,CAAC;IACrC,6EAA6E;IAC7E,QAAQ,CAAC,aAAa,CAAC,EAAE,MAAM,CAAC;IAChC;;;OAGG;IACH,QAAQ,CAAC,UAAU,CAAC,EAAE,YAAY,CAAC;IACnC;;;;OAIG;IACH,QAAQ,CAAC,WAAW,CAAC,EAAE,MAAM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