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
@@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.SqlDatabase = void 0;
4
4
  exports.jsonColumnStorageOf = jsonColumnStorageOf;
5
5
  exports.getRowAfterUpdate = getRowAfterUpdate;
6
+ exports.orderBySql = orderBySql;
6
7
  exports.buildFindSql = buildFindSql;
7
8
  exports.groupByImpl = groupByImpl;
8
9
  exports.buildGroupBySql = buildGroupBySql;
@@ -90,6 +91,13 @@ var SqlDatabase = /** @class */ (function () {
90
91
  enumerable: false,
91
92
  configurable: true
92
93
  });
94
+ Object.defineProperty(SqlDatabase.prototype, "nativeNullSort", {
95
+ get: function () {
96
+ return this.sql.nativeNullSort;
97
+ },
98
+ enumerable: false,
99
+ configurable: true
100
+ });
93
101
  /* @internal*/
94
102
  SqlDatabase.prototype._getSourceSql = function () {
95
103
  return this.sql;
@@ -175,7 +183,7 @@ var SqlDatabase = /** @class */ (function () {
175
183
  end: this.end,
176
184
  doesNotSupportReturningSyntax: this.sql.doesNotSupportReturningSyntax,
177
185
  doesNotSupportReturningSyntaxOnlyForUpdate: this.sql.doesNotSupportReturningSyntaxOnlyForUpdate,
178
- orderByNullsFirst: this.sql.orderByNullsFirst,
186
+ nativeNullSort: this.sql.nativeNullSort,
179
187
  afterMutation: this.sql.afterMutation,
180
188
  maxParametersInOneSqlStatement: this.sql.maxParametersInOneSqlStatement,
181
189
  }))];
