remult 3.4.2 → 3.5.0-next.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 (72) hide show
  1. package/esm/internals.js +1 -0
  2. package/esm/postgres/postgres-data-provider.js +4 -5
  3. package/esm/remult-duckdb.js +4 -0
  4. package/esm/remult-knex/index.js +69 -14
  5. package/esm/remult-mongo.js +25 -7
  6. package/esm/remult-sqlite-core.js +4 -1
  7. package/esm/server/remult-api-server.js +14 -6
  8. package/esm/src/buildRestDataProvider.js +8 -6
  9. package/esm/src/context.js +1 -1
  10. package/esm/src/data-api.js +2 -2
  11. package/esm/src/data-interfaces.js +2 -1
  12. package/esm/src/data-providers/array-entity-data-provider.js +9 -4
  13. package/esm/src/data-providers/rest-data-provider-using-fetch.js +13 -6
  14. package/esm/src/data-providers/retry.js +11 -6
  15. package/esm/src/data-providers/sql-database.js +40 -30
  16. package/esm/src/filter/filter-consumer-bridge-to-sql-request.js +35 -4
  17. package/esm/src/filter/filter-interfaces.js +18 -4
  18. package/esm/src/live-query/SubscriptionChannel.js +13 -8
  19. package/esm/src/remult-proxy.js +12 -0
  20. package/esm/src/remult-static.js +2 -0
  21. package/esm/src/remult3/Fields.js +14 -14
  22. package/esm/src/remult3/RepositoryImplementation.js +69 -32
  23. package/esm/src/remult3/remult3.js +2 -0
  24. package/esm/src/server-action.js +43 -14
  25. package/esm/src/sort.js +18 -0
  26. package/index.d.ts +4 -3
  27. package/internals.d.ts +1 -0
  28. package/internals.js +3 -1
  29. package/package.json +41 -1
  30. package/postgres/postgres-data-provider.d.ts +2 -3
  31. package/postgres/postgres-data-provider.js +4 -4
  32. package/remult-duckdb.d.ts +2 -0
  33. package/remult-duckdb.js +4 -0
  34. package/remult-knex/index.d.ts +2 -0
  35. package/remult-knex/index.js +153 -83
  36. package/remult-mongo.js +28 -7
  37. package/remult-sqlite-core.d.ts +2 -1
  38. package/remult-sqlite-core.js +4 -0
  39. package/server/remult-api-server.d.ts +28 -1
  40. package/server/remult-api-server.js +19 -12
  41. package/src/buildRestDataProvider.d.ts +1 -1
  42. package/src/buildRestDataProvider.js +8 -6
  43. package/src/column-interfaces.d.ts +5 -1
  44. package/src/context.js +1 -1
  45. package/src/data-api.js +2 -2
  46. package/src/data-interfaces.d.ts +0 -1
  47. package/src/data-interfaces.js +3 -0
  48. package/src/data-providers/array-entity-data-provider.js +9 -4
  49. package/src/data-providers/rest-data-provider-using-fetch.d.ts +1 -1
  50. package/src/data-providers/rest-data-provider-using-fetch.js +11 -4
  51. package/src/data-providers/retry.d.ts +4 -1
  52. package/src/data-providers/retry.js +26 -16
  53. package/src/data-providers/sql-database.d.ts +7 -3
  54. package/src/data-providers/sql-database.js +99 -81
  55. package/src/filter/filter-consumer-bridge-to-sql-request.d.ts +8 -0
  56. package/src/filter/filter-consumer-bridge-to-sql-request.js +58 -19
  57. package/src/filter/filter-interfaces.js +19 -2
  58. package/src/live-query/SubscriptionChannel.js +16 -10
  59. package/src/remult-proxy.js +28 -0
  60. package/src/remult-static.d.ts +4 -0
  61. package/src/remult-static.js +2 -0
  62. package/src/remult3/Fields.d.ts +5 -3
  63. package/src/remult3/Fields.js +24 -13
  64. package/src/remult3/RepositoryImplementation.d.ts +7 -0
  65. package/src/remult3/RepositoryImplementation.js +325 -264
  66. package/src/remult3/remult3.d.ts +68 -5
  67. package/src/remult3/remult3.js +2 -0
  68. package/src/server-action.d.ts +34 -2
  69. package/src/server-action.js +195 -115
  70. package/src/sort.d.ts +19 -0
  71. package/src/sort.js +20 -1
  72. package/src/sql-command.d.ts +2 -1
