remult 0.20.4 → 0.20.5-exp.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (47) hide show
  1. package/esm/graphql.d.ts +16 -4
  2. package/esm/graphql.js +670 -130
  3. package/esm/index.d.ts +1 -2
  4. package/esm/index.js +1 -2
  5. package/esm/remult-express.d.ts +1 -1
  6. package/esm/remult-express.js +1 -1
  7. package/esm/remult-fastify.js +2 -6
  8. package/esm/remult-fresh.js +1 -1
  9. package/esm/remult-mongo.d.ts +11 -3
  10. package/esm/remult-mongo.js +21 -11
  11. package/esm/remult-next.js +1 -7
  12. package/esm/server/expressBridge.d.ts +4 -4
  13. package/esm/server/expressBridge.js +27 -10
  14. package/esm/src/backend-tests/backend-database-test-setup.backend-spec.d.ts +6 -0
  15. package/esm/src/backend-tests/backend-database-test-setup.backend-spec.js +147 -0
  16. package/esm/src/data-api.d.ts +2 -2
  17. package/esm/src/remult3/RepositoryImplementation.d.ts +3 -0
  18. package/esm/src/remult3/RepositoryImplementation.js +7 -0
  19. package/esm/src/shared-tests/db-tests-setup.d.ts +1 -0
  20. package/esm/src/shared-tests/db-tests-setup.js +3 -2
  21. package/esm/src/shared-tests/db-tests.db-tests.d.ts +1 -2
  22. package/esm/src/shared-tests/db-tests.db-tests.js +10 -5
  23. package/graphql.d.ts +16 -4
  24. package/graphql.js +791 -189
  25. package/index.d.ts +1 -2
  26. package/index.js +2 -3
  27. package/package.json +1 -2
  28. package/remult-express.d.ts +1 -1
  29. package/remult-express.js +1 -7
  30. package/remult-fastify.js +4 -6
  31. package/remult-fresh.js +1 -1
  32. package/remult-mongo.d.ts +11 -3
  33. package/remult-mongo.js +26 -15
  34. package/remult-next.js +2 -6
  35. package/server/expressBridge.d.ts +4 -4
  36. package/server/expressBridge.js +32 -14
  37. package/src/backend-tests/backend-database-test-setup.backend-spec.d.ts +6 -0
  38. package/src/backend-tests/backend-database-test-setup.backend-spec.js +306 -0
  39. package/src/data-api.d.ts +2 -2
  40. package/src/id-entity.d.ts +1 -1
  41. package/src/id-entity.js +3 -3
  42. package/src/remult3/RepositoryImplementation.d.ts +3 -0
  43. package/src/remult3/RepositoryImplementation.js +13 -1
  44. package/src/shared-tests/db-tests-setup.d.ts +1 -0
  45. package/src/shared-tests/db-tests-setup.js +3 -2
  46. package/src/shared-tests/db-tests.db-tests.d.ts +1 -2
  47. package/src/shared-tests/db-tests.db-tests.js +41 -20
package/esm/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- export { Field, Fields, FieldsMetadata, Entity, EntityBase, ControllerBase, FieldRef, IdFieldRef, FieldsRef, EntityMetadata, EntityOrderBy, EntityFilter, FindOptions, QueryResult, QueryOptions, Repository, FieldType, FindFirstOptions, ComparisonValueFilter, ValueFilter, IdFilter, ContainsStringValueFilter, getFields, EntityRef, getEntityRef, SortSegments, ValueListFieldType, getValueList, ValueListFieldOptions, ValueListInfo, OmitEB, Paginator, CaptionTransformer, LiveQuery, LiveQueryChangeInfo } from './src/remult3';
1
+ export { Field, Fields, FieldsMetadata, Entity, IdEntity, EntityBase, ControllerBase, FieldRef, IdFieldRef, FieldsRef, EntityMetadata, EntityOrderBy, EntityFilter, FindOptions, QueryResult, QueryOptions, Repository, FieldType, FindFirstOptions, ComparisonValueFilter, ValueFilter, IdFilter, ContainsStringValueFilter, getFields, EntityRef, getEntityRef, SortSegments, ValueListFieldType, getValueList, ValueListFieldOptions, ValueListInfo, OmitEB, Paginator, CaptionTransformer, LiveQuery, LiveQueryChangeInfo } from './src/remult3';
2
2
  export { StringFieldOptions } from './src/remult3/RepositoryImplementation';
