remult 3.4.2 → 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 (55) 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 +40 -8
  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 +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 +40 -30
  15. package/esm/src/live-query/SubscriptionChannel.js +13 -8
  16. package/esm/src/remult-static.js +1 -0
  17. package/esm/src/remult3/Fields.js +14 -14
  18. package/esm/src/remult3/RepositoryImplementation.js +45 -32
  19. package/esm/src/sort.js +18 -0
  20. package/index.d.ts +1 -1
  21. package/internals.d.ts +1 -0
  22. package/internals.js +3 -1
  23. package/package.json +41 -1
  24. package/postgres/postgres-data-provider.d.ts +2 -3
  25. package/postgres/postgres-data-provider.js +4 -4
  26. package/remult-duckdb.d.ts +2 -0
  27. package/remult-duckdb.js +4 -0
  28. package/remult-knex/index.d.ts +2 -0
  29. package/remult-knex/index.js +125 -78
  30. package/remult-mongo.js +28 -7
  31. package/remult-sqlite-core.d.ts +2 -1
  32. package/remult-sqlite-core.js +4 -0
  33. package/server/remult-api-server.d.ts +12 -1
  34. package/server/remult-api-server.js +1 -1
  35. package/src/buildRestDataProvider.js +3 -1
  36. package/src/column-interfaces.d.ts +5 -1
  37. package/src/data-api.js +2 -2
  38. package/src/data-interfaces.d.ts +0 -1
  39. package/src/data-interfaces.js +3 -0
  40. package/src/data-providers/array-entity-data-provider.js +5 -0
  41. package/src/data-providers/rest-data-provider-using-fetch.js +1 -1
  42. package/src/data-providers/retry.d.ts +4 -1
  43. package/src/data-providers/retry.js +26 -16
  44. package/src/data-providers/sql-database.d.ts +7 -3
  45. package/src/data-providers/sql-database.js +99 -81
  46. package/src/live-query/SubscriptionChannel.js +16 -10
  47. package/src/remult-static.d.ts +2 -0
  48. package/src/remult-static.js +1 -0
  49. package/src/remult3/Fields.d.ts +5 -3
  50. package/src/remult3/Fields.js +24 -13
  51. package/src/remult3/RepositoryImplementation.js +274 -264
  52. package/src/remult3/remult3.d.ts +6 -1
  53. package/src/sort.d.ts +19 -0
  54. package/src/sort.js +20 -1
  55. package/src/sql-command.d.ts +2 -1
@@ -2,7 +2,7 @@ import { LookupColumn, makeTitle } from '../column.js';
2
2
  import { CompoundIdField } from '../CompoundIdField.js';
3
3
  import { Remult } from '../context.js';
4
4
  import { Filter } from '../filter/filter-interfaces.js';
5
- import { Sort } from '../sort.js';
5
+ import { Sort, isNullFirstSort } from '../sort.js';
6
6
  import { GroupByCountMember, GroupByForApiKey, } from './remult3.js';
7
7
  import { assign } from '../../assign.js';
8
8
  import { EntityError, } from '../data-interfaces.js';
@@ -57,37 +57,31 @@ export class RepositoryImplementation {
57
57
  return this;
58
58
  }