@@ -470,7 +470,7 @@ var filterHelper = /** @class */ (function () {
470
470
  try {
471
471
  for (var values_1 = tslib_1.__values(values), values_1_1 = values_1.next(); !values_1_1.done; values_1_1 = values_1.next()) {
472
472
  var v = values_1_1.value;
473
- add.isDifferentFrom(_this.metadata, _this.processVal(v));
473
+ _this.isDifferentFrom(v).__applyToConsumer(add);
474
474
  }
475
475
  }
476
476
  catch (e_3_1) { e_3 = { error: e_3_1 }; }
@@ -487,6 +487,14 @@ var filterHelper = /** @class */ (function () {
487
487
  val = this.processVal(val);
488
488
  if ((val === null || val === undefined) && this.metadata.allowNull)
489
489
  return new Filter(function (add) { return add.isNotNull(_this.metadata); });
490
+ // NULL is a value, so it differs from every other value. SQL `<>` does not.
491
+ if (this.metadata.allowNull)
492
+ return new Filter(function (add) {
493
+ return add.or([
494
+ new Filter(function (add) { return add.isDifferentFrom(_this.metadata, val); }),
495
+ new Filter(function (add) { return add.isNull(_this.metadata); }),
496
+ ]);
497
+ });
490
498
  return new Filter(function (add) { return add.isDifferentFrom(_this.metadata, val); });
491
499
  };
492
500
  filterHelper.prototype.isLessOrEqualTo = function (val) {
@@ -509,6 +517,15 @@ var filterHelper = /** @class */ (function () {
509
517
  filterHelper.prototype.isIn = function (val) {
510
518
  var _this = this;
511
519
  val = val.map(function (x) { return _this.processVal(x); });
520
+ if (this.metadata.allowNull) {
521
+ var values_2 = val.filter(function (x) { return x !== null && x !== undefined; });
522
+ if (values_2.length !== val.length) {
523
+ var isNull_1 = new Filter(function (add) { return add.isNull(_this.metadata); });
524
+ if (values_2.length === 0)
525
+ return isNull_1;
526
+ return new Filter(function (add) { return add.or([_this.isIn(values_2), isNull_1]); });
527
+ }
528
+ }
512
529
  if ((val === null || val === void 0 ? void 0 : val.length) == 1 && val[0] != undefined && val[0] !== null)
513
530
  return new Filter(function (add) { return add.isEqualTo(_this.metadata, val[0]); });
514
531
  return new Filter(function (add) { return add.isIn(_this.metadata, val); });
@@ -847,7 +864,7 @@ function buildFilterFromRequestParameters(entity, filterInfo) {
847
864
  addFilter('.ne', function (val) { return ({ $ne: val }); });
848
865
  addFilter('.in', function (val) { return val; }, true);
849
866
  var nullFilter = filterInfo.get(col.key + '.null');
850
- if (nullFilter) {
867
+ if (nullFilter !== undefined) {
851
868
  nullFilter = nullFilter.toString().trim().toLowerCase();
852
869
  switch (nullFilter) {
853
870
  case 'y':
@@ -64,12 +64,6 @@ var LiveQuerySubscriber = /** @class */ (function () {
64
64
  var _this = this;
65
65
  what(function (reducer) {
66
66
  _this.defaultQueryState = reducer(_this.defaultQueryState);
67
- if (changes.find(function (c) { return c.type === 'add' || c.type === 'replace'; })) {
68
- if (_this.query.options.orderBy) {
69
- var o_1 = sort_js_1.Sort.translateOrderByToSort(_this.repo.metadata, _this.query.options.orderBy);
70
- _this.defaultQueryState.sort(function (a, b) { return o_1.compare(a, b); });
71
- }
72
- }
73
67
  });
74
68
  var _loop_1 = function (l) {
75
69
  what(function (reducer) {
@@ -138,8 +132,10 @@ var LiveQuerySubscriber = /** @class */ (function () {
138
132
  element.data.item = loadedItems[index];
139
133
  }
140
134
  this.forListeners(function (listener) {
141
- listener(function (items) {
142
- var e_2, _a;
135
+ listener(function (items, _a) {
136
+ var e_2, _b;
137
+ var _c;
138
+ var _d = _a === void 0 ? {} : _a, _e = _d.pos, pos = _e === void 0 ? 'auto' : _e;
143
139
  if (!items)
144
140
  items = [];
145
141
  var _loop_2 = function (message) {
@@ -160,7 +156,10 @@ var LiveQuerySubscriber = /** @class */ (function () {
160
156
  return _this.repo.metadata.idMetadata.getId(x) !==
161
157
  _this.repo.metadata.idMetadata.getId(message.data.item);
162
158
  });
163
- items.push(message.data.item);
159
+ if (pos === 'first')
160
+ items.unshift(message.data.item);
161
+ else
162
+ items.push(message.data.item);
164
163
  break;
165
164
  case 'remove':
166
165
  items = items.filter(function (x) {
@@ -178,10 +177,17 @@ var LiveQuerySubscriber = /** @class */ (function () {
178
177
  catch (e_2_1) { e_2 = { error: e_2_1 }; }
179
178
  finally {
180
179
  try {
181
- if (data_1_1 && !data_1_1.done && (_a = data_1.return)) _a.call(data_1);
180
+ if (data_1_1 && !data_1_1.done && (_b = data_1.return)) _b.call(data_1);
182
181
  }
183
182
  finally { if (e_2) throw e_2.error; }
184
183
  }
184
+ var orderBy = (_c = _this.query.options.orderBy) !== null && _c !== void 0 ? _c : _this.repo.metadata.options.defaultOrderBy;
185
+ if (pos === 'auto' &&
186
+ orderBy &&
187
+ data.some(function (m) { return m.type === 'add' || m.type === 'replace'; })) {
188
+ var o_1 = sort_js_1.Sort.translateOrderByToSort(_this.repo.metadata, orderBy);
189
+ items.sort(function (a, b) { return o_1.compare(a, b); });
190
+ }
185
191
  return items;
186
192
  });
187
193
  }, data);
@@ -173,6 +173,34 @@ var RemultProxy = /** @class */ (function () {
173
173
  .remultFactory())
174
174
  .repo.apply(_a, tslib_1.__spreadArray([], tslib_1.__read(args), false)).findId(a, b);
175
175
  },
176
+ _a.findIdOrThrow = function (a, b) {
177
+ var _a;
178
+ return (_a = self
179
+ .remultFactory())
180
+ .repo.apply(_a, tslib_1.__spreadArray([], tslib_1.__read(args), false)).findIdOrThrow(a, b);
181
+ },
182
+ _a.findFirstOrThrow = function () {
183
+ var _a, _b;
184
+ var args2 = [];
185
+ for (var _i = 0; _i < arguments.length; _i++) {
186
+ args2[_i] = arguments[_i];
187
+ }
188
+ return (_a = (_b = self
189
+ .remultFactory())
190
+ .repo.apply(_b, tslib_1.__spreadArray([], tslib_1.__read(args), false)))
191
+ .findFirstOrThrow.apply(_a, tslib_1.__spreadArray([], tslib_1.__read(args2), false));
192
+ },
193
+ _a.findOneOrThrow = function () {
194
+ var _a, _b;
195
+ var args2 = [];
196
+ for (var _i = 0; _i < arguments.length; _i++) {
197
+ args2[_i] = arguments[_i];
198
+ }
199
+ return (_a = (_b = self
200
+ .remultFactory())
201
+ .repo.apply(_b, tslib_1.__spreadArray([], tslib_1.__read(args), false)))
202
+ .findOneOrThrow.apply(_a, tslib_1.__spreadArray([], tslib_1.__read(args2), false));
203
+ },
176
204
  //@ts-ignore
177
205
  _a.toJson = function (json) {
178
206
  var _a;
@@ -2,6 +2,8 @@ import type { ClassType } from '../classType.js';
2
2
  import type { ClassHelper, Remult, RemultAsyncLocalStorage } from './context.js';
3
3
  import type { DataProvider } from './data-interfaces.js';
4
4
  import type { columnInfo } from './remult3/columnInfo.js';
5
+ import type { BackendMethodSerializer } from './server-action.js';
6
+ import type { NullSort } from './sort.js';
5
7
  export declare const remultStatic: {
6
8
  defaultRemultFactory: () => Remult;
7
9
  remultFactory: () => Remult;
@@ -21,8 +23,10 @@ export declare const remultStatic: {
21
23
  };
22
24
  fieldOptionsEnricher: any;
23
25
  labelTransformer: any;
26
+ nullSort: typeof NullSort;
24
27
  defaultIdFactory: () => string;
25
28
  defaultDataProvider: () => Promise<DataProvider | undefined>;
29
+ backendMethodSerializer: BackendMethodSerializer | undefined;
26
30
  };
27
31
  export declare function defaultFactory(): Remult;
28
32
  export declare function resetFactory(): void;
@@ -25,10 +25,12 @@ var x = {
25
25
  },
26
26
  fieldOptionsEnricher: undefined,
27
27
  labelTransformer: undefined,
28
+ nullSort: undefined,
28
29
  defaultIdFactory: undefined,
29
30
  defaultDataProvider: function () {
30
31
  return undefined;
31
32
  },
33
+ backendMethodSerializer: undefined,
32
34
  };
33
35
  if ((typeof process !== 'undefined' &&
34
36
  process.env['IGNORE_GLOBAL_REMULT_IN_TESTS']) ||
@@ -2,6 +2,10 @@ import type { ClassType } from '../../classType.js';
2
2
  import type { FieldOptions } from '../column-interfaces.js';
3
3
  import type { Remult } from '../context.js';
4
4
  import type { FindOptions, RelationOptions, ClassFieldDecorator, ClassFieldDecoratorContextStub } from './remult3.js';
5
+ export type IdFieldOptions<entityType = unknown> = FieldOptions<entityType, string> & {
6
+ /** Generates the id for a new row. Defaults to `Fields.defaultIdFactory`. */
7
+ idFactory?: () => string;
8
+ };
5
9
  export declare class Fields {
6
10
  /**
7
11
  * Stored as a JSON.stringify - to store as json use Fields.json
@@ -71,9 +75,7 @@ export declare class Fields {
71
75
  * }
72
76
  * ```
73
77
  */
74
- static id<entityType = unknown>(options?: FieldOptions<entityType, string> & {
75
- idFactory?: () => string;
76
- }): ClassFieldDecorator<entityType, string | undefined>;
78
+ static id<entityType = unknown>(...options: (IdFieldOptions<entityType> | ((options: IdFieldOptions<entityType>, remult: Remult) => void))[]): ClassFieldDecorator<entityType, string | undefined>;
77
79
  /**
78
80
  * Defines a field that can hold a value from a specified set of string literals.
79
81
  * @param {() => readonly valueType[]} optionalValues - A function that returns an array of allowed string literals.
@@ -106,6 +106,8 @@ var Fields = /** @class */ (function () {
106
106
  return Field.apply(void 0, tslib_1.__spreadArray([function () { return Date; },
107
107
  {
108
108
  allowApiUpdate: false,
109
+ // Set on every insert, so never NULL.
110
+ allowNull: false,
109
111
  saving: function (_, ref, _a) {
110
112
  var isNew = _a.isNew;
111
113
  if (isNew)
@@ -121,6 +123,8 @@ var Fields = /** @class */ (function () {
121
123
  return Field.apply(void 0, tslib_1.__spreadArray([function () { return Date; },
122
124
  {
123
125
  allowApiUpdate: false,
126
+ // Set on every save, so never NULL.
127
+ allowNull: false,
124
128
  saving: function (_, ref) {
125
129
  ref.value = new Date();
126
130
  },
@@ -132,7 +136,7 @@ var Fields = /** @class */ (function () {
132
136
  for (var _i = 0; _i < arguments.length; _i++) {
133
137
  options[_i] = arguments[_i];
134
138
  }
135
- return Fields.id(tslib_1.__assign({ idFactory: function () { return crypto.randomUUID(); } }, options));
139
+ return Fields.id.apply(Fields, tslib_1.__spreadArray([{ idFactory: function () { return crypto.randomUUID(); } }], tslib_1.__read(options), false));
136
140
  };
137
141
  Object.defineProperty(Fields, "defaultIdFactory", {
138
142
  /**
@@ -197,18 +201,25 @@ var Fields = /** @class */ (function () {
197
201
  * }
198
202
  * ```
199
203
  */
200
- Fields.id = function (options) {
201
- return Field(function () { return String; }, tslib_1.__assign({ allowApiUpdate: false, defaultValue: function () {
202
- var _a;
203
- var idFactory = (_a = options === null || options === void 0 ? void 0 : options.idFactory) !== null && _a !== void 0 ? _a : Fields.defaultIdFactory;
204
- return idFactory();
205
- }, saving: function (_, r) {
206
- var _a;
207
- if (!r.value) {
208
- var idFactory = (_a = options === null || options === void 0 ? void 0 : options.idFactory) !== null && _a !== void 0 ? _a : Fields.defaultIdFactory;
209
- r.value = idFactory();
210
- }
211
- } }, options));
204
+ Fields.id = function () {
205
+ var options = [];
206
+ for (var _i = 0; _i < arguments.length; _i++) {
207
+ options[_i] = arguments[_i];
208
+ }
209
+ // Read once the options are merged, so a later option or a function option can set it.
210
+ var idFactory;
211
+ var newId = function () { return (idFactory !== null && idFactory !== void 0 ? idFactory : Fields.defaultIdFactory)(); };
212
+ return Field.apply(void 0, tslib_1.__spreadArray(tslib_1.__spreadArray([function () { return String; },
213
+ {
214
+ allowApiUpdate: false,
215
+ defaultValue: newId,
216
+ saving: function (_, r) {
217
+ if (!r.value)
218
+ r.value = newId();
219
+ },
220
+ }], tslib_1.__read(options), false), [function (o) {
221
+ idFactory = o.idFactory;
222
+ }], false));
212
223
  };
213
224
  /**
214
225
  * Defines a field that can hold a value from a specified set of string literals.
@@ -22,6 +22,10 @@ export declare class RepositoryImplementation<entityType> implements Repository<
22
22
  message: string;
23
23
  httpStatusCode: number;
24
24
  };
25
+ _noMatchError(): {
26
+ message: string;
27
+ httpStatusCode: number;
28
+ };
25
29
  [getInternalKey](): this;
26
30
  _createAfterFilter(orderBy: EntityOrderBy<entityType>, lastRow: entityType): Promise<EntityFilter<entityType>>;
27
31
  relations(item: entityType): RepositoryRelations<entityType>;
@@ -90,6 +94,9 @@ export declare class RepositoryImplementation<entityType> implements Repository<
90
94
  create(item?: Partial<MembersOnly<entityType>>): entityType;
91
95
  _fixTypes(item: any): Promise<any>;
92
96
  findId(id: any, options?: FindFirstOptionsBase<entityType>): Promise<entityType | undefined | null>;
97
+ findIdOrThrow(id: any, options?: FindFirstOptionsBase<entityType>): Promise<entityType>;
98
+ findFirstOrThrow(where?: EntityFilter<entityType>, options?: FindFirstOptions<entityType>): Promise<entityType>;
99
+ findOneOrThrow(options?: FindFirstOptions<entityType>): Promise<entityType>;
93
100
  _translateWhereToFilter(where?: EntityFilter<entityType>): Promise<Filter>;
94
101
  }
95
102
  export type EntityOptionsFactory = (remult: Remult) => EntityOptions;