3
3
  export { describeClass } from './src/remult3/DecoratorReplacer';
4
4
  export { EntityOptions } from './src/entity';
@@ -16,7 +16,6 @@ export { JsonDataProvider, JsonEntityStorage } from './src/data-providers/json-d
16
16
  export { Controller, BackendMethodOptions, BackendMethod, ProgressListener } from './src/server-action';
17
17
  export { Allowed, Allow, Remult, RemultContext, ApiClient, isBackend, AllowedForInstance, EventDispatcher, EventSource, UserInfo } from './src/context';
18
18
  export { ExternalHttpProvider } from './src/buildRestDataProvider';
19
- export { IdEntity } from './src/id-entity';
20
19
  export { SortSegment, Sort } from './src/sort';
21
20
  export { OneToMany, CompoundIdField } from './src/column';
22
21
  export { Filter } from './src/filter/filter-interfaces';
package/esm/index.js CHANGED
@@ -1,7 +1,7 @@
1
1
  /*
2
2
  * Public API Surface of remult
3
3
  */
4
- export { Field, Fields, Entity, EntityBase, ControllerBase, FieldType, getFields, getEntityRef, ValueListFieldType, getValueList, ValueListInfo, CaptionTransformer } from './src/remult3';
4
+ export { Field, Fields, Entity, IdEntity, EntityBase, ControllerBase, FieldType, getFields, getEntityRef, ValueListFieldType, getValueList, ValueListInfo, CaptionTransformer } from './src/remult3';
5
5
  export { describeClass } from './src/remult3/DecoratorReplacer';
6
6
  export { RestDataProvider } from './src/data-providers/rest-data-provider'; //V
7
7
  export { InMemoryDataProvider } from './src/data-providers/in-memory-database'; //V
@@ -13,7 +13,6 @@ export { JsonDataProvider } from './src/data-providers/json-data-provider'; //V
13
13
  //export * from './src/data-api'; //reconsider if to make internal
14
14
  export { Controller, BackendMethod, ProgressListener } from './src/server-action';
15
15
  export { Allow, Remult, isBackend, EventSource } from './src/context';
16
- export { IdEntity } from './src/id-entity';
17
16
  export { Sort } from './src/sort';
18
17
  export { OneToMany, CompoundIdField } from './src/column';
19
18
  export { Filter } from './src/filter/filter-interfaces';
@@ -1,5 +1,5 @@
1
1
  import * as express from 'express';
2
- import { RemultServerCore, RemultServerOptions } from './server/expressBridge';
2
+ import type { RemultServerCore, RemultServerOptions } from './server/expressBridge';
3
3
  export declare function remultExpress(options?: RemultServerOptions<express.Request> & {
4
4
  bodyParser?: boolean;
5
5
  bodySizeLimit?: string;
@@ -20,6 +20,6 @@ export function remultExpress(options) {
20
20
  return Object.assign(app, {
21
21
  getRemult: (req) => server.getRemult(req),
22
22
  openApiDoc: (options) => server.openApiDoc(options),
23
- withRemult: (...args) => server.withRemult(...args)
23
+ withRemult: (req, res, next) => server.withRemult(req, res, next),
24
24
  });
25
25
  }
@@ -31,7 +31,7 @@ export function remultFastify(options) {
31
31
  }
32
32
  const api = createRemultServer(options, {
33
33
  buildGenericRequestInfo: req => req,
34
- getRequestBody: async (req) => req.body
34
+ getRequestBody: async (req) => req.body,
35
35
  });
36
36
  const pluginFunction = async (instance, op) => {
37
37
  //@ts-ignore
@@ -61,10 +61,6 @@ export function remultFastify(options) {
61
61
  return Object.assign(pluginFunction, {
62
62
  getRemult: x => api.getRemult(x),
63
63
  openApiDoc: x => api.openApiDoc(x),
64
- withRemult: (req, what) => new Promise((resolve) => {
65
- return api.withRemult(req, undefined, () => {
66
- what().then(resolve);
67
- });
68
- })
64
+ withRemult: (req, what) => api.withRemultPromise(req, what),
69
65
  });
70
66
  }
@@ -22,7 +22,7 @@ export function remultFresh(options, response) {
22
22
  return ctx.next();
23
23
  }
24
24
  ;
25
- }
25
+ },
26
26
  };
27
27
  }
28
28
  ;
