remult 3.4.1 → 3.5.0-next.1

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 (68) 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 +41 -9
  5. package/esm/remult-mongo.js +27 -9
  6. package/esm/remult-sqlite-core.js +4 -1
  7. package/esm/server/remult-api-server.js +1 -1
  8. package/esm/src/buildRestDataProvider.js +4 -2
  9. package/esm/src/data-api.js +2 -2
  10. package/esm/src/data-interfaces.js +2 -1
  11. package/esm/src/data-providers/array-entity-data-provider.js +5 -0
  12. package/esm/src/data-providers/rest-data-provider-using-fetch.js +2 -2
  13. package/esm/src/data-providers/retry.js +11 -6
  14. package/esm/src/data-providers/sql-database.js +41 -31
  15. package/esm/src/filter/entityDbName.js +6 -10
  16. package/esm/src/filter/fieldDbName.js +8 -14
  17. package/esm/src/filter/filter-consumer-bridge-to-sql-request.js +4 -4
  18. package/esm/src/filter/sqlExpressionInProgress.js +14 -0
  19. package/esm/src/live-query/SubscriptionChannel.js +13 -8
  20. package/esm/src/remult-static.js +1 -0
  21. package/esm/src/remult3/Fields.js +14 -14
  22. package/esm/src/remult3/QueryResultImpl.js +2 -0
  23. package/esm/src/remult3/RepositoryImplementation.js +47 -33
  24. package/esm/src/sort.js +18 -0
  25. package/index.d.ts +1 -1
  26. package/internals.d.ts +1 -0
  27. package/internals.js +3 -1
  28. package/package.json +41 -1
  29. package/postgres/postgres-data-provider.d.ts +2 -3
  30. package/postgres/postgres-data-provider.js +4 -4
  31. package/remult-duckdb.d.ts +2 -0
  32. package/remult-duckdb.js +4 -0
  33. package/remult-knex/index.d.ts +2 -0
  34. package/remult-knex/index.js +127 -80
  35. package/remult-mongo.js +29 -8
  36. package/remult-sqlite-core.d.ts +2 -1
  37. package/remult-sqlite-core.js +4 -0
  38. package/server/remult-api-server.d.ts +12 -1
  39. package/server/remult-api-server.js +1 -1
  40. package/src/buildRestDataProvider.js +3 -1
  41. package/src/column-interfaces.d.ts +5 -1
  42. package/src/data-api.js +2 -2
  43. package/src/data-interfaces.d.ts +0 -1
  44. package/src/data-interfaces.js +3 -0
  45. package/src/data-providers/array-entity-data-provider.js +5 -0
  46. package/src/data-providers/rest-data-provider-using-fetch.js +1 -1
  47. package/src/data-providers/retry.d.ts +4 -1
  48. package/src/data-providers/retry.js +26 -16
  49. package/src/data-providers/sql-database.d.ts +7 -3
  50. package/src/data-providers/sql-database.js +100 -84
  51. package/src/filter/entityDbName.d.ts +1 -1
  52. package/src/filter/entityDbName.js +14 -16
  53. package/src/filter/fieldDbName.js +19 -25
  54. package/src/filter/filter-consumer-bridge-to-sql-request.d.ts +1 -1
  55. package/src/filter/filter-consumer-bridge-to-sql-request.js +7 -5
  56. package/src/filter/sqlExpressionInProgress.d.ts +2 -0
  57. package/src/filter/sqlExpressionInProgress.js +27 -0
  58. package/src/live-query/SubscriptionChannel.js +16 -10
  59. package/src/remult-static.d.ts +2 -0
  60. package/src/remult-static.js +1 -0
  61. package/src/remult3/Fields.d.ts +5 -3
  62. package/src/remult3/Fields.js +24 -13
  63. package/src/remult3/QueryResultImpl.js +2 -0
  64. package/src/remult3/RepositoryImplementation.js +284 -272
  65. package/src/remult3/remult3.d.ts +6 -1
  66. package/src/sort.d.ts +19 -0
  67. package/src/sort.js +20 -1
  68. package/src/sql-command.d.ts +2 -1
