@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/bun.js CHANGED
@@ -398,30 +398,36 @@ class InMemoryTabularStorage extends BaseTabularStorage {
398
398
  }
399
399
  async put(value) {
400
400
  let entityToStore = value;
401
- if (this.hasAutoGeneratedKey() && this.autoGeneratedKeyName) {
402
- const keyName = this.autoGeneratedKeyName;
403
- const clientProvidedValue = value[keyName];
404
- const hasClientValue = clientProvidedValue !== undefined && clientProvidedValue !== null;
405
- let shouldGenerate = false;
406
- if (this.clientProvidedKeys === "never") {
407
- shouldGenerate = true;
408
- } else if (this.clientProvidedKeys === "always") {
409
- if (!hasClientValue) {
410
- throw new Error(`Auto-generated key "${keyName}" is required when clientProvidedKeys is "always"`);
401
+ const savedCounter = this.autoIncrementCounter;
402
+ try {
403
+ if (this.hasAutoGeneratedKey() && this.autoGeneratedKeyName) {
404
+ const keyName = this.autoGeneratedKeyName;
405
+ const clientProvidedValue = value[keyName];
406
+ const hasClientValue = clientProvidedValue !== undefined && clientProvidedValue !== null;
407
+ let shouldGenerate = false;
408
+ if (this.clientProvidedKeys === "never") {
409
+ shouldGenerate = true;
410
+ } else if (this.clientProvidedKeys === "always") {
411
+ if (!hasClientValue) {
412
+ throw new Error(`Auto-generated key "${keyName}" is required when clientProvidedKeys is "always"`);
413
+ }
414
+ shouldGenerate = false;
415
+ } else {
416
+ shouldGenerate = !hasClientValue;
417
+ }
418
+ if (shouldGenerate) {
419
+ const generatedValue = this.generateKeyValue(keyName, this.autoGeneratedKeyStrategy);
420
+ entityToStore = { ...value, [keyName]: generatedValue };
411
421
  }
412
- shouldGenerate = false;
413
- } else {
414
- shouldGenerate = !hasClientValue;
415
- }
416
- if (shouldGenerate) {
417
- const generatedValue = this.generateKeyValue(keyName, this.autoGeneratedKeyStrategy);
418
- entityToStore = { ...value, [keyName]: generatedValue };
419
422
  }
423
+ const { key } = this.separateKeyValueFromCombined(entityToStore);
424
+ const id = await makeFingerprint2(key);
425
+ this._lastPutWasInsert = !this.values.has(id);
426
+ this.values.set(id, entityToStore);
427
+ } catch (e) {
428
+ this.autoIncrementCounter = savedCounter;
429
+ throw e;
420
430
  }
421
- const { key } = this.separateKeyValueFromCombined(entityToStore);
422
- const id = await makeFingerprint2(key);
423
- this._lastPutWasInsert = !this.values.has(id);
424
- this.values.set(id, entityToStore);
425
431
  this.events.emit("put", entityToStore);
426
432
  return entityToStore;
427
433
  }
@@ -1277,46 +1283,42 @@ class KvStorage {
1277
1283
  }
1278
1284
 
1279
1285
  // src/kv/KvViaTabularStorage.ts
1286
+ var PRIMITIVE_SCHEMA_TYPES = new Set(["number", "boolean", "string", "blob"]);
1287
+
1280
1288
  class KvViaTabularStorage extends KvStorage {
1289
+ get needsJsonSerialization() {
1290
+ if (this._needsJsonSerialization === undefined) {
1291
+ const schemaType = typeof this.valueSchema === "object" && this.valueSchema !== null && "type" in this.valueSchema ? this.valueSchema.type : undefined;
1292
+ this._needsJsonSerialization = !PRIMITIVE_SCHEMA_TYPES.has(schemaType);
1293
+ }
1294
+ return this._needsJsonSerialization;
1295
+ }
1296
+ _needsJsonSerialization;
1281
1297
  async setupDatabase() {
1282
1298
  await this.tabularRepository.setupDatabase?.();
1283
1299
  }
1284
1300
  async put(key, value) {
1285
- const schemaType = typeof this.valueSchema === "object" && this.valueSchema !== null && "type" in this.valueSchema ? this.valueSchema.type : undefined;
1286
- const shouldStringify = !["number", "boolean", "string", "blob"].includes(schemaType);
1287
- if (shouldStringify) {
1301
+ if (this.needsJsonSerialization) {
1288
1302
  value = JSON.stringify(value);
1289
1303
  }
1290
1304
  await this.tabularRepository.put({ key, value });
1291
1305
  }
1292
1306
  async putBulk(items) {
1293
- const schemaType = typeof this.valueSchema === "object" && this.valueSchema !== null && "type" in this.valueSchema ? this.valueSchema.type : undefined;
1294
- const shouldStringify = !["number", "boolean", "string", "blob"].includes(schemaType);
1295
- const entities = items.map(({ key, value }) => {
1296
- if (shouldStringify) {
1297
- value = JSON.stringify(value);
1298
- }
1299
- return { key, value };
1300
- });
1307
+ const entities = this.needsJsonSerialization ? items.map(({ key, value }) => ({ key, value: JSON.stringify(value) })) : items;
1301
1308
  await this.tabularRepository.putBulk(entities);
1302
1309
  }
1303
1310
  async get(key) {
1304
1311
  const result = await this.tabularRepository.get({ key });
1305
- if (result) {
1306
- const schemaType = typeof this.valueSchema === "object" && this.valueSchema !== null && "type" in this.valueSchema ? this.valueSchema.type : undefined;
1307
- const shouldParse = !["number", "boolean", "string", "blob"].includes(schemaType);
1308
- if (shouldParse) {
1309
- try {
1310
- return JSON.parse(result.value);
1311
- } catch (e) {
1312
- return result.value;
1313
- }
1314
- } else {
1312
+ if (!result)
1313
+ return;
1314
+ if (this.needsJsonSerialization) {
1315
+ try {
1316
+ return JSON.parse(result.value);
1317
+ } catch (e) {
1315
1318
  return result.value;
1316
1319
  }
1317
- } else {
1318
- return;
1319
1320
  }
1321
+ return result.value;
1320
1322
  }
1321
1323
  async delete(key) {
1322
1324
  return await this.tabularRepository.delete({ key });
@@ -1327,9 +1329,7 @@ class KvViaTabularStorage extends KvStorage {
1327
1329
  return values.map((value) => ({
1328
1330
  key: value.key,
1329
1331
  value: (() => {
1330
- const schemaType = typeof this.valueSchema === "object" && this.valueSchema !== null && "type" in this.valueSchema ? this.valueSchema.type : undefined;
1331
- const shouldParse = !["number", "boolean", "string"].includes(schemaType);
1332
- if (shouldParse && typeof value.value === "string") {
1332
+ if (this.needsJsonSerialization && typeof value.value === "string") {
1333
1333
  try {
1334
1334
  return JSON.parse(value.value);
1335
1335
  } catch (e) {
@@ -2503,31 +2503,40 @@ import { createServiceToken as createServiceToken13 } from "@workglow/util";
2503
2503
  // src/tabular/BaseSqlTabularStorage.ts
2504
2504
  class BaseSqlTabularStorage extends BaseTabularStorage {
2505
2505
  table;
2506
+ _pkColsCache = new Map;
2507
+ _valColsCache = new Map;
2508
+ _pkColListCache = new Map;
2509
+ _valColListCache = new Map;
2506
2510
  constructor(table = "tabular_store", schema, primaryKeyNames, indexes = [], clientProvidedKeys = "if-missing") {
2507
2511
  super(schema, primaryKeyNames, indexes, clientProvidedKeys);
2508
2512
  this.table = table;
2509
2513
  this.validateTableAndSchema();
2510
2514
  }
2511
2515
  constructPrimaryKeyColumns($delimiter = "") {
2512
- const cols = Object.entries(this.primaryKeySchema.properties).map(([key, typeDef]) => {
2513
- const sqlType = this.mapTypeToSQL(typeDef);
2514
- return `${$delimiter}${key}${$delimiter} ${sqlType} NOT NULL`;
2515
- }).join(", ");
2516
- return cols;
2516
+ let cached = this._pkColsCache.get($delimiter);
2517
+ if (cached === undefined) {
2518
+ cached = Object.entries(this.primaryKeySchema.properties).map(([key, typeDef]) => {
2519
+ const sqlType = this.mapTypeToSQL(typeDef);
2520
+ return `${$delimiter}${key}${$delimiter} ${sqlType} NOT NULL`;
2521
+ }).join(", ");
2522
+ this._pkColsCache.set($delimiter, cached);
2523
+ }
2524
+ return cached;
2517
2525
  }
2518
2526
  constructValueColumns($delimiter = "") {
2519
- const requiredSet = new Set(this.valueSchema.required ?? []);
2520
- const cols = Object.entries(this.valueSchema.properties).map(([key, typeDef]) => {
2521
- const sqlType = this.mapTypeToSQL(typeDef);
2522
- const isRequired = requiredSet.has(key);
2523
- const nullable = !isRequired || this.isNullable(typeDef);
2524
- return `${$delimiter}${key}${$delimiter} ${sqlType}${nullable ? " NULL" : " NOT NULL"}`;
2525
- }).join(", ");
2526
- if (cols.length > 0) {
2527
- return `, ${cols}`;
2528
- } else {
2529
- return "";
2530
- }
2527
+ let cached = this._valColsCache.get($delimiter);
2528
+ if (cached === undefined) {
2529
+ const requiredSet = new Set(this.valueSchema.required ?? []);
2530
+ const cols = Object.entries(this.valueSchema.properties).map(([key, typeDef]) => {
2531
+ const sqlType = this.mapTypeToSQL(typeDef);
2532
+ const isRequired = requiredSet.has(key);
2533
+ const nullable = !isRequired || this.isNullable(typeDef);
2534
+ return `${$delimiter}${key}${$delimiter} ${sqlType}${nullable ? " NULL" : " NOT NULL"}`;
2535
+ }).join(", ");
2536
+ cached = cols.length > 0 ? `, ${cols}` : "";
2537
+ this._valColsCache.set($delimiter, cached);
2538
+ }
2539
+ return cached;
2531
2540
  }
2532
2541
  isNullable(typeDef) {
2533
2542
  if (typeof typeDef === "boolean")
@@ -2547,10 +2556,20 @@ class BaseSqlTabularStorage extends BaseTabularStorage {
2547
2556
  return false;
2548
2557
  }
2549
2558
  primaryKeyColumnList($delimiter = "") {
2550
- return $delimiter + this.primaryKeyColumns().join(`${$delimiter}, ${$delimiter}`) + $delimiter;
2559
+ let cached = this._pkColListCache.get($delimiter);
2560
+ if (cached === undefined) {
2561
+ cached = $delimiter + this.primaryKeyColumns().join(`${$delimiter}, ${$delimiter}`) + $delimiter;
2562
+ this._pkColListCache.set($delimiter, cached);
2563
+ }
2564
+ return cached;
2551
2565
  }
2552
2566
  valueColumnList($delimiter = "") {
2553
- return $delimiter + this.valueColumns().join(`${$delimiter}, ${$delimiter}`) + $delimiter;
2567
+ let cached = this._valColListCache.get($delimiter);
2568
+ if (cached === undefined) {
2569
+ cached = $delimiter + this.valueColumns().join(`${$delimiter}, ${$delimiter}`) + $delimiter;
2570
+ this._valColListCache.set($delimiter, cached);
2571
+ }
2572
+ return cached;
2554
2573
  }
2555
2574
  getNonNullType(typeDef) {
2556
2575
  if (typeof typeDef === "boolean")
@@ -4285,6 +4304,7 @@ class SupabaseKvStorage extends KvViaTabularStorage {
4285
4304
  // src/queue/PostgresQueueStorage.ts
4286
4305
  import { createServiceToken as createServiceToken21, makeFingerprint as makeFingerprint6, uuid4 as uuid45 } from "@workglow/util";
4287
4306
  var POSTGRES_QUEUE_STORAGE = createServiceToken21("jobqueue.storage.postgres");
4307
+ var SAFE_IDENTIFIER = /^[a-zA-Z][a-zA-Z0-9_]*$/;
4288
4308
 
4289
4309
  class PostgresQueueStorage {
4290
4310
  db;
@@ -4297,6 +4317,11 @@ class PostgresQueueStorage {
4297
4317
  this.queueName = queueName;
4298
4318
  this.prefixes = options?.prefixes ?? [];
4299
4319
  this.prefixValues = options?.prefixValues ?? {};
4320
+ for (const prefix of this.prefixes) {
4321
+ if (!SAFE_IDENTIFIER.test(prefix.name)) {
4322
+ throw new Error(`Prefix column name must start with a letter and contain only letters, digits, and underscores, got: ${prefix.name}`);
4323
+ }
4324
+ }
4300
4325
  if (this.prefixes.length > 0) {
4301
4326
  const prefixNames = this.prefixes.map((p) => p.name).join("_");
4302
4327
  this.tableName = `job_queue_${prefixNames}`;
@@ -4331,7 +4356,13 @@ class PostgresQueueStorage {
4331
4356
  async setupDatabase() {
4332
4357
  let sql;
4333
4358
  try {
4334
- sql = `CREATE TYPE job_status AS ENUM (${Object.values(JobStatus).map((v) => `'${v}'`).join(",")})`;
4359
+ const enumValues = Object.values(JobStatus);
4360
+ for (const v of enumValues) {
4361
+ if (!SAFE_IDENTIFIER.test(v)) {
4362
+ throw new Error(`Invalid JobStatus enum value: ${v}`);
4363
+ }
4364
+ }
4365
+ sql = `CREATE TYPE job_status AS ENUM (${enumValues.map((v) => `'${v}'`).join(",")})`;
4335
4366
  await this.db.query(sql);
4336
4367
  } catch (e) {
4337
4368
  if (e.code !== "42710")
@@ -8340,4 +8371,4 @@ export {
8340
8371
  BaseTabularStorage
8341
8372
  };
8342
8373
 
8343
- //# debugId=F54F1115EFC431E364756E2164756E21
8374
+ //# debugId=738960D72FA07CB864756E2164756E21