@workglow/storage 0.2.6 → 0.2.8

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 (41) hide show
  1. package/dist/browser.js +86 -67
  2. package/dist/browser.js.map +13 -13
  3. package/dist/bun.js +99 -68
  4. package/dist/bun.js.map +19 -19
  5. package/dist/kv/KvViaTabularStorage.d.ts +3 -8
  6. package/dist/kv/KvViaTabularStorage.d.ts.map +1 -1
  7. package/dist/node.js +99 -68
  8. package/dist/node.js.map +19 -19
  9. package/dist/queue/PostgresQueueStorage.d.ts.map +1 -1
  10. package/dist/tabular/BaseSqlTabularStorage.d.ts +6 -1
  11. package/dist/tabular/BaseSqlTabularStorage.d.ts.map +1 -1
  12. package/dist/tabular/BaseTabularStorage.d.ts +1 -1
  13. package/dist/tabular/BaseTabularStorage.d.ts.map +1 -1
  14. package/dist/tabular/CachedTabularStorage.d.ts +1 -1
  15. package/dist/tabular/CachedTabularStorage.d.ts.map +1 -1
  16. package/dist/tabular/FsFolderTabularStorage.d.ts +1 -1
  17. package/dist/tabular/FsFolderTabularStorage.d.ts.map +1 -1
  18. package/dist/tabular/HuggingFaceTabularStorage.d.ts.map +1 -1
  19. package/dist/tabular/InMemoryTabularStorage.d.ts +1 -1
  20. package/dist/tabular/InMemoryTabularStorage.d.ts.map +1 -1
  21. package/dist/tabular/IndexedDbTabularStorage.d.ts +1 -1
  22. package/dist/tabular/IndexedDbTabularStorage.d.ts.map +1 -1
  23. package/dist/tabular/PostgresTabularStorage.d.ts +1 -1
  24. package/dist/tabular/PostgresTabularStorage.d.ts.map +1 -1
  25. package/dist/tabular/SharedInMemoryTabularStorage.d.ts +1 -1
  26. package/dist/tabular/SharedInMemoryTabularStorage.d.ts.map +1 -1
  27. package/dist/tabular/SqliteTabularStorage.d.ts +1 -1
  28. package/dist/tabular/SqliteTabularStorage.d.ts.map +1 -1
  29. package/dist/tabular/SupabaseTabularStorage.d.ts +1 -1
  30. package/dist/tabular/SupabaseTabularStorage.d.ts.map +1 -1
  31. package/dist/vector/InMemoryVectorStorage.d.ts +3 -3
  32. package/dist/vector/InMemoryVectorStorage.d.ts.map +1 -1
  33. package/dist/vector/IndexedDbVectorStorage.d.ts +3 -3
  34. package/dist/vector/IndexedDbVectorStorage.d.ts.map +1 -1
  35. package/dist/vector/PostgresVectorStorage.d.ts +2 -2
  36. package/dist/vector/PostgresVectorStorage.d.ts.map +1 -1
  37. package/dist/vector/SqliteAiVectorStorage.d.ts +2 -2
  38. package/dist/vector/SqliteAiVectorStorage.d.ts.map +1 -1
  39. package/dist/vector/SqliteVectorStorage.d.ts +2 -2
  40. package/dist/vector/SqliteVectorStorage.d.ts.map +1 -1
  41. package/package.json +7 -7
package/dist/browser.js CHANGED
@@ -395,30 +395,36 @@ class InMemoryTabularStorage extends BaseTabularStorage {
395
395
  }
