@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.
- package/dist/browser.js +86 -67
- package/dist/browser.js.map +13 -13
- package/dist/bun.js +99 -68
- package/dist/bun.js.map +19 -19
- package/dist/kv/KvViaTabularStorage.d.ts +3 -8
- package/dist/kv/KvViaTabularStorage.d.ts.map +1 -1
- package/dist/node.js +99 -68
- package/dist/node.js.map +19 -19
- package/dist/queue/PostgresQueueStorage.d.ts.map +1 -1
- package/dist/tabular/BaseSqlTabularStorage.d.ts +6 -1
- package/dist/tabular/BaseSqlTabularStorage.d.ts.map +1 -1
- package/dist/tabular/BaseTabularStorage.d.ts +1 -1
- package/dist/tabular/BaseTabularStorage.d.ts.map +1 -1
- package/dist/tabular/CachedTabularStorage.d.ts +1 -1
- package/dist/tabular/CachedTabularStorage.d.ts.map +1 -1
- package/dist/tabular/FsFolderTabularStorage.d.ts +1 -1
- package/dist/tabular/FsFolderTabularStorage.d.ts.map +1 -1
- package/dist/tabular/HuggingFaceTabularStorage.d.ts.map +1 -1
- package/dist/tabular/InMemoryTabularStorage.d.ts +1 -1
- package/dist/tabular/InMemoryTabularStorage.d.ts.map +1 -1
- package/dist/tabular/IndexedDbTabularStorage.d.ts +1 -1
- package/dist/tabular/IndexedDbTabularStorage.d.ts.map +1 -1
- package/dist/tabular/PostgresTabularStorage.d.ts +1 -1
- package/dist/tabular/PostgresTabularStorage.d.ts.map +1 -1
- package/dist/tabular/SharedInMemoryTabularStorage.d.ts +1 -1
- package/dist/tabular/SharedInMemoryTabularStorage.d.ts.map +1 -1
- package/dist/tabular/SqliteTabularStorage.d.ts +1 -1
- package/dist/tabular/SqliteTabularStorage.d.ts.map +1 -1
- package/dist/tabular/SupabaseTabularStorage.d.ts +1 -1
- package/dist/tabular/SupabaseTabularStorage.d.ts.map +1 -1
- package/dist/vector/InMemoryVectorStorage.d.ts +3 -3
- package/dist/vector/InMemoryVectorStorage.d.ts.map +1 -1
- package/dist/vector/IndexedDbVectorStorage.d.ts +3 -3
- package/dist/vector/IndexedDbVectorStorage.d.ts.map +1 -1
- package/dist/vector/PostgresVectorStorage.d.ts +2 -2
- package/dist/vector/PostgresVectorStorage.d.ts.map +1 -1
- package/dist/vector/SqliteAiVectorStorage.d.ts +2 -2
- package/dist/vector/SqliteAiVectorStorage.d.ts.map +1 -1
- package/dist/vector/SqliteVectorStorage.d.ts +2 -2
- package/dist/vector/SqliteVectorStorage.d.ts.map +1 -1
- package/package.json +7 -7
|
@@ -6,19 +6,14 @@
|
|
|
6
6
|
import type { BaseTabularStorage } from "../tabular/BaseTabularStorage";
|
|
7
7
|
import { DefaultKeyValueKey, DefaultKeyValueSchema } from "./IKvStorage";
|
|
8
8
|
import { KvStorage } from "./KvStorage";
|
|
9
|
-
/**
|
|
10
|
-
* Abstract base class for key-value storage repositories that uses a tabular repository for storage.
|
|
11
|
-
* Has a basic event emitter for listening to repository events.
|
|
12
|
-
*
|
|
13
|
-
* @template Key - The type of the primary key
|
|
14
|
-
* @template Value - The type of the value being stored
|
|
15
|
-
* @template Combined - Combined type of Key & Value
|
|
16
|
-
*/
|
|
17
9
|
export declare abstract class KvViaTabularStorage<Key extends string = string, Value extends any = any, Combined = {
|
|
18
10
|
key: Key;
|
|
19
11
|
value: Value;
|
|
20
12
|
}> extends KvStorage<Key, Value, Combined> {
|
|
21
13
|
abstract tabularRepository: BaseTabularStorage<typeof DefaultKeyValueSchema, typeof DefaultKeyValueKey>;
|
|
14
|
+
/** Whether values need JSON serialization (cached from schema) */
|
|
15
|
+
private get needsJsonSerialization();
|
|
16
|
+
private _needsJsonSerialization;
|
|
22
17
|
/**
|
|
23
18
|
* Sets up the database for the repository.
|
|
24
19
|
* Must be called before using any other methods.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"KvViaTabularStorage.d.ts","sourceRoot":"","sources":["../../src/kv/KvViaTabularStorage.ts"],"names":[],"mappings":"AA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;;;;;OAMG;IACU,GAAG,CAAC,GAAG,EAAE,GAAG,GAAG,OAAO,CAAC,KAAK,GAAG,SAAS,CAAC,CAYrD;IAED;;;OAGG;IACU,MAAM,CAAC,GAAG,EAAE,GAAG,GAAG,OAAO,CAAC,IAAI,CAAC,CAE3C;IAED;;;OAGG;IACU,MAAM,IAAI,OAAO,CAAC,QAAQ,EAAE,GAAG,SAAS,CAAC,CAoBrD;IAED;;OAEG;IACU,SAAS,IAAI,OAAO,CAAC,IAAI,CAAC,CAEtC;IAED;;;OAGG;IACU,IAAI,IAAI,OAAO,CAAC,MAAM,CAAC,CAEnC;IAED;;OAEG;IACH,OAAO,IAAI,IAAI,CAEd;CACF"}
|
package/dist/node.js
CHANGED
|
@@ -398,30 +398,36 @@ class InMemoryTabularStorage extends BaseTabularStorage {
|
|
|
398
398
|
}
|
|
399
399
|
async put(value) {
|
|
400
400
|
let entityToStore = value;
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
shouldGenerate =
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
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
|
-
|
|
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
|
|
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
|
-
|
|
1307
|
-
|
|
1308
|
-
|
|
1309
|
-
|
|
1310
|
-
|
|
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
|
-
|
|
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
|
-
|
|
2513
|
-
|
|
2514
|
-
|
|
2515
|
-
|
|
2516
|
-
|
|
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
|
-
|
|
2520
|
-
|
|
2521
|
-
const
|
|
2522
|
-
const
|
|
2523
|
-
|
|
2524
|
-
|
|
2525
|
-
|
|
2526
|
-
|
|
2527
|
-
|
|
2528
|
-
|
|
2529
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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=
|
|
8374
|
+
//# debugId=BF385EFC980250A064756E2164756E21
|