remult 3.1.0-next.1 → 3.1.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.
@@ -84,51 +84,77 @@ export class Fields {
84
84
  ...options,
85
85
  });
86
86
  }
87
- static _defaultIdFactory = () => crypto.randomUUID();
87
+ /**
88
+ * ### cuid
89
+ * @example
90
+ * ```ts
91
+ * import { Fields } from 'remult'
92
+ * import { createId } from '@paralleldrive/cuid2'
93
+ * Fields.defaultIdFactory = () => createId()
94
+ * ```
95
+ *
96
+ * ### nanoid
97
+ * @example
98
+ * ```ts
99
+ * import { Fields } from 'remult'
100
+ * import { nanoid } from 'nanoid'
101
+ * Fields.defaultIdFactory = () => nanoid()
102
+ * ```
103
+ *
104
+ */
88
105
  static get defaultIdFactory() {
89
- return this._defaultIdFactory;
106
+ return (remultStatic.defaultIdFactory ??
107
+ (remultStatic.defaultIdFactory = () => crypto.randomUUID()));
90
108
  }
91
109
  static set defaultIdFactory(value) {
92
- this._defaultIdFactory = value;
93
110
  remultStatic.defaultIdFactory = value;
94
111
  }
95
112
  /**
113
+ * ### Default
96
114
  * Defines a field that will be used as the id of the entity.
97
115
  * By default it will use `crypto.randomUUID` to generate the id.
98
116
  *
99
- * You can change the algorithm used to generate the id by setting the `Fields.defaultIdFactory`
100
- * to a different function like:
101
- *
102
- * This needs to be done in a shared file to be accessible frontend and backend.
117
+ * ---
118
+ * ### Global `IdFactory`
119
+ * You can change the algorithm used to generate the id by setting `Fields.defaultIdFactory`
120
+ * globally. This code needs to be in a shared file to be accessible frontend and backend.
103
121
  *
104
122
  * ```ts
105
123
  * import { createId } from '@paralleldrive/cuid2'
106
124
  * Fields.defaultIdFactory = () => createId()
107
125
  * ```
108
126
  *
109
- * You can also pass an id factory as an option to the `@Fields.id` to have a different value locally.
127
+ * ---
128
+ * ### Local `IdFactory`
129
+ * You can also pass an `idFactory` as an option to the `@Fields.id` to have a different value on this field.
110
130
  * @example
111
131
  * ```ts
112
132
  * import { createId } from '@paralleldrive/cuid2'
113
133
  * // import { v4 as uuid } from 'uuid'
134
+ * // import { nanoid } from 'nanoid'
114
135
  *
115
136
  * class MyEntity {
116
137
  * \@Fields.id({
117
138
  * idFactory: () => createId()
118
139
  * // idFactory: () => uuid()
140
+ * // idFactory: () => nanoid()
119
141
  * })
120
142
  * id: string = '';
121
143
  * }
122
144
  * ```
123
145
  */
124
146
  static id(options) {
125
- let idFactory = options?.idFactory ?? remultStatic.defaultIdFactory;
126
147
  return Field(() => String, {
127
148
  allowApiUpdate: false,
128
- defaultValue: () => idFactory(),
149
+ defaultValue: () => {
150
+ let idFactory = options?.idFactory ?? Fields.defaultIdFactory;
151
+ return idFactory();
152
+ },
129
153
  saving: (_, r) => {
130
- if (!r.value)
154
+ if (!r.value) {
155
+ let idFactory = options?.idFactory ?? Fields.defaultIdFactory;
131
156
  r.value = idFactory();
157
+ }
132
158
  },
133
159
  ...options,
134
160
  });
@@ -202,8 +228,6 @@ export class Fields {
202
228
  return Field(() => Boolean, ...options);
203
229
  }
204
230
  }
