remult 0.25.8 → 0.26.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.
Files changed (102) hide show
  1. package/README.md +194 -129
  2. package/esm/graphql.js +131 -59
  3. package/esm/index.js +1 -1
  4. package/esm/migrations/compare-migration-snapshots.js +61 -0
  5. package/esm/migrations/generate-migrations.js +68 -0
  6. package/esm/migrations/index.js +2 -0
  7. package/esm/migrations/migrate.js +90 -0
  8. package/esm/migrations/migration-types.js +36 -0
  9. package/esm/migrations/update-migrations-ts-file.js +115 -0
  10. package/esm/postgres/postgres-data-provider.js +24 -3
  11. package/esm/postgres/schema-builder.js +35 -26
  12. package/esm/remult-better-sqlite3.js +47 -0
  13. package/esm/remult-bun-sqlite.js +41 -0
  14. package/esm/remult-knex/index.js +180 -51
  15. package/esm/remult-mongo.js +2 -2
  16. package/esm/remult-sql-js.js +3 -78
  17. package/esm/remult-sqlite-core.js +104 -0
  18. package/esm/server/expressBridge.js +130 -29
  19. package/esm/server/initAsyncHooks.js +75 -1
  20. package/esm/src/CompoundIdField.js +8 -2
  21. package/esm/src/context.js +20 -24
  22. package/esm/src/data-api.js +71 -30
  23. package/esm/src/data-providers/rest-data-provider.js +17 -15
  24. package/esm/src/data-providers/sql-database.js +32 -7
  25. package/esm/src/data-providers/web-sql-data-provider.js +1 -9
  26. package/esm/src/filter/filter-consumer-bridge-to-sql-request.js +37 -11
  27. package/esm/src/filter/filter-interfaces.js +229 -17
  28. package/esm/src/isOfType.js +9 -0
  29. package/esm/src/remult3/Fields.js +70 -18
  30. package/esm/src/remult3/RepositoryImplementation.js +47 -21
  31. package/esm/src/remult3/addValidator.js +15 -0
  32. package/esm/src/remult3/remult3.js +28 -57
  33. package/esm/src/server-action.js +4 -4
  34. package/esm/src/validators.js +6 -3
  35. package/esm/src/valueConverters.js +1 -1
  36. package/graphql.js +156 -71
  37. package/index.d.ts +4 -4
  38. package/index.js +2 -1
  39. package/migrations/compare-migration-snapshots.d.ts +27 -0
  40. package/migrations/compare-migration-snapshots.js +165 -0
  41. package/migrations/generate-migrations.d.ts +21 -0
  42. package/migrations/generate-migrations.js +104 -0
  43. package/migrations/index.d.ts +3 -0
  44. package/migrations/index.js +7 -0
  45. package/migrations/migrate.d.ts +27 -0
  46. package/migrations/migrate.js +175 -0
  47. package/migrations/migration-types.d.ts +48 -0
  48. package/migrations/migration-types.js +88 -0
  49. package/migrations/update-migrations-ts-file.d.ts +1 -0
  50. package/migrations/update-migrations-ts-file.js +141 -0
  51. package/package.json +21 -1
  52. package/postgres/postgres-data-provider.d.ts +8 -3
  53. package/postgres/postgres-data-provider.js +46 -3
  54. package/postgres/schema-builder.js +81 -48
  55. package/remult-better-sqlite3.d.ts +12 -0
  56. package/remult-better-sqlite3.js +64 -0
  57. package/remult-bun-sqlite.d.ts +11 -0
  58. package/remult-bun-sqlite.js +56 -0
  59. package/remult-knex/index.d.ts +12 -5
  60. package/remult-knex/index.js +257 -90
  61. package/remult-mongo.d.ts +2 -2
  62. package/remult-mongo.js +2 -2
  63. package/remult-sql-js.d.ts +2 -15
  64. package/remult-sql-js.js +11 -152
  65. package/remult-sqlite-core.d.ts +21 -0
  66. package/remult-sqlite-core.js +217 -0
  67. package/server/expressBridge.d.ts +30 -1
  68. package/server/expressBridge.js +140 -32
  69. package/server/initAsyncHooks.d.ts +16 -0
  70. package/server/initAsyncHooks.js +128 -2
  71. package/src/CompoundIdField.js +8 -2
  72. package/src/column-interfaces.d.ts +9 -3
  73. package/src/context.d.ts +8 -6
  74. package/src/context.js +27 -36
  75. package/src/data-api.d.ts +13 -9
  76. package/src/data-api.js +184 -135
  77. package/src/data-providers/rest-data-provider.js +26 -15
  78. package/src/data-providers/sql-database.d.ts +17 -5
  79. package/src/data-providers/sql-database.js +31 -7
  80. package/src/data-providers/web-sql-data-provider.d.ts +1 -2
  81. package/src/data-providers/web-sql-data-provider.js +4 -8
  82. package/src/entity.d.ts +64 -6
  83. package/src/filter/filter-consumer-bridge-to-sql-request.d.ts +31 -7
  84. package/src/filter/filter-consumer-bridge-to-sql-request.js +46 -21
  85. package/src/filter/filter-interfaces.d.ts +106 -39
  86. package/src/filter/filter-interfaces.js +312 -32
  87. package/src/isOfType.d.ts +2 -0
  88. package/src/isOfType.js +14 -0
  89. package/src/remult3/Fields.d.ts +36 -0
  90. package/src/remult3/Fields.js +81 -19
  91. package/src/remult3/RepositoryImplementation.d.ts +7 -3
  92. package/src/remult3/RepositoryImplementation.js +106 -71
  93. package/src/remult3/addValidator.d.ts +2 -0
  94. package/src/remult3/addValidator.js +20 -0
  95. package/src/remult3/remult3.d.ts +6 -2
  96. package/src/remult3/remult3.js +28 -57
  97. package/src/server-action.d.ts +1 -1
  98. package/src/server-action.js +5 -4
  99. package/src/sql-command.d.ts +6 -1
  100. package/src/validators.d.ts +1 -0
  101. package/src/validators.js +10 -4
  102. package/src/valueConverters.js +1 -1
