s3db.js 7.4.1 → 7.4.2

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "s3db.js",
3
- "version": "7.4.1",
3
+ "version": "7.4.2",
4
4
  "description": "Use AWS S3, the world's most reliable document storage, as a database with this ORM.",
5
5
  "main": "dist/s3db.cjs.js",
6
6
  "module": "dist/s3db.es.js",
@@ -113,6 +113,23 @@ export class Database extends EventEmitter {
113
113
 
114
114
  if (versionData) {
115
115
  // Extract configuration from version data at root level
116
+ // Restore ID generator configuration
117
+ let restoredIdGenerator, restoredIdSize;
118
+ if (versionData.idGenerator !== undefined) {
119
+ if (versionData.idGenerator === 'custom_function') {
120
+ // Custom function was used but can't be restored - use default
121
+ restoredIdGenerator = undefined;
122
+ restoredIdSize = versionData.idSize || 22;
123
+ } else if (typeof versionData.idGenerator === 'number') {
124
+ // Size-based generator
125
+ restoredIdGenerator = versionData.idGenerator;
126
+ restoredIdSize = versionData.idSize || versionData.idGenerator;
127
+ }
128
+ } else {
129
+ // Legacy resource without saved ID config
130
+ restoredIdSize = versionData.idSize || 22;
131
+ }
132
+
116
133
  this.resources[name] = new Resource({
117
134
  name,
118
135
  client: this.client,
@@ -131,7 +148,9 @@ export class Database extends EventEmitter {
131
148
  autoDecrypt: versionData.autoDecrypt !== undefined ? versionData.autoDecrypt : true,
132
149
  hooks: versionData.hooks || {},
133
150
  versioningEnabled: this.versioningEnabled,
134
- map: versionData.map
151
+ map: versionData.map,
152
+ idGenerator: restoredIdGenerator,
153
+ idSize: restoredIdSize
135
154
  });
136
155
  }
137
156
  }
@@ -334,6 +353,8 @@ export class Database extends EventEmitter {
334
353
  autoDecrypt: resource.config.autoDecrypt,
335
354
  cache: resource.config.cache,
336
355
  hooks: resource.config.hooks,
356
+ idSize: resource.idSize,
357
+ idGenerator: resource.idGeneratorType,
337
358
  createdAt: isNewVersion ? new Date().toISOString() : existingVersionData?.createdAt
338
359
  }
339
360
  }
@@ -142,6 +142,19 @@ export class Resource extends EventEmitter {
142
142
 
143
143
  // Configure ID generator
144
144
  this.idGenerator = this.configureIdGenerator(customIdGenerator, idSize);
145
+
146
+ // Store ID configuration for persistence
147
+ // If customIdGenerator is a number, use it as idSize
148
+ // Otherwise, use the provided idSize or default to 22
149
+ if (typeof customIdGenerator === 'number' && customIdGenerator > 0) {
150
+ this.idSize = customIdGenerator;
151
+ } else if (typeof idSize === 'number' && idSize > 0) {
152
+ this.idSize = idSize;
153
+ } else {
154
+ this.idSize = 22;
155
+ }
156
+
157
+ this.idGeneratorType = this.getIdGeneratorType(customIdGenerator, this.idSize);
145
158
 
146
159
  // Store configuration - all at root level
147
160
  this.config = {
@@ -220,9 +233,9 @@ export class Resource extends EventEmitter {
220
233
  * @private
221
234
  */
222
235
  configureIdGenerator(customIdGenerator, idSize) {
223
- // If a custom function is provided, use it
236
+ // If a custom function is provided, wrap it to ensure string output
224
237
  if (typeof customIdGenerator === 'function') {
225
- return customIdGenerator;
238
+ return () => String(customIdGenerator());
226
239
  }
227
240
  // If customIdGenerator is a number (size), create a generator with that size
228
241
  if (typeof customIdGenerator === 'number' && customIdGenerator > 0) {
@@ -236,6 +249,22 @@ export class Resource extends EventEmitter {
236
249
  return defaultIdGenerator;
237
250
  }
238
251
 
252
+ /**
253
+ * Get a serializable representation of the ID generator type
254
+ * @param {Function|number} customIdGenerator - Custom ID generator function or size
255
+ * @param {number} idSize - Size for auto-generated IDs
256
+ * @returns {string|number} Serializable ID generator type
257
+ * @private
258
+ */
259
+ getIdGeneratorType(customIdGenerator, idSize) {
260
+ // If a custom function is provided
261
+ if (typeof customIdGenerator === 'function') {
262
+ return 'custom_function';
263
+ }
264
+ // For number generators or default size, return the actual idSize
265
+ return idSize;
266
+ }
267
+
239
268
  /**
240
269
  * Get resource options (for backward compatibility with tests)
241
270
  */