typesql-cli 0.10.0 → 0.10.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.
Files changed (90) hide show
  1. package/README.md +1 -1
  2. package/cli.js +9 -20
  3. package/cli.js.map +1 -1
  4. package/code-generator.d.ts.map +1 -1
  5. package/code-generator.js +46 -111
  6. package/code-generator.js.map +1 -1
  7. package/describe-dynamic-query.d.ts.map +1 -1
  8. package/describe-dynamic-query.js +6 -13
  9. package/describe-dynamic-query.js.map +1 -1
  10. package/describe-nested-query.d.ts.map +1 -1
  11. package/describe-nested-query.js +11 -27
  12. package/describe-nested-query.js.map +1 -1
  13. package/describe-query.d.ts.map +1 -1
  14. package/describe-query.js +9 -18
  15. package/describe-query.js.map +1 -1
  16. package/drivers/libsql.d.ts.map +1 -1
  17. package/drivers/libsql.js +1 -2
  18. package/drivers/libsql.js.map +1 -1
  19. package/mysql-mapping.d.ts.map +1 -1
  20. package/mysql-mapping.js +4 -4
  21. package/mysql-mapping.js.map +1 -1
  22. package/mysql-query-analyzer/collect-constraints.d.ts.map +1 -1
  23. package/mysql-query-analyzer/collect-constraints.js +17 -24
  24. package/mysql-query-analyzer/collect-constraints.js.map +1 -1
  25. package/mysql-query-analyzer/infer-column-nullability.d.ts.map +1 -1
  26. package/mysql-query-analyzer/infer-column-nullability.js +13 -29
  27. package/mysql-query-analyzer/infer-column-nullability.js.map +1 -1
  28. package/mysql-query-analyzer/infer-param-nullability.d.ts.map +1 -1
  29. package/mysql-query-analyzer/infer-param-nullability.js +8 -10
  30. package/mysql-query-analyzer/infer-param-nullability.js.map +1 -1
  31. package/mysql-query-analyzer/parse.d.ts.map +1 -1
  32. package/mysql-query-analyzer/parse.js +15 -20
  33. package/mysql-query-analyzer/parse.js.map +1 -1
  34. package/mysql-query-analyzer/select-columns.d.ts +1 -0
  35. package/mysql-query-analyzer/select-columns.d.ts.map +1 -1
  36. package/mysql-query-analyzer/select-columns.js +51 -43
  37. package/mysql-query-analyzer/select-columns.js.map +1 -1
  38. package/mysql-query-analyzer/traverse.d.ts.map +1 -1
  39. package/mysql-query-analyzer/traverse.js +61 -144
  40. package/mysql-query-analyzer/traverse.js.map +1 -1
  41. package/mysql-query-analyzer/types.d.ts +2 -2
  42. package/mysql-query-analyzer/types.d.ts.map +1 -1
  43. package/mysql-query-analyzer/unify.d.ts.map +1 -1
  44. package/mysql-query-analyzer/unify.js +15 -59
  45. package/mysql-query-analyzer/unify.js.map +1 -1
  46. package/mysql-query-analyzer/util.d.ts.map +1 -1
  47. package/mysql-query-analyzer/util.js +2 -3
  48. package/mysql-query-analyzer/util.js.map +1 -1
  49. package/mysql-query-analyzer/verify-multiple-result.d.ts.map +1 -1
  50. package/mysql-query-analyzer/verify-multiple-result.js +3 -7
  51. package/mysql-query-analyzer/verify-multiple-result.js.map +1 -1
  52. package/package.json +47 -46
  53. package/queryExectutor.d.ts.map +1 -1
  54. package/queryExectutor.js +37 -38
  55. package/queryExectutor.js.map +1 -1
  56. package/sql-generator.d.ts.map +1 -1
  57. package/sql-generator.js +5 -8
  58. package/sql-generator.js.map +1 -1
  59. package/sqlite-query-analyzer/code-generator.d.ts +2 -2
  60. package/sqlite-query-analyzer/code-generator.d.ts.map +1 -1
  61. package/sqlite-query-analyzer/code-generator.js +119 -219
  62. package/sqlite-query-analyzer/code-generator.js.map +1 -1
  63. package/sqlite-query-analyzer/parser.d.ts.map +1 -1
  64. package/sqlite-query-analyzer/parser.js +9 -23
  65. package/sqlite-query-analyzer/parser.js.map +1 -1
  66. package/sqlite-query-analyzer/query-executor.d.ts +6 -2
  67. package/sqlite-query-analyzer/query-executor.d.ts.map +1 -1
  68. package/sqlite-query-analyzer/query-executor.js +34 -22
  69. package/sqlite-query-analyzer/query-executor.js.map +1 -1
  70. package/sqlite-query-analyzer/replace-list-params.d.ts.map +1 -1
  71. package/sqlite-query-analyzer/replace-list-params.js +1 -2
  72. package/sqlite-query-analyzer/replace-list-params.js.map +1 -1
  73. package/sqlite-query-analyzer/sqlite-describe-nested-query.d.ts.map +1 -1
  74. package/sqlite-query-analyzer/sqlite-describe-nested-query.js +9 -22
  75. package/sqlite-query-analyzer/sqlite-describe-nested-query.js.map +1 -1
  76. package/sqlite-query-analyzer/traverse.d.ts.map +1 -1
  77. package/sqlite-query-analyzer/traverse.js +55 -98
  78. package/sqlite-query-analyzer/traverse.js.map +1 -1
  79. package/sqlite-query-analyzer/types.d.ts +1 -1
  80. package/sqlite-query-analyzer/types.d.ts.map +1 -1
  81. package/ts-dynamic-query-descriptor.d.ts.map +1 -1
  82. package/ts-dynamic-query-descriptor.js +3 -4
  83. package/ts-dynamic-query-descriptor.js.map +1 -1
  84. package/ts-nested-descriptor.d.ts.map +1 -1
  85. package/ts-nested-descriptor.js +2 -3
  86. package/ts-nested-descriptor.js.map +1 -1
  87. package/types.d.ts +6 -2
  88. package/types.d.ts.map +1 -1
  89. package/util.d.ts.map +1 -1
  90. package/util.js.map +1 -1
