remult 3.0.6 → 3.0.7-next.0

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,29 +1,4 @@
1
1
  import { type RemultAsyncLocalStorageCore } from '../src/context.js';
2
- /**
3
- * Initializes async context tracking for the server.
4
- *
5
- * This should be called before handling any incoming requests or calling `withRemult()`.
6
- * @example
7
- * import { remult, repo, withRemult } from 'remult';
8
- * import { initAsyncHooks } from 'remult/async-hooks';
9
- *
10
- * import { Task } from './entities/Task.js';
11
- *
12
- * initAsyncHooks();
13
- *
14
- * // Thx to the `initAsyncHooks` above,
15
- * // we have isolated async contexts with multiple `withRemult()`,
16
- * // without needing to initialize a `remultApi` all the time!
17
- * withRemult(async () => {
18
- * remult.user = { id: '42' };
19
- * repo(Task).find()
20
- * });
21
- *
22
- * withRemult(async () => {
23
- * remult.user = { id: '21' };
24
- * repo(Task).find()
25
- * });
26
- */
27
2
  export declare function initAsyncHooks(): void;
28
3
  export declare class AsyncLocalStorageBridgeToRemultAsyncLocalStorageCoreImpl<T> implements RemultAsyncLocalStorageCore<T> {
29
4
  private asyncLocalStorage;
@@ -7,31 +7,6 @@ var async_hooks_1 = require("async_hooks");
7
7
  var context_js_1 = require("../src/context.js");
8
8
  var remult_static_js_1 = require("../src/remult-static.js");
9
9
  var init = false;
10
- /**
11
- * Initializes async context tracking for the server.
12
- *
13
- * This should be called before handling any incoming requests or calling `withRemult()`.
14
- * @example
15
- * import { remult, repo, withRemult } from 'remult';
16
- * import { initAsyncHooks } from 'remult/async-hooks';
17
- *
18
- * import { Task } from './entities/Task.js';
19
- *
20
- * initAsyncHooks();
21
- *
22
- * // Thx to the `initAsyncHooks` above,
23
- * // we have isolated async contexts with multiple `withRemult()`,
24
- * // without needing to initialize a `remultApi` all the time!
25
- * withRemult(async () => {
26
- * remult.user = { id: '42' };
27
- * repo(Task).find()
28
- * });
29
- *
30
- * withRemult(async () => {
31
- * remult.user = { id: '21' };
32
- * repo(Task).find()
33
- * });
34
- */
35
10
  function initAsyncHooks() {
36
11
  var _this = this;
37
12
  if (init)
@@ -94,7 +94,7 @@ export interface RemultServerOptions<RequestType> {
94
94
  sendError: (httpStatusCode: number, body: any) => void;
95
95
  }) => Promise<void> | undefined;
96
96
  /**
97
- * Modules are here to group code by feature.
97
+ * Modules are here to group code by feature. [Module Guide](https://remult.dev/docs/modules)
98
98
  *
99
99
  * @example
100
100
  * import { Module } from 'remult/server'
@@ -12,11 +12,12 @@ export interface FieldOptions<entityType = unknown, valueType = unknown> {
12
12
  * <input placeholder={taskRepo.metadata.fields.title.label}/>
13
13
  */
14
14
  label?: string;
15
- /** If it can store null in the database */
15
+ /** If it can store `null` in the database. @default false */
16
16
  allowNull?: boolean;
17
17
  /** If a value is required. Short-cut to say `validate: Validators.required`.
18
- @see option [validate](https://remult.dev/docs/ref_field#validate) below
19
- @see validator [required](https://remult.dev/docs/ref_validators#required)
18
+ * @default false
19
+ * @see option [validate](https://remult.dev/docs/ref_field#validate)
20
+ * @see validator [required](https://remult.dev/docs/ref_validators#required)
20
21
  */
21
22
  required?: boolean;
22
23
  /**
@@ -21,6 +21,7 @@ export declare const remultStatic: {
21
21
  };
22
22
  fieldOptionsEnricher: any;
23
23
  labelTransformer: any;
24
+ defaultIdFactory: () => string;
24
25
  defaultDataProvider: () => Promise<DataProvider | undefined>;
25
26
  };
26
27
  export declare function defaultFactory(): Remult;
@@ -25,6 +25,7 @@ var x = {
25
25
  },
26
26
  fieldOptionsEnricher: undefined,
27
27
  labelTransformer: undefined,
28
+ defaultIdFactory: undefined,
28
29
  defaultDataProvider: function () {
29
30
  return undefined;
30
31
  },
@@ -15,13 +15,43 @@ export declare class Fields {
15
15
  static number<entityType = unknown>(...options: (FieldOptions<entityType, number> | ((options: FieldOptions<entityType, number>, remult: Remult) => void))[]): ClassFieldDecorator<entityType, number | undefined>;
16
16
  static createdAt<entityType = unknown>(...options: (FieldOptions<entityType, Date> | ((options: FieldOptions<entityType, Date>, remult: Remult) => void))[]): ClassFieldDecorator<entityType, Date | undefined>;
17
17
  static updatedAt<entityType = unknown>(...options: (FieldOptions<entityType, Date> | ((options: FieldOptions<entityType, Date>, remult: Remult) => void))[]): ClassFieldDecorator<entityType, Date | undefined>;
18
+ /** @deprecated use `@Fields.id()` instead */
18
19
  static uuid<entityType = unknown>(...options: (FieldOptions<entityType, string> | ((options: FieldOptions<entityType, string>, remult: Remult) => void))[]): ClassFieldDecorator<entityType, string | undefined>;
20
+ private static _defaultIdFactory;
21
+ static get defaultIdFactory(): () => string;
22
+ static set defaultIdFactory(value: () => string);
19
23
  /**
20
- * A CUID (Collision Resistant Unique Identifier) field.
21
- * This id value is determined on the backend on insert, and can't be updated through the API.
22
- * The CUID is generated using the `@paralleldrive/cuid2` npm package.
24
+ * Defines a field that will be used as the id of the entity.
25
+ * By default it will use `crypto.randomUUID` to generate the id.
26
+ *
27
+ * You can change the algorithm used to generate the id by setting the `Fields.defaultIdFactory`
28
+ * to a different function like:
29
+ *
30
+ * This needs to be done in a shared file to be accessible frontend and backend.
31
+ *
32
+ * ```ts
33
+ * import { createId } from '@paralleldrive/cuid2'
34
+ * Fields.defaultIdFactory = () => createId()
35
+ * ```
36
+ *
37
+ * You can also pass an id factory as an option to the `@Fields.id` to have a different value locally.
38
+ * @example
39
+ * ```ts
40
+ * import { createId } from '@paralleldrive/cuid2'
41
+ * // import { v4 as uuid } from 'uuid'
42
+ *
43
+ * class MyEntity {
44
+ * \@Fields.id({
45
+ * idFactory: () => createId()
46
+ * // idFactory: () => uuid()
47
+ * })
48
+ * id: string = '';
49
+ * }
50
+ * ```
23
51
  */
24
- static cuid<entityType = unknown>(...options: (FieldOptions<entityType, string> | ((options: FieldOptions<entityType, string>, remult: Remult) => void))[]): ClassFieldDecorator<entityType, string | undefined>;
52
+ static id<entityType = unknown>(options?: FieldOptions<entityType, string> & {
53
+ idFactory?: () => string;
54
+ }): ClassFieldDecorator<entityType, string | undefined>;
25
55
  /**
26
56
  * Defines a field that can hold a value from a specified set of string literals.
27
57
  * @param {() => readonly valueType[]} optionalValues - A function that returns an array of allowed string literals.
@@ -4,8 +4,6 @@ exports.Relations = exports.Fields = void 0;
4
4
  exports.Field = Field;
5
5
  exports.checkTarget = checkTarget;
6
6
  var tslib_1 = require("tslib");
7
- var cuid2_1 = require("@paralleldrive/cuid2");
8
- var uuid_1 = require("uuid");
9
7
  var valueConverters_js_1 = require("../valueConverters.js");
10
8
  var RepositoryImplementation_js_1 = require("./RepositoryImplementation.js");
11
9
  var validators_js_1 = require("../validators.js");
@@ -128,53 +126,62 @@ var Fields = /** @class */ (function () {
128
126
  },
129
127
  }], tslib_1.__read(options), false));