396
396
  async put(value) {
397
397
  let entityToStore = value;
398
- if (this.hasAutoGeneratedKey() && this.autoGeneratedKeyName) {
399
- const keyName = this.autoGeneratedKeyName;
400
- const clientProvidedValue = value[keyName];
401
- const hasClientValue = clientProvidedValue !== undefined && clientProvidedValue !== null;
402
- let shouldGenerate = false;
403
- if (this.clientProvidedKeys === "never") {
404
- shouldGenerate = true;
405
- } else if (this.clientProvidedKeys === "always") {
406
- if (!hasClientValue) {
407
- throw new Error(`Auto-generated key "${keyName}" is required when clientProvidedKeys is "always"`);
398
+ const savedCounter = this.autoIncrementCounter;
399
+ try {
400
+ if (this.hasAutoGeneratedKey() && this.autoGeneratedKeyName) {
401
+ const keyName = this.autoGeneratedKeyName;
402
+ const clientProvidedValue = value[keyName];
403
+ const hasClientValue = clientProvidedValue !== undefined && clientProvidedValue !== null;
404
+ let shouldGenerate = false;
405
+ if (this.clientProvidedKeys === "never") {
406
+ shouldGenerate = true;
407
+ } else if (this.clientProvidedKeys === "always") {
408
+ if (!hasClientValue) {
409
+ throw new Error(`Auto-generated key "${keyName}" is required when clientProvidedKeys is "always"`);
410
+ }
411
+ shouldGenerate = false;
412
+ } else {
413
+ shouldGenerate = !hasClientValue;
414
+ }
415
+ if (shouldGenerate) {
416
+ const generatedValue = this.generateKeyValue(keyName, this.autoGeneratedKeyStrategy);
417
+ entityToStore = { ...value, [keyName]: generatedValue };
408
418
  }
409
- shouldGenerate = false;
410
- } else {
411
- shouldGenerate = !hasClientValue;
412
- }
413
- if (shouldGenerate) {
414
- const generatedValue = this.generateKeyValue(keyName, this.autoGeneratedKeyStrategy);
415
- entityToStore = { ...value, [keyName]: generatedValue };
416
419
  }
420
+ const { key } = this.separateKeyValueFromCombined(entityToStore);
421
+ const id = await makeFingerprint2(key);
422
+ this._lastPutWasInsert = !this.values.has(id);
423
+ this.values.set(id, entityToStore);
424
+ } catch (e) {
425
+ this.autoIncrementCounter = savedCounter;
426
+ throw e;
417
427
  }
418
- const { key } = this.separateKeyValueFromCombined(entityToStore);
419
- const id = await makeFingerprint2(key);
420
- this._lastPutWasInsert = !this.values.has(id);
421
- this.values.set(id, entityToStore);
422
428
  this.events.emit("put", entityToStore);
423
429
  return entityToStore;
424
430
  }
@@ -1274,46 +1280,42 @@ class KvStorage {
1274
1280
  }
1275
1281
 
1276
1282
  // src/kv/KvViaTabularStorage.ts
1283
+ var PRIMITIVE_SCHEMA_TYPES = new Set(["number", "boolean", "string", "blob"]);
1284
+
1277
1285
  class KvViaTabularStorage extends KvStorage {
1286
+ get needsJsonSerialization() {
1287
+ if (this._needsJsonSerialization === undefined) {
1288
+ const schemaType = typeof this.valueSchema === "object" && this.valueSchema !== null && "type" in this.valueSchema ? this.valueSchema.type : undefined;
1289
+ this._needsJsonSerialization = !PRIMITIVE_SCHEMA_TYPES.has(schemaType);
1290
+ }
1291
+ return this._needsJsonSerialization;
1292
+ }
1293
+ _needsJsonSerialization;
1278
1294
  async setupDatabase() {
1279
1295
  await this.tabularRepository.setupDatabase?.();
1280
1296
  }
1281
1297
  async put(key, value) {
1282
- const schemaType = typeof this.valueSchema === "object" && this.valueSchema !== null && "type" in this.valueSchema ? this.valueSchema.type : undefined;
1283
- const shouldStringify = !["number", "boolean", "string", "blob"].includes(schemaType);
1284
- if (shouldStringify) {
1298
+ if (this.needsJsonSerialization) {
1285
1299
  value = JSON.stringify(value);
1286
1300
  }
1287
1301
  await this.tabularRepository.put({ key, value });
1288
1302
  }
1289
1303
  async putBulk(items) {
1290
- const schemaType = typeof this.valueSchema === "object" && this.valueSchema !== null && "type" in this.valueSchema ? this.valueSchema.type : undefined;
1291
- const shouldStringify = !["number", "boolean", "string", "blob"].includes(schemaType);
1292
- const entities = items.map(({ key, value }) => {
1293
- if (shouldStringify) {
1294
- value = JSON.stringify(value);
1295
- }
1296
- return { key, value };
1297
- });
1304
+ const entities = this.needsJsonSerialization ? items.map(({ key, value }) => ({ key, value: JSON.stringify(value) })) : items;
1298
1305
  await this.tabularRepository.putBulk(entities);
1299
1306
  }
1300
1307
  async get(key) {
1301
1308
  const result = await this.tabularRepository.get({ key });
1302
- if (result) {
1303
- const schemaType = typeof this.valueSchema === "object" && this.valueSchema !== null && "type" in this.valueSchema ? this.valueSchema.type : undefined;
1304
- const shouldParse = !["number", "boolean", "string", "blob"].includes(schemaType);
1305
- if (shouldParse) {
1306
- try {
1307
- return JSON.parse(result.value);
1308
- } catch (e) {
1309
- return result.value;
1310
- }
1311
- } else {
1309
+ if (!result)
1310
+ return;
1311
+ if (this.needsJsonSerialization) {
1312
+ try {
1313
+ return JSON.parse(result.value);
1314
+ } catch (e) {
1312
1315
  return result.value;
1313
1316
  }
1314
- } else {
1315
- return;
1316
1317
  }
1318
+ return result.value;
1317
1319
  }
1318
1320
  async delete(key) {
1319
1321
  return await this.tabularRepository.delete({ key });
@@ -1324,9 +1326,7 @@ class KvViaTabularStorage extends KvStorage {
1324
1326
  return values.map((value) => ({
1325
1327
  key: value.key,
1326
1328
  value: (() => {
1327
- const schemaType = typeof this.valueSchema === "object" && this.valueSchema !== null && "type" in this.valueSchema ? this.valueSchema.type : undefined;
1328
- const shouldParse = !["number", "boolean", "string"].includes(schemaType);
1329
- if (shouldParse && typeof value.value === "string") {
1329
+ if (this.needsJsonSerialization && typeof value.value === "string") {
1330
1330
  try {
1331
1331
  return JSON.parse(value.value);
1332
1332
  } catch (e) {
@@ -3230,31 +3230,40 @@ import { createServiceToken as createServiceToken14 } from "@workglow/util";
3230
3230
  // src/tabular/BaseSqlTabularStorage.ts
3231
3231
  class BaseSqlTabularStorage extends BaseTabularStorage {
3232
3232
  table;
3233
+ _pkColsCache = new Map;
3234
+ _valColsCache = new Map;
3235
+ _pkColListCache = new Map;
3236
+ _valColListCache = new Map;
3233
3237
  constructor(table = "tabular_store", schema, primaryKeyNames, indexes = [], clientProvidedKeys = "if-missing") {
3234
3238
  super(schema, primaryKeyNames, indexes, clientProvidedKeys);
3235
3239
  this.table = table;
3236
3240
  this.validateTableAndSchema();
3237
3241
  }
3238
3242
  constructPrimaryKeyColumns($delimiter = "") {
3239
- const cols = Object.entries(this.primaryKeySchema.properties).map(([key, typeDef]) => {
3240
- const sqlType = this.mapTypeToSQL(typeDef);
3241
- return `${$delimiter}${key}${$delimiter} ${sqlType} NOT NULL`;
3242
- }).join(", ");
3243
- return cols;
3243
+ let cached = this._pkColsCache.get($delimiter);
3244
+ if (cached === undefined) {
3245
+ cached = Object.entries(this.primaryKeySchema.properties).map(([key, typeDef]) => {
3246
+ const sqlType = this.mapTypeToSQL(typeDef);
3247
+ return `${$delimiter}${key}${$delimiter} ${sqlType} NOT NULL`;
3248
+ }).join(", ");
3249
+ this._pkColsCache.set($delimiter, cached);
3250
+ }
3251
+ return cached;
3244
3252
  }
3245
3253
  constructValueColumns($delimiter = "") {
3246
- const requiredSet = new Set(this.valueSchema.required ?? []);
3247
- const cols = Object.entries(this.valueSchema.properties).map(([key, typeDef]) => {
3248
- const sqlType = this.mapTypeToSQL(typeDef);
3249
- const isRequired = requiredSet.has(key);
3250
- const nullable = !isRequired || this.isNullable(typeDef);
3251
- return `${$delimiter}${key}${$delimiter} ${sqlType}${nullable ? " NULL" : " NOT NULL"}`;
3252
- }).join(", ");
3253
- if (cols.length > 0) {
3254
- return `, ${cols}`;
3255
- } else {
3256
- return "";
3257
- }
3254
+ let cached = this._valColsCache.get($delimiter);
3255
+ if (cached === undefined) {
3256
+ const requiredSet = new Set(this.valueSchema.required ?? []);
3257
+ const cols = Object.entries(this.valueSchema.properties).map(([key, typeDef]) => {
3258
+ const sqlType = this.mapTypeToSQL(typeDef);
3259
+ const isRequired = requiredSet.has(key);
3260
+ const nullable = !isRequired || this.isNullable(typeDef);
3261
+ return `${$delimiter}${key}${$delimiter} ${sqlType}${nullable ? " NULL" : " NOT NULL"}`;
3262
+ }).join(", ");
3263
+ cached = cols.length > 0 ? `, ${cols}` : "";
3264
+ this._valColsCache.set($delimiter, cached);
3265
+ }
3266
+ return cached;
3258
3267
  }
3259
3268
  isNullable(typeDef) {
3260
3269
  if (typeof typeDef === "boolean")
@@ -3274,10 +3283,20 @@ class BaseSqlTabularStorage extends BaseTabularStorage {
3274
3283
  return false;
3275
3284
  }
3276
3285
  primaryKeyColumnList($delimiter = "") {
3277
- return $delimiter + this.primaryKeyColumns().join(`${$delimiter}, ${$delimiter}`) + $delimiter;
3286
+ let cached = this._pkColListCache.get($delimiter);
3287
+ if (cached === undefined) {
3288
+ cached = $delimiter + this.primaryKeyColumns().join(`${$delimiter}, ${$delimiter}`) + $delimiter;
3289
+ this._pkColListCache.set($delimiter, cached);
3290
+ }
3291
+ return cached;
3278
3292
  }
3279
3293
  valueColumnList($delimiter = "") {
3280
- return $delimiter + this.valueColumns().join(`${$delimiter}, ${$delimiter}`) + $delimiter;
3294
+ let cached = this._valColListCache.get($delimiter);
3295
+ if (cached === undefined) {
3296
+ cached = $delimiter + this.valueColumns().join(`${$delimiter}, ${$delimiter}`) + $delimiter;
3297
+ this._valColListCache.set($delimiter, cached);
3298
+ }
3299
+ return cached;
3281
3300
  }
3282
3301
  getNonNullType(typeDef) {
3283
3302
  if (typeof typeDef === "boolean")
@@ -5549,4 +5568,4 @@ export {
5549
5568
  BaseTabularStorage
5550
5569
  };
5551
5570
 
5552
- //# debugId=EAA8208BC9EF6C8264756E2164756E21
5571
+ //# debugId=AE3242FDE13E44E164756E2164756E21