59
59
  async _createAfterFilter(orderBy, lastRow) {
60
- let values = new Map();
61
- for (const s of Sort.translateOrderByToSort(this.metadata, orderBy)
62
- .Segments) {
63
- let existingVal = lastRow[s.field.key];
64
- // if (typeof existingVal !== "string" && typeof existingVal !== "number") {
65
- // }
66
- // else {
67
- // let ei = getEntitySettings(s.field.valueType, false);
68
- // if (ei) {
69
- // existingVal = await this.remult.repo(s.field.valueType).findId(existingVal);
70
- // }
71
- // }
72
- values.set(s.field.key, existingVal);
73
- }
74
- let r = { $or: [] };
75
- let equalToColumn = [];
76
- for (const s of Sort.translateOrderByToSort(this.metadata, orderBy)
77
- .Segments) {
78
- let f = {};
79
- for (const c of equalToColumn) {
80
- f[c.key] = values.get(c.key);
81
- }
82
- equalToColumn.push(s.field);
83
- if (s.isDescending) {
84
- f[s.field.key] = { $lt: values.get(s.field.key) };
60
+ const ref = this.getEntityRef(lastRow);
61
+ const filter = { $or: [] };
62
+ const or = filter.$or;
63
+ let equal = {};
64
+ for (const { field, isDescending } of Sort.translateOrderByToSort(this.metadata, orderBy).Segments) {
65
+ // A string key cannot name an entity member for TypeScript.
66
+ const keyIs = (condition) => ({ [field.key]: condition });
67
+ // An unloaded relation reads `undefined`, its id is there.
68
+ const value = ref.fields.find(field.key).getId() ?? null;
69
+ const nullFirst = isNullFirstSort(isDescending);
70
+ if (value === null) {
71
+ if (nullFirst)
72
+ or.push({ ...equal, $not: keyIs(null) });
85
73
  }
86
- else
87
- f[s.field.key] = { $gt: values.get(s.field.key) };
88
- r.$or.push(f);
74
+ else {
75
+ or.push({
76
+ ...equal,
77
+ ...keyIs(isDescending ? { $lt: value } : { $gt: value }),
78
+ });
79
+ if (field.allowNull && !nullFirst)
80
+ or.push({ ...equal, ...keyIs(null) });
81
+ }
82
+ equal = { ...equal, ...keyIs(value) };
89
83
  }
90
- return r;
84
+ return filter;
91
85
  }
92
86
  relations(item) {
93
87
  return new Proxy({}, {
@@ -760,7 +754,9 @@ export class RepositoryImplementation {
760
754
  }
761
755
  else {
762
756
  for (const row of result) {
763
- Reflect.deleteProperty(row, col.key);
757
+ // Deleting the relation's accessor would drop its change tracking, so only a plain value goes.
758
+ if (!Object.getOwnPropertyDescriptor(row, col.key)?.get)
759
+ Reflect.deleteProperty(row, col.key);
764
760
  }
765
761
  }
766
762
  }
@@ -2118,6 +2114,19 @@ export function buildLabel(label, key, remult, metaData) {
2118
2114
  return makeTitle(key);
2119
2115
  return '';
2120
2116
  }
2117
+ /** Migrations give the column a default value (`''`, `0`, `false`), so it is `not null`. */
2118
+ function hasColumnDefault({ valueType, valueConverter, }) {
2119
+ if (valueType === Number || valueType === Boolean)
2120
+ return true;
2121
+ if (valueType === Date)
2122
+ return false;
2123
+ // A relation id is an integer or a string, and its fieldTypeInDb would load the related entity.
2124
+ if (getEntitySettings(valueType, false))
2125
+ return true;
2126
+ const type = valueConverter?.fieldTypeInDb;
2127
+ // None is a string column, default ''. `dbid` is a Mongo ObjectId marker, not a column type.
2128
+ return !type || type === 'dbid' || type === 'integer';
2129
+ }
2121
2130
  export class columnDefsImpl {
2122
2131
  settings;
2123
2132
  entityDefs;
@@ -2150,7 +2159,7 @@ export class columnDefsImpl {
2150
2159
  return result;
2151
2160
  },
2152
2161
  });
2153
- this.allowNull = !!this.settings.allowNull;
2162
+ this.allowNull = this.settings.allowNull ?? !hasColumnDefault(settings);
2154
2163
  this.valueType = this.settings.valueType;
2155
2164
  this.key = this.settings.key;
2156
2165
  this.inputType = this.settings.inputType;
@@ -2276,6 +2285,10 @@ class EntityFullInfo {
2276
2285
  else
2277
2286
  this.idMetadata.field = [...this.fields][0];
2278
2287
  }
2288
+ // The database rejects NULL in a primary key.
2289
+ for (const f of this.idMetadata.fields)
2290
+ if (f)
2291
+ f.allowNull = f.options.allowNull ?? false;
2279
2292
  }
2280
2293
  apiUpdateAllowed(item) {
2281
2294
  if (this.options.allowApiUpdate === undefined)
package/esm/src/sort.js CHANGED
@@ -1,3 +1,4 @@
1
+ import { remultStatic } from './remult-static.js';
1
2
  import { getRelationFieldInfo } from './remult3/relationInfoMember.js';
2
3
  /**
3
4
  * The `Sort` class is used to describe sorting criteria for queries. It is mainly used internally,
@@ -161,9 +162,26 @@ export class Sort {
161
162
  return orderBy;
162
163
  }
163
164
  }
165
+ /**
166
+ * Where NULL sorts, for the whole app: set it on both the server and the client.
167
+ * @see https://remult.dev/docs/field-types#where-null-sorts
168
+ */
169
+ export const NullSort = remultStatic.nullSort || (remultStatic.nullSort = { order: 'native' });
170
+ /** Remult's own order, without a database: `native` is NULL lowest. */
171
+ export function isNullFirstSort(isDescending) {
172
+ if (NullSort.order === 'native')
173
+ return !isDescending;
174
+ return NullSort.order[isDescending ? 'desc' : 'asc'] === 'NULLS FIRST';
175
+ }
164
176
  export function compareForSort(left, right, descending) {
165
177
  left = fixValueForSort(left);
166
178
  right = fixValueForSort(right);
179
+ // `null > 'a'` and `null < 'a'` are both false, which is not an order.
180
+ if (left == null || right == null) {
181
+ if (left == right)
182
+ return 0;
183
+ return (left == null) === isNullFirstSort(descending) ? -1 : 1;
184
+ }
167
185
  let r = 0;
168
186
  if (left > right)
169
187
  r = 1;
package/index.d.ts CHANGED
@@ -45,7 +45,7 @@ export { Allow, Remult, withRemult, isBackend, EventSource, } from './src/contex
45
45
  export type { Allowed, RemultContext, ApiClient, AllowedForInstance, EventDispatcher, UserInfo, } from './src/context.js';
46
46
  export type { ExternalHttpProvider } from './src/buildRestDataProvider.js';
47
47
  export { Sort } from './src/sort.js';
48
- export type { SortSegment } from './src/sort.js';
48
+ export type { SortSegment, NativeNullSort, NullOrder } from './src/sort.js';
49
49
  export { CompoundIdField } from './src/CompoundIdField.js';
50
50
  export { Filter } from './src/filter/filter-interfaces.js';
51
51
  export type { FilterConsumer, FilterPreciseValues, } from './src/filter/filter-interfaces.js';
package/internals.d.ts CHANGED
@@ -18,6 +18,7 @@ export type { ClassType } from './classType.js';
18
18
  export type { SubscriptionServerRouteApi, SubscriptionServerRouteHandlerArgs, } from './server/remult-api-server.js';
19
19
  export type { DataApiResponse } from './src/data-api.js';
20
20
  export { flags } from './src/remult3/remult3.js';
21
+ export { NullSort } from './src/sort.js';
21
22
  export { SseSubscriptionClient } from './src/live-query/SseSubscriptionClient.js';
22
23
  export { DataProviderPromiseWrapper } from './src/data-interfaces.js';
23
24
  export { pagedQueryResult } from './src/remult3/pagedQueryResult.js';
package/internals.js CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.pagedQueryResult = exports.DataProviderPromiseWrapper = exports.SseSubscriptionClient = exports.flags = exports.isOfType = exports.actionInfo = exports.walkFilter = exports.omitFilterField = exports.__updateEntityBasedOnWhere = exports.getEntitySettings = exports.fieldOptionsEnricher = exports.getControllerRef = exports.controllerRefImpl = exports.decorateColumnSettings = exports.getRelationFieldInfo = exports.getRelationInfo = exports.sqlRelationsFilter = exports.sqlRelations = void 0;
3
+ exports.pagedQueryResult = exports.DataProviderPromiseWrapper = exports.SseSubscriptionClient = exports.NullSort = exports.flags = exports.isOfType = exports.actionInfo = exports.walkFilter = exports.omitFilterField = exports.__updateEntityBasedOnWhere = exports.getEntitySettings = exports.fieldOptionsEnricher = exports.getControllerRef = exports.controllerRefImpl = exports.decorateColumnSettings = exports.getRelationFieldInfo = exports.getRelationInfo = exports.sqlRelationsFilter = exports.sqlRelations = void 0;
4
4
  var remult_static_js_1 = require("./src/remult-static.js");
5
5
  var sql_relations_js_1 = require("./src/data-providers/sql-relations.js");
6
6
  Object.defineProperty(exports, "sqlRelations", { enumerable: true, get: function () { return sql_relations_js_1.sqlRelations; } });
@@ -25,6 +25,8 @@ var isOfType_js_1 = require("./src/isOfType.js");
25
25
  Object.defineProperty(exports, "isOfType", { enumerable: true, get: function () { return isOfType_js_1.isOfType; } });
26
26
  var remult3_js_1 = require("./src/remult3/remult3.js");
27
27
  Object.defineProperty(exports, "flags", { enumerable: true, get: function () { return remult3_js_1.flags; } });
28
+ var sort_js_1 = require("./src/sort.js");
29
+ Object.defineProperty(exports, "NullSort", { enumerable: true, get: function () { return sort_js_1.NullSort; } });
28
30
  var SseSubscriptionClient_js_1 = require("./src/live-query/SseSubscriptionClient.js");
29
31
  Object.defineProperty(exports, "SseSubscriptionClient", { enumerable: true, get: function () { return SseSubscriptionClient_js_1.SseSubscriptionClient; } });
30
32
  var data_interfaces_js_1 = require("./src/data-interfaces.js");
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "remult",
3
- "version": "3.4.2",
3
+ "version": "3.5.0-next.1",
4
4
  "description": "A CRUD framework for full-stack TypeScript",
5
5
  "homepage": "https://remult.dev",
6
6
  "repository": {
@@ -26,6 +26,46 @@
26
26
  "dependencies": {
27
27
  "tslib": "^2.3.1"
28
28
  },
29
+ "peerDependencies": {
30
+ "@duckdb/node-api": ">=1.3.2-alpha.26",
31
+ "@libsql/client": ">=0.5.6",
32
+ "better-sqlite3": ">=12.2.0",
33
+ "cloudflare": ">=4.4.1",
34
+ "knex": ">=2.4.0",
35
+ "mongodb": ">=4.17.1",
36
+ "pg": ">=8.3.0",
37
+ "sql.js": ">=1.9.0",
38
+ "sqlite3": ">=5.1.7"
39
+ },
40
+ "peerDependenciesMeta": {
41
+ "@duckdb/node-api": {
42
+ "optional": true
43
+ },
44
+ "@libsql/client": {
45
+ "optional": true
46
+ },
47
+ "better-sqlite3": {
48
+ "optional": true
49
+ },
50
+ "cloudflare": {
51
+ "optional": true
52
+ },
53
+ "knex": {
54
+ "optional": true
55
+ },
56
+ "mongodb": {
57
+ "optional": true
58
+ },
59
+ "pg": {
60
+ "optional": true
61
+ },
62
+ "sql.js": {
63
+ "optional": true
64
+ },
65
+ "sqlite3": {
66
+ "optional": true
67
+ }
68
+ },
29
69
  "exports": {
30
70
  ".": {
31
71
  "require": "./index.js",
@@ -1,6 +1,7 @@
1
1
  import type { ClientBase, PoolConfig, QueryResult } from 'pg';
2
2
  import { SqlDatabase } from '../src/data-providers/sql-database.js';
3
3
  import type { EntityMetadata } from '../src/remult3/remult3.js';
4
+ import type { NativeNullSort } from '../src/sort.js';
4
5
  import type { SqlCommand, SqlImplementation } from '../src/sql-command.js';
5
6
  import type { CanBuildMigrations, MigrationBuilder, MigrationCode } from '../migrations/migration-types.js';
6
7
  import type { DataProvider } from '../index.js';
@@ -23,13 +24,12 @@ export declare class PostgresDataProvider implements SqlImplementation, CanBuild
23
24
  wrapIdentifier?: (name: string) => string;
24
25
  caseInsensitiveIdentifiers?: boolean;
25
26
  schema?: string;
26
- orderByNullsFirst?: boolean;
27
27
  } | undefined);
28
28
  end(): Promise<void>;
29
29
  provideMigrationBuilder(builder: MigrationCode): MigrationBuilder;
30
30
  wrapIdentifier: (name: string) => string;
31
31
  ensureSchema(entities: EntityMetadata<any>[]): Promise<void>;
32
- orderByNullsFirst?: boolean;
32
+ nativeNullSort: NativeNullSort;
33
33
  transaction(action: (dataProvider: SqlImplementation) => Promise<void>): Promise<void>;
34
34
  }
35
35
  export interface PostgresCommandSource {
@@ -43,7 +43,6 @@ export declare function createPostgresDataProvider(options?: {
43
43
  wrapIdentifier?: (name: string) => string;
44
44
  caseInsensitiveIdentifiers?: boolean;
45
45
  schema?: string;
46
- orderByNullsFirst?: boolean;
47
46
  }): Promise<SqlDatabase>;
48
47
  export declare function preparePostgresQueueStorage(sql: SqlDatabase): Promise<import("../server/remult-api-server.js").EntityQueueStorage>;
49
48
  export declare class PostgresSchemaWrapper implements PostgresPool {
@@ -24,12 +24,14 @@ var PostgresDataProvider = /** @class */ (function () {
24
24
  })
25
25
  .join('.');
26
26
  };
27
+ this.nativeNullSort = {
28
+ order: { asc: 'NULLS LAST', desc: 'NULLS FIRST' },
29
+ syntax: 'NULLS FIRST/LAST',
30
+ };
27
31
  if (options === null || options === void 0 ? void 0 : options.wrapIdentifier)
28
32
  this.wrapIdentifier = options.wrapIdentifier;
29
33
  if (!(options === null || options === void 0 ? void 0 : options.wrapIdentifier) && (options === null || options === void 0 ? void 0 : options.caseInsensitiveIdentifiers))
30
34
  this.wrapIdentifier = function (name) { return name; };
31
- if (options === null || options === void 0 ? void 0 : options.orderByNullsFirst)
32
- this.orderByNullsFirst = options.orderByNullsFirst;
33
35
  if (options === null || options === void 0 ? void 0 : options.schema) {
34
36
  this.pool = new PostgresSchemaWrapper(pool, options.schema);
35
37
  }
@@ -134,7 +136,6 @@ var PostgresDataProvider = /** @class */ (function () {
134
136
  //@ts-ignore
135
137
  pool: client,
136
138
  wrapIdentifier: this.wrapIdentifier,
137
- orderByNullsFirst: this.orderByNullsFirst,
138
139
  })];
139
140
  case 4:
140
141
  _a.sent();
@@ -233,7 +234,6 @@ function createPostgresDataProvider(options) {
233
234
  wrapIdentifier: options.wrapIdentifier,
234
235
  caseInsensitiveIdentifiers: options.caseInsensitiveIdentifiers,
235
236
  schema: options.schema,
236
- orderByNullsFirst: options.orderByNullsFirst,
237
237
  }));
238
238
  return [2 /*return*/, db];
239
239
  });