@@ -73,7 +73,7 @@ export interface RemultServerOptions<RequestType> {
73
73
  * @param info.entity - (Optional) The entity metadata associated with the error, if applicable.
74
74
  * @param info.exception - (Optional) The exception object or error that occurred.
75
75
  * @param info.httpStatusCode - The HTTP status code.
76
- * @param info.responseBody - The body of the response.
76
+ * @param info.responseBody - The body of the response, without the stack.
77
77
  * @param info.sendError - A method to send a custom error response. Call this method with the desired HTTP status code and response body.
78
78
  *
79
79
  * @returns A promise that resolves when the error handling is complete.
@@ -85,6 +85,17 @@ export interface RemultServerOptions<RequestType> {
85
85
  * }
86
86
  * }
87
87
  * })
88
+ * @example
89
+ * // Send the stack outside production
90
+ * export const api = remultApi({
91
+ * error: async (e) => {
92
+ * if (process.env.NODE_ENV === 'production') return
93
+ * e.sendError(e.httpStatusCode, {
94
+ * ...e.responseBody,
95
+ * stack: e.exception?.stack,
96
+ * })
97
+ * },
98
+ * })
88
99
  */
89
100
  error?: (info: {
90
101
  req?: RequestType;
@@ -1255,7 +1255,7 @@ var ResponseBridgeToDataApiResponse = /** @class */ (function () {
1255
1255
  console.error({
1256
1256
  message: body.message,
1257
1257
  httpStatusCode: httpStatusCode,
1258
- stack: (_a = data.stack) === null || _a === void 0 ? void 0 : _a.split('\n'),
1258
+ stack: (_a = exception === null || exception === void 0 ? void 0 : exception.stack) === null || _a === void 0 ? void 0 : _a.split('\n'),
1259
1259
  url: (_b = _this.genReq) === null || _b === void 0 ? void 0 : _b.url,
1260
1260
  method: (_c = _this.genReq) === null || _c === void 0 ? void 0 : _c.method,
1261
1261
  });
@@ -39,7 +39,9 @@ var HttpProviderBridgeToRestDataProviderHttpProvider = /** @class */ (function (
39
39
  var _this = this;
40
40
  return tslib_1.__generator(this, function (_a) {
41
41
  switch (_a.label) {
42
- case 0: return [4 /*yield*/, (0, retry_js_1.retry)(function () { return toPromise(_this.http.post(url, data)); })];
42
+ case 0: return [4 /*yield*/, (0, retry_js_1.retry)(function () { return toPromise(_this.http.post(url, data)); }, {
43
+ read: (0, retry_js_1.isReadPost)(url),
44
+ })];
43
45
  case 1: return [2 /*return*/, _a.sent()];
44
46
  }
45
47
  });
@@ -17,7 +17,11 @@ export interface FieldOptions<entityType = unknown, valueType = unknown> {
17
17
  * ```
18
18
  */
19
19
  label?: string;
20
- /** If it can store `null` in the database. @default false */
20
+ /** If it can store `null` in the database.
21
+ *
22
+ * Defaults to `true` on a Date, `Fields.json` or another custom `fieldTypeInDb` (except `integer`, or on a number or boolean), as migrations create those columns without a default value. `false` otherwise, on an id, and on `Fields.createdAt` / `updatedAt`.
23
+ * @see [allowNull by field type](https://remult.dev/docs/field-types#allownull-by-field-type)
24
+ */
21
25
  allowNull?: boolean;
22
26
  /** If a value is required. Short-cut to say `validate: Validators.required`.
23
27
  * @default false
package/src/data-api.js CHANGED
@@ -1127,11 +1127,11 @@ function serializeError(data) {
1127
1127
  data = { message: data.message, modelState: data.modelState };
1128
1128
  }
1129
1129
  else if (data instanceof Error) {
1130
- data = { message: data.message, stack: data.stack };
1130
+ data = { message: data.message };
1131
1131
  }
1132
1132
  var x = JSON.parse(JSON.stringify(data));
1133
1133
  if (!x.message && !x.modelState)
1134
- data = { message: data.message, stack: data.stack };
1134
+ data = { message: data.message };
1135
1135
  if (typeof x === 'string')
1136
1136
  data = { message: x };
1137
1137
  return data;
@@ -80,7 +80,6 @@ export declare class EntityError<entityType = unknown> extends Error implements
80
80
  modelState?: {
81
81
  [Properties in keyof Partial<MembersOnly<entityType>>]?: string;
82
82
  };
83
- stack?: string;
84
83
  exception?: any;
85
84
  httpStatusCode?: number;
86
85
  }
@@ -17,7 +17,10 @@ function extractSort(sort) {
17
17
  var EntityError = /** @class */ (function (_super) {
18
18
  tslib_1.__extends(EntityError, _super);
19
19
  function EntityError(errorInfo) {
20
+ var _newTarget = this.constructor;
20
21
  var _this = _super.call(this, errorInfo.message) || this;
22
+ // The ES5 build gets a plain Error back from super(), so instanceof EntityError would be false.
23
+ Object.setPrototypeOf(_this, _newTarget.prototype);
21
24
  Object.assign(_this, errorInfo);
22
25
  return _this;
23
26
  }
@@ -795,19 +795,24 @@ var FilterConsumerBridgeToObject = /** @class */ (function () {
795
795
  if (this.row[this.dbNames.$dbNameOf(col)] == col.valueConverter.toJson(val))
796
796
  this.ok = false;
797
797
  };
798
+ // As in SQL, NULL never matches an ordering comparison.
798
799
  FilterConsumerBridgeToObject.prototype.isGreaterOrEqualTo = function (col, val) {
800
+ this.isNotNull(col);
799
801
  if (this.row[this.dbNames.$dbNameOf(col)] < col.valueConverter.toJson(val))
800
802
  this.ok = false;
801
803
  };
802
804
  FilterConsumerBridgeToObject.prototype.isGreaterThan = function (col, val) {
805
+ this.isNotNull(col);
803
806
  if (this.row[this.dbNames.$dbNameOf(col)] <= col.valueConverter.toJson(val))
804
807
  this.ok = false;
805
808
  };
806
809
  FilterConsumerBridgeToObject.prototype.isLessOrEqualTo = function (col, val) {
810
+ this.isNotNull(col);
807
811
  if (this.row[this.dbNames.$dbNameOf(col)] > col.valueConverter.toJson(val))
808
812
  this.ok = false;
809
813
  };
810
814
  FilterConsumerBridgeToObject.prototype.isLessThan = function (col, val) {
815
+ this.isNotNull(col);
811
816
  if (this.row[this.dbNames.$dbNameOf(col)] >= col.valueConverter.toJson(val))
812
817
  this.ok = false;
813
818
  };
@@ -43,7 +43,7 @@ var RestDataProviderHttpProviderUsingFetch = /** @class */ (function () {
43
43
  method: 'post',
44
44
  body: JSON.stringify(data),
45
45
  });
46
- })];
46
+ }, { read: (0, retry_js_1.isReadPost)(url) })];
47
47
  case 1: return [2 /*return*/, _a.sent()];
48
48
  }
49
49
  });
@@ -1 +1,4 @@
1
- export declare function retry<T>(what: () => Promise<T>): Promise<T>;
1
+ export declare function retry<T>(what: () => Promise<T>, options?: {
2
+ read: boolean;
3
+ }): Promise<T>;
4
+ export declare const isReadPost: (url: string) => boolean;
@@ -1,31 +1,37 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.isReadPost = void 0;
3
4
  exports.retry = retry;
4
5
  var tslib_1 = require("tslib");
5
6
  var remult3_js_1 = require("../remult3/remult3.js");
6
- function retry(what) {
7
- return tslib_1.__awaiter(this, void 0, void 0, function () {
7
+ // These mean the proxy never reached the server, so any request is safe to send again.
8
+ var isUnreachable = function (err) {
9
+ var _a, _b, _c;
10
+ return ((_a = err.message) === null || _a === void 0 ? void 0 : _a.startsWith('Error occurred while trying to proxy')) ||
11
+ ((_b = err.message) === null || _b === void 0 ? void 0 : _b.startsWith('Error occured while trying to proxy')) ||
12
+ ((_c = err.message) === null || _c === void 0 ? void 0 : _c.includes('http proxy error'));
13
+ };
14
+ // The server may have run the request before failing, so only reads are safe to send again.
15
+ var isServerFailure = function (err) { var _a; return ((_a = err.message) === null || _a === void 0 ? void 0 : _a.startsWith('Gateway Timeout')) || err.status == 500; };
16
+ function retry(what_1) {
17
+ return tslib_1.__awaiter(this, arguments, void 0, function (what, options) {
8
18
  var i, err_1;
9
- var _a, _b, _c, _d;
10
- return tslib_1.__generator(this, function (_e) {
11
- switch (_e.label) {
19
+ if (options === void 0) { options = { read: true }; }
20
+ return tslib_1.__generator(this, function (_a) {
21
+ switch (_a.label) {
12
22
  case 0:
13
23
  i = 0;
14
- _e.label = 1;
24
+ _a.label = 1;
15
25
  case 1:
16
26
  if (!true) return [3 /*break*/, 8];
17
- _e.label = 2;
27
+ _a.label = 2;
18
28
  case 2:
19
- _e.trys.push([2, 4, , 7]);
29
+ _a.trys.push([2, 4, , 7]);
20
30
  return [4 /*yield*/, what()];
21
- case 3: return [2 /*return*/, _e.sent()];
31
+ case 3: return [2 /*return*/, _a.sent()];
22
32
  case 4:
23
- err_1 = _e.sent();
24
- if (!((((_a = err_1.message) === null || _a === void 0 ? void 0 : _a.startsWith('Error occurred while trying to proxy')) ||
25
- ((_b = err_1.message) === null || _b === void 0 ? void 0 : _b.startsWith('Error occured while trying to proxy')) ||
26
- ((_c = err_1.message) === null || _c === void 0 ? void 0 : _c.includes('http proxy error')) ||
27
- ((_d = err_1.message) === null || _d === void 0 ? void 0 : _d.startsWith('Gateway Timeout')) ||
28
- err_1.status == 500) &&
33
+ err_1 = _a.sent();
34
+ if (!((isUnreachable(err_1) || (options.read && isServerFailure(err_1))) &&
29
35
  i++ < remult3_js_1.flags.error500RetryCount)) return [3 /*break*/, 6];
30
36
  return [4 /*yield*/, new Promise(function (res, req) {
31
37
  setTimeout(function () {
@@ -33,7 +39,7 @@ function retry(what) {
33
39
  }, 500);
34
40
  })];
35
41
  case 5:
36
- _e.sent();
42
+ _a.sent();
37
43
  return [3 /*break*/, 1];
38
44
  case 6: throw err_1;
39
45
  case 7: return [3 /*break*/, 1];
@@ -42,3 +48,7 @@ function retry(what) {
42
48
  });
43
49
  });
44
50
  }
51
+ // Finds with a long filter, counts and aggregates are POSTs too, but they only read.
52
+ var readActions = /[?&]__action=(?:(?:get|query|groupBy|count)(?:&|$)|liveQuery-)/;
53
+ var isReadPost = function (url) { return readActions.test(url); };
54
+ exports.isReadPost = isReadPost;
@@ -4,6 +4,7 @@ import type { FieldMetadata } from '../column-interfaces.js';
4
4
  import type { CustomSqlFilterBuilderFunction, EntityDbNamesBase } from '../filter/filter-consumer-bridge-to-sql-request.js';
5
5
  import { type EntityFilter, type EntityMetadata, type FindOptions, type GroupByOptions, type GroupByResult, type MembersOnly, type NumericKeys, type Repository } from '../remult3/remult3.js';
6
6
  import type { RepositoryOverloads } from '../remult3/RepositoryImplementation.js';
7
+ import type { NativeNullSort } from '../sort.js';
7
8
  import type { CanBuildMigrations, MigrationBuilder, MigrationCode } from '../../migrations/migration-types.js';
8
9
  /**
9
10
  * A DataProvider for Sql Databases
@@ -39,6 +40,7 @@ export declare class SqlDatabase implements DataProvider, HasWrapIdentifier, Has
39
40
  */
40
41
  wrapIdentifier: (name: string) => string;
41
42
  get jsonColumnStorage(): JsonColumnStorage;
43
+ get nativeNullSort(): NativeNullSort;
42
44
  ensureSchema(entities: EntityMetadata[]): Promise<void>;
43
45
  /**
44
46
  * Gets the entity data provider.
@@ -131,8 +133,10 @@ export interface RawSelectOptions {
131
133
  }
132
134
  export declare function jsonColumnStorageOf(r: Repository<any>): JsonColumnStorage;
133
135
  export declare function getRowAfterUpdate<entityType>(meta: EntityMetadata<entityType>, dataProvider: EntityDataProvider, data: any, id: any, operation: string): any;
136
+ /** Writes the NULL order only on a nullable column, where NullSort differs from the database's own. */
137
+ export declare function orderBySql(expr: string, isDescending: boolean | undefined, nullable: boolean, native: NativeNullSort): string;
134
138
  /** The select that `find` runs, and the mapping of one result row to field values. With `aliasColumns`, every column is aliased through `wrapIdentifier`, so a row can be read by alias as well as by position. */
135
- export declare function buildFindSql(entity: EntityMetadata, options: EntityDataProviderFindOptions | undefined, e: EntityDbNamesBase, r: SqlCommandWithParameters, orderByNullsFirst: boolean | undefined, limitSyntax: (limit: number, offset: number) => string, { aliasColumns, jsonColumnStorage, }?: {
139
+ export declare function buildFindSql(entity: EntityMetadata, options: EntityDataProviderFindOptions | undefined, e: EntityDbNamesBase, r: SqlCommandWithParameters, nativeNullSort: NativeNullSort, limitSyntax: (limit: number, offset: number) => string, { aliasColumns, jsonColumnStorage, }?: {
136
140
  aliasColumns?: boolean;
137
141
  jsonColumnStorage?: JsonColumnStorage;
138
142
  }): Promise<{
@@ -141,9 +145,9 @@ export declare function buildFindSql(entity: EntityMetadata, options: EntityData
141
145
  /** `valueAt` reads one column of a result row, by its position in the select or by its alias (the field key). */
142
146
  toResult(valueAt: (indexInSelect: number, alias: string) => any): any;
143
147
  }>;
144
- export declare function groupByImpl(options: EntityDataProviderGroupByOptions | undefined, e: EntityDbNamesBase, r: SqlCommand, orderByNullFirst: boolean | undefined, limitSyntax: (limit: number, offset: number) => string, jsonColumnStorage?: JsonColumnStorage): Promise<any[]>;
148
+ export declare function groupByImpl(options: EntityDataProviderGroupByOptions | undefined, e: EntityDbNamesBase, r: SqlCommand, nativeNullSort: NativeNullSort, limitSyntax: (limit: number, offset: number) => string, jsonColumnStorage?: JsonColumnStorage): Promise<any[]>;
145
149
  /** The select that `groupBy` runs, and the mapping of one result row back to a `GroupByResult`. Every column is aliased through `wrapIdentifier`, so a row can be read by alias as well as by position. */
146
- export declare function buildGroupBySql(options: EntityDataProviderGroupByOptions | undefined, e: EntityDbNamesBase, r: SqlCommandWithParameters, orderByNullFirst: boolean | undefined, limitSyntax: (limit: number, offset: number) => string, jsonColumnStorage?: JsonColumnStorage): Promise<{
150
+ export declare function buildGroupBySql(options: EntityDataProviderGroupByOptions | undefined, e: EntityDbNamesBase, r: SqlCommandWithParameters, nativeNullSort: NativeNullSort, limitSyntax: (limit: number, offset: number) => string, jsonColumnStorage?: JsonColumnStorage): Promise<{
147
151
  select: string;
148
152
  /** `valueAt` reads one column of a result row, by its position in the select or by its alias. */
149
153
  toResult(valueAt: (indexInSelect: number, alias: string) => any): any;