@@ -0,0 +1,141 @@
1
+ "use strict";
2
+ // consider replacing with https://www.npmjs.com/package/ts-morph
3
+ Object.defineProperty(exports, "__esModule", { value: true });
4
+ exports.updateMigrationsFile = void 0;
5
+ var tslib_1 = require("tslib");
6
+ function updateMigrationsFile(filePath, steps) {
7
+ return tslib_1.__awaiter(this, void 0, void 0, function () {
8
+ function buildSteps() {
9
+ return steps.map(function (step) {
10
+ return ts.factory.createPropertyAssignment(ts.factory.createIdentifier((++maxStep).toString()), ts.factory.createIdentifier(step));
11
+ });
12
+ }
13
+ var ts, fs, maxStep, importPath, importName, sourceFile, migrationsExportFound, updatedSourceFile, importFound_1, newMigrationsExport, code, prettier, err_1, options, err_2;
14
+ return tslib_1.__generator(this, function (_a) {
15
+ switch (_a.label) {
16
+ case 0: return [4 /*yield*/, Promise.resolve().then(function () { return tslib_1.__importStar(require('typescript')); })];
17
+ case 1:
18
+ ts = _a.sent();
19
+ //@ts-ignore
20
+ if (ts.default)
21
+ ts = ts.default;
22
+ return [4 /*yield*/, Promise.resolve().then(function () { return tslib_1.__importStar(require('fs')); })];
23
+ case 2:
24
+ fs = _a.sent();
25
+ maxStep = -1;
26
+ if (!fs.existsSync(filePath)) {
27
+ fs.writeFileSync(filePath, '');
28
+ }
29
+ importPath = 'remult/migrations';
30
+ importName = 'Migrations';
31
+ sourceFile = ts.createSourceFile(filePath, fs.readFileSync(filePath).toString(), ts.ScriptTarget.Latest, true);
32
+ migrationsExportFound = false;
33
+ updatedSourceFile = ts.transform(sourceFile, [
34
+ function (context) {
35
+ return function (node) {
36
+ var visitor = function (node) {
37
+ if (ts.isVariableStatement(node) &&
38
+ node.declarationList.declarations.length > 0) {
39
+ var declaration = node.declarationList.declarations[0];
40
+ if (ts.isIdentifier(declaration.name) &&
41
+ declaration.name.text === 'migrations' &&
42
+ declaration.initializer &&
43
+ ts.isObjectLiteralExpression(declaration.initializer)) {
44
+ migrationsExportFound = true;
45
+ declaration.initializer.properties.forEach(function (property) {
46
+ if (ts.isPropertyAssignment(property)) {
47
+ if (ts.isNumericLiteral(property.name)) {
48
+ var step = parseInt(property.name.text);
49
+ if (step > maxStep) {
50
+ maxStep = step;
51
+ }
52
+ }
53
+ }
54
+ });
55
+ var steps_1 = buildSteps();
56
+ // Found the "migrations" array
57
+ // Update the initializer with the new steps
58
+ var updatedInitializer = ts.factory.updateObjectLiteralExpression(declaration.initializer, tslib_1.__spreadArray(tslib_1.__spreadArray([], tslib_1.__read(declaration.initializer.properties), false), tslib_1.__read(steps_1), false));
59
+ // Update the declaration with the new initializer
60
+ var updatedDeclaration = ts.factory.updateVariableDeclaration(declaration, declaration.name, undefined, declaration.type, // Preserve the original type annotation if present
61
+ updatedInitializer);
62
+ return ts.factory.updateVariableStatement(node, node.modifiers, ts.factory.updateVariableDeclarationList(node.declarationList, [
63
+ updatedDeclaration,
64
+ ]));
65
+ }
66
+ }
67
+ return ts.visitEachChild(node, visitor, context);
68
+ };
69
+ return ts.visitNode(node, visitor);
70
+ };
71
+ },
72
+ ]).transformed[0];
73
+ if (!migrationsExportFound) {
74
+ importFound_1 = false;
75
+ // Check if the import statement already exists
76
+ sourceFile.statements.forEach(function (statement) {
77
+ var _a, _b;
78
+ if (ts.isImportDeclaration(statement) &&
79
+ ts.isStringLiteral(statement.moduleSpecifier) &&
80
+ statement.moduleSpecifier.text === "".concat(importPath)) {
81
+ (_b = (_a = statement.importClause) === null || _a === void 0 ? void 0 : _a.namedBindings) === null || _b === void 0 ? void 0 : _b.forEachChild(function (namedBinding) {
82
+ if (ts.isImportSpecifier(namedBinding) &&
83
+ namedBinding.name.text === importName) {
84
+ importFound_1 = true;
85
+ }
86
+ });
87
+ }
88
+ });
89
+ // If the import statement doesn't exist, create it
90
+ if (!importFound_1) {
91
+ updatedSourceFile = ts.factory.updateSourceFile(updatedSourceFile, tslib_1.__spreadArray([
92
+ ts.factory.createExpressionStatement(ts.factory.createIdentifier("import type { ".concat(importName, " } from '").concat(importPath, "'")))
93
+ ], tslib_1.__read(updatedSourceFile.statements), false));
94
+ }
95
+ newMigrationsExport = ts.factory.createVariableStatement([ts.factory.createModifier(ts.SyntaxKind.ExportKeyword)], ts.factory.createVariableDeclarationList([
96
+ ts.factory.createVariableDeclaration('migrations', undefined, // You can add a type annotation here if needed
97
+ ts.factory.createTypeReferenceNode(importName, undefined), ts.factory.createObjectLiteralExpression(buildSteps(), false)),
98
+ ], ts.NodeFlags.Const));
99
+ // Add the new export to the end of the file
100
+ updatedSourceFile = ts.factory.updateSourceFile(updatedSourceFile, tslib_1.__spreadArray(tslib_1.__spreadArray([], tslib_1.__read(updatedSourceFile.statements), false), [
101
+ newMigrationsExport,
102
+ ], false));
103
+ }
104
+ code = ts.createPrinter().printFile(updatedSourceFile);
105
+ _a.label = 3;
106
+ case 3:
107
+ _a.trys.push([3, 11, , 12]);
108
+ prettier = void 0;
109
+ _a.label = 4;
110
+ case 4:
111
+ _a.trys.push([4, 6, , 7]);
112
+ return [4 /*yield*/, Promise.resolve("".concat('prettier' + '')).then(function (s) { return tslib_1.__importStar(require(s)); })];
113
+ case 5:
114
+ prettier = _a.sent();
115
+ return [3 /*break*/, 7];
116
+ case 6:
117
+ err_1 = _a.sent();
118
+ console.warn('error importing prettier,for better formatting run `npm i -D prettier`. Writing raw code to file.');
119
+ return [3 /*break*/, 7];
120
+ case 7:
121
+ if (!prettier) return [3 /*break*/, 10];
122
+ return [4 /*yield*/, prettier.resolveConfig()];
123
+ case 8:
124
+ options = _a.sent();
125
+ return [4 /*yield*/, prettier.format(code, tslib_1.__assign(tslib_1.__assign({}, options), { parser: 'typescript' }))];
126
+ case 9:
127
+ code = _a.sent();
128
+ _a.label = 10;
129
+ case 10: return [3 /*break*/, 12];
130
+ case 11:
131
+ err_2 = _a.sent();
132
+ console.log('error formatting code, writing raw code to file.');
133
+ return [3 /*break*/, 12];
134
+ case 12:
135
+ fs.writeFileSync(filePath, code);
136
+ return [2 /*return*/];
137
+ }
138
+ });
139
+ });
140
+ }
141
+ exports.updateMigrationsFile = updateMigrationsFile;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "remult",
3
- "version": "0.25.8",
3
+ "version": "0.26.0",
4
4
  "description": "A CRUD framework for full-stack TypeScript",
