@workglow/storage 0.2.5 → 0.2.7

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 (42) hide show
  1. package/dist/browser.js +89 -95
  2. package/dist/browser.js.map +14 -14
  3. package/dist/bun.js +127 -122
  4. package/dist/bun.js.map +20 -20
  5. package/dist/kv/KvViaTabularStorage.d.ts +3 -8
  6. package/dist/kv/KvViaTabularStorage.d.ts.map +1 -1
  7. package/dist/node.js +127 -122
  8. package/dist/node.js.map +20 -20
  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 +2 -2
  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/util/IndexedDbTable.d.ts.map +1 -1
  32. package/dist/vector/InMemoryVectorStorage.d.ts +8 -6
  33. package/dist/vector/InMemoryVectorStorage.d.ts.map +1 -1
  34. package/dist/vector/IndexedDbVectorStorage.d.ts +7 -9
  35. package/dist/vector/IndexedDbVectorStorage.d.ts.map +1 -1
  36. package/dist/vector/PostgresVectorStorage.d.ts +5 -5
  37. package/dist/vector/PostgresVectorStorage.d.ts.map +1 -1
  38. package/dist/vector/SqliteAiVectorStorage.d.ts +6 -6
  39. package/dist/vector/SqliteAiVectorStorage.d.ts.map +1 -1
  40. package/dist/vector/SqliteVectorStorage.d.ts +11 -7
  41. package/dist/vector/SqliteVectorStorage.d.ts.map +1 -1
  42. package/package.json +5 -5
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) {
@@ -2066,13 +2066,11 @@ function textRelevance(text, query) {
2066
2066
 
2067
2067
  class InMemoryVectorStorage extends InMemoryTabularStorage {
2068
2068
  vectorDimensions;
2069
- VectorType;
2070
2069
  vectorPropertyName;
2071
2070
  metadataPropertyName;
2072
- constructor(schema, primaryKeyNames, indexes = [], dimensions, VectorType = Float32Array) {
2071
+ constructor(schema, primaryKeyNames, indexes = [], dimensions, _vectorCtor = Float32Array) {
2073
2072
  super(schema, primaryKeyNames, indexes);
2074
2073
  this.vectorDimensions = dimensions;
2075
- this.VectorType = VectorType;
2076
2074
  const vectorProp = getVectorProperty(schema);
2077
2075
  if (!vectorProp) {
2078
2076
  throw new Error("Schema must have a property with type array and format TypedArray");
@@ -2298,24 +2296,6 @@ async function saveSchemaMetadata(db, tableName, snapshot) {
2298
2296
  }
2299
2297
  });
2300
2298
  }
2301
- async function loadSchemaMetadata(db, tableName) {
2302
- return new Promise((resolve) => {
2303
- try {
2304
- if (!db.objectStoreNames.contains(METADATA_STORE_NAME)) {
2305
- resolve(null);
2306
- return;
2307
- }
2308
- const transaction = db.transaction(METADATA_STORE_NAME, "readonly");
2309
- const store = transaction.objectStore(METADATA_STORE_NAME);
2310
- const request = store.get(tableName);
2311
- request.onsuccess = () => resolve(request.result || null);
2312
- request.onerror = () => resolve(null);
2313
- transaction.onerror = () => resolve(null);
2314
- } catch (err) {
2315
- resolve(null);
2316
- }
2317
- });
2318
- }
2319
2299
  async function openIndexedDbTable(tableName, version, upgradeNeededCallback) {
2320
2300
  return new Promise((resolve, reject) => {
2321
2301
  const openRequest = indexedDB.open(tableName, version);
@@ -2407,7 +2387,6 @@ async function performIncrementalMigration(db, tableName, diff, options = {}) {
2407
2387
  db.close();
2408
2388
  options.onMigrationProgress?.(`Migrating ${tableName} from version ${currentVersion} to ${newVersion}...`, 0);
2409
2389
  return openIndexedDbTable(tableName, newVersion, (event) => {
2410
- const db2 = event.target.result;
2411
2390
  const transaction = event.target.transaction;
2412
2391
  const store = transaction.objectStore(tableName);
2413
2392
  for (const indexName of diff.indexesToRemove) {
@@ -2469,7 +2448,6 @@ async function performDestructiveMigration(db, tableName, primaryKey, expectedIn
2469
2448
  options.onMigrationProgress?.(`Recreating object store...`, 0.75);
2470
2449
  const newDb = await openIndexedDbTable(tableName, newVersion, (event) => {
2471
2450
  const db2 = event.target.result;
2472
- const transaction = event.target.transaction;
2473
2451
  if (db2.objectStoreNames.contains(tableName)) {
2474
2452
  db2.deleteObjectStore(tableName);
2475
2453
  }
@@ -2570,7 +2548,6 @@ async function ensureIndexedDbTable(tableName, primaryKey, expectedIndexes = [],
2570
2548
  }
2571
2549
  });
2572
2550
  }
2573
- const metadata = await loadSchemaMetadata(db, tableName);
2574
2551
  if (!db.objectStoreNames.contains(tableName)) {
2575
2552
  options.onMigrationProgress?.(`Object store ${tableName} does not exist, creating...`, 0);
2576
2553
  db.close();
@@ -3253,31 +3230,40 @@ import { createServiceToken as createServiceToken14 } from "@workglow/util";
3253
3230
  // src/tabular/BaseSqlTabularStorage.ts
3254
3231
  class BaseSqlTabularStorage extends BaseTabularStorage {
3255
3232
  table;
3233
+ _pkColsCache = new Map;
3234
+ _valColsCache = new Map;
3235
+ _pkColListCache = new Map;
3236
+ _valColListCache = new Map;
3256
3237
  constructor(table = "tabular_store", schema, primaryKeyNames, indexes = [], clientProvidedKeys = "if-missing") {
3257
3238
  super(schema, primaryKeyNames, indexes, clientProvidedKeys);
3258
3239
  this.table = table;
3259
3240
  this.validateTableAndSchema();
3260
3241
  }
3261
3242
  constructPrimaryKeyColumns($delimiter = "") {
3262
- const cols = Object.entries(this.primaryKeySchema.properties).map(([key, typeDef]) => {
3263
- const sqlType = this.mapTypeToSQL(typeDef);
3264
- return `${$delimiter}${key}${$delimiter} ${sqlType} NOT NULL`;
3265
- }).join(", ");
3266
- 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;
3267
3252
  }
3268
3253
  constructValueColumns($delimiter = "") {
3269
- const requiredSet = new Set(this.valueSchema.required ?? []);
3270
- const cols = Object.entries(this.valueSchema.properties).map(([key, typeDef]) => {
3271
- const sqlType = this.mapTypeToSQL(typeDef);
3272
- const isRequired = requiredSet.has(key);
3273
- const nullable = !isRequired || this.isNullable(typeDef);
3274
- return `${$delimiter}${key}${$delimiter} ${sqlType}${nullable ? " NULL" : " NOT NULL"}`;
3275
- }).join(", ");
3276
- if (cols.length > 0) {
3277
- return `, ${cols}`;
3278
- } else {
3279
- return "";
3280
- }
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;
3281
3267
  }
3282
3268
  isNullable(typeDef) {
3283
3269
  if (typeof typeDef === "boolean")
@@ -3297,10 +3283,20 @@ class BaseSqlTabularStorage extends BaseTabularStorage {
3297
3283
  return false;
3298
3284
  }
3299
3285
  primaryKeyColumnList($delimiter = "") {
3300
- 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;
3301
3292
  }
3302
3293
  valueColumnList($delimiter = "") {
3303
- 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;
3304
3300
  }
3305
3301
  getNonNullType(typeDef) {
3306
3302
  if (typeof typeDef === "boolean")
@@ -5405,8 +5401,8 @@ class SupabaseRateLimiterStorage {
5405
5401
  }
5406
5402
  }
5407
5403
  // src/vector/IndexedDbVectorStorage.ts
5408
- import { cosineSimilarity as cosineSimilarity2 } from "@workglow/util/schema";
5409
5404
  import { createServiceToken as createServiceToken21 } from "@workglow/util";
5405
+ import { cosineSimilarity as cosineSimilarity2 } from "@workglow/util/schema";
5410
5406
  var IDB_VECTOR_REPOSITORY = createServiceToken21("storage.vectorRepository.indexedDb");
5411
5407
  function matchesFilter2(metadata, filter) {
5412
5408
  for (const [key, value] of Object.entries(filter)) {
@@ -5434,13 +5430,11 @@ function textRelevance2(text, query) {
5434
5430
 
5435
5431
  class IndexedDbVectorStorage extends IndexedDbTabularStorage {
5436
5432
  vectorDimensions;
5437
- VectorType;
5438
5433
  vectorPropertyName;
5439
5434
  metadataPropertyName;
5440
- constructor(table = "vectors", schema, primaryKeyNames, indexes = [], dimensions, VectorType = Float32Array, migrationOptions = {}, clientProvidedKeys = "if-missing") {
5435
+ constructor(table = "vectors", schema, primaryKeyNames, indexes = [], dimensions, _vectorCtor = Float32Array, migrationOptions = {}, clientProvidedKeys = "if-missing") {
5441
5436
  super(table, schema, primaryKeyNames, indexes, migrationOptions, clientProvidedKeys);
5442
5437
  this.vectorDimensions = dimensions;
5443
- this.VectorType = VectorType;
5444
5438
  const vectorProp = getVectorProperty(schema);
5445
5439
  if (!vectorProp) {
5446
5440
  throw new Error("Schema must have a property with type array and format TypedArray");
@@ -5574,4 +5568,4 @@ export {
5574
5568
  BaseTabularStorage
5575
5569
  };
5576
5570
 
5577
- //# debugId=5DBA415CCD0F79AA64756E2164756E21
5571
+ //# debugId=AE3242FDE13E44E164756E2164756E21