@quickback-dev/cli 0.58.2 → 0.58.3
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 +3 -1
- package/dist/commands/compile.d.ts +5 -0
- package/dist/commands/compile.d.ts.map +1 -1
- package/dist/commands/compile.js +34 -0
- package/dist/commands/compile.js.map +1 -1
- package/dist/commands/schema.d.ts +16 -0
- package/dist/commands/schema.d.ts.map +1 -0
- package/dist/commands/schema.js +122 -0
- package/dist/commands/schema.js.map +1 -0
- package/dist/commands/schema.test.d.ts +2 -0
- package/dist/commands/schema.test.d.ts.map +1 -0
- package/dist/commands/schema.test.js +57 -0
- package/dist/commands/schema.test.js.map +1 -0
- package/dist/docs/content.d.ts.map +1 -1
- package/dist/docs/content.js +7 -2
- package/dist/docs/content.js.map +1 -1
- package/dist/index.js +10 -2
- package/dist/index.js.map +1 -1
- package/dist/lib/action-harvest.d.ts +9 -6
- package/dist/lib/action-harvest.d.ts.map +1 -1
- package/dist/lib/action-harvest.js +28 -15
- package/dist/lib/action-harvest.js.map +1 -1
- package/dist/lib/action-harvest.test.js +21 -0
- package/dist/lib/action-harvest.test.js.map +1 -1
- package/dist/lib/api-client.d.ts +53 -0
- package/dist/lib/api-client.d.ts.map +1 -1
- package/dist/lib/api-client.js +2 -0
- package/dist/lib/api-client.js.map +1 -1
- package/dist/lib/api-client.test.js +18 -0
- package/dist/lib/api-client.test.js.map +1 -1
- package/dist/lib/migrations/command-runner.d.ts +10 -0
- package/dist/lib/migrations/command-runner.d.ts.map +1 -0
- package/dist/lib/migrations/command-runner.js +66 -0
- package/dist/lib/migrations/command-runner.js.map +1 -0
- package/dist/lib/migrations/drizzle.d.ts +12 -0
- package/dist/lib/migrations/drizzle.d.ts.map +1 -0
- package/dist/lib/migrations/drizzle.js +60 -0
- package/dist/lib/migrations/drizzle.js.map +1 -0
- package/dist/lib/migrations/drizzle.test.d.ts +2 -0
- package/dist/lib/migrations/drizzle.test.d.ts.map +1 -0
- package/dist/lib/migrations/drizzle.test.js +61 -0
- package/dist/lib/migrations/drizzle.test.js.map +1 -0
- package/dist/lib/migrations/errors.d.ts +6 -0
- package/dist/lib/migrations/errors.d.ts.map +1 -0
- package/dist/lib/migrations/errors.js +9 -0
- package/dist/lib/migrations/errors.js.map +1 -0
- package/dist/lib/migrations/executor.d.ts +52 -0
- package/dist/lib/migrations/executor.d.ts.map +1 -0
- package/dist/lib/migrations/executor.js +190 -0
- package/dist/lib/migrations/executor.js.map +1 -0
- package/dist/lib/migrations/executor.test.d.ts +2 -0
- package/dist/lib/migrations/executor.test.d.ts.map +1 -0
- package/dist/lib/migrations/executor.test.js +241 -0
- package/dist/lib/migrations/executor.test.js.map +1 -0
- package/dist/lib/migrations/index.d.ts +11 -0
- package/dist/lib/migrations/index.d.ts.map +1 -0
- package/dist/lib/migrations/index.js +11 -0
- package/dist/lib/migrations/index.js.map +1 -0
- package/dist/lib/migrations/inventory.d.ts +19 -0
- package/dist/lib/migrations/inventory.d.ts.map +1 -0
- package/dist/lib/migrations/inventory.js +135 -0
- package/dist/lib/migrations/inventory.js.map +1 -0
- package/dist/lib/migrations/inventory.test.d.ts +2 -0
- package/dist/lib/migrations/inventory.test.d.ts.map +1 -0
- package/dist/lib/migrations/inventory.test.js +81 -0
- package/dist/lib/migrations/inventory.test.js.map +1 -0
- package/dist/lib/migrations/lock.d.ts +6 -0
- package/dist/lib/migrations/lock.d.ts.map +1 -0
- package/dist/lib/migrations/lock.js +101 -0
- package/dist/lib/migrations/lock.js.map +1 -0
- package/dist/lib/migrations/lock.test.d.ts +2 -0
- package/dist/lib/migrations/lock.test.d.ts.map +1 -0
- package/dist/lib/migrations/lock.test.js +35 -0
- package/dist/lib/migrations/lock.test.js.map +1 -0
- package/dist/lib/migrations/paths.d.ts +12 -0
- package/dist/lib/migrations/paths.d.ts.map +1 -0
- package/dist/lib/migrations/paths.js +82 -0
- package/dist/lib/migrations/paths.js.map +1 -0
- package/dist/lib/migrations/paths.test.d.ts +2 -0
- package/dist/lib/migrations/paths.test.d.ts.map +1 -0
- package/dist/lib/migrations/paths.test.js +32 -0
- package/dist/lib/migrations/paths.test.js.map +1 -0
- package/dist/lib/migrations/plan.d.ts +7 -0
- package/dist/lib/migrations/plan.d.ts.map +1 -0
- package/dist/lib/migrations/plan.js +189 -0
- package/dist/lib/migrations/plan.js.map +1 -0
- package/dist/lib/migrations/plan.test.d.ts +2 -0
- package/dist/lib/migrations/plan.test.d.ts.map +1 -0
- package/dist/lib/migrations/plan.test.js +104 -0
- package/dist/lib/migrations/plan.test.js.map +1 -0
- package/dist/lib/migrations/staging.d.ts +18 -0
- package/dist/lib/migrations/staging.d.ts.map +1 -0
- package/dist/lib/migrations/staging.js +150 -0
- package/dist/lib/migrations/staging.js.map +1 -0
- package/dist/lib/migrations/staging.test.d.ts +2 -0
- package/dist/lib/migrations/staging.test.d.ts.map +1 -0
- package/dist/lib/migrations/staging.test.js +73 -0
- package/dist/lib/migrations/staging.test.js.map +1 -0
- package/dist/lib/migrations/types.d.ts +49 -0
- package/dist/lib/migrations/types.d.ts.map +1 -0
- package/dist/lib/migrations/types.js +18 -0
- package/dist/lib/migrations/types.js.map +1 -0
- package/dist/lib/schema-engine-loader.d.ts +8 -0
- package/dist/lib/schema-engine-loader.d.ts.map +1 -0
- package/dist/lib/schema-engine-loader.js +26 -0
- package/dist/lib/schema-engine-loader.js.map +1 -0
- package/dist/lib/schema-sync/index.d.ts +41 -0
- package/dist/lib/schema-sync/index.d.ts.map +1 -0
- package/dist/lib/schema-sync/index.js +246 -0
- package/dist/lib/schema-sync/index.js.map +1 -0
- package/dist/lib/schema-sync/index.test.d.ts +2 -0
- package/dist/lib/schema-sync/index.test.d.ts.map +1 -0
- package/dist/lib/schema-sync/index.test.js +128 -0
- package/dist/lib/schema-sync/index.test.js.map +1 -0
- package/dist/schema-engine/index.d.ts +3 -0
- package/dist/schema-engine/index.d.ts.map +1 -0
- package/dist/schema-engine/index.js +854 -0
- package/dist/schema-engine/index.js.map +7 -0
- package/dist/schema-engine/managed-columns.d.ts +82 -0
- package/dist/schema-engine/managed-columns.d.ts.map +1 -0
- package/dist/schema-engine/source-sync.d.ts +64 -0
- package/dist/schema-engine/source-sync.d.ts.map +1 -0
- package/dist/skill/docs/compiler/definitions/actions.md +14 -6
- package/dist/skill/docs/compiler/targets/hono-api/using-the-api/openapi.md +7 -4
- package/dist/skill/docs/compiler/targets/hono-api/using-the-api/typescript.md +141 -0
- package/package.json +4 -2
- package/src/skill/docs/compiler/definitions/actions.md +14 -6
- package/src/skill/docs/compiler/targets/hono-api/using-the-api/openapi.md +7 -4
- package/src/skill/docs/compiler/targets/hono-api/using-the-api/typescript.md +141 -0
|
@@ -0,0 +1,854 @@
|
|
|
1
|
+
// ../schema-engine/src/managed-columns.ts
|
|
2
|
+
var SCHEMA_ENGINE_VERSION = "2026-07-28.1";
|
|
3
|
+
var SQLITE_EPOCH_SENTINEL = `'1970-01-01T00:00:00.000Z'`;
|
|
4
|
+
var SQLITE_ISO_NOW_FN = `() => new Date().toISOString()`;
|
|
5
|
+
var AUDIT_JS_NAMES = ["createdAt", "modifiedAt", "createdBy", "modifiedBy"];
|
|
6
|
+
var SOFT_DELETE_JS_NAMES = ["deletedAt", "deletedBy"];
|
|
7
|
+
var AUDIT_COLUMN_SPECS = [
|
|
8
|
+
{
|
|
9
|
+
jsName: "createdAt",
|
|
10
|
+
snakeName: "created_at",
|
|
11
|
+
kind: "timestamp",
|
|
12
|
+
notNull: true,
|
|
13
|
+
audit: true,
|
|
14
|
+
requirement: "audit"
|
|
15
|
+
},
|
|
16
|
+
{
|
|
17
|
+
jsName: "modifiedAt",
|
|
18
|
+
snakeName: "modified_at",
|
|
19
|
+
kind: "timestamp",
|
|
20
|
+
notNull: true,
|
|
21
|
+
audit: true,
|
|
22
|
+
requirement: "audit"
|
|
23
|
+
},
|
|
24
|
+
{
|
|
25
|
+
jsName: "createdBy",
|
|
26
|
+
snakeName: "created_by",
|
|
27
|
+
kind: "text",
|
|
28
|
+
notNull: false,
|
|
29
|
+
audit: true,
|
|
30
|
+
requirement: "audit"
|
|
31
|
+
},
|
|
32
|
+
{
|
|
33
|
+
jsName: "modifiedBy",
|
|
34
|
+
snakeName: "modified_by",
|
|
35
|
+
kind: "text",
|
|
36
|
+
notNull: false,
|
|
37
|
+
audit: true,
|
|
38
|
+
requirement: "audit"
|
|
39
|
+
}
|
|
40
|
+
];
|
|
41
|
+
var SOFT_DELETE_COLUMN_SPECS = [
|
|
42
|
+
{
|
|
43
|
+
jsName: "deletedAt",
|
|
44
|
+
snakeName: "deleted_at",
|
|
45
|
+
kind: "timestamp",
|
|
46
|
+
notNull: false,
|
|
47
|
+
audit: false,
|
|
48
|
+
requirement: "soft-delete"
|
|
49
|
+
},
|
|
50
|
+
{
|
|
51
|
+
jsName: "deletedBy",
|
|
52
|
+
snakeName: "deleted_by",
|
|
53
|
+
kind: "text",
|
|
54
|
+
notNull: false,
|
|
55
|
+
audit: false,
|
|
56
|
+
requirement: "soft-delete"
|
|
57
|
+
}
|
|
58
|
+
];
|
|
59
|
+
var MANAGED_AUDIT_COLUMN_SPECS = [
|
|
60
|
+
...AUDIT_COLUMN_SPECS,
|
|
61
|
+
...SOFT_DELETE_COLUMN_SPECS
|
|
62
|
+
];
|
|
63
|
+
var MANAGED_COLUMN_SPECS = MANAGED_AUDIT_COLUMN_SPECS;
|
|
64
|
+
var MANAGED_AUDIT_JS_NAMES = /* @__PURE__ */ new Set([
|
|
65
|
+
...AUDIT_JS_NAMES,
|
|
66
|
+
...SOFT_DELETE_JS_NAMES
|
|
67
|
+
]);
|
|
68
|
+
function managedSqlName(spec, convention) {
|
|
69
|
+
return convention === "snake_case" ? spec.snakeName : spec.jsName;
|
|
70
|
+
}
|
|
71
|
+
function managedColumnExpr(spec, dialect, sqlName) {
|
|
72
|
+
if (spec.kind === "text" || spec.jsName === "deletedAt" && dialect === "sqlite") {
|
|
73
|
+
return dialect === "postgres" && spec.kind === "timestamp" ? `timestamp("${sqlName}", { withTimezone: true })` : `text("${sqlName}")`;
|
|
74
|
+
}
|
|
75
|
+
if (spec.jsName === "deletedAt") {
|
|
76
|
+
return `timestamp("${sqlName}", { withTimezone: true })`;
|
|
77
|
+
}
|
|
78
|
+
const pgUpdate = spec.jsName === "modifiedAt" ? `.$onUpdate(() => new Date())` : "";
|
|
79
|
+
const sqliteUpdate = spec.jsName === "modifiedAt" ? `.$onUpdate(${SQLITE_ISO_NOW_FN})` : "";
|
|
80
|
+
return dialect === "postgres" ? `timestamp("${sqlName}", { withTimezone: true }).notNull().defaultNow()${pgUpdate}` : `text("${sqlName}").notNull().default(${SQLITE_EPOCH_SENTINEL}).$defaultFn(${SQLITE_ISO_NOW_FN})${sqliteUpdate}`;
|
|
81
|
+
}
|
|
82
|
+
function managedColumnLine(spec, dialect, convention) {
|
|
83
|
+
return `${spec.jsName}: ${managedColumnExpr(
|
|
84
|
+
spec,
|
|
85
|
+
dialect,
|
|
86
|
+
managedSqlName(spec, convention)
|
|
87
|
+
)},`;
|
|
88
|
+
}
|
|
89
|
+
function auditRequirement() {
|
|
90
|
+
return {
|
|
91
|
+
kind: "audit",
|
|
92
|
+
columns: AUDIT_COLUMN_SPECS,
|
|
93
|
+
qHelper: "audit"
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
function softDeleteRequirement() {
|
|
97
|
+
return {
|
|
98
|
+
kind: "soft-delete",
|
|
99
|
+
columns: SOFT_DELETE_COLUMN_SPECS,
|
|
100
|
+
qHelper: "softDelete"
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
function idRequirement(options) {
|
|
104
|
+
const sqlName = options.namingConvention === "camelCase" ? "id" : "id";
|
|
105
|
+
return {
|
|
106
|
+
kind: "id",
|
|
107
|
+
jsName: "id",
|
|
108
|
+
sqlName,
|
|
109
|
+
qExpression: options.qExpression ?? "q.id()",
|
|
110
|
+
rawExpression: options.rawExpression ?? `text("${sqlName}").primaryKey()`,
|
|
111
|
+
acceptedQExpressions: options.acceptedQExpressions,
|
|
112
|
+
acceptedRawExpressions: options.acceptedRawExpressions
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
function scopeRequirement(options) {
|
|
116
|
+
const jsName = options.jsName ?? `${options.kind}Id`;
|
|
117
|
+
const convention = options.namingConvention ?? "snake_case";
|
|
118
|
+
const snakeName = jsName.replace(/[A-Z]/g, (character) => `_${character.toLowerCase()}`);
|
|
119
|
+
const sqlName = options.sqlName ?? (convention === "snake_case" ? snakeName : jsName);
|
|
120
|
+
return {
|
|
121
|
+
kind: "scope",
|
|
122
|
+
jsName,
|
|
123
|
+
sqlName,
|
|
124
|
+
qExpression: options.qExpression ?? `q.scope("${options.kind}")`,
|
|
125
|
+
rawExpression: options.rawExpression ?? `text("${sqlName}").notNull()`,
|
|
126
|
+
acceptedQExpressions: options.acceptedQExpressions,
|
|
127
|
+
acceptedRawExpressions: options.acceptedRawExpressions
|
|
128
|
+
};
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
// ../schema-engine/src/source-sync.ts
|
|
132
|
+
var MANAGED_MARKER = "// \u2500\u2500 quickback:audit (compiler-managed \u2014 edits are validated, not merged) \u2500\u2500";
|
|
133
|
+
function planSchemaSource(options) {
|
|
134
|
+
const ts = options.typescript;
|
|
135
|
+
const namingConvention = options.namingConvention ?? "snake_case";
|
|
136
|
+
const sourceFile = ts.createSourceFile(
|
|
137
|
+
options.fileName,
|
|
138
|
+
options.source,
|
|
139
|
+
ts.ScriptTarget.Latest,
|
|
140
|
+
true,
|
|
141
|
+
options.fileName.endsWith(".tsx") ? ts.ScriptKind.TSX : ts.ScriptKind.TS
|
|
142
|
+
);
|
|
143
|
+
const diagnostics = [];
|
|
144
|
+
const parseDiagnostics = sourceFile.parseDiagnostics;
|
|
145
|
+
if (parseDiagnostics && parseDiagnostics.length > 0) {
|
|
146
|
+
for (const diagnostic of parseDiagnostics) {
|
|
147
|
+
diagnostics.push({
|
|
148
|
+
code: "SOURCE_PARSE_ERROR",
|
|
149
|
+
fileName: options.fileName,
|
|
150
|
+
start: diagnostic.start,
|
|
151
|
+
end: diagnostic.start !== void 0 && diagnostic.length !== void 0 ? diagnostic.start + diagnostic.length : void 0,
|
|
152
|
+
message: `TypeScript could not parse this schema source: ` + ts.flattenDiagnosticMessageText(diagnostic.messageText, "\n")
|
|
153
|
+
});
|
|
154
|
+
}
|
|
155
|
+
return {
|
|
156
|
+
fileName: options.fileName,
|
|
157
|
+
source: options.source,
|
|
158
|
+
tables: [],
|
|
159
|
+
edits: [],
|
|
160
|
+
diagnostics,
|
|
161
|
+
updatedSource: options.source,
|
|
162
|
+
changed: false,
|
|
163
|
+
canApply: false
|
|
164
|
+
};
|
|
165
|
+
}
|
|
166
|
+
const discovered = discoverTables(ts, sourceFile, options.dialect, namingConvention);
|
|
167
|
+
const edits = [];
|
|
168
|
+
const imports = /* @__PURE__ */ new Map();
|
|
169
|
+
for (const table of discovered) {
|
|
170
|
+
let requirements;
|
|
171
|
+
try {
|
|
172
|
+
requirements = options.resolveRequirements(table.context);
|
|
173
|
+
} catch (error) {
|
|
174
|
+
diagnostics.push({
|
|
175
|
+
code: "REQUIREMENT_RESOLUTION_FAILED",
|
|
176
|
+
fileName: options.fileName,
|
|
177
|
+
tableId: table.context.id,
|
|
178
|
+
tableName: table.context.tableName,
|
|
179
|
+
start: table.context.sourceStart,
|
|
180
|
+
end: table.context.sourceEnd,
|
|
181
|
+
message: `Could not determine managed-column requirements for table "${table.context.tableName}": ${error instanceof Error ? error.message : String(error)}`
|
|
182
|
+
});
|
|
183
|
+
continue;
|
|
184
|
+
}
|
|
185
|
+
if (!Array.isArray(requirements)) {
|
|
186
|
+
diagnostics.push({
|
|
187
|
+
code: "INVALID_REQUIREMENT",
|
|
188
|
+
fileName: options.fileName,
|
|
189
|
+
tableId: table.context.id,
|
|
190
|
+
tableName: table.context.tableName,
|
|
191
|
+
message: `The managed-column resolver for table "${table.context.tableName}" did not return an array.`
|
|
192
|
+
});
|
|
193
|
+
continue;
|
|
194
|
+
}
|
|
195
|
+
const entries = [];
|
|
196
|
+
const seenKinds = /* @__PURE__ */ new Set();
|
|
197
|
+
let needsManagedMarker = false;
|
|
198
|
+
for (const requirement of requirements) {
|
|
199
|
+
const key = requirement.kind === "scope" ? `${requirement.kind}:${requirement.jsName}` : requirement.kind;
|
|
200
|
+
if (seenKinds.has(key)) {
|
|
201
|
+
diagnostics.push({
|
|
202
|
+
code: "INVALID_REQUIREMENT",
|
|
203
|
+
fileName: options.fileName,
|
|
204
|
+
tableId: table.context.id,
|
|
205
|
+
tableName: table.context.tableName,
|
|
206
|
+
message: `Requirement "${key}" was supplied more than once for table "${table.context.tableName}".`
|
|
207
|
+
});
|
|
208
|
+
continue;
|
|
209
|
+
}
|
|
210
|
+
seenKinds.add(key);
|
|
211
|
+
if (isManagedGroupRequirement(requirement)) {
|
|
212
|
+
const result2 = planManagedGroup(
|
|
213
|
+
ts,
|
|
214
|
+
sourceFile,
|
|
215
|
+
table,
|
|
216
|
+
requirement,
|
|
217
|
+
options.fileName,
|
|
218
|
+
diagnostics
|
|
219
|
+
);
|
|
220
|
+
entries.push(...result2.entries);
|
|
221
|
+
needsManagedMarker ||= result2.needsMarker;
|
|
222
|
+
for (const identifier of result2.requiredImports) {
|
|
223
|
+
addImportRequirement(
|
|
224
|
+
imports,
|
|
225
|
+
drizzleCoreModule(table.context.dialect),
|
|
226
|
+
identifier,
|
|
227
|
+
table.context.id
|
|
228
|
+
);
|
|
229
|
+
}
|
|
230
|
+
if (result2.needsQImport) {
|
|
231
|
+
addImportRequirement(imports, "@quickback/compiler", "q", table.context.id);
|
|
232
|
+
}
|
|
233
|
+
continue;
|
|
234
|
+
}
|
|
235
|
+
const result = planPhysicalColumn(
|
|
236
|
+
ts,
|
|
237
|
+
sourceFile,
|
|
238
|
+
table,
|
|
239
|
+
requirement,
|
|
240
|
+
options.fileName,
|
|
241
|
+
diagnostics
|
|
242
|
+
);
|
|
243
|
+
entries.push(...result.entries);
|
|
244
|
+
for (const identifier of result.requiredImports) {
|
|
245
|
+
addImportRequirement(
|
|
246
|
+
imports,
|
|
247
|
+
drizzleCoreModule(table.context.dialect),
|
|
248
|
+
identifier,
|
|
249
|
+
table.context.id
|
|
250
|
+
);
|
|
251
|
+
}
|
|
252
|
+
if (result.needsQImport) {
|
|
253
|
+
addImportRequirement(imports, "@quickback/compiler", "q", table.context.id);
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
if (entries.length === 0) continue;
|
|
257
|
+
entries.sort((a, b) => a.order - b.order);
|
|
258
|
+
if (table.context.style === "raw-drizzle" && needsManagedMarker && !options.source.slice(table.columns.getStart(sourceFile), table.columns.getEnd()).includes("quickback:audit")) {
|
|
259
|
+
const firstManagedIndex = entries.findIndex((entry) => entry.order >= 20);
|
|
260
|
+
entries.splice(firstManagedIndex < 0 ? entries.length : firstManagedIndex, 0, {
|
|
261
|
+
source: MANAGED_MARKER,
|
|
262
|
+
order: 19
|
|
263
|
+
});
|
|
264
|
+
}
|
|
265
|
+
edits.push(
|
|
266
|
+
...planObjectInsertion(
|
|
267
|
+
sourceFile,
|
|
268
|
+
table.columns,
|
|
269
|
+
entries.map((entry) => entry.source),
|
|
270
|
+
table.context.id
|
|
271
|
+
)
|
|
272
|
+
);
|
|
273
|
+
}
|
|
274
|
+
for (const [moduleName, requirement] of imports) {
|
|
275
|
+
planNamedImport(
|
|
276
|
+
ts,
|
|
277
|
+
sourceFile,
|
|
278
|
+
moduleName,
|
|
279
|
+
requirement,
|
|
280
|
+
options.fileName,
|
|
281
|
+
edits,
|
|
282
|
+
diagnostics
|
|
283
|
+
);
|
|
284
|
+
}
|
|
285
|
+
let updatedSource = options.source;
|
|
286
|
+
try {
|
|
287
|
+
updatedSource = applySchemaSourceEdits(options.source, edits);
|
|
288
|
+
} catch (error) {
|
|
289
|
+
diagnostics.push({
|
|
290
|
+
code: "OVERLAPPING_SOURCE_EDITS",
|
|
291
|
+
fileName: options.fileName,
|
|
292
|
+
message: error instanceof Error ? error.message : String(error)
|
|
293
|
+
});
|
|
294
|
+
}
|
|
295
|
+
return {
|
|
296
|
+
fileName: options.fileName,
|
|
297
|
+
source: options.source,
|
|
298
|
+
tables: discovered.map((table) => table.context),
|
|
299
|
+
edits,
|
|
300
|
+
diagnostics,
|
|
301
|
+
updatedSource,
|
|
302
|
+
changed: updatedSource !== options.source,
|
|
303
|
+
canApply: diagnostics.length === 0
|
|
304
|
+
};
|
|
305
|
+
}
|
|
306
|
+
function isManagedGroupRequirement(requirement) {
|
|
307
|
+
return requirement.kind === "audit" || requirement.kind === "soft-delete";
|
|
308
|
+
}
|
|
309
|
+
function applySchemaSourceEdits(source, edits) {
|
|
310
|
+
const ordered = [...edits].sort((left, right) => {
|
|
311
|
+
if (left.start !== right.start) return right.start - left.start;
|
|
312
|
+
return right.end - left.end;
|
|
313
|
+
});
|
|
314
|
+
let previousStart = source.length + 1;
|
|
315
|
+
let output = source;
|
|
316
|
+
for (const edit of ordered) {
|
|
317
|
+
if (edit.start < 0 || edit.end < edit.start || edit.end > source.length || edit.end > previousStart) {
|
|
318
|
+
throw new Error(
|
|
319
|
+
`Schema source edits overlap or address an invalid span: ${edit.start}..${edit.end} (${edit.reason}).`
|
|
320
|
+
);
|
|
321
|
+
}
|
|
322
|
+
output = output.slice(0, edit.start) + edit.text + output.slice(edit.end);
|
|
323
|
+
previousStart = edit.start;
|
|
324
|
+
}
|
|
325
|
+
return output;
|
|
326
|
+
}
|
|
327
|
+
function discoverTables(ts, sourceFile, defaultDialect, namingConvention) {
|
|
328
|
+
const resourceByVariable = /* @__PURE__ */ new Map();
|
|
329
|
+
const collectResources = (node) => {
|
|
330
|
+
if (ts.isCallExpression(node) && isNamedCall(ts, node.expression, "defineTable")) {
|
|
331
|
+
const tableArg = node.arguments[0];
|
|
332
|
+
const configArg = node.arguments[1];
|
|
333
|
+
if (tableArg && ts.isIdentifier(tableArg) && configArg) {
|
|
334
|
+
resourceByVariable.set(tableArg.text, configArg);
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
ts.forEachChild(node, collectResources);
|
|
338
|
+
};
|
|
339
|
+
collectResources(sourceFile);
|
|
340
|
+
const tables = [];
|
|
341
|
+
const occurrenceByName = /* @__PURE__ */ new Map();
|
|
342
|
+
const visit = (node) => {
|
|
343
|
+
if (!ts.isCallExpression(node)) {
|
|
344
|
+
ts.forEachChild(node, visit);
|
|
345
|
+
return;
|
|
346
|
+
}
|
|
347
|
+
const callKind = tableCallKind(ts, node.expression);
|
|
348
|
+
if (!callKind) {
|
|
349
|
+
ts.forEachChild(node, visit);
|
|
350
|
+
return;
|
|
351
|
+
}
|
|
352
|
+
const nameArg = node.arguments[0];
|
|
353
|
+
const bodyArg = node.arguments[1];
|
|
354
|
+
if (!nameArg || !ts.isStringLiteralLike(nameArg) || !bodyArg) {
|
|
355
|
+
ts.forEachChild(node, visit);
|
|
356
|
+
return;
|
|
357
|
+
}
|
|
358
|
+
const variableName = enclosingVariableName(ts, node);
|
|
359
|
+
let columns;
|
|
360
|
+
let resource;
|
|
361
|
+
let style;
|
|
362
|
+
let dialect = defaultDialect;
|
|
363
|
+
if (callKind === "feature") {
|
|
364
|
+
if (!ts.isObjectLiteralExpression(bodyArg)) {
|
|
365
|
+
ts.forEachChild(node, visit);
|
|
366
|
+
return;
|
|
367
|
+
}
|
|
368
|
+
columns = objectPropertyObject(ts, bodyArg, "columns");
|
|
369
|
+
resource = bodyArg;
|
|
370
|
+
style = "q-feature";
|
|
371
|
+
} else if (callKind === "q-table") {
|
|
372
|
+
if (!ts.isObjectLiteralExpression(bodyArg)) {
|
|
373
|
+
ts.forEachChild(node, visit);
|
|
374
|
+
return;
|
|
375
|
+
}
|
|
376
|
+
columns = bodyArg;
|
|
377
|
+
resource = variableName ? resourceByVariable.get(variableName) : void 0;
|
|
378
|
+
style = "q-table";
|
|
379
|
+
} else {
|
|
380
|
+
if (!ts.isObjectLiteralExpression(bodyArg)) {
|
|
381
|
+
ts.forEachChild(node, visit);
|
|
382
|
+
return;
|
|
383
|
+
}
|
|
384
|
+
columns = bodyArg;
|
|
385
|
+
resource = variableName ? resourceByVariable.get(variableName) : void 0;
|
|
386
|
+
style = "raw-drizzle";
|
|
387
|
+
dialect = callKind === "pg-table" ? "postgres" : "sqlite";
|
|
388
|
+
}
|
|
389
|
+
if (!columns) {
|
|
390
|
+
ts.forEachChild(node, visit);
|
|
391
|
+
return;
|
|
392
|
+
}
|
|
393
|
+
const tableName = nameArg.text;
|
|
394
|
+
const occurrence = (occurrenceByName.get(tableName) ?? 0) + 1;
|
|
395
|
+
occurrenceByName.set(tableName, occurrence);
|
|
396
|
+
tables.push({
|
|
397
|
+
context: {
|
|
398
|
+
id: `${tableName}@${occurrence}`,
|
|
399
|
+
fileName: sourceFile.fileName,
|
|
400
|
+
tableName,
|
|
401
|
+
variableName,
|
|
402
|
+
style,
|
|
403
|
+
dialect,
|
|
404
|
+
namingConvention,
|
|
405
|
+
hasResourceConfig: resource !== void 0,
|
|
406
|
+
deleteMode: resource ? readDeleteMode(ts, resource) : "default",
|
|
407
|
+
sourceStart: node.getStart(sourceFile),
|
|
408
|
+
sourceEnd: node.getEnd()
|
|
409
|
+
},
|
|
410
|
+
columns
|
|
411
|
+
});
|
|
412
|
+
};
|
|
413
|
+
visit(sourceFile);
|
|
414
|
+
return tables.sort((left, right) => left.context.sourceStart - right.context.sourceStart);
|
|
415
|
+
}
|
|
416
|
+
function tableCallKind(ts, expression) {
|
|
417
|
+
if (ts.isIdentifier(expression)) {
|
|
418
|
+
if (expression.text === "feature") return "feature";
|
|
419
|
+
if (expression.text === "pgTable") return "pg-table";
|
|
420
|
+
if (expression.text === "sqliteTable") return "sqlite-table";
|
|
421
|
+
return void 0;
|
|
422
|
+
}
|
|
423
|
+
if (!ts.isPropertyAccessExpression(expression)) return void 0;
|
|
424
|
+
if (expression.name.text === "pgTable") return "pg-table";
|
|
425
|
+
if (expression.name.text === "sqliteTable") return "sqlite-table";
|
|
426
|
+
if (ts.isIdentifier(expression.expression) && (expression.expression.text === "q" || expression.expression.text === "qb")) {
|
|
427
|
+
if (expression.name.text === "table") return "q-table";
|
|
428
|
+
if (expression.name.text === "feature") return "feature";
|
|
429
|
+
}
|
|
430
|
+
return void 0;
|
|
431
|
+
}
|
|
432
|
+
function enclosingVariableName(ts, call) {
|
|
433
|
+
const parent = call.parent;
|
|
434
|
+
return ts.isVariableDeclaration(parent) && ts.isIdentifier(parent.name) ? parent.name.text : void 0;
|
|
435
|
+
}
|
|
436
|
+
function isNamedCall(ts, expression, name) {
|
|
437
|
+
return ts.isIdentifier(expression) && expression.text === name || ts.isPropertyAccessExpression(expression) && expression.name.text === name;
|
|
438
|
+
}
|
|
439
|
+
function objectPropertyObject(ts, object, name) {
|
|
440
|
+
const property = object.properties.find(
|
|
441
|
+
(candidate) => ts.isPropertyAssignment(candidate) && propertyName(ts, candidate.name) === name
|
|
442
|
+
);
|
|
443
|
+
return property && ts.isObjectLiteralExpression(property.initializer) ? property.initializer : void 0;
|
|
444
|
+
}
|
|
445
|
+
function readDeleteMode(ts, expression) {
|
|
446
|
+
if (!ts.isObjectLiteralExpression(expression)) return "dynamic";
|
|
447
|
+
let deleteExpression = propertyInitializer(ts, expression, "delete");
|
|
448
|
+
if (!deleteExpression) {
|
|
449
|
+
const crud = propertyInitializer(ts, expression, "crud");
|
|
450
|
+
if (crud && !ts.isObjectLiteralExpression(crud)) return "dynamic";
|
|
451
|
+
if (crud && ts.isObjectLiteralExpression(crud)) {
|
|
452
|
+
deleteExpression = propertyInitializer(ts, crud, "delete");
|
|
453
|
+
}
|
|
454
|
+
}
|
|
455
|
+
if (!deleteExpression) return "default";
|
|
456
|
+
if (!ts.isObjectLiteralExpression(deleteExpression)) return "dynamic";
|
|
457
|
+
const mode = propertyInitializer(ts, deleteExpression, "mode");
|
|
458
|
+
if (!mode) return "default";
|
|
459
|
+
if (!ts.isStringLiteralLike(mode)) return "dynamic";
|
|
460
|
+
if (mode.text === "hard") return "hard";
|
|
461
|
+
if (mode.text === "soft") return "soft";
|
|
462
|
+
return "dynamic";
|
|
463
|
+
}
|
|
464
|
+
function propertyInitializer(ts, object, name) {
|
|
465
|
+
for (const property of object.properties) {
|
|
466
|
+
if (ts.isPropertyAssignment(property) && propertyName(ts, property.name) === name) {
|
|
467
|
+
return property.initializer;
|
|
468
|
+
}
|
|
469
|
+
if (ts.isShorthandPropertyAssignment(property) && property.name.text === name) {
|
|
470
|
+
return property.name;
|
|
471
|
+
}
|
|
472
|
+
}
|
|
473
|
+
return void 0;
|
|
474
|
+
}
|
|
475
|
+
function propertyName(ts, name) {
|
|
476
|
+
if (ts.isIdentifier(name) || ts.isPrivateIdentifier(name)) return name.text;
|
|
477
|
+
if (ts.isStringLiteralLike(name) || ts.isNumericLiteral(name)) return name.text;
|
|
478
|
+
return void 0;
|
|
479
|
+
}
|
|
480
|
+
function planManagedGroup(ts, sourceFile, table, requirement, fileName, diagnostics) {
|
|
481
|
+
if (table.context.style !== "raw-drizzle") {
|
|
482
|
+
const helperSpread = findQHelperSpread(ts, table.columns, requirement.qHelper);
|
|
483
|
+
const explicit = table.columns.properties.filter((property) => {
|
|
484
|
+
if (!ts.isPropertyAssignment(property)) return false;
|
|
485
|
+
const name = propertyName(ts, property.name);
|
|
486
|
+
return requirement.columns.some((column) => column.jsName === name);
|
|
487
|
+
});
|
|
488
|
+
if (helperSpread && explicit.length > 0) {
|
|
489
|
+
diagnostics.push({
|
|
490
|
+
code: "DUPLICATE_MANAGED_REPRESENTATION",
|
|
491
|
+
fileName,
|
|
492
|
+
tableId: table.context.id,
|
|
493
|
+
tableName: table.context.tableName,
|
|
494
|
+
start: explicit[0].getStart(sourceFile),
|
|
495
|
+
end: explicit[explicit.length - 1].getEnd(),
|
|
496
|
+
expected: `...q.${requirement.qHelper}(),`,
|
|
497
|
+
actual: explicit.map((property) => property.getText(sourceFile)).join("\n"),
|
|
498
|
+
message: `Table "${table.context.tableName}" declares both ...q.${requirement.qHelper}() and explicit ${requirement.kind} columns. Remove the explicit duplicates manually.`
|
|
499
|
+
});
|
|
500
|
+
return {
|
|
501
|
+
entries: [],
|
|
502
|
+
requiredImports: /* @__PURE__ */ new Set(),
|
|
503
|
+
needsQImport: false,
|
|
504
|
+
needsMarker: false
|
|
505
|
+
};
|
|
506
|
+
}
|
|
507
|
+
if (helperSpread) {
|
|
508
|
+
return {
|
|
509
|
+
entries: [],
|
|
510
|
+
requiredImports: /* @__PURE__ */ new Set(),
|
|
511
|
+
needsQImport: false,
|
|
512
|
+
needsMarker: false
|
|
513
|
+
};
|
|
514
|
+
}
|
|
515
|
+
if (explicit.length > 0) {
|
|
516
|
+
diagnostics.push({
|
|
517
|
+
code: "DIVERGENT_MANAGED_COLUMN",
|
|
518
|
+
fileName,
|
|
519
|
+
tableId: table.context.id,
|
|
520
|
+
tableName: table.context.tableName,
|
|
521
|
+
start: explicit[0].getStart(sourceFile),
|
|
522
|
+
end: explicit[explicit.length - 1].getEnd(),
|
|
523
|
+
expected: `...q.${requirement.qHelper}(),`,
|
|
524
|
+
actual: explicit.map((property) => property.getText(sourceFile)).join("\n"),
|
|
525
|
+
message: `Table "${table.context.tableName}" uses explicit q-DSL ${requirement.kind} columns. Quickback cannot prove that hand-written q chains match the managed contract; replace them manually with ...q.${requirement.qHelper}().`
|
|
526
|
+
});
|
|
527
|
+
return {
|
|
528
|
+
entries: [],
|
|
529
|
+
requiredImports: /* @__PURE__ */ new Set(),
|
|
530
|
+
needsQImport: false,
|
|
531
|
+
needsMarker: false
|
|
532
|
+
};
|
|
533
|
+
}
|
|
534
|
+
return {
|
|
535
|
+
entries: [{ source: `...q.${requirement.qHelper}(),`, order: groupOrder(requirement.kind) }],
|
|
536
|
+
requiredImports: /* @__PURE__ */ new Set(),
|
|
537
|
+
needsQImport: true,
|
|
538
|
+
needsMarker: false
|
|
539
|
+
};
|
|
540
|
+
}
|
|
541
|
+
const entries = [];
|
|
542
|
+
const requiredImports = /* @__PURE__ */ new Set();
|
|
543
|
+
for (const column of requirement.columns) {
|
|
544
|
+
const sqlName = managedSqlName(column, table.context.namingConvention);
|
|
545
|
+
const expectedExpression = managedColumnExpr(column, table.context.dialect, sqlName);
|
|
546
|
+
const expectedLine = managedColumnLine(
|
|
547
|
+
column,
|
|
548
|
+
table.context.dialect,
|
|
549
|
+
table.context.namingConvention
|
|
550
|
+
);
|
|
551
|
+
const matches = matchingRawProperties(ts, table.columns, column.jsName, sqlName);
|
|
552
|
+
if (matches.length > 1) {
|
|
553
|
+
diagnostics.push(duplicateColumnDiagnostic(fileName, sourceFile, table, column.jsName, matches));
|
|
554
|
+
continue;
|
|
555
|
+
}
|
|
556
|
+
const existing = matches[0];
|
|
557
|
+
if (!existing) {
|
|
558
|
+
entries.push({ source: expectedLine, order: groupOrder(requirement.kind) });
|
|
559
|
+
for (const identifier of calledBuilderIdentifiers(expectedExpression)) {
|
|
560
|
+
requiredImports.add(identifier);
|
|
561
|
+
}
|
|
562
|
+
continue;
|
|
563
|
+
}
|
|
564
|
+
if (propertyName(ts, existing.name) !== column.jsName || !expressionMatches(sourceFile, existing.initializer, [expectedExpression])) {
|
|
565
|
+
diagnostics.push(
|
|
566
|
+
divergentColumnDiagnostic(
|
|
567
|
+
fileName,
|
|
568
|
+
sourceFile,
|
|
569
|
+
table,
|
|
570
|
+
existing,
|
|
571
|
+
expectedLine,
|
|
572
|
+
column.jsName
|
|
573
|
+
)
|
|
574
|
+
);
|
|
575
|
+
}
|
|
576
|
+
}
|
|
577
|
+
return {
|
|
578
|
+
entries,
|
|
579
|
+
requiredImports,
|
|
580
|
+
needsQImport: false,
|
|
581
|
+
needsMarker: entries.length > 0
|
|
582
|
+
};
|
|
583
|
+
}
|
|
584
|
+
function planPhysicalColumn(ts, sourceFile, table, requirement, fileName, diagnostics) {
|
|
585
|
+
const raw = table.context.style === "raw-drizzle";
|
|
586
|
+
const matches = raw ? matchingRawProperties(ts, table.columns, requirement.jsName, requirement.sqlName) : matchingQProperties(ts, table.columns, requirement.jsName);
|
|
587
|
+
if (matches.length > 1) {
|
|
588
|
+
diagnostics.push(
|
|
589
|
+
duplicateColumnDiagnostic(fileName, sourceFile, table, requirement.jsName, matches)
|
|
590
|
+
);
|
|
591
|
+
return { entries: [], requiredImports: /* @__PURE__ */ new Set(), needsQImport: false };
|
|
592
|
+
}
|
|
593
|
+
const expression = raw ? requirement.rawExpression : requirement.qExpression;
|
|
594
|
+
const accepted = raw ? [requirement.rawExpression, ...requirement.acceptedRawExpressions ?? []] : [requirement.qExpression, ...requirement.acceptedQExpressions ?? []];
|
|
595
|
+
const expected = `${requirement.jsName}: ${expression},`;
|
|
596
|
+
const existing = matches[0];
|
|
597
|
+
if (existing) {
|
|
598
|
+
if (propertyName(ts, existing.name) !== requirement.jsName || !expressionMatches(sourceFile, existing.initializer, accepted)) {
|
|
599
|
+
diagnostics.push(
|
|
600
|
+
divergentColumnDiagnostic(
|
|
601
|
+
fileName,
|
|
602
|
+
sourceFile,
|
|
603
|
+
table,
|
|
604
|
+
existing,
|
|
605
|
+
expected,
|
|
606
|
+
requirement.jsName
|
|
607
|
+
)
|
|
608
|
+
);
|
|
609
|
+
}
|
|
610
|
+
return { entries: [], requiredImports: /* @__PURE__ */ new Set(), needsQImport: false };
|
|
611
|
+
}
|
|
612
|
+
return {
|
|
613
|
+
entries: [{ source: expected, order: requirement.kind === "id" ? 0 : 10 }],
|
|
614
|
+
requiredImports: raw ? new Set(calledBuilderIdentifiers(requirement.rawExpression)) : /* @__PURE__ */ new Set(),
|
|
615
|
+
needsQImport: !raw
|
|
616
|
+
};
|
|
617
|
+
}
|
|
618
|
+
function groupOrder(kind) {
|
|
619
|
+
return kind === "audit" ? 20 : 30;
|
|
620
|
+
}
|
|
621
|
+
function findQHelperSpread(ts, object, helper) {
|
|
622
|
+
return object.properties.find((property) => {
|
|
623
|
+
if (!ts.isSpreadAssignment(property) || !ts.isCallExpression(property.expression)) {
|
|
624
|
+
return false;
|
|
625
|
+
}
|
|
626
|
+
const callee = property.expression.expression;
|
|
627
|
+
return ts.isPropertyAccessExpression(callee) && ts.isIdentifier(callee.expression) && callee.expression.text === "q" && callee.name.text === helper && property.expression.arguments.length === 0;
|
|
628
|
+
});
|
|
629
|
+
}
|
|
630
|
+
function matchingQProperties(ts, object, jsName) {
|
|
631
|
+
return object.properties.filter(
|
|
632
|
+
(property) => ts.isPropertyAssignment(property) && propertyName(ts, property.name) === jsName
|
|
633
|
+
);
|
|
634
|
+
}
|
|
635
|
+
function matchingRawProperties(ts, object, jsName, sqlName) {
|
|
636
|
+
return object.properties.filter((property) => {
|
|
637
|
+
if (!ts.isPropertyAssignment(property)) return false;
|
|
638
|
+
if (propertyName(ts, property.name) === jsName) return true;
|
|
639
|
+
return firstStringArgument(ts, property.initializer) === sqlName;
|
|
640
|
+
});
|
|
641
|
+
}
|
|
642
|
+
function firstStringArgument(ts, expression) {
|
|
643
|
+
let current = expression;
|
|
644
|
+
while (ts.isCallExpression(current)) {
|
|
645
|
+
const first = current.arguments[0];
|
|
646
|
+
if (first && ts.isStringLiteralLike(first)) return first.text;
|
|
647
|
+
const callee = current.expression;
|
|
648
|
+
if (!ts.isPropertyAccessExpression(callee)) break;
|
|
649
|
+
current = callee.expression;
|
|
650
|
+
}
|
|
651
|
+
return void 0;
|
|
652
|
+
}
|
|
653
|
+
function expressionMatches(sourceFile, expression, accepted) {
|
|
654
|
+
const actual = normalizeExpression(expression.getText(sourceFile));
|
|
655
|
+
return accepted.some((candidate) => normalizeExpression(candidate) === actual);
|
|
656
|
+
}
|
|
657
|
+
function normalizeExpression(source) {
|
|
658
|
+
return source.replace(/\/\*[\s\S]*?\*\//g, "").replace(/\/\/[^\r\n]*/g, "").replace(/'([^'\\]*(?:\\.[^'\\]*)*)'/g, (_match, value) => `"${value}"`).replace(/\s+/g, "");
|
|
659
|
+
}
|
|
660
|
+
function calledBuilderIdentifiers(expression) {
|
|
661
|
+
const identifiers = /* @__PURE__ */ new Set();
|
|
662
|
+
for (const match of expression.matchAll(/(?:^|[\s:,{(])([A-Za-z_$][\w$]*)\s*\(/g)) {
|
|
663
|
+
const identifier = match[1];
|
|
664
|
+
if (identifier !== "Date") identifiers.add(identifier);
|
|
665
|
+
}
|
|
666
|
+
return [...identifiers].sort();
|
|
667
|
+
}
|
|
668
|
+
function duplicateColumnDiagnostic(fileName, sourceFile, table, jsName, properties) {
|
|
669
|
+
return {
|
|
670
|
+
code: "DUPLICATE_TABLE_COLUMN",
|
|
671
|
+
fileName,
|
|
672
|
+
tableId: table.context.id,
|
|
673
|
+
tableName: table.context.tableName,
|
|
674
|
+
start: properties[0].getStart(sourceFile),
|
|
675
|
+
end: properties[properties.length - 1].getEnd(),
|
|
676
|
+
actual: properties.map((property) => property.getText(sourceFile)).join("\n"),
|
|
677
|
+
message: `Table "${table.context.tableName}" declares managed column "${jsName}" more than once. Resolve the duplicate manually before syncing.`
|
|
678
|
+
};
|
|
679
|
+
}
|
|
680
|
+
function divergentColumnDiagnostic(fileName, sourceFile, table, property, expected, jsName) {
|
|
681
|
+
return {
|
|
682
|
+
code: "DIVERGENT_MANAGED_COLUMN",
|
|
683
|
+
fileName,
|
|
684
|
+
tableId: table.context.id,
|
|
685
|
+
tableName: table.context.tableName,
|
|
686
|
+
start: property.getStart(sourceFile),
|
|
687
|
+
end: property.getEnd(),
|
|
688
|
+
expected,
|
|
689
|
+
actual: property.getText(sourceFile),
|
|
690
|
+
message: `Table "${table.context.tableName}" has a divergent managed column "${jsName}". Quickback will not rewrite it automatically; replace it with the canonical form.`
|
|
691
|
+
};
|
|
692
|
+
}
|
|
693
|
+
function planObjectInsertion(sourceFile, object, entries, tableId) {
|
|
694
|
+
if (entries.length === 0) return [];
|
|
695
|
+
const source = sourceFile.text;
|
|
696
|
+
const lineEnding = source.includes("\r\n") ? "\r\n" : "\n";
|
|
697
|
+
const open = object.getStart(sourceFile);
|
|
698
|
+
const close = object.getEnd() - 1;
|
|
699
|
+
const multiline = source.slice(open, close).includes("\n");
|
|
700
|
+
const edits = [];
|
|
701
|
+
const last = object.properties[object.properties.length - 1];
|
|
702
|
+
const hasTrailingComma = last ? source.slice(last.getEnd(), close).includes(",") : false;
|
|
703
|
+
if (!multiline) {
|
|
704
|
+
if (last) {
|
|
705
|
+
const tail = source.slice(last.getEnd(), close);
|
|
706
|
+
const preservesComment = tail.includes("//") || tail.includes("/*");
|
|
707
|
+
edits.push({
|
|
708
|
+
start: last.getEnd(),
|
|
709
|
+
end: close,
|
|
710
|
+
text: preservesComment ? `${hasTrailingComma ? "" : ","}${tail}${/\s$/.test(tail) ? "" : " "}${entries.join(" ")} ` : `, ${entries.join(" ")} `,
|
|
711
|
+
tableId,
|
|
712
|
+
reason: "insert managed columns into single-line table object"
|
|
713
|
+
});
|
|
714
|
+
} else {
|
|
715
|
+
edits.push({
|
|
716
|
+
start: open + 1,
|
|
717
|
+
end: close,
|
|
718
|
+
text: ` ${entries.join(" ")} `,
|
|
719
|
+
tableId,
|
|
720
|
+
reason: "insert managed columns into empty single-line table object"
|
|
721
|
+
});
|
|
722
|
+
}
|
|
723
|
+
return edits;
|
|
724
|
+
}
|
|
725
|
+
if (last && !hasTrailingComma) {
|
|
726
|
+
edits.push({
|
|
727
|
+
start: last.getEnd(),
|
|
728
|
+
end: last.getEnd(),
|
|
729
|
+
text: ",",
|
|
730
|
+
tableId,
|
|
731
|
+
reason: "terminate the previous table column before managed-column insertion"
|
|
732
|
+
});
|
|
733
|
+
}
|
|
734
|
+
const closeLineStart = source.lastIndexOf("\n", close - 1) + 1;
|
|
735
|
+
const closeIndent = source.slice(closeLineStart, close).match(/^[ \t]*/)?.[0] ?? "";
|
|
736
|
+
const first = object.properties[0];
|
|
737
|
+
let entryIndent;
|
|
738
|
+
if (first) {
|
|
739
|
+
const firstStart = first.getStart(sourceFile);
|
|
740
|
+
const firstLineStart = source.lastIndexOf("\n", firstStart - 1) + 1;
|
|
741
|
+
entryIndent = source.slice(firstLineStart, firstStart);
|
|
742
|
+
} else {
|
|
743
|
+
entryIndent = closeIndent.includes(" ") ? `${closeIndent} ` : `${closeIndent} `;
|
|
744
|
+
}
|
|
745
|
+
const text = entries.map((entry) => `${entryIndent}${entry}`).join(lineEnding) + lineEnding;
|
|
746
|
+
edits.push({
|
|
747
|
+
start: closeLineStart,
|
|
748
|
+
end: closeLineStart,
|
|
749
|
+
text,
|
|
750
|
+
tableId,
|
|
751
|
+
reason: "insert canonical managed-column declarations"
|
|
752
|
+
});
|
|
753
|
+
return edits;
|
|
754
|
+
}
|
|
755
|
+
function addImportRequirement(imports, moduleName, identifier, tableId) {
|
|
756
|
+
let requirement = imports.get(moduleName);
|
|
757
|
+
if (!requirement) {
|
|
758
|
+
requirement = { identifiers: /* @__PURE__ */ new Set(), tableIds: /* @__PURE__ */ new Set() };
|
|
759
|
+
imports.set(moduleName, requirement);
|
|
760
|
+
}
|
|
761
|
+
requirement.identifiers.add(identifier);
|
|
762
|
+
requirement.tableIds.add(tableId);
|
|
763
|
+
}
|
|
764
|
+
function drizzleCoreModule(dialect) {
|
|
765
|
+
return dialect === "postgres" ? "drizzle-orm/pg-core" : "drizzle-orm/sqlite-core";
|
|
766
|
+
}
|
|
767
|
+
function planNamedImport(ts, sourceFile, moduleName, requirement, fileName, edits, diagnostics) {
|
|
768
|
+
const candidates = sourceFile.statements.filter(
|
|
769
|
+
(statement) => ts.isImportDeclaration(statement) && ts.isStringLiteralLike(statement.moduleSpecifier) && statement.moduleSpecifier.text === moduleName && !!statement.importClause && !statement.importClause.isTypeOnly && !!statement.importClause.namedBindings && ts.isNamedImports(statement.importClause.namedBindings)
|
|
770
|
+
);
|
|
771
|
+
const present = /* @__PURE__ */ new Set();
|
|
772
|
+
for (const candidate of candidates) {
|
|
773
|
+
const bindings2 = candidate.importClause.namedBindings;
|
|
774
|
+
for (const element of bindings2.elements) present.add(element.name.text);
|
|
775
|
+
}
|
|
776
|
+
const missing = [...requirement.identifiers].filter((name) => !present.has(name)).sort();
|
|
777
|
+
if (missing.length === 0) return;
|
|
778
|
+
const target = candidates[0];
|
|
779
|
+
if (!target) {
|
|
780
|
+
diagnostics.push({
|
|
781
|
+
code: "MISSING_VALUE_IMPORT",
|
|
782
|
+
fileName,
|
|
783
|
+
tableId: requirement.tableIds.size === 1 ? [...requirement.tableIds][0] : void 0,
|
|
784
|
+
expected: `import { ${missing.join(", ")} } from "${moduleName}";`,
|
|
785
|
+
message: `Managed-column synchronization needs { ${missing.join(", ")} } from "${moduleName}", but no plain named value import can be widened. Add the import manually; namespace and type-only imports are not rewritten.`
|
|
786
|
+
});
|
|
787
|
+
return;
|
|
788
|
+
}
|
|
789
|
+
const bindings = target.importClause.namedBindings;
|
|
790
|
+
const source = sourceFile.text;
|
|
791
|
+
const close = bindings.getEnd() - 1;
|
|
792
|
+
const multiline = source.slice(bindings.getStart(sourceFile), close).includes("\n");
|
|
793
|
+
const last = bindings.elements[bindings.elements.length - 1];
|
|
794
|
+
if (!multiline) {
|
|
795
|
+
if (last) {
|
|
796
|
+
const tail = source.slice(last.getEnd(), close);
|
|
797
|
+
const preservesComment = tail.includes("//") || tail.includes("/*");
|
|
798
|
+
edits.push({
|
|
799
|
+
start: last.getEnd(),
|
|
800
|
+
end: close,
|
|
801
|
+
text: preservesComment ? `${tail.includes(",") ? "" : ","}${tail}${/\s$/.test(tail) ? "" : " "}${missing.join(", ")} ` : `, ${missing.join(", ")} `,
|
|
802
|
+
reason: `add required value imports from ${moduleName}`
|
|
803
|
+
});
|
|
804
|
+
} else {
|
|
805
|
+
edits.push({
|
|
806
|
+
start: bindings.getStart(sourceFile) + 1,
|
|
807
|
+
end: close,
|
|
808
|
+
text: ` ${missing.join(", ")} `,
|
|
809
|
+
reason: `add required value imports from ${moduleName}`
|
|
810
|
+
});
|
|
811
|
+
}
|
|
812
|
+
return;
|
|
813
|
+
}
|
|
814
|
+
if (last && !source.slice(last.getEnd(), close).includes(",")) {
|
|
815
|
+
edits.push({
|
|
816
|
+
start: last.getEnd(),
|
|
817
|
+
end: last.getEnd(),
|
|
818
|
+
text: ",",
|
|
819
|
+
reason: `terminate the existing ${moduleName} import before widening it`
|
|
820
|
+
});
|
|
821
|
+
}
|
|
822
|
+
const lineEnding = source.includes("\r\n") ? "\r\n" : "\n";
|
|
823
|
+
const closeLineStart = source.lastIndexOf("\n", close - 1) + 1;
|
|
824
|
+
const first = bindings.elements[0];
|
|
825
|
+
const indent = first ? source.slice(source.lastIndexOf("\n", first.getStart(sourceFile) - 1) + 1, first.getStart(sourceFile)) : " ";
|
|
826
|
+
edits.push({
|
|
827
|
+
start: closeLineStart,
|
|
828
|
+
end: closeLineStart,
|
|
829
|
+
text: missing.map((name) => `${indent}${name},`).join(lineEnding) + lineEnding,
|
|
830
|
+
reason: `add required value imports from ${moduleName}`
|
|
831
|
+
});
|
|
832
|
+
}
|
|
833
|
+
export {
|
|
834
|
+
AUDIT_COLUMN_SPECS,
|
|
835
|
+
AUDIT_JS_NAMES,
|
|
836
|
+
MANAGED_AUDIT_COLUMN_SPECS,
|
|
837
|
+
MANAGED_AUDIT_JS_NAMES,
|
|
838
|
+
MANAGED_COLUMN_SPECS,
|
|
839
|
+
SCHEMA_ENGINE_VERSION,
|
|
840
|
+
SOFT_DELETE_COLUMN_SPECS,
|
|
841
|
+
SOFT_DELETE_JS_NAMES,
|
|
842
|
+
SQLITE_EPOCH_SENTINEL,
|
|
843
|
+
SQLITE_ISO_NOW_FN,
|
|
844
|
+
applySchemaSourceEdits,
|
|
845
|
+
auditRequirement,
|
|
846
|
+
idRequirement,
|
|
847
|
+
managedColumnExpr,
|
|
848
|
+
managedColumnLine,
|
|
849
|
+
managedSqlName,
|
|
850
|
+
planSchemaSource,
|
|
851
|
+
scopeRequirement,
|
|
852
|
+
softDeleteRequirement
|
|
853
|
+
};
|
|
854
|
+
//# sourceMappingURL=index.js.map
|