remult 0.20.3 → 0.20.5
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/esm/postgres/schema-builder.js +7 -3
- package/esm/remult-mongo.d.ts +11 -3
- package/esm/remult-mongo.js +21 -11
- package/esm/server/expressBridge.js +1 -10
- package/esm/src/backend-tests/backend-database-test-setup.backend-spec.d.ts +6 -0
- package/esm/src/backend-tests/backend-database-test-setup.backend-spec.js +147 -0
- package/esm/src/remult3/RepositoryImplementation.js +2 -2
- package/esm/src/shared-tests/db-tests-setup.d.ts +1 -0
- package/esm/src/shared-tests/db-tests-setup.js +3 -2
- package/esm/src/shared-tests/db-tests.db-tests.js +9 -3
- package/package.json +1 -1
- package/postgres/getEntityTypescriptPostgres.d.ts +1 -0
- package/postgres/getEntityTypescriptPostgres.js +125 -0
- package/postgres/schema-builder.js +6 -3
- package/remult-mongo.d.ts +11 -3
- package/remult-mongo.js +26 -15
- package/server/expressBridge.js +1 -10
- package/src/backend-tests/backend-database-test-setup.backend-spec.d.ts +6 -0
- package/src/backend-tests/backend-database-test-setup.backend-spec.js +306 -0
- package/src/remult3/RepositoryImplementation.js +2 -2
- package/src/shared-tests/db-tests-setup.d.ts +1 -0
- package/src/shared-tests/db-tests-setup.js +3 -2
- package/src/shared-tests/db-tests.db-tests.js +39 -17
|
@@ -128,15 +128,19 @@ export class PostgresSchemaBuilder {
|
|
|
128
128
|
let e = await dbNamesOf(entity);
|
|
129
129
|
let cols = (await cmd.execute(`select column_name
|
|
130
130
|
FROM information_schema.columns
|
|
131
|
-
WHERE table_name=${cmd.addParameterAndReturnSqlToken((e.$entityName).toLocaleLowerCase())} ` + this.additionalWhere)).rows.map(x => x.column_name);
|
|
131
|
+
WHERE table_name=${cmd.addParameterAndReturnSqlToken((e.$entityName).toLocaleLowerCase())} ` + this.additionalWhere)).rows.map(x => x.column_name.toLocaleLowerCase());
|
|
132
132
|
for (const col of entity.fields) {
|
|
133
|
-
if (!isDbReadonly(col, e))
|
|
134
|
-
|
|
133
|
+
if (!isDbReadonly(col, e)) {
|
|
134
|
+
let colName = e.$dbNameOf(col).toLocaleLowerCase();
|
|
135
|
+
if (colName.startsWith('"') && colName.endsWith('"'))
|
|
136
|
+
colName = colName.substring(1, colName.length - 1);
|
|
137
|
+
if (!cols.includes(colName)) {
|
|
135
138
|
let sql = `alter table ${e.$entityName} add column ${postgresColumnSyntax(col, e.$dbNameOf(col))}`;
|
|
136
139
|
if (PostgresSchemaBuilder.logToConsole)
|
|
137
140
|
console.info(sql);
|
|
138
141
|
await this.pool.execute(sql);
|
|
139
142
|
}
|
|
143
|
+
}
|
|
140
144
|
}
|
|
141
145
|
}
|
|
142
146
|
catch (err) {
|
package/esm/remult-mongo.d.ts
CHANGED
|
@@ -1,11 +1,19 @@
|
|
|
1
|
-
import type { MongoClient, Db } from 'mongodb';
|
|
1
|
+
import type { MongoClient, Db, ClientSession } from 'mongodb';
|
|
2
2
|
import { DataProvider, EntityDataProvider, EntityFilter, EntityMetadata, Remult } from '.';
|
|
3
3
|
import { RepositoryOverloads } from './src/remult3';
|
|
4
4
|
export declare class MongoDataProvider implements DataProvider {
|
|
5
5
|
private db;
|
|
6
6
|
private client;
|
|
7
|
-
constructor(db: Db, client: MongoClient
|
|
8
|
-
|
|
7
|
+
constructor(db: Db, client: MongoClient, options?: {
|
|
8
|
+
session?: ClientSession;
|
|
9
|
+
disableTransactions?: boolean;
|
|
10
|
+
});
|
|
11
|
+
session?: ClientSession;
|
|
12
|
+
disableTransactions: boolean;
|
|
13
|
+
static getDb(remult?: Remult): {
|
|
14
|
+
db: Db;
|
|
15
|
+
session: ClientSession;
|
|
16
|
+
};
|
|
9
17
|
getEntityDataProvider(entity: EntityMetadata<any>): EntityDataProvider;
|
|
10
18
|
transaction(action: (dataProvider: DataProvider) => Promise<void>): Promise<void>;
|
|
11
19
|
static filterToRaw<entityType>(entity: RepositoryOverloads<entityType>, condition: EntityFilter<entityType>): Promise<{
|
package/esm/remult-mongo.js
CHANGED
|
@@ -3,30 +3,37 @@ import { dbNamesOf } from './src/filter/filter-consumer-bridge-to-sql-request';
|
|
|
3
3
|
import { getRepository } from './src/remult3';
|
|
4
4
|
import { remult as remultContext } from './src/remult-proxy';
|
|
5
5
|
export class MongoDataProvider {
|
|
6
|
-
constructor(db, client) {
|
|
6
|
+
constructor(db, client, options) {
|
|
7
7
|
this.db = db;
|
|
8
8
|
this.client = client;
|
|
9
|
+
this.disableTransactions = false;
|
|
10
|
+
this.session = options?.session;
|
|
11
|
+
this.disableTransactions = options?.disableTransactions;
|
|
9
12
|
}
|
|
10
13
|
static getDb(remult) {
|
|
11
14
|
const r = (remult || remultContext).dataProvider;
|
|
12
15
|
if (!r.db)
|
|
13
16
|
throw "the data provider is not a MongoDataProvider";
|
|
14
|
-
return r.db;
|
|
17
|
+
return { db: r.db, session: r.session };
|
|
15
18
|
}
|
|
16
19
|
getEntityDataProvider(entity) {
|
|
17
|
-
return new MongoEntityDataProvider(this.db, entity);
|
|
20
|
+
return new MongoEntityDataProvider(this.db, entity, this.session);
|
|
18
21
|
}
|
|
19
22
|
async transaction(action) {
|
|
20
23
|
let session = await this.client.startSession();
|
|
21
24
|
session.startTransaction();
|
|
25
|
+
const db = this.client.db(this.db.databaseName);
|
|
22
26
|
try {
|
|
23
|
-
await action(new MongoDataProvider(
|
|
27
|
+
await action(new MongoDataProvider(db, undefined, { session }));
|
|
24
28
|
await session.commitTransaction();
|
|
25
29
|
}
|
|
26
30
|
catch (err) {
|
|
27
31
|
await session.abortTransaction();
|
|
28
32
|
throw err;
|
|
29
33
|
}
|
|
34
|
+
finally {
|
|
35
|
+
await session.endSession();
|
|
36
|
+
}
|
|
30
37
|
}
|
|
31
38
|
static async filterToRaw(entity, condition) {
|
|
32
39
|
const repo = getRepository(entity);
|
|
@@ -42,9 +49,10 @@ function isNull(x) {
|
|
|
42
49
|
return x?.$null === NULL.$null;
|
|
43
50
|
}
|
|
44
51
|
class MongoEntityDataProvider {
|
|
45
|
-
constructor(db, entity) {
|
|
52
|
+
constructor(db, entity, session) {
|
|
46
53
|
this.db = db;
|
|
47
54
|
this.entity = entity;
|
|
55
|
+
this.session = session;
|
|
48
56
|
}
|
|
49
57
|
translateFromJson(row, nameProvider) {
|
|
50
58
|
let result = {};
|
|
@@ -71,7 +79,7 @@ class MongoEntityDataProvider {
|
|
|
71
79
|
let x = new FilterConsumerBridgeToMongo(e);
|
|
72
80
|
where.__applyToConsumer(x);
|
|
73
81
|
let w = await x.resolveWhere();
|
|
74
|
-
return await collection.countDocuments(w);
|
|
82
|
+
return await collection.countDocuments(w, { session: this.session });
|
|
75
83
|
}
|
|
76
84
|
async find(options) {
|
|
77
85
|
let { collection, e } = await this.collection();
|
|
@@ -79,7 +87,9 @@ class MongoEntityDataProvider {
|
|
|
79
87
|
if (options?.where)
|
|
80
88
|
options.where.__applyToConsumer(x);
|
|
81
89
|
let where = await x.resolveWhere();
|
|
82
|
-
let op = {
|
|
90
|
+
let op = {
|
|
91
|
+
session: this.session
|
|
92
|
+
};
|
|
83
93
|
if (options.limit) {
|
|
84
94
|
op.limit = options.limit;
|
|
85
95
|
if (options.page) {
|
|
@@ -117,19 +127,19 @@ class MongoEntityDataProvider {
|
|
|
117
127
|
}
|
|
118
128
|
let r = await collection.updateOne(await f.resolveWhere(), {
|
|
119
129
|
$set: newR
|
|
120
|
-
});
|
|
130
|
+
}, { session: this.session });
|
|
121
131
|
return this.find({ where: Filter.fromEntityFilter(this.entity, resultFilter) }).then(y => y[0]);
|
|
122
132
|
}
|
|
123
133
|
async delete(id) {
|
|
124
134
|
const { e, collection } = await this.collection();
|
|
125
135
|
let f = new FilterConsumerBridgeToMongo(e);
|
|
126
136
|
Filter.fromEntityFilter(this.entity, this.entity.idMetadata.getIdFilter(id)).__applyToConsumer(f);
|
|
127
|
-
collection.deleteOne(await f.resolveWhere());
|
|
137
|
+
collection.deleteOne(await f.resolveWhere(), { session: this.session });
|
|
128
138
|
}
|
|
129
139
|
async insert(data) {
|
|
130
140
|
let { collection, e } = await this.collection();
|
|
131
|
-
let r = await collection.insertOne(await this.translateToJson(data, e));
|
|
132
|
-
return await this.translateFromJson(await collection.findOne({ _id: r.insertedId }), e);
|
|
141
|
+
let r = await collection.insertOne(await this.translateToJson(data, e), { session: this.session });
|
|
142
|
+
return await this.translateFromJson(await collection.findOne({ _id: r.insertedId }, { session: this.session }), e);
|
|
133
143
|
}
|
|
134
144
|
async collection() {
|
|
135
145
|
const e = await dbNamesOf(this.entity);
|
|
@@ -412,15 +412,7 @@ export class RemultServerImplementation {
|
|
|
412
412
|
parameters.push({
|
|
413
413
|
"name": f.key,
|
|
414
414
|
"in": "query",
|
|
415
|
-
"description": "filter equal to " + f.key,
|
|
416
|
-
"required": false,
|
|
417
|
-
"style": "simple",
|
|
418
|
-
type
|
|
419
|
-
});
|
|
420
|
-
parameters.push({
|
|
421
|
-
"name": f.key + "_ne",
|
|
422
|
-
"in": "query",
|
|
423
|
-
"description": "filter not equal to " + f.key,
|
|
415
|
+
"description": "filter equal to " + f.key + ". See [more filtering options](https://remult.dev/docs/rest-api.html#filter)",
|
|
424
416
|
"required": false,
|
|
425
417
|
"style": "simple",
|
|
426
418
|
type
|
|
@@ -431,7 +423,6 @@ export class RemultServerImplementation {
|
|
|
431
423
|
type: "object",
|
|
432
424
|
properties
|
|
433
425
|
};
|
|
434
|
-
//TODO - handle allow api read also on the get level
|
|
435
426
|
if (JSON.stringify(properties) !== JSON.stringify(mutationProperties)) {
|
|
436
427
|
mutationKey += 'Mutation';
|
|
437
428
|
spec.components.schemas[mutationKey] = {
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { dbTestWhatSignature } from "../shared-tests/db-tests-setup";
|
|
2
|
+
export declare const testKnexPGSqlImpl: (key: string, what: dbTestWhatSignature, focus?: boolean) => void;
|
|
3
|
+
export declare const mySqlTest: import("../shared-tests/db-tests-setup").dbTestMethodSignature;
|
|
4
|
+
export declare function testPostgresImplementation(key: string, what: dbTestWhatSignature, focus?: boolean): void;
|
|
5
|
+
export declare function testMongo(key: string, what: dbTestWhatSignature, focus?: boolean): void;
|
|
6
|
+
import '../shared-tests';
|
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
import { Remult } from "../context";
|
|
2
|
+
import { KnexDataProvider, KnexSchemaBuilder } from '../../remult-knex';
|
|
3
|
+
import * as Knex from 'knex';
|
|
4
|
+
import { MongoClient } from 'mongodb';
|
|
5
|
+
import { config } from 'dotenv';
|
|
6
|
+
import { createPostgresConnection, PostgresSchemaBuilder } from "../../postgres";
|
|
7
|
+
import { addDatabaseToTest, itWithFocus, testAll, TestDbs } from "../shared-tests/db-tests-setup";
|
|
8
|
+
import { describeClass } from "../remult3/DecoratorReplacer";
|
|
9
|
+
import { Entity, Fields } from "../remult3";
|
|
10
|
+
KnexSchemaBuilder.logToConsole = false;
|
|
11
|
+
PostgresSchemaBuilder.logToConsole = false;
|
|
12
|
+
config();
|
|
13
|
+
function testKnexSqlImpl(knex, name) {
|
|
14
|
+
return (key, what, focus = false) => {
|
|
15
|
+
itWithFocus(key + " " + name + " - knex", async () => {
|
|
16
|
+
let db = new KnexDataProvider(knex);
|
|
17
|
+
let remult = new Remult(db);
|
|
18
|
+
await what({
|
|
19
|
+
db, remult,
|
|
20
|
+
createEntity: async (entity) => {
|
|
21
|
+
let repo = remult.repo(entity);
|
|
22
|
+
await knex.schema.dropTableIfExists(await repo.metadata.getDbName());
|
|
23
|
+
await db.ensureSchema([repo.metadata]);
|
|
24
|
+
return repo;
|
|
25
|
+
}
|
|
26
|
+
});
|
|
27
|
+
}, focus);
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
export const testKnexPGSqlImpl = testKnexSqlImpl(Knex.default({
|
|
31
|
+
client: 'pg',
|
|
32
|
+
connection: process.env.DATABASE_URL,
|
|
33
|
+
}), "postgres");
|
|
34
|
+
addDatabaseToTest(testKnexPGSqlImpl);
|
|
35
|
+
if (process.env['TESTS_SQL_SERVER'])
|
|
36
|
+
addDatabaseToTest(testKnexSqlImpl(Knex.default({
|
|
37
|
+
client: 'mssql',
|
|
38
|
+
connection: {
|
|
39
|
+
server: '127.0.0.1',
|
|
40
|
+
database: 'test2',
|
|
41
|
+
user: 'sa',
|
|
42
|
+
password: 'MASTERKEY',
|
|
43
|
+
options: {
|
|
44
|
+
enableArithAbort: true,
|
|
45
|
+
encrypt: false,
|
|
46
|
+
instanceName: 'sqlexpress'
|
|
47
|
+
}
|
|
48
|
+
} //,debug: true
|
|
49
|
+
}), "sql server"));
|
|
50
|
+
if (true)
|
|
51
|
+
addDatabaseToTest(testKnexSqlImpl(Knex.default({
|
|
52
|
+
client: 'better-sqlite3',
|
|
53
|
+
connection: {
|
|
54
|
+
filename: ":memory:"
|
|
55
|
+
},
|
|
56
|
+
}), "sqlite3"));
|
|
57
|
+
export const mySqlTest = addDatabaseToTest(testKnexSqlImpl(Knex.default({
|
|
58
|
+
client: 'mysql2',
|
|
59
|
+
connection: {
|
|
60
|
+
user: 'root',
|
|
61
|
+
password: 'MASTERKEY',
|
|
62
|
+
host: '127.0.0.1',
|
|
63
|
+
database: 'test'
|
|
64
|
+
}
|
|
65
|
+
//debug: true
|
|
66
|
+
}), "mysql2"));
|
|
67
|
+
let pg = createPostgresConnection();
|
|
68
|
+
export function testPostgresImplementation(key, what, focus = false) {
|
|
69
|
+
itWithFocus(key + " - postgres", async () => {
|
|
70
|
+
let db = await pg;
|
|
71
|
+
let remult = new Remult(db);
|
|
72
|
+
await what({
|
|
73
|
+
db, remult,
|
|
74
|
+
createEntity: async (entity) => {
|
|
75
|
+
let repo = remult.repo(entity);
|
|
76
|
+
await db.execute("drop table if exists " + await repo.metadata.getDbName());
|
|
77
|
+
await db.ensureSchema([repo.metadata]);
|
|
78
|
+
return repo;
|
|
79
|
+
}
|
|
80
|
+
});
|
|
81
|
+
}, focus);
|
|
82
|
+
}
|
|
83
|
+
addDatabaseToTest(testPostgresImplementation);
|
|
84
|
+
import { Categories } from "../tests/remult-3-entities";
|
|
85
|
+
import { MongoDataProvider } from "../../remult-mongo";
|
|
86
|
+
testAll("transactions", async ({ db, createEntity }) => {
|
|
87
|
+
let x = await createEntity(Categories);
|
|
88
|
+
await db.transaction(async (db) => {
|
|
89
|
+
let remult = new Remult(db);
|
|
90
|
+
expect(await remult.repo(Categories).count()).toBe(0);
|
|
91
|
+
});
|
|
92
|
+
});
|
|
93
|
+
const mongoConnectionString = process.env['MONGO_TEST_URL']; //"mongodb://localhost:27017/local"
|
|
94
|
+
let client = new MongoClient(mongoConnectionString);
|
|
95
|
+
let done;
|
|
96
|
+
let mongoDbPromise = client.connect().then(c => {
|
|
97
|
+
done = c;
|
|
98
|
+
return c.db("test");
|
|
99
|
+
});
|
|
100
|
+
afterAll(async () => {
|
|
101
|
+
if (done)
|
|
102
|
+
done.close();
|
|
103
|
+
});
|
|
104
|
+
export function testMongo(key, what, focus = false) {
|
|
105
|
+
itWithFocus(key + " - mongo", async () => {
|
|
106
|
+
let mongoDb = await mongoDbPromise;
|
|
107
|
+
let db = new MongoDataProvider(mongoDb, client);
|
|
108
|
+
let remult = new Remult(db);
|
|
109
|
+
await what({
|
|
110
|
+
db, remult,
|
|
111
|
+
createEntity: async (entity) => {
|
|
112
|
+
let repo = remult.repo(entity);
|
|
113
|
+
await mongoDb.collection(await repo.metadata.getDbName()).deleteMany({});
|
|
114
|
+
return repo;
|
|
115
|
+
}
|
|
116
|
+
});
|
|
117
|
+
}, focus);
|
|
118
|
+
}
|
|
119
|
+
addDatabaseToTest(testMongo, TestDbs.mongo);
|
|
120
|
+
it("test mongo without transaction", async () => {
|
|
121
|
+
const client = new MongoClient("mongodb://localhost:27017/local");
|
|
122
|
+
await client.connect();
|
|
123
|
+
const mongoDb = client.db('test');
|
|
124
|
+
const db = new MongoDataProvider(mongoDb, client, { disableTransactions: true });
|
|
125
|
+
var remult = new Remult(db);
|
|
126
|
+
const entity = class {
|
|
127
|
+
constructor() {
|
|
128
|
+
this.id = 0;
|
|
129
|
+
this.title = '';
|
|
130
|
+
}
|
|
131
|
+
};
|
|
132
|
+
describeClass(entity, Entity("testNoTrans"), { id: Fields.number(), title: Fields.string() });
|
|
133
|
+
const repo = remult.repo(entity);
|
|
134
|
+
for (const item of await repo.find()) {
|
|
135
|
+
await repo.delete(item);
|
|
136
|
+
}
|
|
137
|
+
await repo.insert({ id: 1, title: "a" });
|
|
138
|
+
try {
|
|
139
|
+
await db.transaction(async () => {
|
|
140
|
+
await repo.insert({ id: 2, title: "b" });
|
|
141
|
+
throw new Error();
|
|
142
|
+
});
|
|
143
|
+
}
|
|
144
|
+
catch { }
|
|
145
|
+
expect(await repo.count()).toBe(2);
|
|
146
|
+
});
|
|
147
|
+
import '../shared-tests';
|
|
@@ -277,7 +277,7 @@ export class RepositoryImplementation {
|
|
|
277
277
|
result[col.key] = col.valueConverter.fromJson(row[col.key]);
|
|
278
278
|
}
|
|
279
279
|
return result;
|
|
280
|
-
}));
|
|
280
|
+
}), load);
|
|
281
281
|
}
|
|
282
282
|
async loadManyToOneForManyRows(rawRows, load) {
|
|
283
283
|
let loadFields = undefined;
|
|
@@ -1398,7 +1398,7 @@ class EntityFullInfo {
|
|
|
1398
1398
|
return !item ? this.remult.isAllowedForInstance(undefined, this.options.allowApiUpdate) : this.remult.repo(this.entityType).getEntityRef(item).apiUpdateAllowed;
|
|
1399
1399
|
}
|
|
1400
1400
|
get apiReadAllowed() {
|
|
1401
|
-
if (this.options.
|
|
1401
|
+
if (this.options.allowApiRead === undefined)
|
|
1402
1402
|
return true; //TODO2 - consider that this default may be confusing, since it's different from all others.
|
|
1403
1403
|
return this.remult.isAllowed(this.options.allowApiRead);
|
|
1404
1404
|
}
|
|
@@ -13,6 +13,7 @@ export declare function testInMemory(key: string, what: dbTestWhatSignature, foc
|
|
|
13
13
|
export declare const TestDbs: {
|
|
14
14
|
restDataProvider: string;
|
|
15
15
|
mongo: string;
|
|
16
|
+
inMemory: string;
|
|
16
17
|
};
|
|
17
18
|
export declare type dbTestWhatSignature = ((db: {
|
|
18
19
|
db: DataProvider;
|
|
@@ -40,9 +40,10 @@ export function testInMemory(key, what, focus = false) {
|
|
|
40
40
|
}
|
|
41
41
|
export const TestDbs = {
|
|
42
42
|
restDataProvider: "restDataProvider",
|
|
43
|
-
mongo: "mongo"
|
|
43
|
+
mongo: "mongo",
|
|
44
|
+
inMemory: 'in memory'
|
|
44
45
|
};
|
|
45
|
-
|
|
46
|
+
addDatabaseToTest(testInMemory, TestDbs.inMemory);
|
|
46
47
|
export async function testAllDbs(key, doTest, focus = false) {
|
|
47
48
|
testAll(key, async ({ remult, createEntity }) => {
|
|
48
49
|
let createData = async (doInsert, entity) => {
|
|
@@ -749,18 +749,24 @@ testAll("task with enum string", async ({ createEntity }) => {
|
|
|
749
749
|
expect(await r.count({ priority: PriorityWithString.Critical })).toBe(1);
|
|
750
750
|
expect(await r.count({ priority: PriorityWithString.Low })).toBe(0);
|
|
751
751
|
});
|
|
752
|
-
testAll("test transaction rollback", async ({ db }) => {
|
|
752
|
+
testAll("test transaction rollback", async ({ db, createEntity }) => {
|
|
753
753
|
let fail = true;
|
|
754
|
+
const r = await createEntity(stam);
|
|
755
|
+
await r.insert({ title: "task b", id: 1 });
|
|
754
756
|
try {
|
|
755
|
-
await db.transaction(async () => {
|
|
757
|
+
await db.transaction(async (db) => {
|
|
758
|
+
var remultWithTransaction = new Remult(db).repo(stam);
|
|
759
|
+
await remultWithTransaction.insert({ title: "task a", id: 2 });
|
|
760
|
+
expect(await remultWithTransaction.count()).toBe(2);
|
|
756
761
|
throw "error";
|
|
757
762
|
});
|
|
758
763
|
fail = false;
|
|
759
764
|
}
|
|
760
765
|
catch {
|
|
761
766
|
}
|
|
767
|
+
expect(await r.count()).toBe(1);
|
|
762
768
|
expect(fail).toBe(true);
|
|
763
|
-
});
|
|
769
|
+
}, false, { exclude: [TestDbs.inMemory] });
|
|
764
770
|
testAll("test date", async ({ createEntity }) => {
|
|
765
771
|
const e = class {
|
|
766
772
|
constructor() {
|
package/package.json
CHANGED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function getEntityTypescriptPostgres(table: string, schema?: string): Promise<string>;
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getEntityTypescriptPostgres = void 0;
|
|
4
|
+
var tslib_1 = require("tslib");
|
|
5
|
+
var remult_1 = require("remult");
|
|
6
|
+
function getEntityTypescriptPostgres(table, schema) {
|
|
7
|
+
if (schema === void 0) { schema = 'public'; }
|
|
8
|
+
return tslib_1.__awaiter(this, void 0, void 0, function () {
|
|
9
|
+
var command, cols, props, first, _a, _b, _c, column_name, column_default, data_type, datetime_precision, character_maximum_length, decorator, decoratorArgs, type, defaultVal, e_1_1, r;
|
|
10
|
+
var e_1, _d;
|
|
11
|
+
return tslib_1.__generator(this, function (_e) {
|
|
12
|
+
switch (_e.label) {
|
|
13
|
+
case 0:
|
|
14
|
+
command = remult_1.SqlDatabase.getDb(remult_1.remult).createCommand();
|
|
15
|
+
cols = '';
|
|
16
|
+
props = [];
|
|
17
|
+
props.push('allowApiCrud: true');
|
|
18
|
+
if (table.toLocaleLowerCase() != table) {
|
|
19
|
+
// props.push("dbName: '\"" + table + "\"'");
|
|
20
|
+
}
|
|
21
|
+
first = undefined;
|
|
22
|
+
_e.label = 1;
|
|
23
|
+
case 1:
|
|
24
|
+
_e.trys.push([1, 6, 7, 8]);
|
|
25
|
+
return [4 /*yield*/, command.execute("select * from INFORMATION_SCHEMA.COLUMNS where table_schema=" + command.addParameterAndReturnSqlToken(schema) + " and table_name=" + command.addParameterAndReturnSqlToken(table) + "\n \n order by ordinal_position")];
|
|
26
|
+
case 2:
|
|
27
|
+
_a = tslib_1.__values.apply(void 0, [(_e.sent()).rows]), _b = _a.next();
|
|
28
|
+
_e.label = 3;
|
|
29
|
+
case 3:
|
|
30
|
+
if (!!_b.done) return [3 /*break*/, 5];
|
|
31
|
+
_c = _b.value, column_name = _c.column_name, column_default = _c.column_default, data_type = _c.data_type, datetime_precision = _c.datetime_precision, character_maximum_length = _c.character_maximum_length;
|
|
32
|
+
decorator = '@Fields.string';
|
|
33
|
+
decoratorArgs = '';
|
|
34
|
+
type = '';
|
|
35
|
+
defaultVal = "''";
|
|
36
|
+
switch (data_type) {
|
|
37
|
+
case 'decimal':
|
|
38
|
+
case 'real':
|
|
39
|
+
case 'int':
|
|
40
|
+
case 'smallint':
|
|
41
|
+
case 'tinyint':
|
|
42
|
+
case 'bigint':
|
|
43
|
+
case 'float':
|
|
44
|
+
case 'numeric':
|
|
45
|
+
case 'NUMBER':
|
|
46
|
+
case 'money':
|
|
47
|
+
if (datetime_precision === 0)
|
|
48
|
+
decorator = '@Fields.integer';
|
|
49
|
+
else
|
|
50
|
+
decorator = '@Fields.number';
|
|
51
|
+
defaultVal = '0';
|
|
52
|
+
break;
|
|
53
|
+
case 'nchar':
|
|
54
|
+
case 'nvarchar':
|
|
55
|
+
case 'ntext':
|
|
56
|
+
case 'NVARCHAR2':
|
|
57
|
+
case 'text':
|
|
58
|
+
case 'varchar':
|
|
59
|
+
case 'VARCHAR2':
|
|
60
|
+
break;
|
|
61
|
+
case 'char':
|
|
62
|
+
case 'CHAR':
|
|
63
|
+
console.log({
|
|
64
|
+
character_maximum_length: character_maximum_length,
|
|
65
|
+
column_default: column_default,
|
|
66
|
+
data_type: data_type,
|
|
67
|
+
column_name: column_name
|
|
68
|
+
});
|
|
69
|
+
if (character_maximum_length == 8 && column_default == "('00000000')") {
|
|
70
|
+
decorator = '@Fields.dateOnly';
|
|
71
|
+
type = 'Date';
|
|
72
|
+
}
|
|
73
|
+
break;
|
|
74
|
+
case 'DATE':
|
|
75
|
+
case 'datetime':
|
|
76
|
+
case 'datetime2':
|
|
77
|
+
case 'timestamp without time zone':
|
|
78
|
+
decorator = '@Fields.date';
|
|
79
|
+
type = 'Date';
|
|
80
|
+
break;
|
|
81
|
+
case 'bit':
|
|
82
|
+
decorator = '@Fields.boolean';
|
|
83
|
+
break;
|
|
84
|
+
default:
|
|
85
|
+
console.log(data_type);
|
|
86
|
+
break;
|
|
87
|
+
}
|
|
88
|
+
if (column_name.toLocaleLowerCase() != column_name ||
|
|
89
|
+
column_name == 'order')
|
|
90
|
+
decoratorArgs = "{ dbName: '\"" + column_name + "\"' }";
|
|
91
|
+
if (!first)
|
|
92
|
+
first = column_name;
|
|
93
|
+
cols += '\n\n ' + decorator + ("(" + decoratorArgs + ")\n ") + column_name;
|
|
94
|
+
if (!defaultVal) {
|
|
95
|
+
cols += '!';
|
|
96
|
+
cols += ': ';
|
|
97
|
+
cols += type;
|
|
98
|
+
}
|
|
99
|
+
if (defaultVal)
|
|
100
|
+
cols += ' = ' + defaultVal;
|
|
101
|
+
_e.label = 4;
|
|
102
|
+
case 4:
|
|
103
|
+
_b = _a.next();
|
|
104
|
+
return [3 /*break*/, 3];
|
|
105
|
+
case 5: return [3 /*break*/, 8];
|
|
106
|
+
case 6:
|
|
107
|
+
e_1_1 = _e.sent();
|
|
108
|
+
e_1 = { error: e_1_1 };
|
|
109
|
+
return [3 /*break*/, 8];
|
|
110
|
+
case 7:
|
|
111
|
+
try {
|
|
112
|
+
if (_b && !_b.done && (_d = _a.return)) _d.call(_a);
|
|
113
|
+
}
|
|
114
|
+
finally { if (e_1) throw e_1.error; }
|
|
115
|
+
return [7 /*endfinally*/];
|
|
116
|
+
case 8:
|
|
117
|
+
r = "import { Entity, Fields, EntityBase } from \"remult\";\n@Entity<" + table + ">(\"" + table + "\", { \n " + props.join(',\n ') + " \n})\nexport class " + table + " extends EntityBase {" +
|
|
118
|
+
cols +
|
|
119
|
+
'\n}'.replace(' ', '');
|
|
120
|
+
return [2 /*return*/, r];
|
|
121
|
+
}
|
|
122
|
+
});
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
exports.getEntityTypescriptPostgres = getEntityTypescriptPostgres;
|
|
@@ -257,7 +257,7 @@ var PostgresSchemaBuilder = /** @class */ (function () {
|
|
|
257
257
|
};
|
|
258
258
|
PostgresSchemaBuilder.prototype.verifyAllColumns = function (entity) {
|
|
259
259
|
return tslib_1.__awaiter(this, void 0, void 0, function () {
|
|
260
|
-
var cmd, e, cols, _a, _b, col, sql, e_4_1, err_3;
|
|
260
|
+
var cmd, e, cols, _a, _b, col, colName, sql, e_4_1, err_3;
|
|
261
261
|
var e_4, _c;
|
|
262
262
|
return tslib_1.__generator(this, function (_d) {
|
|
263
263
|
switch (_d.label) {
|
|
@@ -269,7 +269,7 @@ var PostgresSchemaBuilder = /** @class */ (function () {
|
|
|
269
269
|
e = _d.sent();
|
|
270
270
|
return [4 /*yield*/, cmd.execute("select column_name \n FROM information_schema.columns \n WHERE table_name=" + cmd.addParameterAndReturnSqlToken((e.$entityName).toLocaleLowerCase()) + " " + this.additionalWhere)];
|
|
271
271
|
case 2:
|
|
272
|
-
cols = (_d.sent()).rows.map(function (x) { return x.column_name; });
|
|
272
|
+
cols = (_d.sent()).rows.map(function (x) { return x.column_name.toLocaleLowerCase(); });
|
|
273
273
|
_d.label = 3;
|
|
274
274
|
case 3:
|
|
275
275
|
_d.trys.push([3, 8, 9, 10]);
|
|
@@ -279,7 +279,10 @@ var PostgresSchemaBuilder = /** @class */ (function () {
|
|
|
279
279
|
if (!!_b.done) return [3 /*break*/, 7];
|
|
280
280
|
col = _b.value;
|
|
281
281
|
if (!!filter_consumer_bridge_to_sql_request_1.isDbReadonly(col, e)) return [3 /*break*/, 6];
|
|
282
|
-
|
|
282
|
+
colName = e.$dbNameOf(col).toLocaleLowerCase();
|
|
283
|
+
if (colName.startsWith('"') && colName.endsWith('"'))
|
|
284
|
+
colName = colName.substring(1, colName.length - 1);
|
|
285
|
+
if (!!cols.includes(colName)) return [3 /*break*/, 6];
|
|
283
286
|
sql = "alter table " + e.$entityName + " add column " + postgresColumnSyntax(col, e.$dbNameOf(col));
|
|
284
287
|
if (PostgresSchemaBuilder.logToConsole)
|
|
285
288
|
console.info(sql);
|
package/remult-mongo.d.ts
CHANGED
|
@@ -1,11 +1,19 @@
|
|
|
1
|
-
import type { MongoClient, Db } from 'mongodb';
|
|
1
|
+
import type { MongoClient, Db, ClientSession } from 'mongodb';
|
|
2
2
|
import { DataProvider, EntityDataProvider, EntityFilter, EntityMetadata, Remult } from '.';
|
|
3
3
|
import { RepositoryOverloads } from './src/remult3';
|
|
4
4
|
export declare class MongoDataProvider implements DataProvider {
|
|
5
5
|
private db;
|
|
6
6
|
private client;
|
|
7
|
-
constructor(db: Db, client: MongoClient
|
|
8
|
-
|
|
7
|
+
constructor(db: Db, client: MongoClient, options?: {
|
|
8
|
+
session?: ClientSession;
|
|
9
|
+
disableTransactions?: boolean;
|
|
10
|
+
});
|
|
11
|
+
session?: ClientSession;
|
|
12
|
+
disableTransactions: boolean;
|
|
13
|
+
static getDb(remult?: Remult): {
|
|
14
|
+
db: Db;
|
|
15
|
+
session: ClientSession;
|
|
16
|
+
};
|
|
9
17
|
getEntityDataProvider(entity: EntityMetadata<any>): EntityDataProvider;
|
|
10
18
|
transaction(action: (dataProvider: DataProvider) => Promise<void>): Promise<void>;
|
|
11
19
|
static filterToRaw<entityType>(entity: RepositoryOverloads<entityType>, condition: EntityFilter<entityType>): Promise<{
|
package/remult-mongo.js
CHANGED
|
@@ -7,45 +7,53 @@ var filter_consumer_bridge_to_sql_request_1 = require("./src/filter/filter-consu
|
|
|
7
7
|
var remult3_1 = require("./src/remult3");
|
|
8
8
|
var remult_proxy_1 = require("./src/remult-proxy");
|
|
9
9
|
var MongoDataProvider = /** @class */ (function () {
|
|
10
|
-
function MongoDataProvider(db, client) {
|
|
10
|
+
function MongoDataProvider(db, client, options) {
|
|
11
11
|
this.db = db;
|
|
12
12
|
this.client = client;
|
|
13
|
+
this.disableTransactions = false;
|
|
14
|
+
this.session = options === null || options === void 0 ? void 0 : options.session;
|
|
15
|
+
this.disableTransactions = options === null || options === void 0 ? void 0 : options.disableTransactions;
|
|
13
16
|
}
|
|
14
17
|
MongoDataProvider.getDb = function (remult) {
|
|
15
18
|
var r = (remult || remult_proxy_1.remult).dataProvider;
|
|
16
19
|
if (!r.db)
|
|
17
20
|
throw "the data provider is not a MongoDataProvider";
|
|
18
|
-
return r.db;
|
|
21
|
+
return { db: r.db, session: r.session };
|
|
19
22
|
};
|
|
20
23
|
MongoDataProvider.prototype.getEntityDataProvider = function (entity) {
|
|
21
|
-
return new MongoEntityDataProvider(this.db, entity);
|
|
24
|
+
return new MongoEntityDataProvider(this.db, entity, this.session);
|
|
22
25
|
};
|
|
23
26
|
MongoDataProvider.prototype.transaction = function (action) {
|
|
24
27
|
return tslib_1.__awaiter(this, void 0, void 0, function () {
|
|
25
|
-
var session, err_1;
|
|
28
|
+
var session, db, err_1;
|
|
26
29
|
return tslib_1.__generator(this, function (_a) {
|
|
27
30
|
switch (_a.label) {
|
|
28
31
|
case 0: return [4 /*yield*/, this.client.startSession()];
|
|
29
32
|
case 1:
|
|
30
33
|
session = _a.sent();
|
|
31
34
|
session.startTransaction();
|
|
35
|
+
db = this.client.db(this.db.databaseName);
|
|
32
36
|
_a.label = 2;
|
|
33
37
|
case 2:
|
|
34
|
-
_a.trys.push([2, 5, ,
|
|
35
|
-
return [4 /*yield*/, action(new MongoDataProvider(
|
|
38
|
+
_a.trys.push([2, 5, 7, 9]);
|
|
39
|
+
return [4 /*yield*/, action(new MongoDataProvider(db, undefined, { session: session }))];
|
|
36
40
|
case 3:
|
|
37
41
|
_a.sent();
|
|
38
42
|
return [4 /*yield*/, session.commitTransaction()];
|
|
39
43
|
case 4:
|
|
40
44
|
_a.sent();
|
|
41
|
-
return [3 /*break*/,
|
|
45
|
+
return [3 /*break*/, 9];
|
|
42
46
|
case 5:
|
|
43
47
|
err_1 = _a.sent();
|
|
44
48
|
return [4 /*yield*/, session.abortTransaction()];
|
|
45
49
|
case 6:
|
|
46
50
|
_a.sent();
|
|
47
51
|
throw err_1;
|
|
48
|
-
case 7: return [
|
|
52
|
+
case 7: return [4 /*yield*/, session.endSession()];
|
|
53
|
+
case 8:
|
|
54
|
+
_a.sent();
|
|
55
|
+
return [7 /*endfinally*/];
|
|
56
|
+
case 9: return [2 /*return*/];
|
|
49
57
|
}
|
|
50
58
|
});
|
|
51
59
|
});
|
|
@@ -82,9 +90,10 @@ function isNull(x) {
|
|
|
82
90
|
return (x === null || x === void 0 ? void 0 : x.$null) === NULL.$null;
|
|
83
91
|
}
|
|
84
92
|
var MongoEntityDataProvider = /** @class */ (function () {
|
|
85
|
-
function MongoEntityDataProvider(db, entity) {
|
|
93
|
+
function MongoEntityDataProvider(db, entity, session) {
|
|
86
94
|
this.db = db;
|
|
87
95
|
this.entity = entity;
|
|
96
|
+
this.session = session;
|
|
88
97
|
}
|
|
89
98
|
MongoEntityDataProvider.prototype.translateFromJson = function (row, nameProvider) {
|
|
90
99
|
var e_1, _a;
|
|
@@ -141,7 +150,7 @@ var MongoEntityDataProvider = /** @class */ (function () {
|
|
|
141
150
|
return [4 /*yield*/, x.resolveWhere()];
|
|
142
151
|
case 2:
|
|
143
152
|
w = _b.sent();
|
|
144
|
-
return [4 /*yield*/, collection.countDocuments(w)];
|
|
153
|
+
return [4 /*yield*/, collection.countDocuments(w, { session: this.session })];
|
|
145
154
|
case 3: return [2 /*return*/, _b.sent()];
|
|
146
155
|
}
|
|
147
156
|
});
|
|
@@ -163,7 +172,9 @@ var MongoEntityDataProvider = /** @class */ (function () {
|
|
|
163
172
|
return [4 /*yield*/, x.resolveWhere()];
|
|
164
173
|
case 2:
|
|
165
174
|
where = _g.sent();
|
|
166
|
-
op = {
|
|
175
|
+
op = {
|
|
176
|
+
session: this.session
|
|
177
|
+
};
|
|
167
178
|
if (options.limit) {
|
|
168
179
|
op.limit = options.limit;
|
|
169
180
|
if (options.page) {
|
|
@@ -246,7 +257,7 @@ var MongoEntityDataProvider = /** @class */ (function () {
|
|
|
246
257
|
return [4 /*yield*/, f.resolveWhere()];
|
|
247
258
|
case 2: return [4 /*yield*/, _g.apply(_f, [_k.sent(), {
|
|
248
259
|
$set: newR
|
|
249
|
-
}])];
|
|
260
|
+
}, { session: this.session }])];
|
|
250
261
|
case 3:
|
|
251
262
|
r = _k.sent();
|
|
252
263
|
return [2 /*return*/, this.find({ where: _1.Filter.fromEntityFilter(this.entity, resultFilter) }).then(function (y) { return y[0]; })];
|
|
@@ -267,7 +278,7 @@ var MongoEntityDataProvider = /** @class */ (function () {
|
|
|
267
278
|
_c = (_b = collection).deleteOne;
|
|
268
279
|
return [4 /*yield*/, f.resolveWhere()];
|
|
269
280
|
case 2:
|
|
270
|
-
_c.apply(_b, [_d.sent()]);
|
|
281
|
+
_c.apply(_b, [_d.sent(), { session: this.session }]);
|
|
271
282
|
return [2 /*return*/];
|
|
272
283
|
}
|
|
273
284
|
});
|
|
@@ -283,11 +294,11 @@ var MongoEntityDataProvider = /** @class */ (function () {
|
|
|
283
294
|
_a = _e.sent(), collection = _a.collection, e = _a.e;
|
|
284
295
|
_c = (_b = collection).insertOne;
|
|
285
296
|
return [4 /*yield*/, this.translateToJson(data, e)];
|
|
286
|
-
case 2: return [4 /*yield*/, _c.apply(_b, [_e.sent()])];
|
|
297
|
+
case 2: return [4 /*yield*/, _c.apply(_b, [_e.sent(), { session: this.session }])];
|
|
287
298
|
case 3:
|
|
288
299
|
r = _e.sent();
|
|
289
300
|
_d = this.translateFromJson;
|
|
290
|
-
return [4 /*yield*/, collection.findOne({ _id: r.insertedId })];
|
|
301
|
+
return [4 /*yield*/, collection.findOne({ _id: r.insertedId }, { session: this.session })];
|
|
291
302
|
case 4: return [4 /*yield*/, _d.apply(this, [_e.sent(), e])];
|
|
292
303
|
case 5: return [2 /*return*/, _e.sent()];
|
|
293
304
|
}
|
package/server/expressBridge.js
CHANGED
|
@@ -719,15 +719,7 @@ var RemultServerImplementation = /** @class */ (function () {
|
|
|
719
719
|
parameters.push({
|
|
720
720
|
"name": f.key,
|
|
721
721
|
"in": "query",
|
|
722
|
-
"description": "filter equal to " + f.key,
|
|
723
|
-
"required": false,
|
|
724
|
-
"style": "simple",
|
|
725
|
-
type: type
|
|
726
|
-
});
|
|
727
|
-
parameters.push({
|
|
728
|
-
"name": f.key + "_ne",
|
|
729
|
-
"in": "query",
|
|
730
|
-
"description": "filter not equal to " + f.key,
|
|
722
|
+
"description": "filter equal to " + f.key + ". See [more filtering options](https://remult.dev/docs/rest-api.html#filter)",
|
|
731
723
|
"required": false,
|
|
732
724
|
"style": "simple",
|
|
733
725
|
type: type
|
|
@@ -746,7 +738,6 @@ var RemultServerImplementation = /** @class */ (function () {
|
|
|
746
738
|
type: "object",
|
|
747
739
|
properties: properties
|
|
748
740
|
};
|
|
749
|
-
//TODO - handle allow api read also on the get level
|
|
750
741
|
if (JSON.stringify(properties) !== JSON.stringify(mutationProperties)) {
|
|
751
742
|
mutationKey += 'Mutation';
|
|
752
743
|
spec.components.schemas[mutationKey] = {
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { dbTestWhatSignature } from "../shared-tests/db-tests-setup";
|
|
2
|
+
export declare const testKnexPGSqlImpl: (key: string, what: dbTestWhatSignature, focus?: boolean) => void;
|
|
3
|
+
export declare const mySqlTest: import("../shared-tests/db-tests-setup").dbTestMethodSignature;
|
|
4
|
+
export declare function testPostgresImplementation(key: string, what: dbTestWhatSignature, focus?: boolean): void;
|
|
5
|
+
export declare function testMongo(key: string, what: dbTestWhatSignature, focus?: boolean): void;
|
|
6
|
+
import '../shared-tests';
|
|
@@ -0,0 +1,306 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.testMongo = exports.testPostgresImplementation = exports.mySqlTest = exports.testKnexPGSqlImpl = void 0;
|
|
4
|
+
var tslib_1 = require("tslib");
|
|
5
|
+
var context_1 = require("../context");
|
|
6
|
+
var remult_knex_1 = require("../../remult-knex");
|
|
7
|
+
var Knex = tslib_1.__importStar(require("knex"));
|
|
8
|
+
var mongodb_1 = require("mongodb");
|
|
9
|
+
var dotenv_1 = require("dotenv");
|
|
10
|
+
var postgres_1 = require("../../postgres");
|
|
11
|
+
var db_tests_setup_1 = require("../shared-tests/db-tests-setup");
|
|
12
|
+
var DecoratorReplacer_1 = require("../remult3/DecoratorReplacer");
|
|
13
|
+
var remult3_1 = require("../remult3");
|
|
14
|
+
remult_knex_1.KnexSchemaBuilder.logToConsole = false;
|
|
15
|
+
postgres_1.PostgresSchemaBuilder.logToConsole = false;
|
|
16
|
+
dotenv_1.config();
|
|
17
|
+
function testKnexSqlImpl(knex, name) {
|
|
18
|
+
var _this = this;
|
|
19
|
+
return function (key, what, focus) {
|
|
20
|
+
if (focus === void 0) { focus = false; }
|
|
21
|
+
db_tests_setup_1.itWithFocus(key + " " + name + " - knex", function () { return tslib_1.__awaiter(_this, void 0, void 0, function () {
|
|
22
|
+
var db, remult;
|
|
23
|
+
var _this = this;
|
|
24
|
+
return tslib_1.__generator(this, function (_a) {
|
|
25
|
+
switch (_a.label) {
|
|
26
|
+
case 0:
|
|
27
|
+
db = new remult_knex_1.KnexDataProvider(knex);
|
|
28
|
+
remult = new context_1.Remult(db);
|
|
29
|
+
return [4 /*yield*/, what({
|
|
30
|
+
db: db, remult: remult,
|
|
31
|
+
createEntity: function (entity) { return tslib_1.__awaiter(_this, void 0, void 0, function () {
|
|
32
|
+
var repo, _a, _b;
|
|
33
|
+
return tslib_1.__generator(this, function (_c) {
|
|
34
|
+
switch (_c.label) {
|
|
35
|
+
case 0:
|
|
36
|
+
repo = remult.repo(entity);
|
|
37
|
+
_b = (_a = knex.schema).dropTableIfExists;
|
|
38
|
+
return [4 /*yield*/, repo.metadata.getDbName()];
|
|
39
|
+
case 1: return [4 /*yield*/, _b.apply(_a, [_c.sent()])];
|
|
40
|
+
case 2:
|
|
41
|
+
_c.sent();
|
|
42
|
+
return [4 /*yield*/, db.ensureSchema([repo.metadata])];
|
|
43
|
+
case 3:
|
|
44
|
+
_c.sent();
|
|
45
|
+
return [2 /*return*/, repo];
|
|
46
|
+
}
|
|
47
|
+
});
|
|
48
|
+
}); }
|
|
49
|
+
})];
|
|
50
|
+
case 1:
|
|
51
|
+
_a.sent();
|
|
52
|
+
return [2 /*return*/];
|
|
53
|
+
}
|
|
54
|
+
});
|
|
55
|
+
}); }, focus);
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
exports.testKnexPGSqlImpl = testKnexSqlImpl(Knex.default({
|
|
59
|
+
client: 'pg',
|
|
60
|
+
connection: process.env.DATABASE_URL,
|
|
61
|
+
}), "postgres");
|
|
62
|
+
db_tests_setup_1.addDatabaseToTest(exports.testKnexPGSqlImpl);
|
|
63
|
+
if (process.env['TESTS_SQL_SERVER'])
|
|
64
|
+
db_tests_setup_1.addDatabaseToTest(testKnexSqlImpl(Knex.default({
|
|
65
|
+
client: 'mssql',
|
|
66
|
+
connection: {
|
|
67
|
+
server: '127.0.0.1',
|
|
68
|
+
database: 'test2',
|
|
69
|
+
user: 'sa',
|
|
70
|
+
password: 'MASTERKEY',
|
|
71
|
+
options: {
|
|
72
|
+
enableArithAbort: true,
|
|
73
|
+
encrypt: false,
|
|
74
|
+
instanceName: 'sqlexpress'
|
|
75
|
+
}
|
|
76
|
+
} //,debug: true
|
|
77
|
+
}), "sql server"));
|
|
78
|
+
if (true)
|
|
79
|
+
db_tests_setup_1.addDatabaseToTest(testKnexSqlImpl(Knex.default({
|
|
80
|
+
client: 'better-sqlite3',
|
|
81
|
+
connection: {
|
|
82
|
+
filename: ":memory:"
|
|
83
|
+
},
|
|
84
|
+
}), "sqlite3"));
|
|
85
|
+
exports.mySqlTest = db_tests_setup_1.addDatabaseToTest(testKnexSqlImpl(Knex.default({
|
|
86
|
+
client: 'mysql2',
|
|
87
|
+
connection: {
|
|
88
|
+
user: 'root',
|
|
89
|
+
password: 'MASTERKEY',
|
|
90
|
+
host: '127.0.0.1',
|
|
91
|
+
database: 'test'
|
|
92
|
+
}
|
|
93
|
+
//debug: true
|
|
94
|
+
}), "mysql2"));
|
|
95
|
+
var pg = postgres_1.createPostgresConnection();
|
|
96
|
+
function testPostgresImplementation(key, what, focus) {
|
|
97
|
+
var _this = this;
|
|
98
|
+
if (focus === void 0) { focus = false; }
|
|
99
|
+
db_tests_setup_1.itWithFocus(key + " - postgres", function () { return tslib_1.__awaiter(_this, void 0, void 0, function () {
|
|
100
|
+
var db, remult;
|
|
101
|
+
var _this = this;
|
|
102
|
+
return tslib_1.__generator(this, function (_a) {
|
|
103
|
+
switch (_a.label) {
|
|
104
|
+
case 0: return [4 /*yield*/, pg];
|
|
105
|
+
case 1:
|
|
106
|
+
db = _a.sent();
|
|
107
|
+
remult = new context_1.Remult(db);
|
|
108
|
+
return [4 /*yield*/, what({
|
|
109
|
+
db: db, remult: remult,
|
|
110
|
+
createEntity: function (entity) { return tslib_1.__awaiter(_this, void 0, void 0, function () {
|
|
111
|
+
var repo, _a, _b, _c;
|
|
112
|
+
return tslib_1.__generator(this, function (_d) {
|
|
113
|
+
switch (_d.label) {
|
|
114
|
+
case 0:
|
|
115
|
+
repo = remult.repo(entity);
|
|
116
|
+
_b = (_a = db).execute;
|
|
117
|
+
_c = "drop table if exists ";
|
|
118
|
+
return [4 /*yield*/, repo.metadata.getDbName()];
|
|
119
|
+
case 1: return [4 /*yield*/, _b.apply(_a, [_c + (_d.sent())])];
|
|
120
|
+
case 2:
|
|
121
|
+
_d.sent();
|
|
122
|
+
return [4 /*yield*/, db.ensureSchema([repo.metadata])];
|
|
123
|
+
case 3:
|
|
124
|
+
_d.sent();
|
|
125
|
+
return [2 /*return*/, repo];
|
|
126
|
+
}
|
|
127
|
+
});
|
|
128
|
+
}); }
|
|
129
|
+
})];
|
|
130
|
+
case 2:
|
|
131
|
+
_a.sent();
|
|
132
|
+
return [2 /*return*/];
|
|
133
|
+
}
|
|
134
|
+
});
|
|
135
|
+
}); }, focus);
|
|
136
|
+
}
|
|
137
|
+
exports.testPostgresImplementation = testPostgresImplementation;
|
|
138
|
+
db_tests_setup_1.addDatabaseToTest(testPostgresImplementation);
|
|
139
|
+
var remult_3_entities_1 = require("../tests/remult-3-entities");
|
|
140
|
+
var remult_mongo_1 = require("../../remult-mongo");
|
|
141
|
+
db_tests_setup_1.testAll("transactions", function (_a) {
|
|
142
|
+
var db = _a.db, createEntity = _a.createEntity;
|
|
143
|
+
return tslib_1.__awaiter(void 0, void 0, void 0, function () {
|
|
144
|
+
var x;
|
|
145
|
+
return tslib_1.__generator(this, function (_b) {
|
|
146
|
+
switch (_b.label) {
|
|
147
|
+
case 0: return [4 /*yield*/, createEntity(remult_3_entities_1.Categories)];
|
|
148
|
+
case 1:
|
|
149
|
+
x = _b.sent();
|
|
150
|
+
return [4 /*yield*/, db.transaction(function (db) { return tslib_1.__awaiter(void 0, void 0, void 0, function () {
|
|
151
|
+
var remult, _a;
|
|
152
|
+
return tslib_1.__generator(this, function (_b) {
|
|
153
|
+
switch (_b.label) {
|
|
154
|
+
case 0:
|
|
155
|
+
remult = new context_1.Remult(db);
|
|
156
|
+
_a = expect;
|
|
157
|
+
return [4 /*yield*/, remult.repo(remult_3_entities_1.Categories).count()];
|
|
158
|
+
case 1:
|
|
159
|
+
_a.apply(void 0, [_b.sent()]).toBe(0);
|
|
160
|
+
return [2 /*return*/];
|
|
161
|
+
}
|
|
162
|
+
});
|
|
163
|
+
}); })];
|
|
164
|
+
case 2:
|
|
165
|
+
_b.sent();
|
|
166
|
+
return [2 /*return*/];
|
|
167
|
+
}
|
|
168
|
+
});
|
|
169
|
+
});
|
|
170
|
+
});
|
|
171
|
+
var mongoConnectionString = process.env['MONGO_TEST_URL']; //"mongodb://localhost:27017/local"
|
|
172
|
+
var client = new mongodb_1.MongoClient(mongoConnectionString);
|
|
173
|
+
var done;
|
|
174
|
+
var mongoDbPromise = client.connect().then(function (c) {
|
|
175
|
+
done = c;
|
|
176
|
+
return c.db("test");
|
|
177
|
+
});
|
|
178
|
+
afterAll(function () { return tslib_1.__awaiter(void 0, void 0, void 0, function () {
|
|
179
|
+
return tslib_1.__generator(this, function (_a) {
|
|
180
|
+
if (done)
|
|
181
|
+
done.close();
|
|
182
|
+
return [2 /*return*/];
|
|
183
|
+
});
|
|
184
|
+
}); });
|
|
185
|
+
function testMongo(key, what, focus) {
|
|
186
|
+
var _this = this;
|
|
187
|
+
if (focus === void 0) { focus = false; }
|
|
188
|
+
db_tests_setup_1.itWithFocus(key + " - mongo", function () { return tslib_1.__awaiter(_this, void 0, void 0, function () {
|
|
189
|
+
var mongoDb, db, remult;
|
|
190
|
+
var _this = this;
|
|
191
|
+
return tslib_1.__generator(this, function (_a) {
|
|
192
|
+
switch (_a.label) {
|
|
193
|
+
case 0: return [4 /*yield*/, mongoDbPromise];
|
|
194
|
+
case 1:
|
|
195
|
+
mongoDb = _a.sent();
|
|
196
|
+
db = new remult_mongo_1.MongoDataProvider(mongoDb, client);
|
|
197
|
+
remult = new context_1.Remult(db);
|
|
198
|
+
return [4 /*yield*/, what({
|
|
199
|
+
db: db, remult: remult,
|
|
200
|
+
createEntity: function (entity) { return tslib_1.__awaiter(_this, void 0, void 0, function () {
|
|
201
|
+
var repo, _a, _b;
|
|
202
|
+
return tslib_1.__generator(this, function (_c) {
|
|
203
|
+
switch (_c.label) {
|
|
204
|
+
case 0:
|
|
205
|
+
repo = remult.repo(entity);
|
|
206
|
+
_b = (_a = mongoDb).collection;
|
|
207
|
+
return [4 /*yield*/, repo.metadata.getDbName()];
|
|
208
|
+
case 1: return [4 /*yield*/, _b.apply(_a, [_c.sent()]).deleteMany({})];
|
|
209
|
+
case 2:
|
|
210
|
+
_c.sent();
|
|
211
|
+
return [2 /*return*/, repo];
|
|
212
|
+
}
|
|
213
|
+
});
|
|
214
|
+
}); }
|
|
215
|
+
})];
|
|
216
|
+
case 2:
|
|
217
|
+
_a.sent();
|
|
218
|
+
return [2 /*return*/];
|
|
219
|
+
}
|
|
220
|
+
});
|
|
221
|
+
}); }, focus);
|
|
222
|
+
}
|
|
223
|
+
exports.testMongo = testMongo;
|
|
224
|
+
db_tests_setup_1.addDatabaseToTest(testMongo, db_tests_setup_1.TestDbs.mongo);
|
|
225
|
+
it("test mongo without transaction", function () { return tslib_1.__awaiter(void 0, void 0, void 0, function () {
|
|
226
|
+
var client, mongoDb, db, remult, entity, repo, _a, _b, item, e_1_1, _c, _d;
|
|
227
|
+
var e_1, _e;
|
|
228
|
+
return tslib_1.__generator(this, function (_f) {
|
|
229
|
+
switch (_f.label) {
|
|
230
|
+
case 0:
|
|
231
|
+
client = new mongodb_1.MongoClient("mongodb://localhost:27017/local");
|
|
232
|
+
return [4 /*yield*/, client.connect()];
|
|
233
|
+
case 1:
|
|
234
|
+
_f.sent();
|
|
235
|
+
mongoDb = client.db('test');
|
|
236
|
+
db = new remult_mongo_1.MongoDataProvider(mongoDb, client, { disableTransactions: true });
|
|
237
|
+
remult = new context_1.Remult(db);
|
|
238
|
+
entity = /** @class */ (function () {
|
|
239
|
+
function class_1() {
|
|
240
|
+
this.id = 0;
|
|
241
|
+
this.title = '';
|
|
242
|
+
}
|
|
243
|
+
return class_1;
|
|
244
|
+
}());
|
|
245
|
+
DecoratorReplacer_1.describeClass(entity, remult3_1.Entity("testNoTrans"), { id: remult3_1.Fields.number(), title: remult3_1.Fields.string() });
|
|
246
|
+
repo = remult.repo(entity);
|
|
247
|
+
_f.label = 2;
|
|
248
|
+
case 2:
|
|
249
|
+
_f.trys.push([2, 8, 9, 10]);
|
|
250
|
+
return [4 /*yield*/, repo.find()];
|
|
251
|
+
case 3:
|
|
252
|
+
_a = tslib_1.__values.apply(void 0, [_f.sent()]), _b = _a.next();
|
|
253
|
+
_f.label = 4;
|
|
254
|
+
case 4:
|
|
255
|
+
if (!!_b.done) return [3 /*break*/, 7];
|
|
256
|
+
item = _b.value;
|
|
257
|
+
return [4 /*yield*/, repo.delete(item)];
|
|
258
|
+
case 5:
|
|
259
|
+
_f.sent();
|
|
260
|
+
_f.label = 6;
|
|
261
|
+
case 6:
|
|
262
|
+
_b = _a.next();
|
|
263
|
+
return [3 /*break*/, 4];
|
|
264
|
+
case 7: return [3 /*break*/, 10];
|
|
265
|
+
case 8:
|
|
266
|
+
e_1_1 = _f.sent();
|
|
267
|
+
e_1 = { error: e_1_1 };
|
|
268
|
+
return [3 /*break*/, 10];
|
|
269
|
+
case 9:
|
|
270
|
+
try {
|
|
271
|
+
if (_b && !_b.done && (_e = _a.return)) _e.call(_a);
|
|
272
|
+
}
|
|
273
|
+
finally { if (e_1) throw e_1.error; }
|
|
274
|
+
return [7 /*endfinally*/];
|
|
275
|
+
case 10: return [4 /*yield*/, repo.insert({ id: 1, title: "a" })];
|
|
276
|
+
case 11:
|
|
277
|
+
_f.sent();
|
|
278
|
+
_f.label = 12;
|
|
279
|
+
case 12:
|
|
280
|
+
_f.trys.push([12, 14, , 15]);
|
|
281
|
+
return [4 /*yield*/, db.transaction(function () { return tslib_1.__awaiter(void 0, void 0, void 0, function () {
|
|
282
|
+
return tslib_1.__generator(this, function (_a) {
|
|
283
|
+
switch (_a.label) {
|
|
284
|
+
case 0: return [4 /*yield*/, repo.insert({ id: 2, title: "b" })];
|
|
285
|
+
case 1:
|
|
286
|
+
_a.sent();
|
|
287
|
+
throw new Error();
|
|
288
|
+
}
|
|
289
|
+
});
|
|
290
|
+
}); })];
|
|
291
|
+
case 13:
|
|
292
|
+
_f.sent();
|
|
293
|
+
return [3 /*break*/, 15];
|
|
294
|
+
case 14:
|
|
295
|
+
_c = _f.sent();
|
|
296
|
+
return [3 /*break*/, 15];
|
|
297
|
+
case 15:
|
|
298
|
+
_d = expect;
|
|
299
|
+
return [4 /*yield*/, repo.count()];
|
|
300
|
+
case 16:
|
|
301
|
+
_d.apply(void 0, [_f.sent()]).toBe(2);
|
|
302
|
+
return [2 /*return*/];
|
|
303
|
+
}
|
|
304
|
+
});
|
|
305
|
+
}); });
|
|
306
|
+
require("../shared-tests");
|
|
@@ -481,7 +481,7 @@ var RepositoryImplementation = /** @class */ (function () {
|
|
|
481
481
|
finally { if (e_7) throw e_7.error; }
|
|
482
482
|
}
|
|
483
483
|
return result;
|
|
484
|
-
}))];
|
|
484
|
+
}), load)];
|
|
485
485
|
});
|
|
486
486
|
});
|
|
487
487
|
};
|
|
@@ -2481,7 +2481,7 @@ var EntityFullInfo = /** @class */ (function () {
|
|
|
2481
2481
|
};
|
|
2482
2482
|
Object.defineProperty(EntityFullInfo.prototype, "apiReadAllowed", {
|
|
2483
2483
|
get: function () {
|
|
2484
|
-
if (this.options.
|
|
2484
|
+
if (this.options.allowApiRead === undefined)
|
|
2485
2485
|
return true; //TODO2 - consider that this default may be confusing, since it's different from all others.
|
|
2486
2486
|
return this.remult.isAllowed(this.options.allowApiRead);
|
|
2487
2487
|
},
|
|
@@ -13,6 +13,7 @@ export declare function testInMemory(key: string, what: dbTestWhatSignature, foc
|
|
|
13
13
|
export declare const TestDbs: {
|
|
14
14
|
restDataProvider: string;
|
|
15
15
|
mongo: string;
|
|
16
|
+
inMemory: string;
|
|
16
17
|
};
|
|
17
18
|
export declare type dbTestWhatSignature = ((db: {
|
|
18
19
|
db: DataProvider;
|
|
@@ -87,9 +87,10 @@ function testInMemory(key, what, focus) {
|
|
|
87
87
|
exports.testInMemory = testInMemory;
|
|
88
88
|
exports.TestDbs = {
|
|
89
89
|
restDataProvider: "restDataProvider",
|
|
90
|
-
mongo: "mongo"
|
|
90
|
+
mongo: "mongo",
|
|
91
|
+
inMemory: 'in memory'
|
|
91
92
|
};
|
|
92
|
-
|
|
93
|
+
addDatabaseToTest(testInMemory, exports.TestDbs.inMemory);
|
|
93
94
|
function testAllDbs(key, doTest, focus) {
|
|
94
95
|
if (focus === void 0) { focus = false; }
|
|
95
96
|
return tslib_1.__awaiter(this, void 0, void 0, function () {
|
|
@@ -1680,35 +1680,57 @@ db_tests_setup_1.testAll("task with enum string", function (_a) {
|
|
|
1680
1680
|
});
|
|
1681
1681
|
});
|
|
1682
1682
|
db_tests_setup_1.testAll("test transaction rollback", function (_a) {
|
|
1683
|
-
var db = _a.db;
|
|
1683
|
+
var db = _a.db, createEntity = _a.createEntity;
|
|
1684
1684
|
return tslib_1.__awaiter(void 0, void 0, void 0, function () {
|
|
1685
|
-
var fail, _b;
|
|
1686
|
-
return tslib_1.__generator(this, function (
|
|
1687
|
-
switch (
|
|
1685
|
+
var fail, r, _b, _c;
|
|
1686
|
+
return tslib_1.__generator(this, function (_d) {
|
|
1687
|
+
switch (_d.label) {
|
|
1688
1688
|
case 0:
|
|
1689
1689
|
fail = true;
|
|
1690
|
-
|
|
1690
|
+
return [4 /*yield*/, createEntity(stam)];
|
|
1691
1691
|
case 1:
|
|
1692
|
-
|
|
1693
|
-
return [4 /*yield*/,
|
|
1694
|
-
return tslib_1.__generator(this, function (_a) {
|
|
1695
|
-
throw "error";
|
|
1696
|
-
});
|
|
1697
|
-
}); })];
|
|
1692
|
+
r = _d.sent();
|
|
1693
|
+
return [4 /*yield*/, r.insert({ title: "task b", id: 1 })];
|
|
1698
1694
|
case 2:
|
|
1699
|
-
|
|
1700
|
-
|
|
1701
|
-
return [3 /*break*/, 4];
|
|
1695
|
+
_d.sent();
|
|
1696
|
+
_d.label = 3;
|
|
1702
1697
|
case 3:
|
|
1703
|
-
|
|
1704
|
-
return [
|
|
1698
|
+
_d.trys.push([3, 5, , 6]);
|
|
1699
|
+
return [4 /*yield*/, db.transaction(function (db) { return tslib_1.__awaiter(void 0, void 0, void 0, function () {
|
|
1700
|
+
var remultWithTransaction, _a;
|
|
1701
|
+
return tslib_1.__generator(this, function (_b) {
|
|
1702
|
+
switch (_b.label) {
|
|
1703
|
+
case 0:
|
|
1704
|
+
remultWithTransaction = new context_1.Remult(db).repo(stam);
|
|
1705
|
+
return [4 /*yield*/, remultWithTransaction.insert({ title: "task a", id: 2 })];
|
|
1706
|
+
case 1:
|
|
1707
|
+
_b.sent();
|
|
1708
|
+
_a = expect;
|
|
1709
|
+
return [4 /*yield*/, remultWithTransaction.count()];
|
|
1710
|
+
case 2:
|
|
1711
|
+
_a.apply(void 0, [_b.sent()]).toBe(2);
|
|
1712
|
+
throw "error";
|
|
1713
|
+
}
|
|
1714
|
+
});
|
|
1715
|
+
}); })];
|
|
1705
1716
|
case 4:
|
|
1717
|
+
_d.sent();
|
|
1718
|
+
fail = false;
|
|
1719
|
+
return [3 /*break*/, 6];
|
|
1720
|
+
case 5:
|
|
1721
|
+
_b = _d.sent();
|
|
1722
|
+
return [3 /*break*/, 6];
|
|
1723
|
+
case 6:
|
|
1724
|
+
_c = expect;
|
|
1725
|
+
return [4 /*yield*/, r.count()];
|
|
1726
|
+
case 7:
|
|
1727
|
+
_c.apply(void 0, [_d.sent()]).toBe(1);
|
|
1706
1728
|
expect(fail).toBe(true);
|
|
1707
1729
|
return [2 /*return*/];
|
|
1708
1730
|
}
|
|
1709
1731
|
});
|
|
1710
1732
|
});
|
|
1711
|
-
});
|
|
1733
|
+
}, false, { exclude: [db_tests_setup_1.TestDbs.inMemory] });
|
|
1712
1734
|
db_tests_setup_1.testAll("test date", function (_a) {
|
|
1713
1735
|
var createEntity = _a.createEntity;
|
|
1714
1736
|
return tslib_1.__awaiter(void 0, void 0, void 0, function () {
|