@@ -1,9 +1,11 @@
1
1
  import type { DuckDBConnection } from '@duckdb/node-api';
2
2
  import { SqliteCoreDataProvider } from './remult-sqlite-core.js';
3
+ import type { NativeNullSort } from './src/sort.js';
3
4
  import { type FieldMetadata, type EntityMetadata } from './index.js';
4
5
  export declare class DuckDBDataProvider extends SqliteCoreDataProvider {
5
6
  private connection;
6
7
  constructor(connection: DuckDBConnection);
8
+ nativeNullSort: NativeNullSort;
7
9
  wrapIdentifier(name: string): string;
8
10
  getCreateTableSql(entity: EntityMetadata<any>): Promise<string[]>;
9
11
  addColumnSqlSyntax(x: FieldMetadata, dbName: string, isAlterColumn: boolean): string;
package/remult-duckdb.js CHANGED
@@ -14,6 +14,10 @@ var DuckDBDataProvider = /** @class */ (function (_super) {
14
14
  return [2 /*return*/, this.connection.disconnectSync()];
15
15
  }); }); }, false, true) || this;
16
16
  _this.connection = connection;
17
+ _this.nativeNullSort = {
18
+ order: { asc: 'NULLS LAST', desc: 'NULLS LAST' },
19
+ syntax: 'NULLS FIRST/LAST',
20
+ };
17
21
  return _this;