@@ -3,7 +3,9 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.generateTsCode = exports.generateCrud = exports.validateAndGenerateCode = void 0;
6
+ exports.validateAndGenerateCode = validateAndGenerateCode;
7
+ exports.generateCrud = generateCrud;
8
+ exports.generateTsCode = generateTsCode;
7
9
  const Either_1 = require("fp-ts/lib/Either");
8
10
  const parser_1 = require("./parser");
9
11
  const code_generator_1 = require("../code-generator");
@@ -25,11 +27,8 @@ function validateAndGenerateCode(client, sql, queryName, sqliteDbSchema, isCrud
25
27
  const code = generateTsCode(sql, queryName, sqliteDbSchema, client.type, isCrud);
26
28
  return code;
27
29
  }
28
- exports.validateAndGenerateCode = validateAndGenerateCode;
29
30
  function mapToColumnInfo(col, checkOptional) {
30
- const defaultValue = col.columnKey === 'PRI' && col.column_type === 'INTEGER'
31
- ? 'AUTOINCREMENT'
32
- : col.defaultValue;
31
+ const defaultValue = col.columnKey === 'PRI' && col.column_type === 'INTEGER' ? 'AUTOINCREMENT' : col.defaultValue;
33
32
  const columnInfo = {
34
33
  columnName: col.column,
35
34
  notNull: col.notNull,
@@ -46,9 +45,7 @@ function generateCrud(client, queryType, tableName, dbSchema) {
46
45
  if (keys.length === 0) {
47
46
  keys.push(...columns.filter((col) => col.columnKey === 'UNI'));
48
47
  }
49
- const keyColumnInfo = keys
50
- .map((key) => mapToColumnInfo(key, false))
51
- .map((col) => mapColumnToTsFieldDescriptor(col, client));
48
+ const keyColumnInfo = keys.map((key) => mapToColumnInfo(key, false)).map((col) => mapColumnToTsFieldDescriptor(col, client));
52
49
  const resultColumns = mapColumns(client, queryType, columnInfo, false);
53
50
  const params = columnInfo.map((col) => mapColumnToTsFieldDescriptor(col, client));
54
51
  const tsDescriptor = {
@@ -58,15 +55,12 @@ function generateCrud(client, queryType, tableName, dbSchema) {
58
55
  columns: resultColumns,
59
56
  parameterNames: [],
60
57
  parameters: queryType === 'Insert' ? params : keyColumnInfo,
61
- data: queryType === 'Update'
62
- ? params.filter((param) => { var _a; return param.name !== ((_a = keyColumnInfo[0]) === null || _a === void 0 ? void 0 : _a.name); })
63
- : []
58
+ data: queryType === 'Update' ? params.filter((param) => { var _a; return param.name !== ((_a = keyColumnInfo[0]) === null || _a === void 0 ? void 0 : _a.name); }) : []
64
59
  };
65
60
  const queryName = getQueryName(queryType, tableName);
66
61
  const code = generateCodeFromTsDescriptor(client, queryName, tsDescriptor, true, tableName);
67
62
  return code;
68
63
  }
69
- exports.generateCrud = generateCrud;
70
64
  function getQueryName(queryType, tableName) {
71
65
  const camelCaseName = (0, code_generator_1.convertToCamelCaseName)(tableName);
72
66
  const captitalizedName = (0, code_generator_1.capitalize)(camelCaseName);
@@ -90,7 +84,6 @@ function generateTsCode(sql, queryName, sqliteDbSchema, client, isCrud = false)
90
84
  const code = generateCodeFromTsDescriptor(client, queryName, tsDescriptor, isCrud);
91
85
  return (0, Either_1.right)(code);
92
86
  }
93
- exports.generateTsCode = generateTsCode;
94
87
  function createTsDescriptor(queryInfo, client) {
95
88
  var _a;
96
89
  const tsDescriptor = {
@@ -145,14 +138,10 @@ function mapColumns(client, queryType, columns, returning = false) {
145
138
  }
146
139
  ];
147
140
  if (queryType === 'Insert' && !returning) {
148
- return client === 'better-sqlite3'
149
- ? sqliteInsertColumns
150
- : libSqlInsertColumns;
141
+ return client === 'better-sqlite3' || client == 'bun:sqlite' ? sqliteInsertColumns : libSqlInsertColumns;
151
142
  }
152
143
  if (queryType === 'Update' || queryType === 'Delete') {
153
- return client === 'better-sqlite3'
154
- ? [sqliteInsertColumns[0]]
155
- : [libSqlInsertColumns[0]];
144
+ return client === 'better-sqlite3' || client == 'bun:sqlite' ? [sqliteInsertColumns[0]] : [libSqlInsertColumns[0]];
156
145
  }
157
146
  const escapedColumnsNames = (0, code_generator_1.renameInvalidNames)(columns.map((col) => col.columnName));
158
147
  return columns.map((col, index) => mapColumnToTsFieldDescriptor(Object.assign(Object.assign({}, col), { columnName: escapedColumnsNames[index] }), client));
@@ -205,6 +194,8 @@ function mapColumnType(sqliteType, client) {
205
194
  return 'Date';
206
195
  case 'BLOB':
207
196
  return client === 'better-sqlite3' ? 'Uint8Array' : 'ArrayBuffer';
197
+ case '?':
198
+ return 'any';
208
199
  }
209
200
  }
210
201
  function generateCodeFromTsDescriptor(client, queryName, tsDescriptor, isCrud = false, tableName = '') {
@@ -223,38 +214,30 @@ function generateCodeFromTsDescriptor(client, queryName, tsDescriptor, isCrud =
223
214
  const selectColumnsTypeName = `${capitalizedName}Select`;
224
215
  const whereTypeName = `${capitalizedName}Where`;
225
216
  const orderByTypeName = `${capitalizedName}OrderBy`;
226
- const generateOrderBy = tsDescriptor.orderByColumns != null &&
227
- tsDescriptor.orderByColumns.length > 0;
217
+ const generateOrderBy = tsDescriptor.orderByColumns != null && tsDescriptor.orderByColumns.length > 0;
228
218
  const uniqueParams = (0, code_generator_1.removeDuplicatedParameters2)(tsDescriptor.parameters);
229
219
  const uniqueUpdateParams = (0, code_generator_1.removeDuplicatedParameters2)(tsDescriptor.data || []);
230
- const orderByField = generateOrderBy
231
- ? `orderBy: [${orderByTypeName}, 'asc' | 'desc'][]`
232
- : undefined;
233
- const paramsTypes = (0, code_generator_1.removeDuplicatedParameters2)(tsDescriptor.dynamicQuery2 == null
234
- ? tsDescriptor.parameters
235
- : (0, ts_dynamic_query_descriptor_1.mapToDynamicParams)(tsDescriptor.parameters));
236
- let functionArguments = client === 'better-sqlite3'
237
- ? 'db: Database'
238
- : 'client: Client | Transaction';
220
+ const orderByField = generateOrderBy ? `orderBy: [${orderByTypeName}, 'asc' | 'desc'][]` : undefined;
221
+ const paramsTypes = (0, code_generator_1.removeDuplicatedParameters2)(tsDescriptor.dynamicQuery2 == null ? tsDescriptor.parameters : (0, ts_dynamic_query_descriptor_1.mapToDynamicParams)(tsDescriptor.parameters));
222
+ let functionArguments = client === 'better-sqlite3' || client == 'bun:sqlite' ? 'db: Database' : 'client: Client | Transaction';
239
223
  functionArguments += queryType === 'Update' ? `, data: ${dataTypeName}` : '';
240
224
  if (tsDescriptor.dynamicQuery2 == null) {
241
- functionArguments +=
242
- tsDescriptor.parameters.length > 0 || generateOrderBy
243
- ? `, params: ${paramsTypeName}`
244
- : '';
225
+ functionArguments += tsDescriptor.parameters.length > 0 || generateOrderBy ? `, params: ${paramsTypeName}` : '';
245
226
  }
246
227
  else {
247
228
  functionArguments += `, ${orderByField ? 'params' : 'params?'}: ${dynamicParamsTypeName}`;
248
229
  }
249
230
  const orNull = queryType === 'Select' ? ' | null' : '';
250
- const returnType = tsDescriptor.multipleRowsResult
251
- ? `${resultTypeName}[]`
252
- : `${resultTypeName}${orNull}`;
231
+ const returnType = tsDescriptor.multipleRowsResult ? `${resultTypeName}[]` : `${resultTypeName}${orNull}`;
253
232
  const allParameters = (((_a = tsDescriptor.data) === null || _a === void 0 ? void 0 : _a.map((param) => fromDriver('data', param))) || []).concat(tsDescriptor.parameters.map((param) => fromDriver('params', param)));
254
- const queryParams = allParameters.length > 0 ? `[${allParameters.join(', ')}]` : '';
233
+ const queryParamsWithoutBrackets = allParameters.length > 0 ? `${allParameters.join(', ')}` : '';
234
+ const queryParams = queryParamsWithoutBrackets !== '' ? `[${queryParamsWithoutBrackets}]` : '';
255
235
  if (client === 'better-sqlite3') {
256
236
  writer.writeLine(`import type { Database } from 'better-sqlite3';`);
257
237
  }
238
+ if (client === 'bun:sqlite') {
239
+ writer.writeLine(`import type { Database } from 'bun:sqlite';`);
240
+ }
258
241
  if (client === 'libsql') {
259
242
  writer.writeLine(`import type { Client, Transaction } from '@libsql/client';`);
260
243
  }
@@ -273,9 +256,7 @@ function generateCodeFromTsDescriptor(client, queryName, tsDescriptor, isCrud =
273
256
  });
274
257
  writer.blankLine();
275
258
  (0, code_generator_1.writeTypeBlock)(writer, paramsTypes, paramsTypeName, false, tsDescriptor.dynamicQuery2 ? undefined : orderByField);
276
- const resultTypes = tsDescriptor.dynamicQuery2 == null
277
- ? tsDescriptor.columns
278
- : (0, ts_dynamic_query_descriptor_1.mapToDynamicResultColumns)(tsDescriptor.columns);
259
+ const resultTypes = tsDescriptor.dynamicQuery2 == null ? tsDescriptor.columns : (0, ts_dynamic_query_descriptor_1.mapToDynamicResultColumns)(tsDescriptor.columns);
279
260
  (0, code_generator_1.writeTypeBlock)(writer, resultTypes, resultTypeName, false);
280
261
  const selectFields = (0, ts_dynamic_query_descriptor_1.mapToDynamicSelectColumns)(tsDescriptor.columns);
281
262
  (0, code_generator_1.writeTypeBlock)(writer, selectFields, selectColumnsTypeName, false);
@@ -306,13 +287,9 @@ function generateCodeFromTsDescriptor(client, queryName, tsDescriptor, isCrud =
306
287
  writer.blankLine();
307
288
  const asyncModified = client === 'libsql' ? 'async ' : '';
308
289
  const returnTypeModifier = client === 'libsql' ? `Promise<${returnType}>` : returnType;
309
- writer
310
- .write(`export ${asyncModified}function ${camelCaseName}(${functionArguments}): ${returnTypeModifier}`)
311
- .block(() => {
290
+ writer.write(`export ${asyncModified}function ${camelCaseName}(${functionArguments}): ${returnTypeModifier}`).block(() => {
312
291
  var _a, _b, _c, _d, _e, _f;
313
- writer
314
- .write('const where = whereConditionsToObject(params?.where);')
315
- .newLine();
292
+ writer.write('const where = whereConditionsToObject(params?.where);').newLine();
316
293
  if (orderByField != null) {
317
294
  writer.writeLine('const orderBy = orderByToObject(params.orderBy);');
318
295
  }
@@ -328,32 +305,22 @@ function generateCodeFromTsDescriptor(client, queryName, tsDescriptor, isCrud =
328
305
  }
329
306
  const whereConditions = withFragment.dependOnFields.map((fieldIndex) => `where.${tsDescriptor.columns[fieldIndex].name} != null`);
330
307
  const orderByConditions = ((_a = withFragment.dependOnOrderBy) === null || _a === void 0 ? void 0 : _a.map((orderBy) => `orderBy['${orderBy}'] != null`)) || [];
331
- const allConditions = [
332
- ...selectConditions,
333
- ...whereConditions,
334
- ...orderByConditions
335
- ];
308
+ const allConditions = [...selectConditions, ...whereConditions, ...orderByConditions];
336
309
  const paramValues = withFragment.parameters.map((param) => `params?.params?.${param}`);
337
- writer
338
- .write(`if (${allConditions.join(`${node_os_1.EOL}\t|| `)})`)
339
- .block(() => {
310
+ writer.write(`if (${allConditions.join(`${node_os_1.EOL}\t|| `)})`).block(() => {
340
311
  writer.write(`withClause += EOL + \`${withFragment.fragment}\`;`);
341
312
  paramValues.forEach((paramValues) => {
342
313
  writer.writeLine(`paramsValues.push(${paramValues});`);
343
314
  });
344
315
  });
345
316
  });
346
- writer
347
- .write(`let sql = 'WITH ' + withClause + EOL + 'SELECT';`)
348
- .newLine();
317
+ writer.write(`let sql = 'WITH ' + withClause + EOL + 'SELECT';`).newLine();
349
318
  }
350
319
  else {
351
320
  writer.write(`let sql = 'SELECT';`).newLine();
352
321
  }
353
322
  (_c = tsDescriptor.dynamicQuery2) === null || _c === void 0 ? void 0 : _c.select.forEach((select, index) => {
354
- writer
355
- .write(`if (params?.select == null || params.select.${tsDescriptor.columns[index].name})`)
356
- .block(() => {
323
+ writer.write(`if (params?.select == null || params.select.${tsDescriptor.columns[index].name})`).block(() => {
357
324
  writer.writeLine(`sql = appendSelect(sql, \`${select.fragment}\`);`);
358
325
  select.parameters.forEach((param) => {
359
326
  writer.writeLine(`paramsValues.push(params?.params?.${param} ?? null);`);
@@ -372,15 +339,9 @@ function generateCodeFromTsDescriptor(client, queryName, tsDescriptor, isCrud =
372
339
  }
373
340
  const whereConditions = from.dependOnFields.map((fieldIndex) => `where.${tsDescriptor.columns[fieldIndex].name} != null`);
374
341
  const orderByConditions = ((_a = from.dependOnOrderBy) === null || _a === void 0 ? void 0 : _a.map((orderBy) => `orderBy['${orderBy}'] != null`)) || [];
375
- const allConditions = [
376
- ...selectConditions,
377
- ...whereConditions,
378
- ...orderByConditions
379
- ];
342
+ const allConditions = [...selectConditions, ...whereConditions, ...orderByConditions];
380
343
  const paramValues = from.parameters.map((param) => `params?.params?.${param}`);
381
- writer
382
- .write(`if (${allConditions.join(`${node_os_1.EOL}\t|| `)})`)
383
- .block(() => {
344
+ writer.write(`if (${allConditions.join(`${node_os_1.EOL}\t|| `)})`).block(() => {
384
345
  writer.write(`sql += EOL + \`${from.fragment}\`;`);
385
346
  paramValues.forEach((paramValues) => {
386
347
  writer.writeLine(`paramsValues.push(${paramValues});`);
@@ -401,9 +362,7 @@ function generateCodeFromTsDescriptor(client, queryName, tsDescriptor, isCrud =
401
362
  writer.writeLine('const where = whereCondition(condition);');
402
363
  (_a = tsDescriptor.dynamicQuery2) === null || _a === void 0 ? void 0 : _a.select.forEach((select, index) => {
403
364
  if (select.parameters.length > 0) {
404
- writer
405
- .write(`if (condition[0] == '${tsDescriptor.columns[index].name}')`)
406
- .block(() => {
365
+ writer.write(`if (condition[0] == '${tsDescriptor.columns[index].name}')`).block(() => {
407
366
  select.parameters.forEach((param) => {
408
367
  writer.writeLine(`paramsValues.push(params?.params?.${param} ?? null);`);
409
368
  });
@@ -434,10 +393,15 @@ function generateCodeFromTsDescriptor(client, queryName, tsDescriptor, isCrud =
434
393
  .indent()
435
394
  .write(`.map(data => mapArrayTo${resultTypeName}(data, params?.select))${tsDescriptor.multipleRowsResult ? '' : '[0]'};`);
436
395
  }
437
- if (client === 'libsql') {
396
+ if (client === 'bun:sqlite') {
397
+ writer.write('return db.prepare(sql)').newLine();
398
+ writer.indent().write('.values(paramsValues)').newLine();
438
399
  writer
439
- .write('return client.execute({ sql, args: paramsValues })')
440
- .newLine();
400
+ .indent()
401
+ .write(`.map(data => mapArrayTo${resultTypeName}(data, params?.select))${tsDescriptor.multipleRowsResult ? '' : '[0]'};`);
402
+ }
403
+ if (client === 'libsql') {
404
+ writer.write('return client.execute({ sql, args: paramsValues })').newLine();
441
405
  writer.indent().write('.then(res => res.rows)').newLine();
442
406
  writer
443
407
  .indent()
@@ -445,15 +409,11 @@ function generateCodeFromTsDescriptor(client, queryName, tsDescriptor, isCrud =
445
409
  }
446
410
  });
447
411
  writer.blankLine();
448
- writer
449
- .write(`function mapArrayTo${resultTypeName}(data: any, select?: ${selectColumnsTypeName})`)
450
- .block(() => {
412
+ writer.write(`function mapArrayTo${resultTypeName}(data: any, select?: ${selectColumnsTypeName})`).block(() => {
451
413
  writer.writeLine(`const result = {} as ${resultTypeName};`);
452
414
  writer.writeLine('let rowIndex = -1;');
453
415
  tsDescriptor.columns.forEach((tsField) => {
454
- writer
455
- .write(`if (select == null || select.${tsField.name})`)
456
- .block(() => {
416
+ writer.write(`if (select == null || select.${tsField.name})`).block(() => {
457
417
  writer.writeLine('rowIndex++;');
458
418
  writer.writeLine(`result.${tsField.name} = ${toDriver('data[rowIndex]', tsField)};`);
459
419
  });
@@ -461,9 +421,7 @@ function generateCodeFromTsDescriptor(client, queryName, tsDescriptor, isCrud =
461
421
  writer.write('return result;');
462
422
  });
463
423
  writer.blankLine();
464
- writer
465
- .write('function appendSelect(sql: string, selectField: string)')
466
- .block(() => {
424
+ writer.write('function appendSelect(sql: string, selectField: string)').block(() => {
467
425
  writer.write(`if (sql == 'SELECT')`).block(() => {
468
426
  writer.writeLine('return sql + EOL + selectField;');
469
427
  });
@@ -472,13 +430,9 @@ function generateCodeFromTsDescriptor(client, queryName, tsDescriptor, isCrud =
472
430
  });
473
431
  });
474
432
  writer.blankLine();
475
- writer
476
- .write(`function whereConditionsToObject(whereConditions?: ${whereTypeName}[])`)
477
- .block(() => {
433
+ writer.write(`function whereConditionsToObject(whereConditions?: ${whereTypeName}[])`).block(() => {
478
434
  writer.writeLine('const obj = {} as any;');
479
- writer
480
- .write('whereConditions?.forEach(condition => ')
481
- .inlineBlock(() => {
435
+ writer.write('whereConditions?.forEach(condition => ').inlineBlock(() => {
482
436
  writer.writeLine('const where = whereCondition(condition);');
483
437
  writer.write('if (where?.hasValue) ').block(() => {
484
438
  writer.writeLine('obj[condition[0]] = true;');
@@ -489,9 +443,7 @@ function generateCodeFromTsDescriptor(client, queryName, tsDescriptor, isCrud =
489
443
  });
490
444
  if (orderByField != null) {
491
445
  writer.blankLine();
492
- writer
493
- .write(`function orderByToObject(orderBy: ${dynamicParamsTypeName}['orderBy'])`)
494
- .block(() => {
446
+ writer.write(`function orderByToObject(orderBy: ${dynamicParamsTypeName}['orderBy'])`).block(() => {
495
447
  writer.writeLine('const obj = {} as any;');
496
448
  writer.write('orderBy?.forEach(order => ').inlineBlock(() => {
497
449
  writer.writeLine('obj[order[0]] = true;');
@@ -507,9 +459,7 @@ function generateCodeFromTsDescriptor(client, queryName, tsDescriptor, isCrud =
507
459
  writer.writeLine('values: any[];');
508
460
  });
509
461
  writer.blankLine();
510
- writer
511
- .write(`function whereCondition(condition: ${whereTypeName}): WhereConditionResult | undefined `)
512
- .block(() => {
462
+ writer.write(`function whereCondition(condition: ${whereTypeName}): WhereConditionResult | undefined `).block(() => {
513
463
  writer.blankLine();
514
464
  writer.writeLine('const selectFragment = selectFragments[condition[0]];');
515
465
  writer.writeLine('const operator = condition[1];');
@@ -542,9 +492,7 @@ function generateCodeFromTsDescriptor(client, queryName, tsDescriptor, isCrud =
542
492
  });
543
493
  }
544
494
  });
545
- writer
546
- .write(`if (operator == 'IN' || operator == 'NOT IN') `)
547
- .block(() => {
495
+ writer.write(`if (operator == 'IN' || operator == 'NOT IN') `).block(() => {
548
496
  if ((0, code_generator_1.hasDateColumn)(tsDescriptor.columns)) {
549
497
  writer.write('return ').block(() => {
550
498
  writer.writeLine("sql: `${selectFragment} ${operator} (${condition[2]?.map(value => isDate(value) ? 'date(?)' : '?').join(', ')})`,");
@@ -560,9 +508,7 @@ function generateCodeFromTsDescriptor(client, queryName, tsDescriptor, isCrud =
560
508
  });
561
509
  }
562
510
  });
563
- writer
564
- .write('if (NumericOperatorList.includes(operator)) ')
565
- .block(() => {
511
+ writer.write('if (NumericOperatorList.includes(operator)) ').block(() => {
566
512
  if ((0, code_generator_1.hasDateColumn)(tsDescriptor.columns)) {
567
513
  writer.writeLine('const value = isDate(condition[2]) ? condition[2]?.toISOString() : condition[2];');
568
514
  writer.writeLine(`const param = isDate(condition[2]) ? 'date(?)' : '?';`);
@@ -627,20 +573,13 @@ function generateCodeFromTsDescriptor(client, queryName, tsDescriptor, isCrud =
627
573
  : `function ${camelCaseName}(${functionArguments}): ${returnType}`;
628
574
  writer.write(`export ${crudFunction}`).block(() => {
629
575
  const idColumn = tsDescriptor.parameters[0].name;
630
- writeExecuteCrudBlock(client, queryType, tableName, tsDescriptor.columns, idColumn, queryParams, paramsTypeName, dataTypeName, resultTypeName, writer);
576
+ writeExecuteCrudBlock(client, queryType, tableName, tsDescriptor.columns, idColumn, client == 'bun:sqlite' ? queryParamsWithoutBrackets : queryParams, paramsTypeName, dataTypeName, resultTypeName, writer);
631
577
  });
632
578
  }
633
- if (tsDescriptor.dynamicQuery2 == null &&
634
- !isCrud &&
635
- (queryType === 'Select' ||
636
- (queryType === 'Insert' && tsDescriptor.returning))) {
579
+ if (tsDescriptor.dynamicQuery2 == null && !isCrud && (queryType === 'Select' || (queryType === 'Insert' && tsDescriptor.returning))) {
637
580
  if (client === 'better-sqlite3') {
638
- writer
639
- .write(`export function ${camelCaseName}(${functionArguments}): ${returnType}`)
640
- .block(() => {
641
- const processedSql = tsDescriptor.orderByColumns
642
- ? (0, code_generator_1.replaceOrderByParam)(sql)
643
- : sql;
581
+ writer.write(`export function ${camelCaseName}(${functionArguments}): ${returnType}`).block(() => {
582
+ const processedSql = tsDescriptor.orderByColumns ? (0, code_generator_1.replaceOrderByParam)(sql) : sql;
644
583
  const sqlSplit = processedSql.split('\n');
645
584
  writer.write('const sql = `').newLine();
646
585
  sqlSplit.forEach((sqlLine) => {
@@ -650,18 +589,26 @@ function generateCodeFromTsDescriptor(client, queryName, tsDescriptor, isCrud =
650
589
  writer.write('return db.prepare(sql)').newLine();
651
590
  writer.indent().write('.raw(true)').newLine();
652
591
  writer.indent().write(`.all(${queryParams})`).newLine();
653
- writer
654
- .indent()
655
- .write(`.map(data => mapArrayTo${resultTypeName}(data))${tsDescriptor.multipleRowsResult ? '' : '[0]'};`);
592
+ writer.indent().write(`.map(data => mapArrayTo${resultTypeName}(data))${tsDescriptor.multipleRowsResult ? '' : '[0]'};`);
593
+ });
594
+ }
595
+ if (client === 'bun:sqlite') {
596
+ writer.write(`export function ${camelCaseName}(${functionArguments}): ${returnType}`).block(() => {
597
+ const processedSql = tsDescriptor.orderByColumns ? (0, code_generator_1.replaceOrderByParam)(sql) : sql;
598
+ const sqlSplit = processedSql.split('\n');
599
+ writer.write('const sql = `').newLine();
600
+ sqlSplit.forEach((sqlLine) => {
601
+ writer.indent().write(sqlLine).newLine();
602
+ });
603
+ writer.indent().write('`').newLine();
604
+ writer.write('return db.prepare(sql)').newLine();
605
+ writer.indent().write(`.values(${queryParamsWithoutBrackets})`).newLine();
606
+ writer.indent().write(`.map(data => mapArrayTo${resultTypeName}(data))${tsDescriptor.multipleRowsResult ? '' : '[0]'};`);
656
607
  });
657
608
  }
658
609
  if (!isCrud && client === 'libsql') {
659
- writer
660
- .write(`export async function ${camelCaseName}(${functionArguments}): Promise<${returnType}>`)
661
- .block(() => {
662
- const processedSql = tsDescriptor.orderByColumns
663
- ? (0, code_generator_1.replaceOrderByParam)(sql)
664
- : sql;
610
+ writer.write(`export async function ${camelCaseName}(${functionArguments}): Promise<${returnType}>`).block(() => {
611
+ const processedSql = tsDescriptor.orderByColumns ? (0, code_generator_1.replaceOrderByParam)(sql) : sql;
665
612
  const sqlSplit = processedSql.split('\n');
666
613
  writer.write('const sql = `').newLine();
667
614
  sqlSplit.forEach((sqlLine) => {
@@ -673,47 +620,33 @@ function generateCodeFromTsDescriptor(client, queryName, tsDescriptor, isCrud =
673
620
  if (queryType === 'Select') {
674
621
  writer.indent().write('.then(res => res.rows)').newLine();
675
622
  if (tsDescriptor.multipleRowsResult) {
676
- writer
677
- .indent()
678
- .write(`.then(rows => rows.map(row => mapArrayTo${resultTypeName}(row)));`);
623
+ writer.indent().write(`.then(rows => rows.map(row => mapArrayTo${resultTypeName}(row)));`);
679
624
  }
680
625
  else {
681
- writer
682
- .indent()
683
- .write(`.then(rows => mapArrayTo${resultTypeName}(rows[0]));`);
626
+ writer.indent().write(`.then(rows => mapArrayTo${resultTypeName}(rows[0]));`);
684
627
  }
685
628
  }
686
629
  if (queryType === 'Insert') {
687
630
  if (tsDescriptor.returning) {
688
631
  writer.indent().write('.then(res => res.rows)').newLine();
689
- writer
690
- .indent()
691
- .write(`.then(rows => mapArrayTo${resultTypeName}(rows[0]));`);
632
+ writer.indent().write(`.then(rows => mapArrayTo${resultTypeName}(rows[0]));`);
692
633
  }
693
634
  else {
694
- writer
695
- .indent()
696
- .write(`.then(res => mapArrayTo${resultTypeName}(res));`);
635
+ writer.indent().write(`.then(res => mapArrayTo${resultTypeName}(res));`);
697
636
  }
698
637
  }
699
638
  });
700
639
  }
701
640
  }
702
- if (!isCrud &&
703
- (queryType === 'Update' ||
704
- queryType === 'Delete' ||
705
- (queryType === 'Insert' && !tsDescriptor.returning))) {
706
- if (client === 'better-sqlite3') {
707
- writer
708
- .write(`export function ${camelCaseName}(${functionArguments}): ${resultTypeName}`)
709
- .block(() => {
710
- writeExecuteBlock(sql, queryParams, resultTypeName, writer);
641
+ if (!isCrud && (queryType === 'Update' || queryType === 'Delete' || (queryType === 'Insert' && !tsDescriptor.returning))) {
642
+ if (client === 'better-sqlite3' || client === 'bun:sqlite') {
643
+ writer.write(`export function ${camelCaseName}(${functionArguments}): ${resultTypeName}`).block(() => {
644
+ const params = client === 'better-sqlite3' ? queryParams : queryParamsWithoutBrackets;
645
+ writeExecuteBlock(sql, params, resultTypeName, writer);
711
646
  });
712
647
  }
713
648
  if (client === 'libsql') {
714
- writer
715
- .write(`export async function ${camelCaseName}(${functionArguments}): Promise<${resultTypeName}>`)
716
- .block(() => {
649
+ writer.write(`export async function ${camelCaseName}(${functionArguments}): Promise<${resultTypeName}>`).block(() => {
717
650
  const sqlSplit = sql.split('\n');
718
651
  writer.write('const sql = `').newLine();
719
652
  sqlSplit.forEach((sqlLine) => {
@@ -722,18 +655,13 @@ function generateCodeFromTsDescriptor(client, queryName, tsDescriptor, isCrud =
722
655
  writer.indent().write('`').newLine();
723
656
  const executeParams = queryParams !== '' ? `{ sql, args: ${queryParams} }` : 'sql';
724
657
  writer.write(`return client.execute(${executeParams})`).newLine();
725
- writer
726
- .indent()
727
- .write(`.then(res => mapArrayTo${resultTypeName}(res));`);
658
+ writer.indent().write(`.then(res => mapArrayTo${resultTypeName}(res));`);
728
659
  });
729
660
  }
730
661
  }
731
- if ((queryType === 'Select' || tsDescriptor.returning) &&
732
- tsDescriptor.dynamicQuery2 == null) {
662
+ if ((queryType === 'Select' || tsDescriptor.returning) && tsDescriptor.dynamicQuery2 == null) {
733
663
  writer.blankLine();
734
- writer
735
- .write(`function mapArrayTo${resultTypeName}(data: any) `)
736
- .block(() => {
664
+ writer.write(`function mapArrayTo${resultTypeName}(data: any) `).block(() => {
737
665
  writer.write(`const result: ${resultTypeName} = `).block(() => {
738
666
  tsDescriptor.columns.forEach((col, index) => {
739
667
  const separator = index < tsDescriptor.columns.length - 1 ? ',' : '';
@@ -743,13 +671,9 @@ function generateCodeFromTsDescriptor(client, queryName, tsDescriptor, isCrud =
743
671
  writer.writeLine('return result;');
744
672
  });
745
673
  }
746
- else if (client === 'libsql' &&
747
- !tsDescriptor.returning &&
748
- tsDescriptor.dynamicQuery2 == null) {
674
+ else if (client === 'libsql' && !tsDescriptor.returning && tsDescriptor.dynamicQuery2 == null) {
749
675
  writer.blankLine();
750
- writer
751
- .write(`function mapArrayTo${resultTypeName}(data: any) `)
752
- .block(() => {
676
+ writer.write(`function mapArrayTo${resultTypeName}(data: any) `).block(() => {
753
677
  writer.write(`const result: ${resultTypeName} = `).block(() => {
754
678
  tsDescriptor.columns.forEach((col, index) => {
755
679
  const separator = index < tsDescriptor.columns.length - 1 ? ',' : '';
@@ -760,9 +684,7 @@ function generateCodeFromTsDescriptor(client, queryName, tsDescriptor, isCrud =
760
684
  });
761
685
  }
762
686
  if (tsDescriptor.orderByColumns) {
763
- const orderByType = tsDescriptor.dynamicQuery2 == null
764
- ? paramsTypeName
765
- : dynamicParamsTypeName;
687
+ const orderByType = tsDescriptor.dynamicQuery2 == null ? paramsTypeName : dynamicParamsTypeName;
766
688
  if (orderByField != null) {
767
689
  writer.blankLine();
768
690
  writer.write('const orderByFragments = ').inlineBlock(() => {
@@ -776,9 +698,7 @@ function generateCodeFromTsDescriptor(client, queryName, tsDescriptor, isCrud =
776
698
  writer.blankLine();
777
699
  writer.writeLine(`export type ${orderByTypeName} = keyof typeof orderByFragments;`);
778
700
  writer.blankLine();
779
- writer
780
- .write(`function escapeOrderBy(orderBy: ${orderByType}['orderBy']): string`)
781
- .block(() => {
701
+ writer.write(`function escapeOrderBy(orderBy: ${orderByType}['orderBy']): string`).block(() => {
782
702
  writer.writeLine(`return orderBy.map(order => \`\${orderByFragments[order[0]]} \${order[1] == 'desc' ? 'desc' : 'asc'}\`).join(', ');`);
783
703
  });
784
704
  }
@@ -803,10 +723,8 @@ function generateCodeFromTsDescriptor(client, queryName, tsDescriptor, isCrud =
803
723
  relations.forEach((relation, index) => {
804
724
  const relationType = (0, code_generator_1.generateRelationType)(capitalizedName, relation.name);
805
725
  if (index === 0) {
806
- if (client === 'better-sqlite3') {
807
- writer
808
- .write(`export function ${camelCaseName}Nested(${functionArguments}): ${relationType}[]`)
809
- .block(() => {
726
+ if (client === 'better-sqlite3' || client == 'bun:sqlite') {
727
+ writer.write(`export function ${camelCaseName}Nested(${functionArguments}): ${relationType}[]`).block(() => {
810
728
  const params = tsDescriptor.parameters.length > 0 ? ', params' : '';
811
729
  writer.writeLine(`const selectResult = ${camelCaseName}(db${params});`);
812
730
  writer.write('if (selectResult.length == 0)').block(() => {
@@ -816,9 +734,7 @@ function generateCodeFromTsDescriptor(client, queryName, tsDescriptor, isCrud =
816
734
  });
817
735
  }
818
736
  else if (client === 'libsql') {
819
- writer
820
- .write(`export async function ${camelCaseName}Nested(${functionArguments}): Promise<${relationType}[]>`)
821
- .block(() => {
737
+ writer.write(`export async function ${camelCaseName}Nested(${functionArguments}): Promise<${relationType}[]>`).block(() => {
822
738
  const params = tsDescriptor.parameters.length > 0 ? ', params' : '';
823
739
  writer.writeLine(`const selectResult = await ${camelCaseName}(client${params});`);
824
740
  writer.write('if (selectResult.length == 0)').block(() => {
@@ -831,9 +747,7 @@ function generateCodeFromTsDescriptor(client, queryName, tsDescriptor, isCrud =
831
747
  writeCollectFunction(writer, relation, tsDescriptor.columns, capitalizedName, resultTypeName);
832
748
  });
833
749
  writer.blankLine();
834
- writer
835
- .write('const groupBy = <T, Q>(array: T[], predicate: (value: T, index: number, array: T[]) => Q) =>')
836
- .block(() => {
750
+ writer.write('const groupBy = <T, Q>(array: T[], predicate: (value: T, index: number, array: T[]) => Q) =>').block(() => {
837
751
  writer
838
752
  .write('return array.reduce((map, value, index, array) => ')
839
753
  .inlineBlock(() => {
@@ -882,18 +796,17 @@ function writeExecutSelectCrudBlock(client, tableName, idColumn, columns, queryP
882
796
  writer.write('return db.prepare(sql)').newLine();
883
797
  writer.indent().write('.raw(true)').newLine();
884
798
  writer.indent().write(`.all(${queryParams})`).newLine();
885
- writer
886
- .indent()
887
- .write(`.map(data => mapArrayTo${resultTypeName}(data))[0];`);
799
+ writer.indent().write(`.map(data => mapArrayTo${resultTypeName}(data))[0];`);
800
+ }
801
+ else if (client === 'bun:sqlite') {
802
+ writer.write('return db.prepare(sql)').newLine();
803
+ writer.indent().write(`.values(${queryParams})`).newLine();
804
+ writer.indent().write(`.map(data => mapArrayTo${resultTypeName}(data))[0];`);
888
805
  }
889
806
  else {
890
- writer
891
- .write(`return client.execute({ sql, args: ${queryParams} })`)
892
- .newLine();
807
+ writer.write(`return client.execute({ sql, args: ${queryParams} })`).newLine();
893
808
  writer.indent().write('.then(res => res.rows)').newLine();
894
- writer
895
- .indent()
896
- .write(`.then(rows => mapArrayTo${resultTypeName}(rows[0]));`);
809
+ writer.indent().write(`.then(rows => mapArrayTo${resultTypeName}(rows[0]));`);
897
810
  }
898
811
  }
899
812
  function writeExecuteInsertCrudBlock(client, tableName, paramTypeName, resultTypeName, writer) {
@@ -903,25 +816,20 @@ function writeExecuteInsertCrudBlock(client, tableName, paramTypeName, resultTyp
903
816
  writer.writeLine('const values = columns.map(col => params[col]!);');
904
817
  writer.blankLine();
905
818
  writer.writeLine('const sql = columns.length == 0');
906
- writer
907
- .indent()
908
- .write(`? \`INSERT INTO ${tableName} DEFAULT VALUES\``)
909
- .newLine();
910
- writer
911
- .indent()
912
- .write(`: \`INSERT INTO ${tableName}(\${columns.join(',')}) VALUES(\${columns.map(_ => '?').join(',')})\``)
913
- .newLine();
819
+ writer.indent().write(`? \`INSERT INTO ${tableName} DEFAULT VALUES\``).newLine();
820
+ writer.indent().write(`: \`INSERT INTO ${tableName}(\${columns.join(',')}) VALUES(\${columns.map(_ => '?').join(',')})\``).newLine();
914
821
  writer.blankLine();
915
822
  if (client === 'better-sqlite3') {
916
823
  writer.write('return db.prepare(sql)').newLine();
917
824
  writer.indent().write(`.run(values) as ${resultTypeName};`);
918
825
  }
826
+ else if (client == 'bun:sqlite') {
827
+ writer.write('return db.prepare(sql)').newLine();
828
+ writer.indent().write(`.run(...values) as ${resultTypeName};`);
829
+ }
919
830
  else {
920
831
  writer.write('return client.execute({ sql, args: values })').newLine();
921
- writer
922
- .indent()
923
- .write(`.then(res => mapArrayTo${resultTypeName}(res));`)
924
- .newLine();
832
+ writer.indent().write(`.then(res => mapArrayTo${resultTypeName}(res));`).newLine();
925
833
  }
926
834
  }
927
835
  function writeExecuteUpdateCrudBlock(client, tableName, idColumn, paramTypeName, resultTypeName, writer) {
@@ -932,22 +840,20 @@ function writeExecuteUpdateCrudBlock(client, tableName, idColumn, paramTypeName,
932
840
  writer.blankLine();
933
841
  writer.writeLine('const sql = `');
934
842
  writer.indent().write(`UPDATE ${tableName}`).newLine();
935
- writer
936
- .indent()
937
- .write(`SET \${columns.map(col => \`\${col} = ?\`).join(', ')}`)
938
- .newLine();
843
+ writer.indent().write(`SET \${columns.map(col => \`\${col} = ?\`).join(', ')}`).newLine();
939
844
  writer.indent().write(`WHERE ${idColumn} = ?\``).newLine();
940
845
  writer.blankLine();
941
846
  if (client === 'better-sqlite3') {
942
847
  writer.write('return db.prepare(sql)').newLine();
943
848
  writer.indent().write(`.run(values) as ${resultTypeName};`);
944
849
  }
850
+ else if (client === 'bun:sqlite') {
851
+ writer.write('return db.prepare(sql)').newLine();
852
+ writer.indent().write(`.run(...values) as ${resultTypeName};`);
853
+ }
945
854
  else {
946
855
  writer.write('return client.execute({ sql, args: values })').newLine();
947
- writer
948
- .indent()
949
- .write(`.then(res => mapArrayTo${resultTypeName}(res));`)
950
- .newLine();
856
+ writer.indent().write(`.then(res => mapArrayTo${resultTypeName}(res));`).newLine();
951
857
  }
952
858
  }
953
859
  function writeExecutDeleteCrudBlock(client, tableName, idColumn, queryParams, resultTypeName, writer) {
@@ -958,19 +864,15 @@ function writeExecutDeleteCrudBlock(client, tableName, idColumn, queryParams, re
958
864
  writer.blankLine();
959
865
  if (client === 'better-sqlite3') {
960
866
  writer.write('return db.prepare(sql)').newLine();
961
- writer
962
- .indent()
963
- .write(`.run(${queryParams}) as ${resultTypeName};`)
964
- .newLine();
867
+ writer.indent().write(`.run(${queryParams}) as ${resultTypeName};`).newLine();
868
+ }
869
+ else if (client === 'bun:sqlite') {
870
+ writer.write('return db.prepare(sql)').newLine();
871
+ writer.indent().write(`.run(${queryParams}) as ${resultTypeName};`).newLine();
965
872
  }
966
873
  else {
967
- writer
968
- .write(`return client.execute({ sql, args: ${queryParams} })`)
969
- .newLine();
970
- writer
971
- .indent()
972
- .write(`.then(res => mapArrayTo${resultTypeName}(res));`)
973
- .newLine();
874
+ writer.write(`return client.execute({ sql, args: ${queryParams} })`).newLine();
875
+ writer.indent().write(`.then(res => mapArrayTo${resultTypeName}(res));`).newLine();
974
876
  }
975
877
  }
976
878
  function toDriver(variableData, param) {
@@ -996,9 +898,7 @@ function writeCollectFunction(writer, relation, columns, capitalizedName, result
996
898
  const relationType = (0, code_generator_1.generateRelationType)(capitalizedName, relation.name);
997
899
  const collectFunctionName = `collect${relationType}`;
998
900
  writer.blankLine();
999
- writer
1000
- .write(`function ${collectFunctionName}(selectResult: ${resultTypeName}[]): ${relationType}[]`)
1001
- .block(() => {
901
+ writer.write(`function ${collectFunctionName}(selectResult: ${resultTypeName}[]): ${relationType}[]`).block(() => {
1002
902
  const groupBy = columns[relation.groupIndex].name;
1003
903
  writer.writeLine(`const grouped = groupBy(selectResult.filter(r => r.${groupBy} != null), r => r.${groupBy});`);
1004
904
  writer