205
- // Initialize remultStatic.defaultIdFactory with the default value
206
- remultStatic.defaultIdFactory = Fields.defaultIdFactory;
207
231
  export class Relations {
208
232
  /**
209
233
  * Define a to-one relation between entities, indicating a one-to-one relationship.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "remult",
3
- "version": "3.1.0-next.1",
3
+ "version": "3.1.0",
4
4
  "description": "A CRUD framework for full-stack TypeScript",
5
5
  "homepage": "https://remult.dev",
6
6
  "repository": {
@@ -17,33 +17,55 @@ export declare class Fields {
17
17
  static updatedAt<entityType = unknown>(...options: (FieldOptions<entityType, Date> | ((options: FieldOptions<entityType, Date>, remult: Remult) => void))[]): ClassFieldDecorator<entityType, Date | undefined>;
18
18
  /** @deprecated use `@Fields.id()` instead */
19
19
  static uuid<entityType = unknown>(...options: (FieldOptions<entityType, string> | ((options: FieldOptions<entityType, string>, remult: Remult) => void))[]): ClassFieldDecorator<entityType, string | undefined>;
20
- private static _defaultIdFactory;
20
+ /**
21
+ * ### cuid
22
+ * @example
23
+ * ```ts
24
+ * import { Fields } from 'remult'
25
+ * import { createId } from '@paralleldrive/cuid2'
26
+ * Fields.defaultIdFactory = () => createId()
27
+ * ```
28
+ *
29
+ * ### nanoid
30
+ * @example
31
+ * ```ts
32
+ * import { Fields } from 'remult'
33
+ * import { nanoid } from 'nanoid'
34
+ * Fields.defaultIdFactory = () => nanoid()
35
+ * ```
36
+ *
37
+ */
21
38
  static get defaultIdFactory(): () => string;
22
39
  static set defaultIdFactory(value: () => string);
23
40
  /**
41
+ * ### Default
24
42
  * Defines a field that will be used as the id of the entity.
25
43
  * By default it will use `crypto.randomUUID` to generate the id.
26
44
  *
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.
45
+ * ---
46
+ * ### Global `IdFactory`
47
+ * You can change the algorithm used to generate the id by setting `Fields.defaultIdFactory`
48
+ * globally. This code needs to be in a shared file to be accessible frontend and backend.
31
49
  *
32
50
  * ```ts
33
51
  * import { createId } from '@paralleldrive/cuid2'
34
52
  * Fields.defaultIdFactory = () => createId()
35
53
  * ```
36
54
  *
37
- * You can also pass an id factory as an option to the `@Fields.id` to have a different value locally.
55
+ * ---
56
+ * ### Local `IdFactory`
57
+ * You can also pass an `idFactory` as an option to the `@Fields.id` to have a different value on this field.
38
58
  * @example
39
59
  * ```ts
40
60
  * import { createId } from '@paralleldrive/cuid2'
41
61
  * // import { v4 as uuid } from 'uuid'
62
+ * // import { nanoid } from 'nanoid'
42
63
  *
43
64
  * class MyEntity {
44
65
  * \@Fields.id({
45
66
  * idFactory: () => createId()
46
67
  * // idFactory: () => uuid()
68
+ * // idFactory: () => nanoid()
47
69
  * })
48
70
  * id: string = '';
49
71
  * }
@@ -135,51 +135,79 @@ var Fields = /** @class */ (function () {
135
135
  return Fields.id(tslib_1.__assign({ idFactory: function () { return crypto.randomUUID(); } }, options));
136
136
  };
137
137
  Object.defineProperty(Fields, "defaultIdFactory", {
138
+ /**
139
+ * ### cuid
140
+ * @example
141
+ * ```ts
142
+ * import { Fields } from 'remult'
143
+ * import { createId } from '@paralleldrive/cuid2'
144
+ * Fields.defaultIdFactory = () => createId()
145
+ * ```
146
+ *
147
+ * ### nanoid
148
+ * @example
149
+ * ```ts
150
+ * import { Fields } from 'remult'
151
+ * import { nanoid } from 'nanoid'
152
+ * Fields.defaultIdFactory = () => nanoid()
153
+ * ```
154
+ *
155
+ */
138
156
  get: function () {
139
- return this._defaultIdFactory;
157
+ var _a;
158
+ return ((_a = remult_static_js_1.remultStatic.defaultIdFactory) !== null && _a !== void 0 ? _a : (remult_static_js_1.remultStatic.defaultIdFactory = function () { return crypto.randomUUID(); }));
140
159
  },
141
160
  set: function (value) {
142
- this._defaultIdFactory = value;
143
161
  remult_static_js_1.remultStatic.defaultIdFactory = value;
144
162
  },
145
163
  enumerable: false,
146
164
  configurable: true
147
165
  });
148
166
  /**
167
+ * ### Default
149
168
  * Defines a field that will be used as the id of the entity.
150
169
  * By default it will use `crypto.randomUUID` to generate the id.
151
170
  *
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.
171
+ * ---
172
+ * ### Global `IdFactory`
173
+ * You can change the algorithm used to generate the id by setting `Fields.defaultIdFactory`
174
+ * globally. This code needs to be in a shared file to be accessible frontend and backend.
156
175
  *
157
176
  * ```ts
158
177
  * import { createId } from '@paralleldrive/cuid2'
159
178
  * Fields.defaultIdFactory = () => createId()
160
179
  * ```
161
180
  *
162
- * You can also pass an id factory as an option to the `@Fields.id` to have a different value locally.
181
+ * ---
182
+ * ### Local `IdFactory`
183
+ * You can also pass an `idFactory` as an option to the `@Fields.id` to have a different value on this field.
163
184
  * @example
164
185
  * ```ts
165
186
  * import { createId } from '@paralleldrive/cuid2'
166
187
  * // import { v4 as uuid } from 'uuid'
188
+ * // import { nanoid } from 'nanoid'
167
189
  *
168
190
  * class MyEntity {
169
191
  * \@Fields.id({
170
192
  * idFactory: () => createId()
171
193
  * // idFactory: () => uuid()
194
+ * // idFactory: () => nanoid()
172
195
  * })
173
196
  * id: string = '';
174
197
  * }
175
198
  * ```
176
199
  */
177
200
  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 idFactory(); }, saving: function (_, r) {
181
- if (!r.value)
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;
182
209
  r.value = idFactory();
210
+ }
183
211
  } }, options));
184
212
  };
185
213
  /**
@@ -276,12 +304,9 @@ var Fields = /** @class */ (function () {
276
304
  }
277
305
  return Field.apply(void 0, tslib_1.__spreadArray([function () { return Boolean; }], tslib_1.__read(options), false));
278
306
  };
279
- Fields._defaultIdFactory = function () { return crypto.randomUUID(); };
280
307
  return Fields;
281
308
  }());
282
309
  exports.Fields = Fields;
283
- // Initialize remultStatic.defaultIdFactory with the default value
284
- remult_static_js_1.remultStatic.defaultIdFactory = Fields.defaultIdFactory;
285
310
  var Relations = /** @class */ (function () {
286
311
  function Relations() {
287
312
  }