@@ -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
- static getDb(remult?: Remult): Db;
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<{
@@ -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(this.db, undefined));
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);
@@ -103,12 +103,6 @@ export function remultNextApp(options) {
103
103
  POST: handler,
104
104
  PUT: handler,
105
105
  DELETE: handler,
106
- withRemult: (what) => {
107
- return new Promise((resolve) => {
108
- return result.withRemult({}, undefined, () => {
109
- what().then(resolve);
110
- });
111
- });
112
- }
106
+ withRemult: (what) => result.withRemultPromise(undefined, what),
113
107
  };
114
108
  }
@@ -1,9 +1,8 @@
1
1
  import { queuedJobInfoResponse } from '../src/server-action';
2
- import { DataProvider } from '../src/data-interfaces';
2
+ import type { DataProvider } from '../src/data-interfaces';
3
3
  import { Remult, UserInfo } from '../src/context';
4
- import { ClassType } from '../classType';
5
- import { Repository } from '../src/remult3';
6
- import { IdEntity } from '../src/id-entity';
4
+ import type { ClassType } from '../classType';
5
+ import { IdEntity, Repository } from '../src/remult3';
7
6
  import { LiveQueryStorage, SubscriptionServer } from '../src/live-query/SubscriptionServer';
8
7
  export interface RemultServerOptions<RequestType> {
9
8
  /**Entities to use for the api */
@@ -63,6 +62,7 @@ export interface RemultServer<RequestType> extends RemultServerCore<RequestType>
63
62
  withRemult(req: RequestType, res: GenericResponse, next: VoidFunction): any;
64
63
  registerRouter(r: GenericRouter): void;
65
64
  handle(req: RequestType, gRes?: GenericResponse): Promise<ServerHandleResponse | undefined>;
65
+ withRemultPromise<T>(request: RequestType, what: () => Promise<T>): Promise<T>;
66
66
  }
