drizzle-sybase 1.1.0 → 1.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +57 -0
- package/binding.gyp +22 -0
- package/dist/introspect/__tests__/introspect.test.d.ts +2 -0
- package/dist/introspect/__tests__/introspect.test.d.ts.map +1 -0
- package/dist/introspect/__tests__/introspect.test.js +411 -0
- package/dist/introspect/__tests__/introspect.test.js.map +1 -0
- package/dist/introspect/cli.d.ts +3 -0
- package/dist/introspect/cli.d.ts.map +1 -0
- package/dist/introspect/cli.js +13 -0
- package/dist/introspect/cli.js.map +1 -0
- package/dist/introspect/codegen.d.ts +24 -0
- package/dist/introspect/codegen.d.ts.map +1 -0
- package/dist/introspect/codegen.js +342 -0
- package/dist/introspect/codegen.js.map +1 -0
- package/dist/introspect/fetch.d.ts +28 -0
- package/dist/introspect/fetch.d.ts.map +1 -0
- package/dist/introspect/fetch.js +154 -0
- package/dist/introspect/fetch.js.map +1 -0
- package/dist/introspect/index.d.ts +50 -0
- package/dist/introspect/index.d.ts.map +1 -0
- package/dist/introspect/index.js +133 -0
- package/dist/introspect/index.js.map +1 -0
- package/dist/introspect/type-map.d.ts +60 -0
- package/dist/introspect/type-map.d.ts.map +1 -0
- package/dist/introspect/type-map.js +72 -0
- package/dist/introspect/type-map.js.map +1 -0
- package/dist/introspect/types.d.ts +62 -0
- package/dist/introspect/types.d.ts.map +1 -0
- package/dist/introspect/types.js +9 -0
- package/dist/introspect/types.js.map +1 -0
- package/dist/native/sybase_native.darwin-arm64.node +0 -0
- package/dist/native/sybase_native.darwin-x64.node +0 -0
- package/dist/native/sybase_native.win32-x64.node +0 -0
- package/package.json +15 -8
- package/scripts/build-win.ps1 +5 -1
|
@@ -0,0 +1,342 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Generate drizzle-sybase source code (table definitions, index metadata and
|
|
3
|
+
* Zod schemas) from introspected {@link TableMeta}.
|
|
4
|
+
*
|
|
5
|
+
* All type decisions are delegated to the shared {@link SYBASE_TYPE_MAP}
|
|
6
|
+
* registry so the builder, Zod and import generators can never disagree.
|
|
7
|
+
*/
|
|
8
|
+
import { effectiveLength, resolveMapping } from "./type-map.js";
|
|
9
|
+
/**
|
|
10
|
+
* Convert a SNAKE_CASE / snake_case catalog name to camelCase.
|
|
11
|
+
*
|
|
12
|
+
* Only an underscore *between* characters introduces a hump; a leading
|
|
13
|
+
* underscore is preserved (so `_internal` stays `_internal` rather than
|
|
14
|
+
* collapsing to `internal`/`Internal`).
|
|
15
|
+
*/
|
|
16
|
+
export function toCamelCase(name) {
|
|
17
|
+
return name
|
|
18
|
+
.toLowerCase()
|
|
19
|
+
.replace(/([^_])_([a-z0-9])/g, (_, p, c) => p + c.toUpperCase());
|
|
20
|
+
}
|
|
21
|
+
const TS_IDENTIFIER = /^[A-Za-z_$][A-Za-z0-9_$]*$/;
|
|
22
|
+
/**
|
|
23
|
+
* Make a string safe to use as a TypeScript identifier (variable / type name).
|
|
24
|
+
*
|
|
25
|
+
* Sybase identifiers may legally start with a digit (e.g. `[2024_data]`), which
|
|
26
|
+
* is not a valid JS/TS identifier, and may collide with reserved words. A `$`
|
|
27
|
+
* prefix is added when the name would otherwise be invalid or reserved.
|
|
28
|
+
*/
|
|
29
|
+
function safeIdentifier(name) {
|
|
30
|
+
if (!TS_IDENTIFIER.test(name) || RESERVED_WORDS.has(name)) {
|
|
31
|
+
return "$" + name.replace(/[^A-Za-z0-9_$]/g, "_");
|
|
32
|
+
}
|
|
33
|
+
return name;
|
|
34
|
+
}
|
|
35
|
+
/** Render an object-literal property key, quoting it when not a bare identifier. */
|
|
36
|
+
function memberKey(name) {
|
|
37
|
+
return TS_IDENTIFIER.test(name) ? name : JSON.stringify(name);
|
|
38
|
+
}
|
|
39
|
+
/** Capitalise the first character (used to build PascalCase type names). */
|
|
40
|
+
function capitalize(name) {
|
|
41
|
+
return name.length > 0 ? name[0].toUpperCase() + name.slice(1) : name;
|
|
42
|
+
}
|
|
43
|
+
const RESERVED_WORDS = new Set([
|
|
44
|
+
"break",
|
|
45
|
+
"case",
|
|
46
|
+
"catch",
|
|
47
|
+
"class",
|
|
48
|
+
"const",
|
|
49
|
+
"continue",
|
|
50
|
+
"debugger",
|
|
51
|
+
"default",
|
|
52
|
+
"delete",
|
|
53
|
+
"do",
|
|
54
|
+
"else",
|
|
55
|
+
"enum",
|
|
56
|
+
"export",
|
|
57
|
+
"extends",
|
|
58
|
+
"false",
|
|
59
|
+
"finally",
|
|
60
|
+
"for",
|
|
61
|
+
"function",
|
|
62
|
+
"if",
|
|
63
|
+
"import",
|
|
64
|
+
"in",
|
|
65
|
+
"instanceof",
|
|
66
|
+
"new",
|
|
67
|
+
"null",
|
|
68
|
+
"return",
|
|
69
|
+
"super",
|
|
70
|
+
"switch",
|
|
71
|
+
"this",
|
|
72
|
+
"throw",
|
|
73
|
+
"true",
|
|
74
|
+
"try",
|
|
75
|
+
"typeof",
|
|
76
|
+
"var",
|
|
77
|
+
"void",
|
|
78
|
+
"while",
|
|
79
|
+
"with",
|
|
80
|
+
"let",
|
|
81
|
+
"static",
|
|
82
|
+
"yield",
|
|
83
|
+
"await",
|
|
84
|
+
"implements",
|
|
85
|
+
"interface",
|
|
86
|
+
"package",
|
|
87
|
+
"private",
|
|
88
|
+
"protected",
|
|
89
|
+
"public"
|
|
90
|
+
]);
|
|
91
|
+
// ---------------------------------------------------------------------------
|
|
92
|
+
// Column builder rendering
|
|
93
|
+
// ---------------------------------------------------------------------------
|
|
94
|
+
function renderColumnBuilder(col, mapping) {
|
|
95
|
+
const colName = JSON.stringify(col.name);
|
|
96
|
+
let builder;
|
|
97
|
+
switch (mapping.size) {
|
|
98
|
+
case "length":
|
|
99
|
+
builder = `${mapping.factory}(${colName}, { length: ${effectiveLength(col, mapping)} })`;
|
|
100
|
+
break;
|
|
101
|
+
case "precision": {
|
|
102
|
+
// precision/scale should always be present for numeric/decimal, but guard
|
|
103
|
+
// against null catalog values rather than emitting `precision: null`.
|
|
104
|
+
const opts = [];
|
|
105
|
+
if (col.precision != null) {
|
|
106
|
+
opts.push(`precision: ${col.precision}`);
|
|
107
|
+
}
|
|
108
|
+
if (col.scale != null) {
|
|
109
|
+
opts.push(`scale: ${col.scale}`);
|
|
110
|
+
}
|
|
111
|
+
builder =
|
|
112
|
+
opts.length > 0
|
|
113
|
+
? `${mapping.factory}(${colName}, { ${opts.join(", ")} })`
|
|
114
|
+
: `${mapping.factory}(${colName})`;
|
|
115
|
+
break;
|
|
116
|
+
}
|
|
117
|
+
default:
|
|
118
|
+
builder = `${mapping.factory}(${colName})`;
|
|
119
|
+
break;
|
|
120
|
+
}
|
|
121
|
+
if (col.isIdentity) {
|
|
122
|
+
builder += ".identity()";
|
|
123
|
+
}
|
|
124
|
+
if (!col.isNullable) {
|
|
125
|
+
builder += ".notNull()";
|
|
126
|
+
}
|
|
127
|
+
return builder;
|
|
128
|
+
}
|
|
129
|
+
// ---------------------------------------------------------------------------
|
|
130
|
+
// Zod rendering
|
|
131
|
+
// ---------------------------------------------------------------------------
|
|
132
|
+
function renderZodType(col, mapping) {
|
|
133
|
+
let zod;
|
|
134
|
+
switch (mapping.value) {
|
|
135
|
+
case "number":
|
|
136
|
+
zod = "z.number()";
|
|
137
|
+
break;
|
|
138
|
+
case "boolean":
|
|
139
|
+
zod = "z.boolean()";
|
|
140
|
+
break;
|
|
141
|
+
case "date":
|
|
142
|
+
zod = "z.date()";
|
|
143
|
+
break;
|
|
144
|
+
case "buffer":
|
|
145
|
+
zod = "z.instanceof(Buffer)";
|
|
146
|
+
break;
|
|
147
|
+
default: {
|
|
148
|
+
zod = "z.string()";
|
|
149
|
+
const isText = mapping.factory === "text" || mapping.factory === "ntext";
|
|
150
|
+
if (col.length > 0 && !isText) {
|
|
151
|
+
zod += `.max(${effectiveLength(col, mapping)})`;
|
|
152
|
+
}
|
|
153
|
+
break;
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
if (col.isNullable) {
|
|
157
|
+
zod += ".nullable()";
|
|
158
|
+
}
|
|
159
|
+
return zod;
|
|
160
|
+
}
|
|
161
|
+
// ---------------------------------------------------------------------------
|
|
162
|
+
// Per-section generators
|
|
163
|
+
// ---------------------------------------------------------------------------
|
|
164
|
+
function findPrimaryKey(table) {
|
|
165
|
+
return table.indexes.find(i => i.isPrimary);
|
|
166
|
+
}
|
|
167
|
+
/** Valid TS variable name for a table's exported const. */
|
|
168
|
+
function tableVarName(table) {
|
|
169
|
+
return safeIdentifier(toCamelCase(table.name));
|
|
170
|
+
}
|
|
171
|
+
/** Valid TS type-name base (PascalCase) for a table's exported types. */
|
|
172
|
+
function tableTypeName(table) {
|
|
173
|
+
return safeIdentifier(capitalize(toCamelCase(table.name)));
|
|
174
|
+
}
|
|
175
|
+
function generateTableDefinition(table, warnings) {
|
|
176
|
+
const varName = tableVarName(table);
|
|
177
|
+
const pk = findPrimaryKey(table);
|
|
178
|
+
const isSingleColumnPk = pk?.columns.length === 1;
|
|
179
|
+
const columnLines = table.columns.map(col => {
|
|
180
|
+
const { mapping, isFallback } = resolveMapping(col.typeName);
|
|
181
|
+
if (isFallback) {
|
|
182
|
+
warnings.push(`Table "${table.name}" column "${col.name}": unmapped Sybase type "${col.typeName}" — emitted as varchar(${col.length}). Verify the generated type.`);
|
|
183
|
+
}
|
|
184
|
+
let line = ` ${memberKey(toCamelCase(col.name))}: ${renderColumnBuilder(col, mapping)}`;
|
|
185
|
+
if (isSingleColumnPk && pk.columns[0] === col.name) {
|
|
186
|
+
line += ".primaryKey()";
|
|
187
|
+
}
|
|
188
|
+
return line;
|
|
189
|
+
});
|
|
190
|
+
const lines = [];
|
|
191
|
+
// Composite primary keys cannot be expressed on individual columns; surface
|
|
192
|
+
// them as a comment so the information is not silently lost (it also remains
|
|
193
|
+
// available in the exported `<table>Indexes` constant below).
|
|
194
|
+
if (pk && pk.columns.length > 1) {
|
|
195
|
+
lines.push(`// Composite primary key: ${pk.columns.map(c => toCamelCase(c)).join(", ")}`);
|
|
196
|
+
}
|
|
197
|
+
lines.push(`export const ${varName} = sybaseTable(${JSON.stringify(table.name)}, {`);
|
|
198
|
+
lines.push(columnLines.join(",\n"));
|
|
199
|
+
lines.push("});");
|
|
200
|
+
return lines.join("\n") + "\n";
|
|
201
|
+
}
|
|
202
|
+
function generateIndexInfo(table) {
|
|
203
|
+
if (table.indexes.length === 0) {
|
|
204
|
+
return "";
|
|
205
|
+
}
|
|
206
|
+
const varName = tableVarName(table);
|
|
207
|
+
const indexLines = table.indexes.map(idx => {
|
|
208
|
+
const flags = [];
|
|
209
|
+
if (idx.isPrimary) {
|
|
210
|
+
flags.push("primary: true");
|
|
211
|
+
}
|
|
212
|
+
if (idx.isUnique) {
|
|
213
|
+
flags.push("unique: true");
|
|
214
|
+
}
|
|
215
|
+
const cols = idx.columns.map(c => JSON.stringify(c)).join(", ");
|
|
216
|
+
const flagStr = flags.length > 0 ? ", " + flags.join(", ") : "";
|
|
217
|
+
return ` { name: ${JSON.stringify(idx.indexName)}, columns: [${cols}]${flagStr} }`;
|
|
218
|
+
});
|
|
219
|
+
return `export const ${varName}Indexes = [\n${indexLines.join(",\n")}\n] as const;\n`;
|
|
220
|
+
}
|
|
221
|
+
function generateSelectSchema(table) {
|
|
222
|
+
const varName = tableVarName(table);
|
|
223
|
+
const typeName = tableTypeName(table);
|
|
224
|
+
const fieldLines = table.columns.map(col => {
|
|
225
|
+
const { mapping } = resolveMapping(col.typeName);
|
|
226
|
+
return ` ${memberKey(toCamelCase(col.name))}: ${renderZodType(col, mapping)}`;
|
|
227
|
+
});
|
|
228
|
+
return (`export const ${varName}Schema = z.object({\n${fieldLines.join(",\n")}\n});\n` +
|
|
229
|
+
`export type ${typeName}Row = z.infer<typeof ${varName}Schema>;\n`);
|
|
230
|
+
}
|
|
231
|
+
function generateInsertSchema(table) {
|
|
232
|
+
const varName = tableVarName(table);
|
|
233
|
+
const typeName = tableTypeName(table);
|
|
234
|
+
const fieldLines = table.columns
|
|
235
|
+
.filter(col => !col.isIdentity)
|
|
236
|
+
.map(col => {
|
|
237
|
+
const { mapping } = resolveMapping(col.typeName);
|
|
238
|
+
let zod = renderZodType(col, mapping);
|
|
239
|
+
if (col.isNullable || col.defaultValue) {
|
|
240
|
+
zod += ".optional()";
|
|
241
|
+
}
|
|
242
|
+
return ` ${memberKey(toCamelCase(col.name))}: ${zod}`;
|
|
243
|
+
});
|
|
244
|
+
return (`export const ${varName}InsertSchema = z.object({\n${fieldLines.join(",\n")}\n});\n` +
|
|
245
|
+
`export type New${typeName} = z.infer<typeof ${varName}InsertSchema>;\n`);
|
|
246
|
+
}
|
|
247
|
+
// ---------------------------------------------------------------------------
|
|
248
|
+
// Import collection (driven by the same registry as everything else)
|
|
249
|
+
// ---------------------------------------------------------------------------
|
|
250
|
+
/** drizzle-sybase column factories, in a stable declaration order for imports. */
|
|
251
|
+
const FACTORY_ORDER = [
|
|
252
|
+
"int",
|
|
253
|
+
"bigint",
|
|
254
|
+
"smallint",
|
|
255
|
+
"tinyint",
|
|
256
|
+
"varchar",
|
|
257
|
+
"nvarchar",
|
|
258
|
+
"char",
|
|
259
|
+
"nchar",
|
|
260
|
+
"text",
|
|
261
|
+
"ntext",
|
|
262
|
+
"datetime",
|
|
263
|
+
"smalldatetime",
|
|
264
|
+
"numeric",
|
|
265
|
+
"decimal",
|
|
266
|
+
"float",
|
|
267
|
+
"real",
|
|
268
|
+
"money",
|
|
269
|
+
"smallmoney",
|
|
270
|
+
"bit",
|
|
271
|
+
"binary",
|
|
272
|
+
"varbinary",
|
|
273
|
+
"image"
|
|
274
|
+
];
|
|
275
|
+
function collectImports(tables) {
|
|
276
|
+
const used = new Set();
|
|
277
|
+
for (const table of tables) {
|
|
278
|
+
for (const col of table.columns) {
|
|
279
|
+
const { mapping } = resolveMapping(col.typeName);
|
|
280
|
+
used.add(mapping.factory);
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
return FACTORY_ORDER.filter(f => used.has(f));
|
|
284
|
+
}
|
|
285
|
+
// ---------------------------------------------------------------------------
|
|
286
|
+
// Top-level generator
|
|
287
|
+
// ---------------------------------------------------------------------------
|
|
288
|
+
const DIVIDER = "// ═══════════════════════════════════════════════════════════════════════════════";
|
|
289
|
+
export function generateSchemaCode(tables, database) {
|
|
290
|
+
const warnings = [];
|
|
291
|
+
const imports = collectImports(tables);
|
|
292
|
+
const lines = [];
|
|
293
|
+
lines.push("/**");
|
|
294
|
+
lines.push(" * Auto-generated Sybase schema definitions.");
|
|
295
|
+
lines.push(` * Database: ${database}`);
|
|
296
|
+
lines.push(` * Generated: ${new Date().toISOString()}`);
|
|
297
|
+
lines.push(" *");
|
|
298
|
+
lines.push(" * DO NOT EDIT — re-run introspectSybase to regenerate.");
|
|
299
|
+
lines.push(" */");
|
|
300
|
+
lines.push("");
|
|
301
|
+
const importList = ["sybaseTable", ...imports].join(", ");
|
|
302
|
+
lines.push(`import { ${importList} } from "drizzle-sybase";`);
|
|
303
|
+
lines.push(`import { z } from "zod";`);
|
|
304
|
+
lines.push("");
|
|
305
|
+
lines.push(DIVIDER);
|
|
306
|
+
lines.push("// Table Definitions");
|
|
307
|
+
lines.push(DIVIDER);
|
|
308
|
+
lines.push("");
|
|
309
|
+
for (const table of tables) {
|
|
310
|
+
lines.push(generateTableDefinition(table, warnings));
|
|
311
|
+
lines.push("");
|
|
312
|
+
}
|
|
313
|
+
lines.push(DIVIDER);
|
|
314
|
+
lines.push("// Indexes");
|
|
315
|
+
lines.push(DIVIDER);
|
|
316
|
+
lines.push("");
|
|
317
|
+
for (const table of tables) {
|
|
318
|
+
const indexCode = generateIndexInfo(table);
|
|
319
|
+
if (indexCode) {
|
|
320
|
+
lines.push(indexCode);
|
|
321
|
+
lines.push("");
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
lines.push(DIVIDER);
|
|
325
|
+
lines.push("// Zod Schemas (Select)");
|
|
326
|
+
lines.push(DIVIDER);
|
|
327
|
+
lines.push("");
|
|
328
|
+
for (const table of tables) {
|
|
329
|
+
lines.push(generateSelectSchema(table));
|
|
330
|
+
lines.push("");
|
|
331
|
+
}
|
|
332
|
+
lines.push(DIVIDER);
|
|
333
|
+
lines.push("// Zod Schemas (Insert)");
|
|
334
|
+
lines.push(DIVIDER);
|
|
335
|
+
lines.push("");
|
|
336
|
+
for (const table of tables) {
|
|
337
|
+
lines.push(generateInsertSchema(table));
|
|
338
|
+
lines.push("");
|
|
339
|
+
}
|
|
340
|
+
return { code: lines.join("\n"), warnings };
|
|
341
|
+
}
|
|
342
|
+
//# sourceMappingURL=codegen.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"codegen.js","sourceRoot":"","sources":["../../src/introspect/codegen.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,EAAE,eAAe,EAAE,cAAc,EAAoB,MAAM,eAAe,CAAC;AAGlF;;;;;;GAMG;AACH,MAAM,UAAU,WAAW,CAAC,IAAY;IACtC,OAAO,IAAI;SACR,WAAW,EAAE;SACb,OAAO,CAAC,oBAAoB,EAAE,CAAC,CAAC,EAAE,CAAS,EAAE,CAAS,EAAE,EAAE,CAAC,CAAC,GAAG,CAAC,CAAC,WAAW,EAAE,CAAC,CAAC;AACrF,CAAC;AAED,MAAM,aAAa,GAAG,4BAA4B,CAAC;AAEnD;;;;;;GAMG;AACH,SAAS,cAAc,CAAC,IAAY;IAClC,IAAI,CAAC,aAAa,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,cAAc,CAAC,GAAG,CAAC,IAAI,CAAC,EAAE,CAAC;QAC1D,OAAO,GAAG,GAAG,IAAI,CAAC,OAAO,CAAC,iBAAiB,EAAE,GAAG,CAAC,CAAC;IACpD,CAAC;IACD,OAAO,IAAI,CAAC;AACd,CAAC;AAED,oFAAoF;AACpF,SAAS,SAAS,CAAC,IAAY;IAC7B,OAAO,aAAa,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,IAAI,CAAC,SAA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
3
|
+
*
|
|
4
|
+
* The SQL here intentionally mirrors the battle-tested queries that shipped via
|
|
5
|
+
* the original patch (sysobjects / syscolumns / systypes / syscomments /
|
|
6
|
+
* sysindexes, the UDT resolution sub-select, and the status bit masks). The
|
|
7
|
+
* changes are limited to:
|
|
8
|
+
* - type safety (typed row shapes instead of `any`),
|
|
9
|
+
* - a single identifier guard ({@link assertSafeIdentifier}),
|
|
10
|
+
* - index key columns resolved in one query per index instead of one query
|
|
11
|
+
* per column (the previous N x M round-trips).
|
|
12
|
+
*/
|
|
13
|
+
import type { ColumnMeta, IndexMeta, IntrospectDb } from "./types.js";
|
|
14
|
+
/**
|
|
15
|
+
* Guard every identifier that is interpolated into catalog SQL. Catalog queries
|
|
16
|
+
* cannot be parameterised (database name, object names and `index_col()`
|
|
17
|
+
* arguments are not bindable), so this allow-list is the single line of defence
|
|
18
|
+
* against injection — applied once, here, at the lowest layer.
|
|
19
|
+
*/
|
|
20
|
+
export declare function assertSafeIdentifier(value: string | undefined, label: string): asserts value is string;
|
|
21
|
+
export interface FetchedTable {
|
|
22
|
+
name: string;
|
|
23
|
+
owner: string;
|
|
24
|
+
}
|
|
25
|
+
export declare function fetchTables(db: IntrospectDb, database: string, tableFilter?: string[], owner?: string): Promise<FetchedTable[]>;
|
|
26
|
+
export declare function fetchColumns(db: IntrospectDb, database: string, tableName: string): Promise<ColumnMeta[]>;
|
|
27
|
+
export declare function fetchIndexes(db: IntrospectDb, database: string, tableName: string): Promise<IndexMeta[]>;
|
|
28
|
+
//# sourceMappingURL=fetch.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"fetch.d.ts","sourceRoot":"","sources":["../../src/introspect/fetch.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAGH,OAAO,KAAK,EAAE,UAAU,EAAE,SAAS,EAAE,YAAY,EAAE,MAAM,YAAY,CAAC;AAItE;;;;;GAKG;AACH,wBAAgB,oBAAoB,CAClC,KAAK,EAAE,MAAM,GAAG,SAAS,EACzB,KAAK,EAAE,MAAM,GACZ,OAAO,CAAC,KAAK,IAAI,MAAM,CAIzB;AAoBD,MAAM,WAAW,YAAY;IAC3B,IAAI,EAAE,MAAM,CAAC;IACb,KAAK,EAAE,MAAM,CAAC;CACf;AAED,wBAAsB,WAAW,CAC/B,EAAE,EAAE,YAAY,EAChB,QAAQ,EAAE,MAAM,EAChB,WAAW,CAAC,EAAE,MAAM,EAAE,EACtB,KAAK,CAAC,EAAE,MAAM,GACb,OAAO,CAAC,YAAY,EAAE,CAAC,CAyBzB;AAqBD,wBAAsB,YAAY,CAChC,EAAE,EAAE,YAAY,EAChB,QAAQ,EAAE,MAAM,EAChB,SAAS,EAAE,MAAM,GAChB,OAAO,CAAC,UAAU,EAAE,CAAC,CA8DvB;AAaD,wBAAsB,YAAY,CAChC,EAAE,EAAE,YAAY,EAChB,QAAQ,EAAE,MAAM,EAChB,SAAS,EAAE,MAAM,GAChB,OAAO,CAAC,SAAS,EAAE,CAAC,CA4BtB"}
|
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Fetch table / column / index metadata from the Sybase ASE system catalogs.
|
|
3
|
+
*
|
|
4
|
+
* The SQL here intentionally mirrors the battle-tested queries that shipped via
|
|
5
|
+
* the original patch (sysobjects / syscolumns / systypes / syscomments /
|
|
6
|
+
* sysindexes, the UDT resolution sub-select, and the status bit masks). The
|
|
7
|
+
* changes are limited to:
|
|
8
|
+
* - type safety (typed row shapes instead of `any`),
|
|
9
|
+
* - a single identifier guard ({@link assertSafeIdentifier}),
|
|
10
|
+
* - index key columns resolved in one query per index instead of one query
|
|
11
|
+
* per column (the previous N x M round-trips).
|
|
12
|
+
*/
|
|
13
|
+
import { escapeString } from "../dialect.js";
|
|
14
|
+
const VALID_SQL_IDENTIFIER = /^[A-Za-z0-9_]+$/;
|
|
15
|
+
/**
|
|
16
|
+
* Guard every identifier that is interpolated into catalog SQL. Catalog queries
|
|
17
|
+
* cannot be parameterised (database name, object names and `index_col()`
|
|
18
|
+
* arguments are not bindable), so this allow-list is the single line of defence
|
|
19
|
+
* against injection — applied once, here, at the lowest layer.
|
|
20
|
+
*/
|
|
21
|
+
export function assertSafeIdentifier(value, label) {
|
|
22
|
+
if (!value || !VALID_SQL_IDENTIFIER.test(value) || value.length > 128) {
|
|
23
|
+
throw new Error(`Unsafe ${label}: "${value}". Only [A-Za-z0-9_] allowed, max 128 chars.`);
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
// ---------------------------------------------------------------------------
|
|
27
|
+
// Status bit masks (syscolumns.status / sysindexes.status)
|
|
28
|
+
// ---------------------------------------------------------------------------
|
|
29
|
+
const COLUMN_STATUS_NULLABLE = 8;
|
|
30
|
+
const COLUMN_STATUS_IDENTITY = 128;
|
|
31
|
+
const INDEX_STATUS_UNIQUE = 2;
|
|
32
|
+
const INDEX_STATUS_PRIMARY = 2048;
|
|
33
|
+
export async function fetchTables(db, database, tableFilter, owner) {
|
|
34
|
+
assertSafeIdentifier(database, "database");
|
|
35
|
+
if (owner !== undefined) {
|
|
36
|
+
assertSafeIdentifier(owner, "owner");
|
|
37
|
+
}
|
|
38
|
+
tableFilter?.forEach(t => assertSafeIdentifier(t, "table"));
|
|
39
|
+
const ownerFilter = owner ? `AND u.name = ${escapeString(owner)}` : "";
|
|
40
|
+
const tableFilterSql = tableFilter && tableFilter.length > 0
|
|
41
|
+
? `AND o.name IN (${tableFilter.map(t => escapeString(t)).join(", ")})`
|
|
42
|
+
: "";
|
|
43
|
+
const result = await db.executeRaw(`SELECT o.name, u.name AS owner
|
|
44
|
+
FROM ${database}..sysobjects o
|
|
45
|
+
LEFT JOIN ${database}..sysusers u ON o.uid = u.uid
|
|
46
|
+
WHERE o.type = 'U'
|
|
47
|
+
AND o.name NOT LIKE 'sys%'
|
|
48
|
+
${ownerFilter}
|
|
49
|
+
${tableFilterSql}
|
|
50
|
+
ORDER BY o.name`);
|
|
51
|
+
return result.rows.map(row => ({ name: row.name, owner: row.owner ?? "" }));
|
|
52
|
+
}
|
|
53
|
+
export async function fetchColumns(db, database, tableName) {
|
|
54
|
+
assertSafeIdentifier(database, "database");
|
|
55
|
+
assertSafeIdentifier(tableName, "tableName");
|
|
56
|
+
// Resolve UDTs to base system types via the systypes hierarchy.
|
|
57
|
+
// UDTs have usertype >= 100; resolve to the base type (usertype < 100) by
|
|
58
|
+
// matching on the internal type number and picking MIN(usertype) as the
|
|
59
|
+
// canonical system type.
|
|
60
|
+
const result = await db.executeRaw(`SELECT
|
|
61
|
+
c.name,
|
|
62
|
+
c.colid,
|
|
63
|
+
CASE
|
|
64
|
+
WHEN t.usertype < 100 THEN t.name
|
|
65
|
+
ELSE (SELECT s.name FROM ${database}..systypes s
|
|
66
|
+
WHERE s.type = t.type AND s.usertype < 100
|
|
67
|
+
AND s.usertype = (SELECT MIN(s2.usertype) FROM ${database}..systypes s2
|
|
68
|
+
WHERE s2.type = t.type AND s2.usertype < 100))
|
|
69
|
+
END AS type_name,
|
|
70
|
+
c.length,
|
|
71
|
+
c.prec,
|
|
72
|
+
c.scale,
|
|
73
|
+
c.status
|
|
74
|
+
FROM ${database}..syscolumns c
|
|
75
|
+
JOIN ${database}..systypes t ON c.usertype = t.usertype
|
|
76
|
+
WHERE c.id = OBJECT_ID('${database}..${tableName}')
|
|
77
|
+
ORDER BY c.colid`);
|
|
78
|
+
const defaultsResult = await db.executeRaw(`SELECT
|
|
79
|
+
c.name AS col_name,
|
|
80
|
+
com.text AS default_text
|
|
81
|
+
FROM ${database}..syscolumns c
|
|
82
|
+
JOIN ${database}..syscomments com ON com.id = c.cdefault
|
|
83
|
+
WHERE c.id = OBJECT_ID('${database}..${tableName}')
|
|
84
|
+
AND c.cdefault != 0`);
|
|
85
|
+
const defaultsMap = new Map(defaultsResult.rows.map(r => [r.col_name, r.default_text?.trim() ?? null]));
|
|
86
|
+
return result.rows.map(row => {
|
|
87
|
+
if (!row.type_name) {
|
|
88
|
+
throw new Error(`Could not resolve a base system type for column "${row.name}" of table "${tableName}". ` +
|
|
89
|
+
`This usually means an unresolvable user-defined type.`);
|
|
90
|
+
}
|
|
91
|
+
return {
|
|
92
|
+
name: row.name,
|
|
93
|
+
colid: row.colid,
|
|
94
|
+
typeName: row.type_name.toLowerCase(),
|
|
95
|
+
length: row.length,
|
|
96
|
+
precision: row.prec,
|
|
97
|
+
scale: row.scale,
|
|
98
|
+
isNullable: (row.status & COLUMN_STATUS_NULLABLE) === COLUMN_STATUS_NULLABLE,
|
|
99
|
+
isIdentity: (row.status & COLUMN_STATUS_IDENTITY) === COLUMN_STATUS_IDENTITY,
|
|
100
|
+
defaultValue: defaultsMap.get(row.name) ?? null
|
|
101
|
+
};
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
export async function fetchIndexes(db, database, tableName) {
|
|
105
|
+
assertSafeIdentifier(database, "database");
|
|
106
|
+
assertSafeIdentifier(tableName, "tableName");
|
|
107
|
+
const result = await db.executeRaw(`SELECT i.name AS index_name, i.indid, i.status, i.keycnt
|
|
108
|
+
FROM ${database}..sysindexes i
|
|
109
|
+
WHERE i.id = OBJECT_ID('${database}..${tableName}')
|
|
110
|
+
AND i.indid > 0
|
|
111
|
+
AND i.indid < 255
|
|
112
|
+
AND i.name NOT LIKE 't%'`);
|
|
113
|
+
const indexes = [];
|
|
114
|
+
for (const row of result.rows) {
|
|
115
|
+
const columns = await fetchIndexColumns(db, database, tableName, row.indid, row.keycnt);
|
|
116
|
+
if (columns.length > 0) {
|
|
117
|
+
indexes.push({
|
|
118
|
+
indexName: row.index_name,
|
|
119
|
+
isPrimary: (row.status & INDEX_STATUS_PRIMARY) !== 0,
|
|
120
|
+
isUnique: (row.status & INDEX_STATUS_UNIQUE) !== 0,
|
|
121
|
+
columns
|
|
122
|
+
});
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
return indexes;
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* Resolve every key column of a single index in one round-trip.
|
|
129
|
+
*
|
|
130
|
+
* The original implementation issued one `index_col()` query per column (and
|
|
131
|
+
* re-derived `indid` with a sub-select each time). Here `indid` is already known
|
|
132
|
+
* from `sysindexes`, and all key positions are unioned into one query, so each
|
|
133
|
+
* index costs exactly one round-trip regardless of key count.
|
|
134
|
+
*/
|
|
135
|
+
async function fetchIndexColumns(db, database, tableName, indid, keycnt) {
|
|
136
|
+
if (keycnt <= 0) {
|
|
137
|
+
return [];
|
|
138
|
+
}
|
|
139
|
+
const objectRef = escapeString(`${database}..${tableName}`);
|
|
140
|
+
const selects = [];
|
|
141
|
+
for (let keyno = 1; keyno <= keycnt; keyno++) {
|
|
142
|
+
selects.push(`SELECT ${keyno} AS keyno, index_col(${objectRef}, ${indid}, ${keyno}) AS col_name`);
|
|
143
|
+
}
|
|
144
|
+
const result = await db.executeRaw(`${selects.join(" UNION ALL ")} ORDER BY keyno`);
|
|
145
|
+
const columns = [];
|
|
146
|
+
for (const r of result.rows) {
|
|
147
|
+
if (!r.col_name) {
|
|
148
|
+
break;
|
|
149
|
+
}
|
|
150
|
+
columns.push(r.col_name);
|
|
151
|
+
}
|
|
152
|
+
return columns;
|
|
153
|
+
}
|
|
154
|
+
//# sourceMappingURL=fetch.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"fetch.js","sourceRoot":"","sources":["../../src/introspect/fetch.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAEH,OAAO,EAAE,YAAY,EAAE,MAAM,eAAe,CAAC;AAG7C,MAAM,oBAAoB,GAAG,iBAAiB,CAAC;AAE/C;;;;;GAKG;AACH,MAAM,UAAU,oBAAoB,CAClC,KAAyB,EACzB,KAAa;IAEb,IAAI,CAAC,KAAK,IAAI,CAAC,oBAAoB,CAAC,IAAI,CAAC,KAAK,CAAC,IAAI,KAAK,CAAC,MAAM,GAAG,GAAG,EAAE,CAAC;QACtE,MAAM,IAAI,KAAK,CAAC,UAAU,KAAK,MAAM,KAAK,8CAA8C,CAAC,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iBAAiB,CAC9B,EAAgB,EAChB,QAAgB,EAChB,SAAiB,EACjB,KAAa,EACb,MAAc;IAEd,IAAI,MAAM,IAAI,CAAC,EAAE,CAAC;QAChB,OAAO,EAAE,CAAC;IACZ,CAAC;IAED,MAAM,SAAS,GAAG,YAAY,CAAC,GAAG,QAAQ,KAAK,SAAS,EAAE,CAAC,CAAC;IAC5D,MAAM,OAAO,GAAa,EAAE,CAAC;IAC7B,KAAK,IAAI,KAAK,GAAG,CAAC,EAAE,KAAK,IAAI,MAAM,EAAE,KAAK,EAAE,EAAE,CAAC;QAC7C,OAAO,CAAC,IAAI,CACV,UAAU,KAAK,wBAAwB,SAAS,KAAK,KAAK,KAAK,KAAK,eAAe,CACpF,CAAC;IACJ,CAAC;IAED,MAAM,MAAM,GAAG,MAAM,EAAE,CAAC,UAAU,CAAc,GAAG,OAAO,CAAC,IAAI,CAAC,aAAa,CAAC,iBAAiB,CAAC,CAAC;IAEjG,MAAM,OAAO,GAAa,EAAE,CAAC;IAC7B,KAAK,MAAM,CAAC,IAAI,MAAM,CAAC,IAAI,EAAE,CAAC;QAC5B,IAAI,CAAC,CAAC,CAAC,QAAQ,EAAE,CAAC;YAChB,MAAM;QACR,CAAC;QACD,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,QAAQ,CAAC,CAAC;IAC3B,CAAC;IACD,OAAO,OAAO,CAAC;AACjB,CAAC"}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Sybase ASE schema introspection — public entry point.
|
|
3
|
+
*
|
|
4
|
+
* Connects to a Sybase database, reads the system catalogs, and returns
|
|
5
|
+
* generated drizzle-sybase source code (table definitions, index metadata and
|
|
6
|
+
* Zod select/insert schemas).
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* ```ts
|
|
10
|
+
* import { introspectSybase } from "drizzle-sybase/introspect";
|
|
11
|
+
*
|
|
12
|
+
* const { code, warnings } = await introspectSybase({
|
|
13
|
+
* host: "sybase-host",
|
|
14
|
+
* port: 5000,
|
|
15
|
+
* database: "mydb",
|
|
16
|
+
* username: "sa",
|
|
17
|
+
* password: "secret",
|
|
18
|
+
* tables: ["users", "orders"]
|
|
19
|
+
* });
|
|
20
|
+
* ```
|
|
21
|
+
*/
|
|
22
|
+
import { type GeneratedCode } from "./codegen.js";
|
|
23
|
+
import type { IntrospectConfig, IntrospectDb } from "./types.js";
|
|
24
|
+
export type { IntrospectConfig, IntrospectDb, ColumnMeta, IndexMeta, TableMeta } from "./types.js";
|
|
25
|
+
export type { GeneratedCode } from "./codegen.js";
|
|
26
|
+
export { SYBASE_TYPE_MAP } from "./type-map.js";
|
|
27
|
+
/**
|
|
28
|
+
* Introspect a Sybase database using an already-connected db handle.
|
|
29
|
+
*
|
|
30
|
+
* Useful when the caller owns connection lifecycle / credentials (e.g. fetched
|
|
31
|
+
* from a secrets manager) and wants to reuse an existing pool. The handle is
|
|
32
|
+
* not closed by this function.
|
|
33
|
+
*/
|
|
34
|
+
export declare function introspectWith(db: IntrospectDb, options: {
|
|
35
|
+
database: string;
|
|
36
|
+
tables?: string[];
|
|
37
|
+
owner?: string;
|
|
38
|
+
}): Promise<GeneratedCode>;
|
|
39
|
+
/**
|
|
40
|
+
* Introspect a Sybase database and return generated code as a string.
|
|
41
|
+
*
|
|
42
|
+
* Opens a short-lived pool, introspects, and always closes the pool.
|
|
43
|
+
*/
|
|
44
|
+
export declare function introspectSybase(config: IntrospectConfig): Promise<GeneratedCode>;
|
|
45
|
+
/**
|
|
46
|
+
* CLI entry point. Parses `process.argv` and runs introspection, printing to
|
|
47
|
+
* stdout or writing to `--out`.
|
|
48
|
+
*/
|
|
49
|
+
export declare function runCli(argv?: string[]): Promise<void>;
|
|
50
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/introspect/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;AAKH,OAAO,EAAsB,KAAK,aAAa,EAAE,MAAM,cAAc,CAAC;AAEtE,OAAO,KAAK,EAAE,gBAAgB,EAAE,YAAY,EAAa,MAAM,YAAY,CAAC;AAE5E,YAAY,EAAE,gBAAgB,EAAE,YAAY,EAAE,UAAU,EAAE,SAAS,EAAE,SAAS,EAAE,MAAM,YAAY,CAAC;AACnG,YAAY,EAAE,aAAa,EAAE,MAAM,cAAc,CAAC;AAClD,OAAO,EAAE,eAAe,EAAE,MAAM,eAAe,CAAC;AAEhD;;;;;;GAMG;AACH,wBAAsB,cAAc,CAClC,EAAE,EAAE,YAAY,EAChB,OAAO,EAAE;IAAE,QAAQ,EAAE,MAAM,CAAC;IAAC,MAAM,CAAC,EAAE,MAAM,EAAE,CAAC;IAAC,KAAK,CAAC,EAAE,MAAM,CAAA;CAAE,GAC/D,OAAO,CAAC,aAAa,CAAC,CAaxB;AAED;;;;GAIG;AACH,wBAAsB,gBAAgB,CAAC,MAAM,EAAE,gBAAgB,GAAG,OAAO,CAAC,aAAa,CAAC,CAoBvF;AAaD;;;GAGG;AACH,wBAAsB,MAAM,CAAC,IAAI,GAAE,MAAM,EAA0B,GAAG,OAAO,CAAC,IAAI,CAAC,CAoDlF"}
|