5
5
  "homepage": "https://remult.dev",
6
6
  "repository": {
@@ -101,6 +101,26 @@
101
101
  "import": "./esm/remult-sql-js.js",
102
102
  "types": "./remult-sql-js.d.ts"
103
103
  },
104
+ "./remult-sqlite-core-js": {
105
+ "require": "./remult-sqlite-core.js",
106
+ "import": "./esm/remult-sqlite-core.js",
107
+ "types": "./remult-sqlite-core.d.ts"
108
+ },
109
+ "./remult-better-sqlite3": {
110
+ "require": "./remult-better-sqlite3.js",
111
+ "import": "./esm/remult-better-sqlite3.js",
112
+ "types": "./remult-better-sqlite3.d.ts"
113
+ },
114
+ "./remult-bun-sqlite": {
115
+ "require": "./remult-bun-sqlite.js",
116
+ "import": "./esm/remult-bun-sqlite.js",
117
+ "types": "./remult-bun-sqlite.d.ts"
118
+ },
119
+ "./migrations": {
120
+ "require": "./migrations/index.js",
121
+ "import": "./esm/migrations/index.js",
122
+ "types": "./migrations/index.d.ts"
123
+ },
104
124
  "./ably": {
105
125
  "require": "./ably.js",
106
126
  "import": "./esm/ably.js",
@@ -1,19 +1,21 @@
1
1
  import type { ClientBase, PoolConfig, QueryResult } from 'pg';
2
- import { Remult } from '../src/context.js';
3
2
  import { SqlDatabase } from '../src/data-providers/sql-database.js';
4
3
  import type { EntityMetadata } from '../src/remult3/remult3.js';
5
4
  import type { SqlCommand, SqlImplementation } from '../src/sql-command.js';
5
+ import type { CanBuildMigrations, MigrationBuilder, MigrationCode } from '../migrations/migration-types.js';
6
+ import type { DataProvider } from '../index.js';
6
7
  export interface PostgresPool extends PostgresCommandSource {
7
8
  connect(): Promise<PostgresClient>;
9
+ end(): Promise<void>;
8
10
  }
9
11
  export interface PostgresClient extends PostgresCommandSource {
10
12
  release(): void;
11
13
  }
12
- export declare class PostgresDataProvider implements SqlImplementation {
14
+ export declare class PostgresDataProvider implements SqlImplementation, CanBuildMigrations {
13
15
  private pool;
14
16
  private options?;
15
17
  supportsJsonColumnType: boolean;
16
- static getDb(remult?: Remult): ClientBase;
18
+ static getDb(dataProvider?: DataProvider): ClientBase;
17
19
  entityIsUsedForTheFirstTime(entity: EntityMetadata): Promise<void>;
18
20
  getLimitSqlSyntax(limit: number, offset: number): string;
19
21
  createCommand(): SqlCommand;
@@ -23,6 +25,8 @@ export declare class PostgresDataProvider implements SqlImplementation {
23
25
  schema?: string;
24
26
  orderByNullsFirst?: boolean;
25
27
  });
28
+ end(): Promise<void>;
29
+ provideMigrationBuilder(builder: MigrationCode): MigrationBuilder;
26
30
  wrapIdentifier: (name: any) => any;
27
31
  ensureSchema(entities: EntityMetadata<any>[]): Promise<void>;
28
32
  orderByNullsFirst?: boolean;
@@ -48,4 +52,5 @@ export declare class PostgresSchemaWrapper implements PostgresPool {
48
52
  constructor(pool: PostgresPool, schema: string);
49
53
  connect(): Promise<PostgresClient>;
50
54
  query(queryText: string, values?: any[]): Promise<QueryResult>;
55
+ end(): Promise<void>;
51
56
  }
@@ -2,6 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.PostgresSchemaWrapper = exports.preparePostgresQueueStorage = exports.createPostgresDataProvider = exports.createPostgresConnection = exports.PostgresDataProvider = void 0;
4
4
  var tslib_1 = require("tslib");
5
+ var remult_proxy_js_1 = require("../src/remult-proxy.js");
5
6
  var pg_1 = tslib_1.__importDefault(require("pg"));
6
7
  var Pool = pg_1.default.Pool;
7
8
  var context_js_1 = require("../src/context.js");
@@ -30,9 +31,11 @@ var PostgresDataProvider = /** @class */ (function () {
30
31
  this.pool = new PostgresSchemaWrapper(pool, options.schema);
31
32
  }
32
33
  }
33
- PostgresDataProvider.getDb = function (remult) {
34
- var sql = sql_database_js_1.SqlDatabase.getDb(remult);
35
- var me = sql._getSourceSql();
34
+ PostgresDataProvider.getDb = function (dataProvider) {
35
+ var r = (dataProvider || remult_proxy_js_1.remult.dataProvider);
36
+ if (!r._getSourceSql)
37
+ throw 'the data provider is not an SqlDatabase';
38
+ var me = r._getSourceSql();
36
39
  if (!me.pool) {
37
40
  throw 'the data provider is not a PostgresDataProvider';
38
41
  }
@@ -49,6 +52,43 @@ var PostgresDataProvider = /** @class */ (function () {
49
52
  PostgresDataProvider.prototype.createCommand = function () {
50
53
  return new PostgresBridgeToSQLCommand(this.pool);
51
54
  };
55
+ PostgresDataProvider.prototype.end = function () {
56
+ return this.pool.end();
57
+ };
58
+ PostgresDataProvider.prototype.provideMigrationBuilder = function (builder) {
59
+ var _this = this;
60
+ var _a;
61
+ var db = new sql_database_js_1.SqlDatabase(this);
62
+ var sb = new schema_builder_js_1.PostgresSchemaBuilder(db, (_a = this.options) === null || _a === void 0 ? void 0 : _a.schema);
63
+ return {
64
+ addColumn: function (meta, field) { return tslib_1.__awaiter(_this, void 0, void 0, function () {
65
+ var _a, _b;
66
+ return tslib_1.__generator(this, function (_c) {
67
+ switch (_c.label) {
68
+ case 0:
69
+ _b = (_a = builder).addSql;
70
+ return [4 /*yield*/, sb.getAddColumnScript(meta, field)];
71
+ case 1:
72
+ _b.apply(_a, [_c.sent()]);
73
+ return [2 /*return*/];
74
+ }
75
+ });
76
+ }); },
77
+ createTable: function (meta) { return tslib_1.__awaiter(_this, void 0, void 0, function () {
78
+ var _a, _b;
79
+ return tslib_1.__generator(this, function (_c) {
80
+ switch (_c.label) {
81
+ case 0:
82
+ _b = (_a = builder).addSql;
83
+ return [4 /*yield*/, sb.createTableScript(meta)];
84
+ case 1:
85
+ _b.apply(_a, [_c.sent()]);
86
+ return [2 /*return*/];
87
+ }
88
+ });
89
+ }); },
90
+ };
91
+ };
52
92
  PostgresDataProvider.prototype.ensureSchema = function (entities) {
53
93
  var _a;
54
94
  return tslib_1.__awaiter(this, void 0, void 0, function () {
@@ -265,6 +305,9 @@ var PostgresSchemaWrapper = /** @class */ (function () {
265
305
  });
266
306
  });
267
307
  };
308
+ PostgresSchemaWrapper.prototype.end = function () {
309
+ return this.pool.end();
310
+ };
268
311
  return PostgresSchemaWrapper;
269
312
  }());
270
313
  exports.PostgresSchemaWrapper = PostgresSchemaWrapper;
@@ -23,7 +23,7 @@ function postgresColumnSyntax(x, dbName) {
23
23
  if (x.valueConverter == valueConverters_js_1.ValueConverters.DateOnly)
24
24
  result += ' date';
25
25
  else
26
- result += ' timestamp';
26
+ result += ' timestamptz';
27
27
  else
28
28
  result += ' ' + x.valueConverter.fieldTypeInDb;
29
29
  }
@@ -176,8 +176,7 @@ var PostgresSchemaBuilder = /** @class */ (function () {
176
176
  _b.label = 3;
177
177
  case 3:
178
178
  _b.trys.push([3, 7, , 8]);
179
- if (!!entity.options.sqlExpression) return [3 /*break*/, 6];
180
- if (!(e.$entityName.toLowerCase().indexOf('from ') < 0)) return [3 /*break*/, 6];
179
+ if (!(0, filter_consumer_bridge_to_sql_request_js_1.shouldCreateEntity)(entity, e)) return [3 /*break*/, 6];
181
180
  return [4 /*yield*/, this.createIfNotExist(entity)];
182
181
  case 4:
183
182
  _b.sent();
@@ -224,45 +223,21 @@ var PostgresSchemaBuilder = /** @class */ (function () {
224
223
  .execute("SELECT 1 FROM information_Schema.tables WHERE " +
225
224
  "".concat(this.whereTableAndSchema(c, e)))
226
225
  .then(function (r) { return tslib_1.__awaiter(_this, void 0, void 0, function () {
227
- var result, _a, _b, x, sql;
228
- var e_3, _c;
229
- return tslib_1.__generator(this, function (_d) {
230
- switch (_d.label) {
226
+ var sql;
227
+ return tslib_1.__generator(this, function (_a) {
228
+ switch (_a.label) {
231
229
  case 0:
232
- if (!(r.rows.length == 0)) return [3 /*break*/, 2];
233
- result = '';
234
- try {
235
- for (_a = tslib_1.__values(entity.fields), _b = _a.next(); !_b.done; _b = _a.next()) {
236
- x = _b.value;
237
- if (!(0, filter_consumer_bridge_to_sql_request_js_1.shouldNotCreateField)(x, e) || (0, RepositoryImplementation_js_1.isAutoIncrement)(x)) {
238
- if (result.length != 0)
239
- result += ',';
240
- result += '\r\n ';
241
- if ((0, RepositoryImplementation_js_1.isAutoIncrement)(x))
242
- result += e.$dbNameOf(x) + ' serial';
243
- else {
244
- result += postgresColumnSyntax(x, e.$dbNameOf(x));
245
- }
246
- if (x == entity.idMetadata.field)
247
- result += ' primary key';
248
- }
249
- }
250
- }
251
- catch (e_3_1) { e_3 = { error: e_3_1 }; }
252
- finally {
253
- try {
254
- if (_b && !_b.done && (_c = _a.return)) _c.call(_a);
255
- }
256
- finally { if (e_3) throw e_3.error; }
257
- }
258
- sql = "CREATE SCHEMA IF NOT EXISTS ".concat(this.schemaOnly(e), ";\nCREATE table ").concat(this.schemaAndName(e), " (").concat(result, "\r\n)");
230
+ if (!(r.rows.length == 0)) return [3 /*break*/, 3];
231
+ return [4 /*yield*/, this.createTableScript(entity)];
232
+ case 1:
233
+ sql = _a.sent();
259
234
  if (PostgresSchemaBuilder.logToConsole)
260
235
  console.info(sql);
261
236
  return [4 /*yield*/, this.pool.execute(sql)];
262
- case 1:
263
- _d.sent();
264
- _d.label = 2;
265
- case 2: return [2 /*return*/];
237
+ case 2:
238
+ _a.sent();
239
+ _a.label = 3;
240
+ case 3: return [2 /*return*/];
266
241
  }
267
242
  });
268
243
  }); })];
@@ -273,6 +248,63 @@ var PostgresSchemaBuilder = /** @class */ (function () {
273
248
  });
274
249
  });
275
250
  };
251
+ /* @internal*/
252
+ PostgresSchemaBuilder.prototype.getAddColumnScript = function (entity, field) {
253
+ return tslib_1.__awaiter(this, void 0, void 0, function () {
254
+ var e;
255
+ return tslib_1.__generator(this, function (_a) {
256
+ switch (_a.label) {
257
+ case 0: return [4 /*yield*/, (0, filter_consumer_bridge_to_sql_request_js_1.dbNamesOf)(entity, this.pool.wrapIdentifier)];
258
+ case 1:
259
+ e = _a.sent();
260
+ return [2 /*return*/, ("ALTER table ".concat(this.schemaAndName(e), " ") +
261
+ "ADD column ".concat(postgresColumnSyntax(field, e.$dbNameOf(field))))];
262
+ }
263
+ });
264
+ });
265
+ };
266
+ /* @internal*/
267
+ PostgresSchemaBuilder.prototype.createTableScript = function (entity) {
268
+ return tslib_1.__awaiter(this, void 0, void 0, function () {
269
+ var result, e, _a, _b, x, sql;
270
+ var e_3, _c;
271
+ return tslib_1.__generator(this, function (_d) {
272
+ switch (_d.label) {
273
+ case 0:
274
+ result = '';
275
+ return [4 /*yield*/, (0, filter_consumer_bridge_to_sql_request_js_1.dbNamesOf)(entity, this.pool.wrapIdentifier)];
276
+ case 1:
277
+ e = _d.sent();
278
+ try {
279
+ for (_a = tslib_1.__values(entity.fields), _b = _a.next(); !_b.done; _b = _a.next()) {
280
+ x = _b.value;
281
+ if (!(0, filter_consumer_bridge_to_sql_request_js_1.shouldNotCreateField)(x, e) || (0, RepositoryImplementation_js_1.isAutoIncrement)(x)) {
282
+ if (result.length != 0)
283
+ result += ',';
284
+ result += '\r\n ';
285
+ if ((0, RepositoryImplementation_js_1.isAutoIncrement)(x))
286
+ result += e.$dbNameOf(x) + ' serial';
287
+ else {
288
+ result += postgresColumnSyntax(x, e.$dbNameOf(x));
289
+ }
290
+ if (x == entity.idMetadata.field)
291
+ result += ' primary key';
292
+ }
293
+ }
294
+ }
295
+ catch (e_3_1) { e_3 = { error: e_3_1 }; }
296
+ finally {
297
+ try {
298
+ if (_b && !_b.done && (_c = _a.return)) _c.call(_a);
299
+ }
300
+ finally { if (e_3) throw e_3.error; }
301
+ }
302
+ sql = "CREATE SCHEMA IF NOT EXISTS ".concat(this.schemaOnly(e), ";\nCREATE table ").concat(this.schemaAndName(e), " (").concat(result, "\r\n)");
303
+ return [2 /*return*/, sql];
304
+ }
305
+ });
306
+ });
307
+ };
276
308
  PostgresSchemaBuilder.prototype.addColumnIfNotExist = function (entity, c) {
277
309
  return tslib_1.__awaiter(this, void 0, void 0, function () {
278
310
  var e, cmd, colName, sql, err_2;
@@ -285,28 +317,29 @@ var PostgresSchemaBuilder = /** @class */ (function () {
285
317
  return [2 /*return*/];
286
318
  _a.label = 2;
287
319
  case 2:
288
- _a.trys.push([2, 6, , 7]);
320
+ _a.trys.push([2, 7, , 8]);
289
321
  cmd = this.pool.createCommand();
290
322
  colName = e.$dbNameOf(c(entity));
291
323
  return [4 /*yield*/, cmd.execute("SELECT 1 FROM information_schema.columns WHERE " +
292
324
  "".concat(this.whereTableAndSchema(cmd, e), " ") +
293
325
  "AND column_name=".concat(cmd.param(colName.toLocaleLowerCase())))];
294
326
  case 3:
295
- if (!((_a.sent()).rows.length == 0)) return [3 /*break*/, 5];
296
- sql = "ALTER table ".concat(this.schemaAndName(e), " ") +
297
- "ADD column ".concat(postgresColumnSyntax(c(entity), colName));
327
+ if (!((_a.sent()).rows.length == 0)) return [3 /*break*/, 6];
328
+ return [4 /*yield*/, this.getAddColumnScript(entity, c(entity))];
329
+ case 4:
330
+ sql = _a.sent();
298
331
  if (PostgresSchemaBuilder.logToConsole)
299
332
  console.info(sql);
300
333
  return [4 /*yield*/, this.pool.execute(sql)];
301
- case 4:
334
+ case 5:
302
335
  _a.sent();
303
- _a.label = 5;
304
- case 5: return [3 /*break*/, 7];
305
- case 6:
336
+ _a.label = 6;
337
+ case 6: return [3 /*break*/, 8];
338
+ case 7:
306
339
  err_2 = _a.sent();
307
340
  console.error(err_2);
308
- return [3 /*break*/, 7];
309
- case 7: return [2 /*return*/];
341
+ return [3 /*break*/, 8];
342
+ case 8: return [2 /*return*/];
310
343
  }
311
344
  });
312
345
  });
@@ -0,0 +1,12 @@
1
+ import type { Database } from 'better-sqlite3';
2
+ import { SqliteCoreDataProvider } from './remult-sqlite-core.js';
3
+ import type { SqlResult } from './src/sql-command.js';
4
+ export declare class BetterSqlite3DataProvider extends SqliteCoreDataProvider {
5
+ constructor(db: Database);
6
+ }
7
+ export declare class BetterSqlite3SqlResult implements SqlResult {
8
+ private result;
9
+ constructor(result: any[]);
10
+ rows: any[];
11
+ getColumnKeyInResultForIndexInSelect(index: number): string;
12
+ }
@@ -0,0 +1,64 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.BetterSqlite3SqlResult = exports.BetterSqlite3DataProvider = void 0;
4
+ var tslib_1 = require("tslib");
5
+ var remult_sqlite_core_js_1 = require("./remult-sqlite-core.js");
6
+ var BetterSqlite3DataProvider = /** @class */ (function (_super) {
7
+ tslib_1.__extends(BetterSqlite3DataProvider, _super);
8
+ function BetterSqlite3DataProvider(db) {
9
+ var _this = _super.call(this, function () { return new BetterSqlite3Command(db); }, function () { return tslib_1.__awaiter(_this, void 0, void 0, function () { return tslib_1.__generator(this, function (_a) {
10
+ db.close();
11
+ return [2 /*return*/];
12
+ }); }); }) || this;
13
+ return _this;
14
+ }
15
+ return BetterSqlite3DataProvider;
16
+ }(remult_sqlite_core_js_1.SqliteCoreDataProvider));
17
+ exports.BetterSqlite3DataProvider = BetterSqlite3DataProvider;
18
+ var BetterSqlite3Command = /** @class */ (function () {
19
+ function BetterSqlite3Command(db) {
20
+ this.db = db;
21
+ this.values = {};
22
+ this.i = 0;
23
+ }
24
+ BetterSqlite3Command.prototype.execute = function (sql) {
25
+ return tslib_1.__awaiter(this, void 0, void 0, function () {
26
+ var s, result;
27
+ return tslib_1.__generator(this, function (_a) {
28
+ s = this.db.prepare(sql);
29
+ if (s.reader) {
30
+ return [2 /*return*/, new BetterSqlite3SqlResult(s.all(this.values))];
31
+ }
32
+ else {
33
+ result = s.run(this.values);
34
+ return [2 /*return*/, new BetterSqlite3SqlResult([])];
35
+ }
36
+ return [2 /*return*/];
37
+ });
38
+ });
39
+ };
40
+ BetterSqlite3Command.prototype.addParameterAndReturnSqlToken = function (val) {
41
+ return this.param(val);
42
+ };
43
+ BetterSqlite3Command.prototype.param = function (val) {
44
+ if (val instanceof Date)
45
+ val = val.valueOf();
46
+ if (typeof val === "boolean")
47
+ val = val ? 1 : 0;
48
+ var key = ':' + (this.i++);
49
+ this.values[key.substring(1)] = (val);
50
+ return key;
51
+ };
52
+ return BetterSqlite3Command;
53
+ }());
54
+ var BetterSqlite3SqlResult = /** @class */ (function () {
55
+ function BetterSqlite3SqlResult(result) {
56
+ this.result = result;
57
+ this.rows = result;
58
+ }
59
+ BetterSqlite3SqlResult.prototype.getColumnKeyInResultForIndexInSelect = function (index) {
60
+ return Object.keys(this.result[0])[index];
61
+ };
62
+ return BetterSqlite3SqlResult;
63
+ }());
64
+ exports.BetterSqlite3SqlResult = BetterSqlite3SqlResult;
@@ -0,0 +1,11 @@
1
+ import { SqliteCoreDataProvider } from "./remult-sqlite-core.js";
2
+ type Database = {
3
+ close(): void;
4
+ query(sql: string): {
5
+ all(args?: any): any[];
6
+ };
7
+ };
8
+ export declare class BunSqliteDataProvider extends SqliteCoreDataProvider {
9
+ constructor(db: Database);
10
+ }
11
+ export {};
@@ -0,0 +1,56 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.BunSqliteDataProvider = void 0;
4
+ var tslib_1 = require("tslib");
5
+ var remult_sqlite_core_js_1 = require("./remult-sqlite-core.js");
6
+ var BunSqliteDataProvider = /** @class */ (function (_super) {
7
+ tslib_1.__extends(BunSqliteDataProvider, _super);
8
+ function BunSqliteDataProvider(db) {
9
+ var _this = _super.call(this, function () { return new BunSqliteCommand(db); }, function () { return tslib_1.__awaiter(_this, void 0, void 0, function () { return tslib_1.__generator(this, function (_a) {
10
+ db.close();
11
+ return [2 /*return*/];
12
+ }); }); }) || this;
13
+ return _this;
14
+ }
15
+ return BunSqliteDataProvider;
16
+ }(remult_sqlite_core_js_1.SqliteCoreDataProvider));
17
+ exports.BunSqliteDataProvider = BunSqliteDataProvider;
18
+ var BunSqliteCommand = /** @class */ (function () {
19
+ function BunSqliteCommand(db) {
20
+ this.db = db;
21
+ this.values = {};
22
+ this.i = 0;
23
+ }
24
+ BunSqliteCommand.prototype.execute = function (sql) {
25
+ return tslib_1.__awaiter(this, void 0, void 0, function () {
26
+ var s;
27
+ return tslib_1.__generator(this, function (_a) {
28
+ s = this.db.query(sql);
29
+ return [2 /*return*/, new BunSqliteSqlResult(s.all(this.values))];
30
+ });
31
+ });
32
+ };
33
+ BunSqliteCommand.prototype.addParameterAndReturnSqlToken = function (val) {
34
+ return this.param(val);
35
+ };
36
+ BunSqliteCommand.prototype.param = function (val) {
37
+ if (val instanceof Date)
38
+ val = val.valueOf();
39
+ if (typeof val === "boolean")
40
+ val = val ? 1 : 0;
41
+ var key = ':' + (this.i++);
42
+ this.values[key] = (val);
43
+ return key;
44
+ };
45
+ return BunSqliteCommand;
46
+ }());
47
+ var BunSqliteSqlResult = /** @class */ (function () {
48
+ function BunSqliteSqlResult(result) {
49
+ this.result = result;
50
+ this.rows = result;
51
+ }
52
+ BunSqliteSqlResult.prototype.getColumnKeyInResultForIndexInSelect = function (index) {
53
+ return Object.keys(this.result[0])[index];
54
+ };
55
+ return BunSqliteSqlResult;
56
+ }());