130
128
  };
129
+ /** @deprecated use `@Fields.id()` instead */
131
130
  Fields.uuid = function () {
132
131
  var options = [];
133
132
  for (var _i = 0; _i < arguments.length; _i++) {
134
133
  options[_i] = arguments[_i];
135
134
  }
136
- return Field.apply(void 0, tslib_1.__spreadArray([function () { return String; },
137
- {
138
- allowApiUpdate: false,
139
- defaultValue: function () { return (0, uuid_1.v4)(); },
140
- saving: function (_, r) {
141
- if (!r.value)
142
- r.value = (0, uuid_1.v4)();
143
- },
144
- }], tslib_1.__read(options), false));
135
+ return Fields.id(tslib_1.__assign({ idFactory: function () { return crypto.randomUUID(); } }, options));
145
136
  };
137
+ Object.defineProperty(Fields, "defaultIdFactory", {
138
+ get: function () {
139
+ return this._defaultIdFactory;
140
+ },
141
+ set: function (value) {
142
+ this._defaultIdFactory = value;
143
+ remult_static_js_1.remultStatic.defaultIdFactory = value;
144
+ },
145
+ enumerable: false,
146
+ configurable: true
147
+ });
146
148
  /**
147
- * A CUID (Collision Resistant Unique Identifier) field.
148
- * This id value is determined on the backend on insert, and can't be updated through the API.
149
- * The CUID is generated using the `@paralleldrive/cuid2` npm package.
149
+ * Defines a field that will be used as the id of the entity.
150
+ * By default it will use `crypto.randomUUID` to generate the id.
151
+ *
152
+ * You can change the algorithm used to generate the id by setting the `Fields.defaultIdFactory`
153
+ * to a different function like:
154
+ *
155
+ * This needs to be done in a shared file to be accessible frontend and backend.
156
+ *
157
+ * ```ts
158
+ * import { createId } from '@paralleldrive/cuid2'
159
+ * Fields.defaultIdFactory = () => createId()
160
+ * ```
161
+ *
162
+ * You can also pass an id factory as an option to the `@Fields.id` to have a different value locally.
163
+ * @example
164
+ * ```ts
165
+ * import { createId } from '@paralleldrive/cuid2'
166
+ * // import { v4 as uuid } from 'uuid'
167
+ *
168
+ * class MyEntity {
169
+ * \@Fields.id({
170
+ * idFactory: () => createId()
171
+ * // idFactory: () => uuid()
172
+ * })
173
+ * id: string = '';
174
+ * }
175
+ * ```
150
176
  */