67
67
  export interface RemultServerCore<RequestType> {
68
68
  getRemult(req: RequestType): Promise<Remult>;
@@ -2,8 +2,7 @@ import { __decorate, __metadata } from "tslib";
2
2
  import { Action, actionInfo, classBackendMethodsArray, serverActionField } from '../src/server-action';
3
3
  import { DataApi, serializeError } from '../src/data-api';
4
4
  import { allEntities, Remult } from '../src/context';
5
- import { Entity, Fields, getEntityKey } from '../src/remult3';
6
- import { IdEntity } from '../src/id-entity';
5
+ import { Entity, Fields, getEntityKey, IdEntity } from '../src/remult3';
7
6
  import { remult } from '../src/remult-proxy';
8
7
  import { LiveQueryPublisher, InMemoryLiveQueryStorage } from '../src/live-query/SubscriptionServer';
9
8
  import { liveQueryKeepAliveRoute, streamUrl } from '../src/live-query/SubscriptionChannel';
@@ -30,6 +29,7 @@ export function createRemultServerCore(options, serverCoreOptions) {
30
29
  if (options.ensureSchema === undefined)
31
30
  options.ensureSchema = true;
32
31
  RemultAsyncLocalStorage.enable();
32
+ const entitiesMetaData = [];
33
33
  dataProvider = dataProvider.then(async (dp) => {
34
34
  var remult = new Remult(dp);
35
35
  await new Promise((res) => {
@@ -42,10 +42,10 @@ export function createRemultServerCore(options, serverCoreOptions) {
42
42
  started = true;
43
43
  console.time("Ensure Schema");
44
44
  };
45
+ entitiesMetaData.push(...options.entities.map(e => remult.repo(e).metadata));
45
46
  if (dp.ensureSchema) {
46
47
  startConsoleLog();
47
- const entitiesMetadata = options.entities.map(e => remult.repo(e).metadata);
48
- await dp.ensureSchema(entitiesMetadata);
48
+ await dp.ensureSchema(entitiesMetaData);
49
49
  }
50
50
  for (const item of [options.liveQueryStorage, options.queueStorage]) {
51
51
  if (item?.ensureSchema) {
@@ -76,7 +76,7 @@ export function createRemultServerCore(options, serverCoreOptions) {
76
76
  if (options.rootPath === undefined)
77
77
  options.rootPath = '/api';
78
78
  actionInfo.runningOnServer = true;
79
- let bridge = new RemultServerImplementation(new inProcessQueueHandler(options.queueStorage), options, dataProvider, serverCoreOptions);
79
+ let bridge = new RemultServerImplementation(new inProcessQueueHandler(options.queueStorage), options, dataProvider, serverCoreOptions, entitiesMetaData);
80
80
  return bridge;
81
81
  }
82
82
  ;
@@ -112,11 +112,12 @@ export class RemultAsyncLocalStorage {
112
112
  RemultAsyncLocalStorage.instance = new RemultAsyncLocalStorage(undefined);
113
113
  /* @internal*/
114
114
  export class RemultServerImplementation {
115
- constructor(queue, options, dataProvider, coreOptions) {
115
+ constructor(queue, options, dataProvider, coreOptions, entitiesMetaData) {
116
116
  this.queue = queue;
117
117
  this.options = options;
118
118
  this.dataProvider = dataProvider;
119
119
  this.coreOptions = coreOptions;
120
+ this.entitiesMetaData = entitiesMetaData;
120
121
  this.liveQueryStorage = new InMemoryLiveQueryStorage();
121
122
  this.runWithSerializedJsonContextData = async (jsonContextData, entityKey, what) => {
122
123
  for (const e of this.options.entities) {
@@ -154,6 +155,23 @@ export class RemultServerImplementation {
154
155
  if (options.subscriptionServer)
155
156
  this.subscriptionServer = options.subscriptionServer;
156
157
  }
158
+ withRemultPromise(request, what) {
159
+ return new Promise((resolve, error) => {
160
+ return this.withRemult(request, undefined, () => {
161
+ try {
162
+ what().then(resolve).catch(error);
163
+ }
164
+ catch (err) {
165
+ error(err);
166
+ }
167
+ });
168
+ });
169
+ }
170
+ getEntities() {
171
+ //TODO - consider using entitiesMetaData - but it may require making it all awaitable
172
+ var r = new Remult();
173
+ return this.options.entities.map(x => r.repo(x).metadata);
174
+ }
157
175
  ;
158
176
  getRouteImpl() {
159
177
  if (!this.routeImpl) {
@@ -249,6 +267,7 @@ export class RemultServerImplementation {
249
267
  //@ts-ignore
250
268
  .delete(this.process(async (c, req, res, orig) => dataApiFactory(c).delete(res, orig.params.id)));
251
269
  }
270
+ //runs with remult but without init request
252
271
  async runWithRemult(what) {
253
272
  let remult = new Remult();
254
273
  remult.liveQueryPublisher = new LiveQueryPublisher(() => remult.subscriptionServer, () => remult.liveQueryStorage, this.runWithSerializedJsonContextData);
@@ -337,7 +356,6 @@ export class RemultServerImplementation {
337
356
  });
338
357
  }
339
358
  openApiDoc(options) {
340
- let r = new Remult();
341
359
  if (!options.version)
342
360
  options.version = "1.0.0";
343
361
  let spec = {
@@ -386,9 +404,8 @@ export class RemultServerImplementation {
386
404
  return item;
387
405
  }
388
406
  };
389
- for (const e of this.options.entities) {
390
- let meta = r.repo(e).metadata;
391
- let key = getEntityKey(e);
407
+ for (const meta of this.getEntities()) {
408
+ let key = meta.key;
392
409
  let parameters = [];
393
410
  if (key) {
394
411
  let mutationKey = key;
@@ -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';
@@ -1,6 +1,6 @@
1
1
  import { Remult } from './context';
2
- import { FindOptions, Repository } from './remult3';
3
- import { ErrorInfo } from './data-interfaces';
2
+ import type { FindOptions, Repository } from './remult3';
3
+ import type { ErrorInfo } from './data-interfaces';
4
4
  export declare class DataApi<T = any> {
5
5
  private repository;
6
6
  private remult;
@@ -327,6 +327,9 @@ export declare class EntityBase {
327
327
  isNew(): boolean;
328
328
  get $(): FieldsRef<this>;
329
329
  }
330
+ export declare class IdEntity extends EntityBase {
331
+ id: string;
332
+ }
330
333
  export declare class ControllerBase {
331
334
  protected remult: Remult;
332
335
  constructor(remult?: Remult);
@@ -1,3 +1,4 @@
1
+ import { __decorate, __metadata } from "tslib";
1
2
  import { CompoundIdField, LookupColumn, makeTitle } from '../column';
2
3
  import { allEntities, Remult, isBackend, queryConfig as queryConfig, setControllerSettings } from "../context";
3
4
  import { entityFilterToJson, Filter } from "../filter/filter-interfaces";
@@ -1842,6 +1843,12 @@ export class EntityBase {
1842
1843
  isNew() { return this._.isNew(); }
1843
1844
  get $() { return this._.fields; }
1844
1845
  }
1846
+ export class IdEntity extends EntityBase {
1847
+ }
1848
+ __decorate([
1849
+ Fields.uuid(),
1850
+ __metadata("design:type", String)
1851
+ ], IdEntity.prototype, "id", void 0);
1845
1852
  export class ControllerBase {
1846
1853
  constructor(remult) {
1847
1854
  this.remult = remult || defaultRemult;
@@ -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
- databasesTesters.push(testInMemory);
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) => {
@@ -1,5 +1,4 @@
1
- import { EntityBase } from "../remult3";
2
- import { IdEntity } from "../id-entity";
1
+ import { EntityBase, IdEntity } from "../remult3";
3
2
  export declare class stam extends EntityBase {
4
3
  id: number;
5
4
  title: string;
@@ -1,6 +1,6 @@
1
1
  import { __decorate, __metadata } from "tslib";
2
2
  import { DataApi } from "../data-api";
3
- import { Fields, Entity, EntityBase, Field } from "../remult3";
3
+ import { Fields, Entity, EntityBase, Field, IdEntity } from "../remult3";
4
4
  import { c } from "../tests/c";
5
5
  import { Done } from "../tests/Done";
6
6
  import { h } from "../tests/h";
@@ -17,7 +17,6 @@ import { entityForrawFilter1 } from "../tests/entityForCustomFilter";
17
17
  import { entityWithValidationsOnColumn } from "../tests/entityWithValidationsOnColumn";
18
18
  import { Validators } from "../validators";
19
19
  import { Status } from "../tests/testModel/models";
20
- import { IdEntity } from "../id-entity";
21
20
  import { describeClass } from "../remult3/DecoratorReplacer";
22
21
  import { DataProviderLiveQueryStorage, LiveQueryStorageEntity } from "../../live-query/data-provider-live-query-storage";
23
22
  import { v4 as uuid } from 'uuid';
@@ -749,18 +748,24 @@ testAll("task with enum string", async ({ createEntity }) => {
749
748
  expect(await r.count({ priority: PriorityWithString.Critical })).toBe(1);
750
749
  expect(await r.count({ priority: PriorityWithString.Low })).toBe(0);
751
750
  });
752
- testAll("test transaction rollback", async ({ db }) => {
751
+ testAll("test transaction rollback", async ({ db, createEntity }) => {
753
752
  let fail = true;
753
+ const r = await createEntity(stam);
754
+ await r.insert({ title: "task b", id: 1 });
754
755
  try {
755
- await db.transaction(async () => {
756
+ await db.transaction(async (db) => {
757
+ var remultWithTransaction = new Remult(db).repo(stam);
758
+ await remultWithTransaction.insert({ title: "task a", id: 2 });
759
+ expect(await remultWithTransaction.count()).toBe(2);
756
760
  throw "error";
757
761
  });
758
762
  fail = false;
759
763
  }
760
764
  catch {
761
765
  }
766
+ expect(await r.count()).toBe(1);
762
767
  expect(fail).toBe(true);
763
- });
768
+ }, false, { exclude: [TestDbs.inMemory] });
764
769
  testAll("test date", async ({ createEntity }) => {
765
770
  const e = class {
766
771
  constructor() {
package/graphql.d.ts CHANGED
@@ -1,5 +1,17 @@
1
- import { RemultExpressServer } from "./remult-express";
2
- export declare function remultGraphql(api: RemultExpressServer): {
3
- rootValue: {};
4
- schema: string;
1
+ import { FieldsMetadata, Remult } from './index';
2
+ import type { ClassType } from './classType';
3
+ export declare function remultGraphql(options: {
4
+ removeComments?: boolean;
5
+ entities: ClassType<any>[];
6
+ getRemultFromRequest?: (req: any) => Remult;
7
+ }): {
8
+ resolvers: {
9
+ Query: Record<string, unknown>;
10
+ Mutation: Record<string, unknown>;
11
+ };
12
+ rootValue: Record<string, any>;
13
+ typeDefs: string;
5
14
  };
15
+ export declare function translateWhereToRestBody<T>(fields: FieldsMetadata<T>, { where }: {
16
+ where: any;
17
+ }): any;