metal-orm 1.0.20 → 1.0.22
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 +460 -460
- package/package.json +6 -2
- package/scripts/generate-entities.mjs +664 -0
- package/scripts/show-sql.mjs +325 -0
|
@@ -0,0 +1,325 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import path from 'path';
|
|
3
|
+
import process from 'process';
|
|
4
|
+
import fs from 'fs';
|
|
5
|
+
import os from 'os';
|
|
6
|
+
import { spawnSync } from 'child_process';
|
|
7
|
+
import { pathToFileURL } from 'url';
|
|
8
|
+
import { createRequire } from 'module';
|
|
9
|
+
|
|
10
|
+
import {
|
|
11
|
+
SqliteDialect,
|
|
12
|
+
PostgresDialect,
|
|
13
|
+
MySqlDialect,
|
|
14
|
+
SqlServerDialect,
|
|
15
|
+
Orm,
|
|
16
|
+
OrmSession
|
|
17
|
+
} from '../dist/index.js';
|
|
18
|
+
|
|
19
|
+
const require = createRequire(import.meta.url);
|
|
20
|
+
const useStdin = process.argv.includes('--stdin');
|
|
21
|
+
|
|
22
|
+
const usage = `Usage: npm run show-sql -- <builder-module> [--dialect=sqlite|postgres|mysql|mssql] [--db=path/to/db.sqlite] [--hydrate] [--e2e]
|
|
23
|
+
npm run show-sql -- --stdin [--dialect=sqlite|postgres|mysql|mssql] [--db=path/to/db.sqlite] [--hydrate] [--e2e] < code.mjs
|
|
24
|
+
cat code.mjs | npm run show-sql -- --stdin [--dialect=sqlite|postgres|mysql|mssql] [--db=path/to/db.sqlite] [--hydrate] [--e2e]
|
|
25
|
+
|
|
26
|
+
The module should export either:
|
|
27
|
+
- default: SelectQueryBuilder instance
|
|
28
|
+
- builder: SelectQueryBuilder instance
|
|
29
|
+
- build(): SelectQueryBuilder (sync or async)
|
|
30
|
+
|
|
31
|
+
Example builder module:
|
|
32
|
+
import { SelectQueryBuilder, defineTable, col } from '../dist/index.js';
|
|
33
|
+
const users = defineTable('users', { id: col.primaryKey(col.int()), name: col.varchar(255) });
|
|
34
|
+
export const build = () => new SelectQueryBuilder(users).select({ id: users.columns.id }).limit(5);
|
|
35
|
+
|
|
36
|
+
Example stdin usage:
|
|
37
|
+
npm run show-sql -- --stdin <<'EOF'
|
|
38
|
+
import { SelectQueryBuilder, defineTable, col } from './dist/index.js';
|
|
39
|
+
const users = defineTable('users', { id: col.primaryKey(col.int()), name: col.varchar(255) });
|
|
40
|
+
export default new SelectQueryBuilder(users).select({ id: users.columns.id }).limit(5);
|
|
41
|
+
EOF
|
|
42
|
+
|
|
43
|
+
Options:
|
|
44
|
+
--hydrate Executes the builder and prints hydrated results (requires sqlite3 if dialect=sqlite).
|
|
45
|
+
--e2e Shortcut to execute the builder (like --hydrate) even when you mainly want SQL output; auto-installs sqlite3 to a temp dir if missing.
|
|
46
|
+
--db Use a specific SQLite database file instead of the in-memory seeded database.
|
|
47
|
+
`;
|
|
48
|
+
|
|
49
|
+
const args = process.argv.slice(2);
|
|
50
|
+
const modulePath = args.find(a => !a.startsWith('--'));
|
|
51
|
+
const dialectArg = args.find(a => a.startsWith('--dialect='))?.split('=')[1] ?? 'sqlite';
|
|
52
|
+
const dbPathArg = args.find(a => a.startsWith('--db='))?.split('=')[1];
|
|
53
|
+
const hydrate = args.includes('--hydrate');
|
|
54
|
+
const e2e = args.includes('--e2e');
|
|
55
|
+
const shouldExecute = hydrate || e2e || !!dbPathArg;
|
|
56
|
+
const isSqliteDialect = dialectArg.toLowerCase() === 'sqlite';
|
|
57
|
+
|
|
58
|
+
if (!modulePath && !useStdin) {
|
|
59
|
+
console.error(usage);
|
|
60
|
+
process.exit(1);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
const dialect = (() => {
|
|
64
|
+
switch (dialectArg.toLowerCase()) {
|
|
65
|
+
case 'sqlite':
|
|
66
|
+
return new SqliteDialect();
|
|
67
|
+
case 'postgres':
|
|
68
|
+
case 'pg':
|
|
69
|
+
return new PostgresDialect();
|
|
70
|
+
case 'mysql':
|
|
71
|
+
return new MySqlDialect();
|
|
72
|
+
case 'mssql':
|
|
73
|
+
case 'sqlserver':
|
|
74
|
+
return new SqlServerDialect();
|
|
75
|
+
default:
|
|
76
|
+
console.warn(`Unknown dialect "${dialectArg}", defaulting to sqlite.`);
|
|
77
|
+
return new SqliteDialect();
|
|
78
|
+
}
|
|
79
|
+
})();
|
|
80
|
+
|
|
81
|
+
const readStdin = () => {
|
|
82
|
+
return new Promise((resolve, reject) => {
|
|
83
|
+
let data = '';
|
|
84
|
+
process.stdin.setEncoding('utf8');
|
|
85
|
+
process.stdin.on('data', chunk => data += chunk);
|
|
86
|
+
process.stdin.on('end', () => resolve(data));
|
|
87
|
+
process.stdin.on('error', reject);
|
|
88
|
+
});
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
const transformImports = (code) => {
|
|
92
|
+
// Transform relative imports to absolute file URLs
|
|
93
|
+
const cwd = process.cwd();
|
|
94
|
+
return code.replace(
|
|
95
|
+
/from\s+['"](\.[^'"]+)['"]/g,
|
|
96
|
+
(match, relativePath) => {
|
|
97
|
+
const absolutePath = path.resolve(cwd, relativePath);
|
|
98
|
+
const fileUrl = pathToFileURL(absolutePath).href;
|
|
99
|
+
return `from '${fileUrl}'`;
|
|
100
|
+
}
|
|
101
|
+
);
|
|
102
|
+
};
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
const loadBuilder = async () => {
|
|
106
|
+
if (useStdin) {
|
|
107
|
+
const code = await readStdin();
|
|
108
|
+
if (!code.trim()) {
|
|
109
|
+
console.error('No code provided via stdin.');
|
|
110
|
+
process.exit(1);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
// Transform relative imports to absolute file URLs
|
|
114
|
+
const transformedCode = transformImports(code);
|
|
115
|
+
|
|
116
|
+
// Create a data URL module from the code
|
|
117
|
+
const dataUrl = `data:text/javascript;base64,${Buffer.from(transformedCode).toString('base64')}`;
|
|
118
|
+
const mod = await import(dataUrl);
|
|
119
|
+
|
|
120
|
+
const candidate = mod.default ?? mod.builder ?? mod.build;
|
|
121
|
+
if (typeof candidate === 'function') {
|
|
122
|
+
return await candidate();
|
|
123
|
+
}
|
|
124
|
+
return candidate;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
const resolved = path.resolve(modulePath);
|
|
128
|
+
const mod = await import(pathToFileURL(resolved).href);
|
|
129
|
+
|
|
130
|
+
const candidate = mod.default ?? mod.builder ?? mod.build;
|
|
131
|
+
if (typeof candidate === 'function') {
|
|
132
|
+
return await candidate();
|
|
133
|
+
}
|
|
134
|
+
return candidate;
|
|
135
|
+
};
|
|
136
|
+
|
|
137
|
+
const builder = await loadBuilder();
|
|
138
|
+
if (!builder || typeof builder.execute !== 'function') {
|
|
139
|
+
console.error('Builder module must export a SelectQueryBuilder instance or a function that returns one.');
|
|
140
|
+
process.exit(1);
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
// --- SQLite Support & Execution Logic ---
|
|
144
|
+
|
|
145
|
+
let sqlite3 = null;
|
|
146
|
+
const sqliteTempDir = path.join(os.tmpdir(), 'metal-orm-sqlite');
|
|
147
|
+
|
|
148
|
+
const tryRequireSqlite3 = () => {
|
|
149
|
+
const candidatePaths = [undefined, process.cwd(), sqliteTempDir];
|
|
150
|
+
for (const candidate of candidatePaths) {
|
|
151
|
+
try {
|
|
152
|
+
const resolved = candidate
|
|
153
|
+
? require.resolve('sqlite3', { paths: [candidate] })
|
|
154
|
+
: require.resolve('sqlite3');
|
|
155
|
+
return require(resolved);
|
|
156
|
+
} catch {
|
|
157
|
+
/* try next path */
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
return null;
|
|
161
|
+
};
|
|
162
|
+
|
|
163
|
+
const installSqlite3Temp = () => {
|
|
164
|
+
fs.mkdirSync(sqliteTempDir, { recursive: true });
|
|
165
|
+
const resolvedNpmCli = (() => {
|
|
166
|
+
try {
|
|
167
|
+
return require.resolve('npm/bin/npm-cli.js');
|
|
168
|
+
} catch {
|
|
169
|
+
const bundled = path.join(
|
|
170
|
+
path.dirname(process.execPath),
|
|
171
|
+
'node_modules',
|
|
172
|
+
'npm',
|
|
173
|
+
'bin',
|
|
174
|
+
'npm-cli.js'
|
|
175
|
+
);
|
|
176
|
+
if (fs.existsSync(bundled)) {
|
|
177
|
+
return bundled;
|
|
178
|
+
}
|
|
179
|
+
return process.env.npm_execpath;
|
|
180
|
+
}
|
|
181
|
+
})();
|
|
182
|
+
|
|
183
|
+
const npmCommand = resolvedNpmCli ? process.execPath
|
|
184
|
+
: process.platform === 'win32'
|
|
185
|
+
? 'npm.cmd'
|
|
186
|
+
: 'npm';
|
|
187
|
+
|
|
188
|
+
const npmArgs = resolvedNpmCli
|
|
189
|
+
? [resolvedNpmCli, 'install', 'sqlite3', '--no-save', '--no-package-lock', '--prefix', sqliteTempDir]
|
|
190
|
+
: ['install', 'sqlite3', '--no-save', '--no-package-lock', '--prefix', sqliteTempDir];
|
|
191
|
+
|
|
192
|
+
const result = spawnSync(npmCommand, npmArgs, {
|
|
193
|
+
stdio: 'inherit'
|
|
194
|
+
});
|
|
195
|
+
|
|
196
|
+
if (result.error) {
|
|
197
|
+
throw new Error(`Failed to install sqlite3: ${result.error.message}`);
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
if (result.status !== 0) {
|
|
201
|
+
throw new Error(`Failed to install sqlite3 (exit code ${result.status ?? 'unknown'}).`);
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
const mod = tryRequireSqlite3();
|
|
205
|
+
if (!mod) {
|
|
206
|
+
throw new Error('sqlite3 installation completed but module could not be required.');
|
|
207
|
+
}
|
|
208
|
+
return mod;
|
|
209
|
+
};
|
|
210
|
+
|
|
211
|
+
const ensureSqlite3 = () => {
|
|
212
|
+
if (sqlite3) return sqlite3;
|
|
213
|
+
|
|
214
|
+
const existing = tryRequireSqlite3();
|
|
215
|
+
if (existing) {
|
|
216
|
+
sqlite3 = existing;
|
|
217
|
+
return sqlite3;
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
if (!isSqliteDialect || !shouldExecute) {
|
|
221
|
+
return null;
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
console.log('sqlite3 not found; installing locally for SQLite execution (no package.json changes)...');
|
|
225
|
+
sqlite3 = installSqlite3Temp();
|
|
226
|
+
return sqlite3;
|
|
227
|
+
};
|
|
228
|
+
|
|
229
|
+
const getSeedSql = () => {
|
|
230
|
+
try {
|
|
231
|
+
const seedPath = path.join(process.cwd(), 'playground/shared/playground/data/seed.ts');
|
|
232
|
+
const content = fs.readFileSync(seedPath, 'utf8');
|
|
233
|
+
// Extract content between backticks
|
|
234
|
+
const match = content.match(/`([\s\S]*)`/);
|
|
235
|
+
return match ? match[1] : null;
|
|
236
|
+
} catch (e) {
|
|
237
|
+
console.warn('Could not load seed data:', e.message);
|
|
238
|
+
return null;
|
|
239
|
+
}
|
|
240
|
+
};
|
|
241
|
+
|
|
242
|
+
const initDb = async () => {
|
|
243
|
+
const sqlite = ensureSqlite3();
|
|
244
|
+
if (!sqlite) {
|
|
245
|
+
console.error("SQLite support requires 'sqlite3'. Please install it to use this feature.");
|
|
246
|
+
process.exit(1);
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
const dbLocation = dbPathArg || ':memory:';
|
|
250
|
+
const db = new sqlite.Database(dbLocation);
|
|
251
|
+
|
|
252
|
+
if (!dbPathArg) {
|
|
253
|
+
// If using in-memory (or no specific DB file provided), try to load seed data
|
|
254
|
+
const seedSql = getSeedSql();
|
|
255
|
+
if (seedSql) {
|
|
256
|
+
await new Promise((resolve, reject) => {
|
|
257
|
+
db.exec(seedSql, (err) => {
|
|
258
|
+
if (err) reject(err);
|
|
259
|
+
else resolve();
|
|
260
|
+
});
|
|
261
|
+
});
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
return db;
|
|
266
|
+
};
|
|
267
|
+
|
|
268
|
+
let dbInstance = null;
|
|
269
|
+
|
|
270
|
+
const executor = {
|
|
271
|
+
async executeSql(sql, params) {
|
|
272
|
+
if (!shouldExecute) {
|
|
273
|
+
return [];
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
if (!dbInstance) {
|
|
277
|
+
dbInstance = await initDb();
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
return new Promise((resolve, reject) => {
|
|
281
|
+
dbInstance.all(sql, params, (err, rows) => {
|
|
282
|
+
if (err) {
|
|
283
|
+
reject(err);
|
|
284
|
+
return;
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
const safeRows = rows ?? [];
|
|
288
|
+
const columns = safeRows.length ? Object.keys(safeRows[0]) : [];
|
|
289
|
+
const values = safeRows.map(row => columns.map(col => row[col]));
|
|
290
|
+
|
|
291
|
+
resolve([{ columns, values }]);
|
|
292
|
+
});
|
|
293
|
+
});
|
|
294
|
+
}
|
|
295
|
+
};
|
|
296
|
+
|
|
297
|
+
const factory = {
|
|
298
|
+
createExecutor: () => executor,
|
|
299
|
+
createTransactionalExecutor: () => executor
|
|
300
|
+
};
|
|
301
|
+
const orm = new Orm({ dialect, executorFactory: factory });
|
|
302
|
+
const session = new OrmSession({
|
|
303
|
+
orm,
|
|
304
|
+
executor,
|
|
305
|
+
queryLogger(entry) {
|
|
306
|
+
console.log('\n--- SQL ---');
|
|
307
|
+
console.log(entry.sql);
|
|
308
|
+
if (entry.params?.length) {
|
|
309
|
+
console.log('Params:', entry.params);
|
|
310
|
+
}
|
|
311
|
+
console.log('-----------');
|
|
312
|
+
}
|
|
313
|
+
});
|
|
314
|
+
|
|
315
|
+
try {
|
|
316
|
+
const result = await builder.execute(session);
|
|
317
|
+
if (shouldExecute) {
|
|
318
|
+
console.log('\n--- Results ---');
|
|
319
|
+
console.log(JSON.stringify(result, null, 2));
|
|
320
|
+
console.log('---------------');
|
|
321
|
+
}
|
|
322
|
+
} catch (err) {
|
|
323
|
+
console.error('\nError executing query:', err.message);
|
|
324
|
+
process.exit(1);
|
|
325
|
+
}
|