151
- Fields.cuid = function () {
152
- var options = [];
153
- for (var _i = 0; _i < arguments.length; _i++) {
154
- options[_i] = arguments[_i];
155
- }
156
- return Field.apply(void 0, tslib_1.__spreadArray([function () { return String; },
157
- {
158
- allowApiUpdate: false,
159
- defaultValue: function () { return (0, cuid2_1.createId)(); },
160
- saving: function (_, r) {
161
- if (!r.value)
162
- r.value = (0, cuid2_1.createId)();
163
- },
164
- }], tslib_1.__read(options), false));
177
+ Fields.id = function (options) {
178
+ var _a;
179
+ var idFactory = (_a = options === null || options === void 0 ? void 0 : options.idFactory) !== null && _a !== void 0 ? _a : remult_static_js_1.remultStatic.defaultIdFactory;
180
+ return Field(function () { return String; }, tslib_1.__assign({ allowApiUpdate: false, defaultValue: function () { return ((options === null || options === void 0 ? void 0 : options.allowNull) ? null : idFactory()); }, saving: function (_, r) {
181
+ if (!r.value && !(options === null || options === void 0 ? void 0 : options.allowNull))
182
+ r.value = idFactory();
183
+ } }, options));
165
184
  };
166
- // static id<entityType = unknown>(
167
- // options?: FieldOptions<entityType, string> & { idFactory?: () => string },
168
- // ): ClassFieldDecorator<entityType, string | undefined> {
169
- // let buildId = options?.idFactory ?? createId
170
- // return Field(() => String, {
171
- // allowApiUpdate: false,
172
- // defaultValue: () => buildId(),
173
- // saving: (_, r) => {
174
- // if (!r.value) r.value = buildId()
175
- // },
176
- // })
177
- // }
178
185
  /**
179
186
  * Defines a field that can hold a value from a specified set of string literals.
180
187
  * @param {() => readonly valueType[]} optionalValues - A function that returns an array of allowed string literals.
@@ -269,9 +276,12 @@ var Fields = /** @class */ (function () {
269
276
  }
270
277
  return Field.apply(void 0, tslib_1.__spreadArray([function () { return Boolean; }], tslib_1.__read(options), false));
271
278
  };
279
+ Fields._defaultIdFactory = function () { return crypto.randomUUID(); };
272
280
  return Fields;
273
281
  }());
274
282
  exports.Fields = Fields;
283
+ // Initialize remultStatic.defaultIdFactory with the default value
284
+ remult_static_js_1.remultStatic.defaultIdFactory = Fields.defaultIdFactory;
275
285
  var Relations = /** @class */ (function () {
276
286
  function Relations() {
277
287
  }
@@ -10,7 +10,7 @@ var IdEntity = /** @class */ (function (_super) {
10
10
  return _super !== null && _super.apply(this, arguments) || this;
11
11
  }
12
12
  tslib_1.__decorate([
13
- Fields_js_1.Fields.uuid(),
13
+ Fields_js_1.Fields.id(),
14
14
  tslib_1.__metadata("design:type", String)
15
15
  ], IdEntity.prototype, "id", void 0);
16
16
  return IdEntity;
@@ -243,7 +243,7 @@ export declare const LabelTransformer: {
243
243
  */
244
244
  transformLabel: (remult: Remult, key: string, label: string, entityMetaData: EntityMetadata<any>) => string;
245
245
  /**
246
- * @obsolete use LabelTransformer.transformLabel instead
246
+ * @deprecated use LabelTransformer.transformLabel instead
247
247
  */
248
248
  transformCaption: (remult: Remult, key: string, label: string, entityMetaData: EntityMetadata<any>) => string;
249
249
  };
@@ -9,7 +9,7 @@ import type { ClassDecoratorContextStub } from './remult3.js';
9
9
  * allowApiCrud: true
10
10
  * })
11
11
  * export class Task {
12
- * @Fields.uuid()
12
+ * @Fields.id()
13
13
  * id!: string;
14
14
  * @Fields.string()
15
15
  * title = '';
@@ -13,7 +13,7 @@ var remult_static_js_1 = require("../remult-static.js");
13
13
  * allowApiCrud: true
14
14
  * })
15
15
  * export class Task {
16
- * @Fields.uuid()
16
+ * @Fields.id()
17
17
  * id!: string;
18
18
  * @Fields.string()
19
19
  * title = '';