18
22
  }
19
23
  DuckDBDataProvider.prototype.wrapIdentifier = function (name) {
@@ -4,6 +4,7 @@ import type { FieldMetadata } from '../src/column-interfaces.js';
4
4
  import type { DataProvider, EntityDataProvider } from '../src/data-interfaces.js';
5
5
  import type { EntityFilter, EntityMetadata } from '../src/remult3/remult3.js';
6
6
  import type { RepositoryOverloads } from '../src/remult3/RepositoryImplementation.js';
7
+ import { type NativeNullSort } from '../src/sort.js';
7
8
  import type { HasJsonColumnStorage, HasWrapIdentifier, JsonColumnStorage, SqlCommand, SqlCommandFactory, SqlResult } from '../src/sql-command.js';
8
9
  import type { CanBuildMigrations, MigrationBuilder, MigrationCode } from '../migrations/migration-types.js';
9
10
  export declare class KnexDataProvider implements DataProvider, HasWrapIdentifier, HasJsonColumnStorage, SqlCommandFactory, CanBuildMigrations {
@@ -15,6 +16,7 @@ export declare class KnexDataProvider implements DataProvider, HasWrapIdentifier
15
16
  execute(sql: string): Promise<SqlResult>;
16
17
  static getDb(dataProvider?: DataProvider): Knex<any, any[]>;
17
18
  wrapIdentifier: (name: string) => string;
19
+ get nativeNullSort(): NativeNullSort;
18
20
  get jsonColumnStorage(): JsonColumnStorage;
19
21
  getEntityDataProvider(entity: EntityMetadata<any>): EntityDataProvider;
20
22
  transaction(action: (dataProvider: DataProvider) => Promise<void>): Promise<void>;