@@ -268,7 +276,7 @@ var SqlDatabase = /** @class */ (function () {
268
276
  case 3:
269
277
  _c = (_h.sent());
270
278
  _h.label = 4;
271
- case 4: return [4 /*yield*/, _a.apply(void 0, _b.concat([_c, (_f = sql === null || sql === void 0 ? void 0 : sql.sqlCommand) !== null && _f !== void 0 ? _f : new myDummySQLCommand(), false, (_g = sql === null || sql === void 0 ? void 0 : sql.limitSyntax) !== null && _g !== void 0 ? _g : limitSyntaxRequired, jsonColumnStorageForRaw(r, sql === null || sql === void 0 ? void 0 : sql.jsonAsString)]))];
279
+ case 4: return [4 /*yield*/, _a.apply(void 0, _b.concat([_c, (_f = sql === null || sql === void 0 ? void 0 : sql.sqlCommand) !== null && _f !== void 0 ? _f : new myDummySQLCommand(), nativeNullSortOf(r), (_g = sql === null || sql === void 0 ? void 0 : sql.limitSyntax) !== null && _g !== void 0 ? _g : limitSyntaxRequired, jsonColumnStorageForRaw(r, sql === null || sql === void 0 ? void 0 : sql.jsonAsString)]))];
272
280
  case 5:
273
281
  built = _h.sent();
274
282
  return [2 /*return*/, {
@@ -304,7 +312,7 @@ var SqlDatabase = /** @class */ (function () {
304
312
  case 3:
305
313
  _c = (_h.sent());
306
314
  _h.label = 4;
307
- case 4: return [4 /*yield*/, _a.apply(void 0, _b.concat([_c, (_f = sql === null || sql === void 0 ? void 0 : sql.sqlCommand) !== null && _f !== void 0 ? _f : new myDummySQLCommand(), false, (_g = sql === null || sql === void 0 ? void 0 : sql.limitSyntax) !== null && _g !== void 0 ? _g : limitSyntaxRequired, {
315
+ case 4: return [4 /*yield*/, _a.apply(void 0, _b.concat([_c, (_f = sql === null || sql === void 0 ? void 0 : sql.sqlCommand) !== null && _f !== void 0 ? _f : new myDummySQLCommand(), nativeNullSortOf(r), (_g = sql === null || sql === void 0 ? void 0 : sql.limitSyntax) !== null && _g !== void 0 ? _g : limitSyntaxRequired, {
308
316
  aliasColumns: true,
309
317
  jsonColumnStorage: jsonColumnStorageForRaw(r, sql === null || sql === void 0 ? void 0 : sql.jsonAsString),
310
318
  }]))];
@@ -521,7 +529,7 @@ var ActualSQLEntityDataProvider = /** @class */ (function () {
521
529
  _b = [options];
522
530
  return [4 /*yield*/, this.init()];
523
531
  case 1: return [4 /*yield*/, _a.apply(void 0, _b.concat([_c.sent(), this.sql.createCommand(),
524
- this.sql._getSourceSql().orderByNullsFirst,
532
+ this.sql.nativeNullSort,
525
533
  this.sql._getSourceSql().getLimitSqlSyntax,
526
534
  this.sql.jsonColumnStorage]))];
527
535
  case 2: return [2 /*return*/, _c.sent()];
@@ -541,7 +549,7 @@ var ActualSQLEntityDataProvider = /** @class */ (function () {
541
549
  r = this.sql.createCommand();
542
550
  if (options && !options.orderBy)
543
551
  options.orderBy = sort_js_1.Sort.createUniqueSort(this.entity, new sort_js_1.Sort());
544
- return [4 /*yield*/, buildFindSql(this.entity, options, e, r, this.sql._getSourceSql().orderByNullsFirst, function (limit, offset) { return _this.strategy.getLimitSqlSyntax(limit, offset); }, { jsonColumnStorage: this.sql.jsonColumnStorage })];
552
+ return [4 /*yield*/, buildFindSql(this.entity, options, e, r, this.sql.nativeNullSort, function (limit, offset) { return _this.strategy.getLimitSqlSyntax(limit, offset); }, { jsonColumnStorage: this.sql.jsonColumnStorage })];
545
553
  case 2:
546
554
  _a = _b.sent(), select = _a.select, toResult = _a.toResult;
547
555
  return [4 /*yield*/, r.execute(select)];
@@ -856,6 +864,13 @@ function wrapIdentifierOf(r) {
856
864
  return (_a = dp.wrapIdentifier) === null || _a === void 0 ? void 0 : _a.bind(dp);
857
865
  return undefined;
858
866
  }
867
+ function nativeNullSortOf(r) {
868
+ var dp = (0, repository_internals_js_1.getRepositoryInternals)(r)._dataProvider;
869
+ if ((0, isOfType_js_1.isOfType)(dp, 'nativeNullSort'))
870
+ return dp.nativeNullSort;
871
+ // Not a SQL repo, so the raw select's database is unknown.
872
+ return { order: 'unknown', syntax: 'CASE WHEN' };
873
+ }
859
874
  function jsonColumnStorageOf(r) {
860
875
  var dp = (0, repository_internals_js_1.getRepositoryInternals)(r)._dataProvider;
861
876
  if ((0, isOfType_js_1.isOfType)(dp, 'jsonColumnStorage'))
@@ -921,17 +936,31 @@ function getRowAfterUpdate(meta, dataProvider, data, id, operation) {
921
936
  return r[0];
922
937
  });
923
938
  }
939
+ /** Writes the NULL order only on a nullable column, where NullSort differs from the database's own. */
940
+ function orderBySql(expr, isDescending, nullable, native) {
941
+ var sql = isDescending ? expr + ' desc' : expr;
942
+ if (!nullable || sort_js_1.NullSort.order === 'native')
943
+ return sql;
944
+ var dir = isDescending ? 'desc' : 'asc';
945
+ var nullSort = sort_js_1.NullSort.order[dir];
946
+ if (native.order !== 'unknown' && native.order[dir] === nullSort)
947
+ return sql;
948
+ if (native.syntax === 'NULLS FIRST/LAST')
949
+ return sql + ' ' + nullSort.toLowerCase();
950
+ var _a = tslib_1.__read(nullSort === 'NULLS FIRST' ? [0, 1] : [1, 0], 2), ifNull = _a[0], otherwise = _a[1];
951
+ return "case when ".concat(expr, " is null then ").concat(ifNull, " else ").concat(otherwise, " end, ").concat(sql);
952
+ }
924
953
  /** 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. */
925
- function buildFindSql(entity_1, options_1, e_7, r_1, orderByNullsFirst_1, limitSyntax_1) {
926
- return tslib_1.__awaiter(this, arguments, void 0, function (entity, options, e, r, orderByNullsFirst, limitSyntax, _a) {
927
- var _b, colKeys, columns, select, where, _c, first, _d, _e, c, _f, _g, e_8_1, page;
928
- var e_8, _h;
929
- var _j = _a === void 0 ? {} : _a, _k = _j.aliasColumns, aliasColumns = _k === void 0 ? false : _k, _l = _j.jsonColumnStorage, jsonColumnStorage = _l === void 0 ? 'native' : _l;
930
- return tslib_1.__generator(this, function (_m) {
931
- switch (_m.label) {
954
+ function buildFindSql(entity_1, options_1, e_7, r_1, nativeNullSort_1, limitSyntax_1) {
955
+ return tslib_1.__awaiter(this, arguments, void 0, function (entity, options, e, r, nativeNullSort, limitSyntax, _a) {
956
+ var _b, colKeys, columns, select, where, _c, first, _d, _e, c, _f, _g, _h, e_8_1, page;
957
+ var e_8, _j;
958
+ var _k = _a === void 0 ? {} : _a, _l = _k.aliasColumns, aliasColumns = _l === void 0 ? false : _l, _m = _k.jsonColumnStorage, jsonColumnStorage = _m === void 0 ? 'native' : _m;
959
+ return tslib_1.__generator(this, function (_o) {
960
+ switch (_o.label) {
932
961
  case 0: return [4 /*yield*/, selectColumns(entity, e, r, options === null || options === void 0 ? void 0 : options.select, options === null || options === void 0 ? void 0 : options.args, aliasColumns)];
933
962
  case 1:
934
- _b = _m.sent(), colKeys = _b.colKeys, columns = _b.select;
963
+ _b = _o.sent(), colKeys = _b.colKeys, columns = _b.select;
935
964
  select = 'select ' + columns + '\n from ' + e.$entityName;
936
965
  if (!options) return [3 /*break*/, 14];
937
966
  if (!options.where) return [3 /*break*/, 3];
@@ -940,19 +969,19 @@ function buildFindSql(entity_1, options_1, e_7, r_1, orderByNullsFirst_1, limitS
940
969
  _c = select;
941
970
  return [4 /*yield*/, where.resolveWhere()];
942
971
  case 2:
943
- select = _c + _m.sent();
944
- _m.label = 3;
972
+ select = _c + _o.sent();
973
+ _o.label = 3;
945
974
  case 3:
946
975
  if (options.limit) {
947
976
  options.orderBy = sort_js_1.Sort.createUniqueSort(entity, options.orderBy);
948
977
  }
949
978
  if (!options.orderBy) return [3 /*break*/, 13];
950
979
  first = true;
951
- _m.label = 4;
980
+ _o.label = 4;
952
981
  case 4:
953
- _m.trys.push([4, 11, 12, 13]);
982
+ _o.trys.push([4, 11, 12, 13]);
954
983
  _d = tslib_1.__values(options.orderBy.Segments), _e = _d.next();
955
- _m.label = 5;
984
+ _o.label = 5;
956
985
  case 5:
957
986
  if (!!_e.done) return [3 /*break*/, 10];
958
987
  c = _e.value;
@@ -963,35 +992,30 @@ function buildFindSql(entity_1, options_1, e_7, r_1, orderByNullsFirst_1, limitS
963
992
  else
964
993
  select += ', ';
965
994
  _f = select;
995
+ _g = orderBySql;
966
996
  if (!c.field.options.sqlExpression) return [3 /*break*/, 6];
967
- _g = c.field.options.key;
997
+ _h = c.field.key;
968
998
  return [3 /*break*/, 8];
969
999
  case 6: return [4 /*yield*/, e.$dbNameOf(c.field)];
970
1000
  case 7:
971
- _g = _m.sent();
972
- _m.label = 8;
1001
+ _h = _o.sent();
1002
+ _o.label = 8;
973
1003
  case 8:
974
- select = _f + _g;
975
- if (c.isDescending)
976
- select += ' desc';
977
- if (orderByNullsFirst) {
978
- if (c.isDescending)
979
- select += ' nulls last';
980
- else
981
- select += ' nulls first';
982
- }
983
- _m.label = 9;
1004
+ select = _f + _g.apply(void 0, [_h, c.isDescending,
1005
+ c.field.allowNull,
1006
+ nativeNullSort]);
1007
+ _o.label = 9;
984
1008
  case 9:
985
1009
  _e = _d.next();
986
1010
  return [3 /*break*/, 5];
987
1011
  case 10: return [3 /*break*/, 13];
988
1012
  case 11:
989
- e_8_1 = _m.sent();
1013
+ e_8_1 = _o.sent();
990
1014
  e_8 = { error: e_8_1 };
991
1015
  return [3 /*break*/, 13];
992
1016
  case 12:
993
1017
  try {
994
- if (_e && !_e.done && (_h = _d.return)) _h.call(_d);
1018
+ if (_e && !_e.done && (_j = _d.return)) _j.call(_d);
995
1019
  }
996
1020
  finally { if (e_8) throw e_8.error; }
997
1021
  return [7 /*endfinally*/];
@@ -1004,7 +1028,7 @@ function buildFindSql(entity_1, options_1, e_7, r_1, orderByNullsFirst_1, limitS
1004
1028
  page = 1;
1005
1029
  select += ' ' + limitSyntax(options.limit, (page - 1) * options.limit);
1006
1030
  }
1007
- _m.label = 14;
1031
+ _o.label = 14;
1008
1032
  case 14: return [2 /*return*/, {
1009
1033
  select: select,
1010
1034
  colKeys: colKeys,
@@ -1091,13 +1115,13 @@ function resultRow(colKeys, valueAt, storage) {
1091
1115
  });
1092
1116
  return result;
1093
1117
  }
1094
- function groupByImpl(options_1, e_9, r_1, orderByNullFirst_1, limitSyntax_1) {
1095
- return tslib_1.__awaiter(this, arguments, void 0, function (options, e, r, orderByNullFirst, limitSyntax, jsonColumnStorage) {
1118
+ function groupByImpl(options_1, e_9, r_1, nativeNullSort_1, limitSyntax_1) {
1119
+ return tslib_1.__awaiter(this, arguments, void 0, function (options, e, r, nativeNullSort, limitSyntax, jsonColumnStorage) {
1096
1120
  var _a, select, toResult, result;
1097
1121
  if (jsonColumnStorage === void 0) { jsonColumnStorage = 'native'; }
1098
1122
  return tslib_1.__generator(this, function (_b) {
1099
1123
  switch (_b.label) {
1100
- case 0: return [4 /*yield*/, buildGroupBySql(options, e, r, orderByNullFirst, limitSyntax, jsonColumnStorage)];
1124
+ case 0: return [4 /*yield*/, buildGroupBySql(options, e, r, nativeNullSort, limitSyntax, jsonColumnStorage)];
1101
1125
  case 1:
1102
1126
  _a = _b.sent(), select = _a.select, toResult = _a.toResult;
1103
1127
  return [4 /*yield*/, r.execute(select)];
@@ -1111,8 +1135,8 @@ function groupByImpl(options_1, e_9, r_1, orderByNullFirst_1, limitSyntax_1) {
1111
1135
  });
1112
1136
  }
1113
1137
  /** 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. */
1114
- function buildGroupBySql(options_1, e_9, r_1, orderByNullFirst_1, limitSyntax_1) {
1115
- return tslib_1.__awaiter(this, arguments, void 0, function (options, e, r, orderByNullFirst, limitSyntax, jsonColumnStorage) {
1138
+ function buildGroupBySql(options_1, e_9, r_1, nativeNullSort_1, limitSyntax_1) {
1139
+ return tslib_1.__awaiter(this, arguments, void 0, function (options, e, r, nativeNullSort, limitSyntax, jsonColumnStorage) {
1116
1140
  function alias(name) {
1117
1141
  aliases.push(name);
1118
1142
  return e.wrapIdentifier(name);
@@ -1122,11 +1146,12 @@ function buildGroupBySql(options_1, e_9, r_1, orderByNullFirst_1, limitSyntax_1)
1122
1146
  ? "count (distinct ".concat(dbName, ")")
1123
1147
  : "".concat(operator, "( ").concat(dbName, " )");
1124
1148
  }
1125
- var aliases, select, groupBy, groupByCols, processResultRow, _loop_2, _a, _b, x, _loop_3, GroupByOperators_1, GroupByOperators_1_1, operator, e_10_1, where, _c, orderBy, _d, _e, x, field, _f, e_11_1, page;
1149
+ var aliases, select, groupBy, groupByCols, processResultRow, _loop_2, _a, _b, x, _loop_3, GroupByOperators_1, GroupByOperators_1_1, operator, e_10_1, where, _c, orderBy, _d, _e, x, field, _f, nullable, e_11_1, page;
1126
1150
  var e_12, _g, e_10, _h, e_11, _j;
1151
+ var _k;
1127
1152
  if (jsonColumnStorage === void 0) { jsonColumnStorage = 'native'; }
1128
- return tslib_1.__generator(this, function (_k) {
1129
- switch (_k.label) {
1153
+ return tslib_1.__generator(this, function (_l) {
1154
+ switch (_l.label) {
1130
1155
  case 0:
1131
1156
  aliases = [];
1132
1157
  select = 'select count(*) as ' + alias('count');
@@ -1169,24 +1194,24 @@ function buildGroupBySql(options_1, e_9, r_1, orderByNullFirst_1, limitSyntax_1)
1169
1194
  }
1170
1195
  _loop_3 = function (operator) {
1171
1196
  var fields, _loop_4, fields_1, fields_1_1, x, e_13_1;
1172
- var e_13, _l;
1173
- return tslib_1.__generator(this, function (_m) {
1174
- switch (_m.label) {
1197
+ var e_13, _m;
1198
+ return tslib_1.__generator(this, function (_o) {
1199
+ switch (_o.label) {
1175
1200
  case 0:
1176
1201
  fields = options === null || options === void 0 ? void 0 : options[operator];
1177
1202
  if (!fields) return [3 /*break*/, 8];
1178
1203
  _loop_4 = function (x) {
1179
1204
  var dbName, turnToNumber;
1180
- return tslib_1.__generator(this, function (_o) {
1181
- switch (_o.label) {
1205
+ return tslib_1.__generator(this, function (_p) {
1206
+ switch (_p.label) {
1182
1207
  case 0:
1183
1208
  if (!x.isServerExpression) return [3 /*break*/, 1];
1184
1209
  return [3 /*break*/, 3];
1185
1210
  case 1: return [4 /*yield*/, e.$dbNameOf(x)];
1186
1211
  case 2:
1187
- dbName = _o.sent();
1212
+ dbName = _p.sent();
1188
1213
  select += ", ".concat(aggregateSqlSyntax(operator, dbName), " as ").concat(alias(x.key + '_' + operator));
1189
- _o.label = 3;
1214
+ _p.label = 3;
1190
1215
  case 3:
1191
1216
  turnToNumber = x.valueType === Number || operator == 'distinctCount';
1192
1217
  processResultRow.push(function (sqlResult, theResult) {
@@ -1201,29 +1226,29 @@ function buildGroupBySql(options_1, e_9, r_1, orderByNullFirst_1, limitSyntax_1)
1201
1226
  }
1202
1227
  });
1203
1228
  };
1204
- _m.label = 1;
1229
+ _o.label = 1;
1205
1230
  case 1:
1206
- _m.trys.push([1, 6, 7, 8]);
1231
+ _o.trys.push([1, 6, 7, 8]);
1207
1232
  fields_1 = (e_13 = void 0, tslib_1.__values(fields)), fields_1_1 = fields_1.next();
1208
- _m.label = 2;
1233
+ _o.label = 2;
1209
1234
  case 2:
1210
1235
  if (!!fields_1_1.done) return [3 /*break*/, 5];
1211
1236
  x = fields_1_1.value;
1212
1237
  return [5 /*yield**/, _loop_4(x)];
1213
1238
  case 3:
1214
- _m.sent();
1215
- _m.label = 4;
1239
+ _o.sent();
1240
+ _o.label = 4;
1216
1241
  case 4:
1217
1242
  fields_1_1 = fields_1.next();
1218
1243
  return [3 /*break*/, 2];
1219
1244
  case 5: return [3 /*break*/, 8];
1220
1245
  case 6:
1221
- e_13_1 = _m.sent();
1246
+ e_13_1 = _o.sent();
1222
1247
  e_13 = { error: e_13_1 };
1223
1248
  return [3 /*break*/, 8];
1224
1249
  case 7:
1225
1250
  try {
1226
- if (fields_1_1 && !fields_1_1.done && (_l = fields_1.return)) _l.call(fields_1);
1251
+ if (fields_1_1 && !fields_1_1.done && (_m = fields_1.return)) _m.call(fields_1);
1227
1252
  }
1228
1253
  finally { if (e_13) throw e_13.error; }
1229
1254
  return [7 /*endfinally*/];
@@ -1231,24 +1256,24 @@ function buildGroupBySql(options_1, e_9, r_1, orderByNullFirst_1, limitSyntax_1)
1231
1256
  }
1232
1257
  });
1233
1258
  };
1234
- _k.label = 1;
1259
+ _l.label = 1;
1235
1260
  case 1:
1236
- _k.trys.push([1, 6, 7, 8]);
1261
+ _l.trys.push([1, 6, 7, 8]);
1237
1262
  GroupByOperators_1 = tslib_1.__values(remult3_js_1.GroupByOperators), GroupByOperators_1_1 = GroupByOperators_1.next();
1238
- _k.label = 2;
1263
+ _l.label = 2;
1239
1264
  case 2:
1240
1265
  if (!!GroupByOperators_1_1.done) return [3 /*break*/, 5];
1241
1266
  operator = GroupByOperators_1_1.value;
1242
1267
  return [5 /*yield**/, _loop_3(operator)];
1243
1268
  case 3:
1244
- _k.sent();
1245
- _k.label = 4;
1269
+ _l.sent();
1270
+ _l.label = 4;
1246
1271
  case 4:
1247
1272
  GroupByOperators_1_1 = GroupByOperators_1.next();
1248
1273
  return [3 /*break*/, 2];
1249
1274
  case 5: return [3 /*break*/, 8];
1250
1275
  case 6:
1251
- e_10_1 = _k.sent();
1276
+ e_10_1 = _l.sent();
1252
1277
  e_10 = { error: e_10_1 };
1253
1278
  return [3 /*break*/, 8];
1254
1279
  case 7:
@@ -1265,18 +1290,18 @@ function buildGroupBySql(options_1, e_9, r_1, orderByNullFirst_1, limitSyntax_1)
1265
1290
  _c = select;
1266
1291
  return [4 /*yield*/, where.resolveWhere()];
1267
1292
  case 9:
1268
- select = _c + _k.sent();
1269
- _k.label = 10;
1293
+ select = _c + _l.sent();
1294
+ _l.label = 10;
1270
1295
  case 10:
1271
1296
  if (groupBy)
1272
1297
  select += groupBy;
1273
1298
  orderBy = '';
1274
1299
  if (!(options === null || options === void 0 ? void 0 : options.orderBy)) return [3 /*break*/, 20];
1275
- _k.label = 11;
1300
+ _l.label = 11;
1276
1301
  case 11:
1277
- _k.trys.push([11, 17, 18, 19]);
1302
+ _l.trys.push([11, 17, 18, 19]);
1278
1303
  _d = tslib_1.__values(options === null || options === void 0 ? void 0 : options.orderBy), _e = _d.next();
1279
- _k.label = 12;
1304
+ _l.label = 12;
1280
1305
  case 12:
1281
1306
  if (!!_e.done) return [3 /*break*/, 16];
1282
1307
  x = _e.value;
@@ -1288,8 +1313,8 @@ function buildGroupBySql(options_1, e_9, r_1, orderByNullFirst_1, limitSyntax_1)
1288
1313
  if (!_f) return [3 /*break*/, 14];
1289
1314
  return [4 /*yield*/, e.$dbNameOf(x.field)];
1290
1315
  case 13:
1291
- _f = (_k.sent());
1292
- _k.label = 14;
1316
+ _f = (_l.sent());
1317
+ _l.label = 14;
1293
1318
  case 14:
1294
1319
  field = _f;
1295
1320
  switch (x.operation) {
@@ -1301,22 +1326,15 @@ function buildGroupBySql(options_1, e_9, r_1, orderByNullFirst_1, limitSyntax_1)
1301
1326
  default:
1302
1327
  field = aggregateSqlSyntax(x.operation, field);
1303
1328
  }
1304
- orderBy += field;
1305
- if (x.isDescending)
1306
- orderBy += ' desc';
1307
- if (orderByNullFirst) {
1308
- if (x.isDescending)
1309
- orderBy += ' nulls last';
1310
- else
1311
- orderBy += ' nulls first';
1312
- }
1313
- _k.label = 15;
1329
+ nullable = !!((_k = x.field) === null || _k === void 0 ? void 0 : _k.allowNull) && x.operation !== 'distinctCount';
1330
+ orderBy += orderBySql(field, x.isDescending, nullable, nativeNullSort);
1331
+ _l.label = 15;
1314
1332
  case 15:
1315
1333
  _e = _d.next();
1316
1334
  return [3 /*break*/, 12];
1317
1335
  case 16: return [3 /*break*/, 19];
1318
1336
  case 17:
1319
- e_11_1 = _k.sent();
1337
+ e_11_1 = _l.sent();
1320
1338
  e_11 = { error: e_11_1 };
1321
1339
  return [3 /*break*/, 19];
1322
1340
  case 18:
@@ -1328,7 +1346,7 @@ function buildGroupBySql(options_1, e_9, r_1, orderByNullFirst_1, limitSyntax_1)
1328
1346
  case 19:
1329
1347
  if (orderBy)
1330
1348
  select += orderBy;
1331
- _k.label = 20;
1349
+ _l.label = 20;
1332
1350
  case 20:
1333
1351
  // Skip paging for a pure aggregate (single row); OFFSET/FETCH without an
1334
1352
  // ORDER BY is also invalid on SQL Server.
@@ -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);
@@ -2,6 +2,7 @@ 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 { NullSort } from './sort.js';
5
6
  export declare const remultStatic: {
6
7
  defaultRemultFactory: () => Remult;
7
8
  remultFactory: () => Remult;
@@ -21,6 +22,7 @@ export declare const remultStatic: {
21
22
  };
22
23
  fieldOptionsEnricher: any;
23
24
  labelTransformer: any;
25
+ nullSort: typeof NullSort;
24
26
  defaultIdFactory: () => string;
25
27
  defaultDataProvider: () => Promise<DataProvider | undefined>;
26
28
  };
@@ -25,6 +25,7 @@ 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;
@@ -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.