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.
- package/README.md +194 -129
- package/esm/graphql.js +131 -59
- package/esm/index.js +1 -1
- package/esm/migrations/compare-migration-snapshots.js +61 -0
- package/esm/migrations/generate-migrations.js +68 -0
- package/esm/migrations/index.js +2 -0
- package/esm/migrations/migrate.js +90 -0
- package/esm/migrations/migration-types.js +36 -0
- package/esm/migrations/update-migrations-ts-file.js +115 -0
- package/esm/postgres/postgres-data-provider.js +24 -3
- package/esm/postgres/schema-builder.js +35 -26
- package/esm/remult-better-sqlite3.js +47 -0
- package/esm/remult-bun-sqlite.js +41 -0
- package/esm/remult-knex/index.js +180 -51
- package/esm/remult-mongo.js +2 -2
- package/esm/remult-sql-js.js +3 -78
- package/esm/remult-sqlite-core.js +104 -0
- package/esm/server/expressBridge.js +130 -29
- package/esm/server/initAsyncHooks.js +75 -1
- package/esm/src/CompoundIdField.js +8 -2
- package/esm/src/context.js +20 -24
- package/esm/src/data-api.js +71 -30
- package/esm/src/data-providers/rest-data-provider.js +17 -15
- package/esm/src/data-providers/sql-database.js +32 -7
- package/esm/src/data-providers/web-sql-data-provider.js +1 -9
- package/esm/src/filter/filter-consumer-bridge-to-sql-request.js +37 -11
- package/esm/src/filter/filter-interfaces.js +229 -17
- package/esm/src/isOfType.js +9 -0
- package/esm/src/remult3/Fields.js +70 -18
- package/esm/src/remult3/RepositoryImplementation.js +47 -21
- package/esm/src/remult3/addValidator.js +15 -0
- package/esm/src/remult3/remult3.js +28 -57
- package/esm/src/server-action.js +4 -4
- package/esm/src/validators.js +6 -3
- package/esm/src/valueConverters.js +1 -1
- package/graphql.js +156 -71
- package/index.d.ts +4 -4
- package/index.js +2 -1
- package/migrations/compare-migration-snapshots.d.ts +27 -0
- package/migrations/compare-migration-snapshots.js +165 -0
- package/migrations/generate-migrations.d.ts +21 -0
- package/migrations/generate-migrations.js +104 -0
- package/migrations/index.d.ts +3 -0
- package/migrations/index.js +7 -0
- package/migrations/migrate.d.ts +27 -0
- package/migrations/migrate.js +175 -0
- package/migrations/migration-types.d.ts +48 -0
- package/migrations/migration-types.js +88 -0
- package/migrations/update-migrations-ts-file.d.ts +1 -0
- package/migrations/update-migrations-ts-file.js +141 -0
- package/package.json +21 -1
- package/postgres/postgres-data-provider.d.ts +8 -3
- package/postgres/postgres-data-provider.js +46 -3
- package/postgres/schema-builder.js +81 -48
- package/remult-better-sqlite3.d.ts +12 -0
- package/remult-better-sqlite3.js +64 -0
- package/remult-bun-sqlite.d.ts +11 -0
- package/remult-bun-sqlite.js +56 -0
- package/remult-knex/index.d.ts +12 -5
- package/remult-knex/index.js +257 -90
- package/remult-mongo.d.ts +2 -2
- package/remult-mongo.js +2 -2
- package/remult-sql-js.d.ts +2 -15
- package/remult-sql-js.js +11 -152
- package/remult-sqlite-core.d.ts +21 -0
- package/remult-sqlite-core.js +217 -0
- package/server/expressBridge.d.ts +30 -1
- package/server/expressBridge.js +140 -32
- package/server/initAsyncHooks.d.ts +16 -0
- package/server/initAsyncHooks.js +128 -2
- package/src/CompoundIdField.js +8 -2
- package/src/column-interfaces.d.ts +9 -3
- package/src/context.d.ts +8 -6
- package/src/context.js +27 -36
- package/src/data-api.d.ts +13 -9
- package/src/data-api.js +184 -135
- package/src/data-providers/rest-data-provider.js +26 -15
- package/src/data-providers/sql-database.d.ts +17 -5
- package/src/data-providers/sql-database.js +31 -7
- package/src/data-providers/web-sql-data-provider.d.ts +1 -2
- package/src/data-providers/web-sql-data-provider.js +4 -8
- package/src/entity.d.ts +64 -6
- package/src/filter/filter-consumer-bridge-to-sql-request.d.ts +31 -7
- package/src/filter/filter-consumer-bridge-to-sql-request.js +46 -21
- package/src/filter/filter-interfaces.d.ts +106 -39
- package/src/filter/filter-interfaces.js +312 -32
- package/src/isOfType.d.ts +2 -0
- package/src/isOfType.js +14 -0
- package/src/remult3/Fields.d.ts +36 -0
- package/src/remult3/Fields.js +81 -19
- package/src/remult3/RepositoryImplementation.d.ts +7 -3
- package/src/remult3/RepositoryImplementation.js +106 -71
- package/src/remult3/addValidator.d.ts +2 -0
- package/src/remult3/addValidator.js +20 -0
- package/src/remult3/remult3.d.ts +6 -2
- package/src/remult3/remult3.js +28 -57
- package/src/server-action.d.ts +1 -1
- package/src/server-action.js +5 -4
- package/src/sql-command.d.ts +6 -1
- package/src/validators.d.ts +1 -0
- package/src/validators.js +10 -4
- 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.
|
|
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(
|
|
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 (
|
|
34
|
-
var
|
|
35
|
-
|
|
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 += '
|
|
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 (
|
|
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
|
|
228
|
-
|
|
229
|
-
|
|
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*/,
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
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
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
case
|
|
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,
|
|
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*/,
|
|
296
|
-
|
|
297
|
-
|
|
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
|
|
334
|
+
case 5:
|
|
302
335
|
_a.sent();
|
|
303
|
-
_a.label =
|
|
304
|
-
case
|
|
305
|
-
case
|
|
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*/,
|
|
309
|
-
case
|
|
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
|
+
}());
|