remult 0.22.11-exp2.0 → 0.22.11

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.
@@ -1,4 +1,4 @@
1
- import type { ClientSession, Db, MongoClient } from 'mongodb';
1
+ import { type ClientSession, type Db, type MongoClient } from 'mongodb';
2
2
  import type { DataProvider, EntityDataProvider, EntityFilter, EntityMetadata, Remult } from '.';
3
3
  import type { RepositoryOverloads } from './src/remult3/RepositoryImplementation';
4
4
  export declare class MongoDataProvider implements DataProvider {
@@ -1,3 +1,4 @@
1
+ import { ObjectId, } from 'mongodb';
1
2
  import { CompoundIdField, Filter } from '.';
2
3
  import { dbNamesOf } from './src/filter/filter-consumer-bridge-to-sql-request';
3
4
  import { remult as remultContext } from './src/remult-proxy';
@@ -72,14 +73,14 @@ class MongoEntityDataProvider {
72
73
  let val = row[nameProvider.$dbNameOf(col)];
73
74
  if (isNull(val))
74
75
  val = null;
75
- result[col.key] = col.valueConverter.fromDb(val);
76
+ result[col.key] = fromDb(col, val);
76
77
  }
77
78
  return result;
78
79
  }
79
80
  translateToDb(row, nameProvider) {
80
81
  let result = {};
81
82
  for (const col of this.entity.fields) {
82
- let val = col.valueConverter.toDb(row[col.key]);
83
+ let val = toDb(col, row[col.key]);
83
84
  if (val === null)
84
85
  val = NULL;
85
86
  result[nameProvider.$dbNameOf(col)] = val;
@@ -218,7 +219,7 @@ class FilterConsumerBridgeToMongo {
218
219
  isIn(col, val) {
219
220
  this.result.push(() => ({
220
221
  [this.nameProvider.$dbNameOf(col)]: {
221
- $in: val.map((x) => col.valueConverter.toDb(x)),
222
+ $in: val.map((x) => toDb(col, x)),
222
223
  },
223
224
  }));
224
225
  }
@@ -250,7 +251,7 @@ class FilterConsumerBridgeToMongo {
250
251
  add(col, val, operator) {
251
252
  this.result.push(() => ({
252
253
  [this.nameProvider.$dbNameOf(col)]: {
253
- [operator]: isNull(val) ? val : col.valueConverter.toDb(val),
254
+ [operator]: isNull(val) ? val : toDb(col, val),
254
255
  },
255
256
  }));
256
257
  }
@@ -267,3 +268,13 @@ class FilterConsumerBridgeToMongo {
267
268
  // })());
268
269
  }
269
270
  }
271
+ function toDb(col, val) {
272
+ if (col.valueConverter.fieldTypeInDb == 'dbid')
273
+ return val ? new ObjectId(val) : val === null ? null : undefined;
274
+ return col.valueConverter.toDb(val);
275
+ }
276
+ function fromDb(col, val) {
277
+ if (col.valueConverter.fieldTypeInDb == 'dbid')
278
+ return val ? val.toHexString() : val === null ? null : undefined;
279
+ return col.valueConverter.fromDb(val);
280
+ }
@@ -2,8 +2,8 @@ import type { JsonEntityStorage } from '../index';
2
2
  import { JsonDataProvider } from '../index';
3
3
  export declare class JsonEntityFileStorage implements JsonEntityStorage {
4
4
  private folderPath;
5
- getItem(entityDbName: string): Promise<string>;
6
- setItem(entityDbName: string, json: string): Promise<void>;
5
+ getItem(entityDbName: string): string;
6
+ setItem(entityDbName: string, json: string): void;
7
7
  constructor(folderPath: string);
8
8
  }
9
9
  export declare class JsonFileDataProvider extends JsonDataProvider {
@@ -1,18 +1,16 @@
1
+ import * as fs from 'fs';
2
+ import * as path from 'path';
1
3
  import { JsonDataProvider } from '../index';
2
4
  export class JsonEntityFileStorage {
3
5
  folderPath;
4
- async getItem(entityDbName) {
5
- const path = await import('path');
6
- const fs = await import('fs');
6
+ getItem(entityDbName) {
7
7
  let fn = path.join(this.folderPath, entityDbName) + '.json';
8
8
  if (fs.existsSync(fn)) {
9
9
  return fs.readFileSync(fn).toString();
10
10
  }
11
11
  return undefined;
12
12
  }
13
- async setItem(entityDbName, json) {
14
- const path = await import('path');
15
- const fs = await import('fs');
13
+ setItem(entityDbName, json) {
16
14
  if (!fs.existsSync(this.folderPath)) {
17
15
  fs.mkdirSync(this.folderPath);
18
16
  }
@@ -1,8 +1,8 @@
1
1
  import type { DataProvider, EntityDataProvider } from '../data-interfaces';
2
2
  import type { EntityMetadata } from '../remult3/remult3';
3
3
  export interface JsonEntityStorage {
4
- getItem(entityDbName: string): Promise<string | null>;
5
- setItem(entityDbName: string, json: string): Promise<void>;
4
+ getItem(entityDbName: string): string | null;
5
+ setItem(entityDbName: string, json: string): any;
6
6
  }
7
7
  export declare class JsonDataProvider implements DataProvider {
8
8
  private storage;
@@ -21,11 +21,11 @@ class JsonEntityDataProvider {
21
21
  async loadEntityData(what) {
22
22
  let data = [];
23
23
  let dbName = await this.entity.getDbName();
24
- let s = await this.helper.getItem(dbName);
24
+ let s = this.helper.getItem(dbName);
25
25
  if (s)
26
26
  data = JSON.parse(s);
27
27
  let dp = new ArrayEntityDataProvider(this.entity, data);
28
- return what(dp, async () => await this.helper.setItem(dbName, JSON.stringify(data, undefined, 2)));
28
+ return what(dp, () => this.helper.setItem(dbName, JSON.stringify(data, undefined, 2)));
29
29
  }
30
30
  p = Promise.resolve();
31
31
  find(options) {
@@ -35,20 +35,20 @@ class JsonEntityDataProvider {
35
35
  return (this.p = this.p.then(() => this.loadEntityData((dp, save) => dp.count(where))));
36
36
  }
37
37
  update(id, data) {
38
- return (this.p = this.p.then(() => this.loadEntityData((dp, save) => dp.update(id, data).then(async (x) => {
39
- await save();
38
+ return (this.p = this.p.then(() => this.loadEntityData((dp, save) => dp.update(id, data).then((x) => {
39
+ save();
40
40
  return x;
41
41
  }))));
42
42
  }
43
43
  delete(id) {
44
- return (this.p = this.p.then(() => this.loadEntityData((dp, save) => dp.delete(id).then(async (x) => {
45
- await save();
44
+ return (this.p = this.p.then(() => this.loadEntityData((dp, save) => dp.delete(id).then((x) => {
45
+ save();
46
46
  return x;
47
47
  }))));
48
48
  }
49
49
  async insert(data) {
50
- return (this.p = this.p.then(() => this.loadEntityData((dp, save) => dp.insert(data).then(async (x) => {
51
- await save();
50
+ return (this.p = this.p.then(() => this.loadEntityData((dp, save) => dp.insert(data).then((x) => {
51
+ save();
52
52
  return x;
53
53
  }))));
54
54
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "remult",
3
- "version": "0.22.11-exp2.0",
3
+ "version": "0.22.11",
4
4
  "description": "A CRUD framework for full-stack TypeScript",
5
5
  "types": "index.d.ts",
6
6
  "module": "esm/index.js",
package/remult-mongo.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import type { ClientSession, Db, MongoClient } from 'mongodb';
1
+ import { type ClientSession, type Db, type MongoClient } from 'mongodb';
2
2
  import type { DataProvider, EntityDataProvider, EntityFilter, EntityMetadata, Remult } from '.';
3
3
  import type { RepositoryOverloads } from './src/remult3/RepositoryImplementation';
4
4
  export declare class MongoDataProvider implements DataProvider {
package/remult-mongo.js CHANGED
@@ -2,6 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.MongoDataProvider = void 0;
4
4
  var tslib_1 = require("tslib");
5
+ var mongodb_1 = require("mongodb");
5
6
  var _1 = require(".");
6
7
  var filter_consumer_bridge_to_sql_request_1 = require("./src/filter/filter-consumer-bridge-to-sql-request");
7
8
  var remult_proxy_1 = require("./src/remult-proxy");
@@ -111,7 +112,7 @@ var MongoEntityDataProvider = /** @class */ (function () {
111
112
  var val = row[nameProvider.$dbNameOf(col)];
112
113
  if (isNull(val))
113
114
  val = null;
114
- result[col.key] = col.valueConverter.fromDb(val);
115
+ result[col.key] = fromDb(col, val);
115
116
  }
116
117
  }
117
118
  catch (e_1_1) { e_1 = { error: e_1_1 }; }
@@ -129,7 +130,7 @@ var MongoEntityDataProvider = /** @class */ (function () {
129
130
  try {
130
131
  for (var _b = tslib_1.__values(this.entity.fields), _c = _b.next(); !_c.done; _c = _b.next()) {
131
132
  var col = _c.value;
132
- var val = col.valueConverter.toDb(row[col.key]);
133
+ var val = toDb(col, row[col.key]);
133
134
  if (val === null)
134
135
  val = NULL;
135
136
  result[nameProvider.$dbNameOf(col)] = val;
@@ -458,7 +459,7 @@ var FilterConsumerBridgeToMongo = /** @class */ (function () {
458
459
  var _a;
459
460
  return (_a = {},
460
461
  _a[_this.nameProvider.$dbNameOf(col)] = {
461
- $in: val.map(function (x) { return col.valueConverter.toDb(x); }),
462
+ $in: val.map(function (x) { return toDb(col, x); }),
462
463
  },
463
464
  _a);
464
465
  });
@@ -494,7 +495,7 @@ var FilterConsumerBridgeToMongo = /** @class */ (function () {
494
495
  var _a, _b;
495
496
  return (_a = {},
496
497
  _a[_this.nameProvider.$dbNameOf(col)] = (_b = {},
497
- _b[operator] = isNull(val) ? val : col.valueConverter.toDb(val),
498
+ _b[operator] = isNull(val) ? val : toDb(col, val),
498
499
  _b),
499
500
  _a);
500
501
  });
@@ -513,3 +514,13 @@ var FilterConsumerBridgeToMongo = /** @class */ (function () {
513
514
  };
514
515
  return FilterConsumerBridgeToMongo;
515
516
  }());
517
+ function toDb(col, val) {
518
+ if (col.valueConverter.fieldTypeInDb == 'dbid')
519
+ return val ? new mongodb_1.ObjectId(val) : val === null ? null : undefined;
520
+ return col.valueConverter.toDb(val);
521
+ }
522
+ function fromDb(col, val) {
523
+ if (col.valueConverter.fieldTypeInDb == 'dbid')
524
+ return val ? val.toHexString() : val === null ? null : undefined;
525
+ return col.valueConverter.fromDb(val);
526
+ }
@@ -2,8 +2,8 @@ import type { JsonEntityStorage } from '../index';
2
2
  import { JsonDataProvider } from '../index';
3
3
  export declare class JsonEntityFileStorage implements JsonEntityStorage {
4
4
  private folderPath;
5
- getItem(entityDbName: string): Promise<string>;
6
- setItem(entityDbName: string, json: string): Promise<void>;
5
+ getItem(entityDbName: string): string;
6
+ setItem(entityDbName: string, json: string): void;
7
7
  constructor(folderPath: string);
8
8
  }
9
9
  export declare class JsonFileDataProvider extends JsonDataProvider {
@@ -2,49 +2,25 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.JsonFileDataProvider = exports.JsonEntityFileStorage = void 0;
4
4
  var tslib_1 = require("tslib");
5
+ var fs = tslib_1.__importStar(require("fs"));
6
+ var path = tslib_1.__importStar(require("path"));
5
7
  var index_1 = require("../index");
6
8
  var JsonEntityFileStorage = /** @class */ (function () {
7
9
  function JsonEntityFileStorage(folderPath) {
8
10
  this.folderPath = folderPath;
9
11
  }
10
12
  JsonEntityFileStorage.prototype.getItem = function (entityDbName) {
11
- return tslib_1.__awaiter(this, void 0, void 0, function () {
12
- var path, fs, fn;
13
- return tslib_1.__generator(this, function (_a) {
14
- switch (_a.label) {
15
- case 0: return [4 /*yield*/, Promise.resolve().then(function () { return tslib_1.__importStar(require('path')); })];
16
- case 1:
17
- path = _a.sent();
18
- return [4 /*yield*/, Promise.resolve().then(function () { return tslib_1.__importStar(require('fs')); })];
19
- case 2:
20
- fs = _a.sent();
21
- fn = path.join(this.folderPath, entityDbName) + '.json';
22
- if (fs.existsSync(fn)) {
23
- return [2 /*return*/, fs.readFileSync(fn).toString()];
24
- }
25
- return [2 /*return*/, undefined];
26
- }
27
- });
28
- });
13
+ var fn = path.join(this.folderPath, entityDbName) + '.json';
14
+ if (fs.existsSync(fn)) {
15
+ return fs.readFileSync(fn).toString();
16
+ }
17
+ return undefined;
29
18
  };
30
19
  JsonEntityFileStorage.prototype.setItem = function (entityDbName, json) {
31
- return tslib_1.__awaiter(this, void 0, void 0, function () {
32
- var path, fs;
33
- return tslib_1.__generator(this, function (_a) {
34
- switch (_a.label) {
35
- case 0: return [4 /*yield*/, Promise.resolve().then(function () { return tslib_1.__importStar(require('path')); })];
36
- case 1:
37
- path = _a.sent();
38
- return [4 /*yield*/, Promise.resolve().then(function () { return tslib_1.__importStar(require('fs')); })];
39
- case 2:
40
- fs = _a.sent();
41
- if (!fs.existsSync(this.folderPath)) {
42
- fs.mkdirSync(this.folderPath);
43
- }
44
- return [2 /*return*/, fs.writeFileSync(path.join(this.folderPath, entityDbName) + '.json', json)];
45
- }
46
- });
47
- });
20
+ if (!fs.existsSync(this.folderPath)) {
21
+ fs.mkdirSync(this.folderPath);
22
+ }
23
+ return fs.writeFileSync(path.join(this.folderPath, entityDbName) + '.json', json);
48
24
  };
49
25
  return JsonEntityFileStorage;
50
26
  }());
@@ -1,8 +1,8 @@
1
1
  import type { DataProvider, EntityDataProvider } from '../data-interfaces';
2
2
  import type { EntityMetadata } from '../remult3/remult3';
3
3
  export interface JsonEntityStorage {
4
- getItem(entityDbName: string): Promise<string | null>;
5
- setItem(entityDbName: string, json: string): Promise<void>;
4
+ getItem(entityDbName: string): string | null;
5
+ setItem(entityDbName: string, json: string): any;
6
6
  }
7
7
  export declare class JsonDataProvider implements DataProvider {
8
8
  private storage;
@@ -42,18 +42,13 @@ var JsonEntityDataProvider = /** @class */ (function () {
42
42
  return [4 /*yield*/, this.entity.getDbName()];
43
43
  case 1:
44
44
  dbName = _a.sent();
45
- return [4 /*yield*/, this.helper.getItem(dbName)];
46
- case 2:
47
- s = _a.sent();
45
+ s = this.helper.getItem(dbName);
48
46
  if (s)
49
47
  data = JSON.parse(s);
50
48
  dp = new array_entity_data_provider_1.ArrayEntityDataProvider(this.entity, data);
51
- return [2 /*return*/, what(dp, function () { return tslib_1.__awaiter(_this, void 0, void 0, function () { return tslib_1.__generator(this, function (_a) {
52
- switch (_a.label) {
53
- case 0: return [4 /*yield*/, this.helper.setItem(dbName, JSON.stringify(data, undefined, 2))];
54
- case 1: return [2 /*return*/, _a.sent()];
55
- }
56
- }); }); })];
49
+ return [2 /*return*/, what(dp, function () {
50
+ return _this.helper.setItem(dbName, JSON.stringify(data, undefined, 2));
51
+ })];
57
52
  }
58
53
  });
59
54
  });
@@ -74,16 +69,10 @@ var JsonEntityDataProvider = /** @class */ (function () {
74
69
  var _this = this;
75
70
  return (this.p = this.p.then(function () {
76
71
  return _this.loadEntityData(function (dp, save) {
77
- return dp.update(id, data).then(function (x) { return tslib_1.__awaiter(_this, void 0, void 0, function () {
78
- return tslib_1.__generator(this, function (_a) {
79
- switch (_a.label) {
80
- case 0: return [4 /*yield*/, save()];
81
- case 1:
82
- _a.sent();
83
- return [2 /*return*/, x];
84
- }
85
- });
86
- }); });
72
+ return dp.update(id, data).then(function (x) {
73
+ save();
74
+ return x;
75
+ });
87
76
  });
88
77
  }));
89
78
  };
@@ -91,16 +80,10 @@ var JsonEntityDataProvider = /** @class */ (function () {
91
80
  var _this = this;
92
81
  return (this.p = this.p.then(function () {
93
82
  return _this.loadEntityData(function (dp, save) {
94
- return dp.delete(id).then(function (x) { return tslib_1.__awaiter(_this, void 0, void 0, function () {
95
- return tslib_1.__generator(this, function (_a) {
96
- switch (_a.label) {
97
- case 0: return [4 /*yield*/, save()];
98
- case 1:
99
- _a.sent();
100
- return [2 /*return*/, x];
101
- }
102
- });
103
- }); });
83
+ return dp.delete(id).then(function (x) {
84
+ save();
85
+ return x;
86
+ });
104
87
  });
105
88
  }));
106
89
  };
@@ -110,16 +93,10 @@ var JsonEntityDataProvider = /** @class */ (function () {
110
93
  return tslib_1.__generator(this, function (_a) {
111
94
  return [2 /*return*/, (this.p = this.p.then(function () {
112
95
  return _this.loadEntityData(function (dp, save) {
113
- return dp.insert(data).then(function (x) { return tslib_1.__awaiter(_this, void 0, void 0, function () {
114
- return tslib_1.__generator(this, function (_a) {
115
- switch (_a.label) {
116
- case 0: return [4 /*yield*/, save()];
117
- case 1:
118
- _a.sent();
119
- return [2 /*return*/, x];
120
- }
121
- });
122
- }); });
96
+ return dp.insert(data).then(function (x) {
97
+ save();
98
+ return x;
99
+ });
123
100
  });
124
101
  }))];
125
102
  });