@mastra/pg 0.2.10-alpha.3 → 0.2.10-alpha.5
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/.turbo/turbo-build.log +7 -7
- package/CHANGELOG.md +13 -0
- package/dist/_tsup-dts-rollup.d.cts +17 -1
- package/dist/_tsup-dts-rollup.d.ts +17 -1
- package/dist/index.cjs +154 -39
- package/dist/index.js +154 -39
- package/package.json +2 -2
- package/src/storage/index.test.ts +277 -12
- package/src/storage/index.ts +90 -21
- package/src/vector/index.test.ts +483 -0
- package/src/vector/index.ts +101 -21
package/dist/index.js
CHANGED
|
@@ -291,10 +291,15 @@ var PgVector = class extends MastraVector {
|
|
|
291
291
|
describeIndexCache = /* @__PURE__ */ new Map();
|
|
292
292
|
createdIndexes = /* @__PURE__ */ new Map();
|
|
293
293
|
mutexesByName = /* @__PURE__ */ new Map();
|
|
294
|
+
schema;
|
|
295
|
+
setupSchemaPromise = null;
|
|
294
296
|
installVectorExtensionPromise = null;
|
|
295
297
|
vectorExtensionInstalled = void 0;
|
|
296
|
-
|
|
298
|
+
schemaSetupComplete = void 0;
|
|
299
|
+
constructor(config) {
|
|
297
300
|
super();
|
|
301
|
+
const connectionString = typeof config === "string" ? config : config.connectionString;
|
|
302
|
+
this.schema = typeof config === "string" ? void 0 : config.schemaName;
|
|
298
303
|
const basePool = new pg.Pool({
|
|
299
304
|
connectionString,
|
|
300
305
|
max: 20,
|
|
@@ -329,6 +334,9 @@ var PgVector = class extends MastraVector {
|
|
|
329
334
|
if (!this.mutexesByName.has(indexName)) this.mutexesByName.set(indexName, new Mutex());
|
|
330
335
|
return this.mutexesByName.get(indexName);
|
|
331
336
|
}
|
|
337
|
+
getTableName(indexName) {
|
|
338
|
+
return this.schema ? `${this.schema}.${indexName}` : indexName;
|
|
339
|
+
}
|
|
332
340
|
transformFilter(filter) {
|
|
333
341
|
const translator = new PGFilterTranslator();
|
|
334
342
|
return translator.translate(filter);
|
|
@@ -360,6 +368,7 @@ var PgVector = class extends MastraVector {
|
|
|
360
368
|
if (indexInfo.type === "ivfflat" && probes) {
|
|
361
369
|
await client.query(`SET LOCAL ivfflat.probes = ${probes}`);
|
|
362
370
|
}
|
|
371
|
+
const tableName = this.getTableName(indexName);
|
|
363
372
|
const query = `
|
|
364
373
|
WITH vector_scores AS (
|
|
365
374
|
SELECT
|
|
@@ -367,7 +376,7 @@ var PgVector = class extends MastraVector {
|
|
|
367
376
|
1 - (embedding <=> '${vectorStr}'::vector) as score,
|
|
368
377
|
metadata
|
|
369
378
|
${includeVector ? ", embedding" : ""}
|
|
370
|
-
FROM ${
|
|
379
|
+
FROM ${tableName}
|
|
371
380
|
${filterQuery}
|
|
372
381
|
)
|
|
373
382
|
SELECT *
|
|
@@ -389,13 +398,14 @@ var PgVector = class extends MastraVector {
|
|
|
389
398
|
async upsert(...args) {
|
|
390
399
|
const params = this.normalizeArgs("upsert", args);
|
|
391
400
|
const { indexName, vectors, metadata, ids } = params;
|
|
401
|
+
const tableName = this.getTableName(indexName);
|
|
392
402
|
const client = await this.pool.connect();
|
|
393
403
|
try {
|
|
394
404
|
await client.query("BEGIN");
|
|
395
405
|
const vectorIds = ids || vectors.map(() => crypto.randomUUID());
|
|
396
406
|
for (let i = 0; i < vectors.length; i++) {
|
|
397
407
|
const query = `
|
|
398
|
-
INSERT INTO ${
|
|
408
|
+
INSERT INTO ${tableName} (vector_id, embedding, metadata)
|
|
399
409
|
VALUES ($1, $2::vector, $3::jsonb)
|
|
400
410
|
ON CONFLICT (vector_id)
|
|
401
411
|
DO UPDATE SET
|
|
@@ -432,12 +442,54 @@ var PgVector = class extends MastraVector {
|
|
|
432
442
|
const existingIndexCacheKey = this.createdIndexes.get(indexName);
|
|
433
443
|
return existingIndexCacheKey && existingIndexCacheKey === newKey;
|
|
434
444
|
}
|
|
445
|
+
async setupSchema(client) {
|
|
446
|
+
if (!this.schema || this.schemaSetupComplete) {
|
|
447
|
+
return;
|
|
448
|
+
}
|
|
449
|
+
if (!this.setupSchemaPromise) {
|
|
450
|
+
this.setupSchemaPromise = (async () => {
|
|
451
|
+
try {
|
|
452
|
+
const schemaCheck = await client.query(
|
|
453
|
+
`
|
|
454
|
+
SELECT EXISTS (
|
|
455
|
+
SELECT 1 FROM information_schema.schemata
|
|
456
|
+
WHERE schema_name = $1
|
|
457
|
+
)
|
|
458
|
+
`,
|
|
459
|
+
[this.schema]
|
|
460
|
+
);
|
|
461
|
+
const schemaExists = schemaCheck.rows[0].exists;
|
|
462
|
+
if (!schemaExists) {
|
|
463
|
+
try {
|
|
464
|
+
await client.query(`CREATE SCHEMA IF NOT EXISTS ${this.schema}`);
|
|
465
|
+
this.logger.info(`Schema "${this.schema}" created successfully`);
|
|
466
|
+
} catch (error) {
|
|
467
|
+
this.logger.error(`Failed to create schema "${this.schema}"`, { error });
|
|
468
|
+
throw new Error(
|
|
469
|
+
`Unable to create schema "${this.schema}". This requires CREATE privilege on the database. Either create the schema manually or grant CREATE privilege to the user.`
|
|
470
|
+
);
|
|
471
|
+
}
|
|
472
|
+
}
|
|
473
|
+
this.schemaSetupComplete = true;
|
|
474
|
+
this.logger.debug(`Schema "${this.schema}" is ready for use`);
|
|
475
|
+
} catch (error) {
|
|
476
|
+
this.schemaSetupComplete = void 0;
|
|
477
|
+
this.setupSchemaPromise = null;
|
|
478
|
+
throw error;
|
|
479
|
+
} finally {
|
|
480
|
+
this.setupSchemaPromise = null;
|
|
481
|
+
}
|
|
482
|
+
})();
|
|
483
|
+
}
|
|
484
|
+
await this.setupSchemaPromise;
|
|
485
|
+
}
|
|
435
486
|
async createIndex(...args) {
|
|
436
487
|
const params = this.normalizeArgs("createIndex", args, [
|
|
437
488
|
"indexConfig",
|
|
438
489
|
"buildIndex"
|
|
439
490
|
]);
|
|
440
491
|
const { indexName, dimension, metric = "cosine", indexConfig = {}, buildIndex = true } = params;
|
|
492
|
+
const tableName = this.getTableName(indexName);
|
|
441
493
|
if (!indexName.match(/^[a-zA-Z_][a-zA-Z0-9_]*$/)) {
|
|
442
494
|
throw new Error("Invalid index name format");
|
|
443
495
|
}
|
|
@@ -455,22 +507,22 @@ var PgVector = class extends MastraVector {
|
|
|
455
507
|
}
|
|
456
508
|
const client = await this.pool.connect();
|
|
457
509
|
try {
|
|
510
|
+
await this.setupSchema(client);
|
|
458
511
|
await this.installVectorExtension(client);
|
|
459
512
|
await client.query(`
|
|
460
|
-
CREATE TABLE IF NOT EXISTS ${
|
|
513
|
+
CREATE TABLE IF NOT EXISTS ${tableName} (
|
|
461
514
|
id SERIAL PRIMARY KEY,
|
|
462
515
|
vector_id TEXT UNIQUE NOT NULL,
|
|
463
516
|
embedding vector(${dimension}),
|
|
464
517
|
metadata JSONB DEFAULT '{}'::jsonb
|
|
465
518
|
);
|
|
466
|
-
|
|
519
|
+
`);
|
|
467
520
|
this.createdIndexes.set(indexName, indexCacheKey);
|
|
468
521
|
if (buildIndex) {
|
|
469
522
|
await this.setupIndex({ indexName, metric, indexConfig }, client);
|
|
470
523
|
}
|
|
471
524
|
} catch (error) {
|
|
472
525
|
this.createdIndexes.delete(indexName);
|
|
473
|
-
console.error("Failed to create vector table:", error);
|
|
474
526
|
throw error;
|
|
475
527
|
} finally {
|
|
476
528
|
client.release();
|
|
@@ -499,8 +551,9 @@ var PgVector = class extends MastraVector {
|
|
|
499
551
|
async setupIndex({ indexName, metric, indexConfig }, client) {
|
|
500
552
|
const mutex = this.getMutexByName(`build-${indexName}`);
|
|
501
553
|
await mutex.runExclusive(async () => {
|
|
554
|
+
const tableName = this.getTableName(indexName);
|
|
502
555
|
if (this.createdIndexes.has(indexName)) {
|
|
503
|
-
await client.query(`DROP INDEX IF EXISTS ${
|
|
556
|
+
await client.query(`DROP INDEX IF EXISTS ${tableName}_vector_idx`);
|
|
504
557
|
}
|
|
505
558
|
if (indexConfig.type === "flat") {
|
|
506
559
|
this.describeIndexCache.delete(indexName);
|
|
@@ -513,7 +566,7 @@ var PgVector = class extends MastraVector {
|
|
|
513
566
|
const efConstruction = indexConfig.hnsw?.efConstruction ?? 32;
|
|
514
567
|
indexSQL = `
|
|
515
568
|
CREATE INDEX IF NOT EXISTS ${indexName}_vector_idx
|
|
516
|
-
ON ${
|
|
569
|
+
ON ${tableName}
|
|
517
570
|
USING hnsw (embedding ${metricOp})
|
|
518
571
|
WITH (
|
|
519
572
|
m = ${m},
|
|
@@ -525,12 +578,12 @@ var PgVector = class extends MastraVector {
|
|
|
525
578
|
if (indexConfig.ivf?.lists) {
|
|
526
579
|
lists = indexConfig.ivf.lists;
|
|
527
580
|
} else {
|
|
528
|
-
const size = (await client.query(`SELECT COUNT(*) FROM ${
|
|
581
|
+
const size = (await client.query(`SELECT COUNT(*) FROM ${tableName}`)).rows[0].count;
|
|
529
582
|
lists = Math.max(100, Math.min(4e3, Math.floor(Math.sqrt(size) * 2)));
|
|
530
583
|
}
|
|
531
584
|
indexSQL = `
|
|
532
585
|
CREATE INDEX IF NOT EXISTS ${indexName}_vector_idx
|
|
533
|
-
ON ${
|
|
586
|
+
ON ${tableName}
|
|
534
587
|
USING ivfflat (embedding ${metricOp})
|
|
535
588
|
WITH (lists = ${lists});
|
|
536
589
|
`;
|
|
@@ -582,10 +635,10 @@ var PgVector = class extends MastraVector {
|
|
|
582
635
|
const vectorTablesQuery = `
|
|
583
636
|
SELECT DISTINCT table_name
|
|
584
637
|
FROM information_schema.columns
|
|
585
|
-
WHERE table_schema =
|
|
638
|
+
WHERE table_schema = $1
|
|
586
639
|
AND udt_name = 'vector';
|
|
587
640
|
`;
|
|
588
|
-
const vectorTables = await client.query(vectorTablesQuery);
|
|
641
|
+
const vectorTables = await client.query(vectorTablesQuery, [this.schema || "public"]);
|
|
589
642
|
return vectorTables.rows.map((row) => row.table_name);
|
|
590
643
|
} finally {
|
|
591
644
|
client.release();
|
|
@@ -594,14 +647,16 @@ var PgVector = class extends MastraVector {
|
|
|
594
647
|
async describeIndex(indexName) {
|
|
595
648
|
const client = await this.pool.connect();
|
|
596
649
|
try {
|
|
650
|
+
const tableName = this.getTableName(indexName);
|
|
597
651
|
const dimensionQuery = `
|
|
598
652
|
SELECT atttypmod as dimension
|
|
599
653
|
FROM pg_attribute
|
|
600
654
|
WHERE attrelid = $1::regclass
|
|
601
655
|
AND attname = 'embedding';
|
|
602
656
|
`;
|
|
603
|
-
const countQuery = `
|
|
604
|
-
|
|
657
|
+
const countQuery = `
|
|
658
|
+
SELECT COUNT(*) as count
|
|
659
|
+
FROM ${tableName};
|
|
605
660
|
`;
|
|
606
661
|
const indexQuery = `
|
|
607
662
|
SELECT
|
|
@@ -612,10 +667,10 @@ var PgVector = class extends MastraVector {
|
|
|
612
667
|
JOIN pg_class c ON i.indexrelid = c.oid
|
|
613
668
|
JOIN pg_am am ON c.relam = am.oid
|
|
614
669
|
JOIN pg_opclass opclass ON i.indclass[0] = opclass.oid
|
|
615
|
-
WHERE c.relname = '${
|
|
670
|
+
WHERE c.relname = '${tableName}_vector_idx';
|
|
616
671
|
`;
|
|
617
672
|
const [dimResult, countResult, indexResult] = await Promise.all([
|
|
618
|
-
client.query(dimensionQuery, [
|
|
673
|
+
client.query(dimensionQuery, [tableName]),
|
|
619
674
|
client.query(countQuery),
|
|
620
675
|
client.query(indexQuery)
|
|
621
676
|
]);
|
|
@@ -652,7 +707,8 @@ var PgVector = class extends MastraVector {
|
|
|
652
707
|
async deleteIndex(indexName) {
|
|
653
708
|
const client = await this.pool.connect();
|
|
654
709
|
try {
|
|
655
|
-
|
|
710
|
+
const tableName = this.getTableName(indexName);
|
|
711
|
+
await client.query(`DROP TABLE IF EXISTS ${tableName} CASCADE`);
|
|
656
712
|
this.createdIndexes.delete(indexName);
|
|
657
713
|
} catch (error) {
|
|
658
714
|
await client.query("ROLLBACK");
|
|
@@ -664,7 +720,8 @@ var PgVector = class extends MastraVector {
|
|
|
664
720
|
async truncateIndex(indexName) {
|
|
665
721
|
const client = await this.pool.connect();
|
|
666
722
|
try {
|
|
667
|
-
|
|
723
|
+
const tableName = this.getTableName(indexName);
|
|
724
|
+
await client.query(`TRUNCATE ${tableName}`);
|
|
668
725
|
} catch (e) {
|
|
669
726
|
await client.query("ROLLBACK");
|
|
670
727
|
throw new Error(`Failed to truncate vector table: ${e.message}`);
|
|
@@ -696,8 +753,9 @@ var PgVector = class extends MastraVector {
|
|
|
696
753
|
if (updateParts.length === 0) {
|
|
697
754
|
return;
|
|
698
755
|
}
|
|
756
|
+
const tableName = this.getTableName(indexName);
|
|
699
757
|
const query = `
|
|
700
|
-
UPDATE ${
|
|
758
|
+
UPDATE ${tableName}
|
|
701
759
|
SET ${updateParts.join(", ")}
|
|
702
760
|
WHERE vector_id = $1
|
|
703
761
|
`;
|
|
@@ -709,8 +767,9 @@ var PgVector = class extends MastraVector {
|
|
|
709
767
|
async deleteIndexById(indexName, id) {
|
|
710
768
|
const client = await this.pool.connect();
|
|
711
769
|
try {
|
|
770
|
+
const tableName = this.getTableName(indexName);
|
|
712
771
|
const query = `
|
|
713
|
-
DELETE FROM ${
|
|
772
|
+
DELETE FROM ${tableName}
|
|
714
773
|
WHERE vector_id = $1
|
|
715
774
|
`;
|
|
716
775
|
await client.query(query, [id]);
|
|
@@ -722,9 +781,13 @@ var PgVector = class extends MastraVector {
|
|
|
722
781
|
var PostgresStore = class extends MastraStorage {
|
|
723
782
|
db;
|
|
724
783
|
pgp;
|
|
784
|
+
schema;
|
|
785
|
+
setupSchemaPromise = null;
|
|
786
|
+
schemaSetupComplete = void 0;
|
|
725
787
|
constructor(config) {
|
|
726
788
|
super({ name: "PostgresStore" });
|
|
727
789
|
this.pgp = pgPromise();
|
|
790
|
+
this.schema = config.schema;
|
|
728
791
|
this.db = this.pgp(
|
|
729
792
|
`connectionString` in config ? { connectionString: config.connectionString } : {
|
|
730
793
|
host: config.host,
|
|
@@ -736,9 +799,12 @@ var PostgresStore = class extends MastraStorage {
|
|
|
736
799
|
}
|
|
737
800
|
);
|
|
738
801
|
}
|
|
802
|
+
getTableName(indexName) {
|
|
803
|
+
return this.schema ? `${this.schema}."${indexName}"` : `"${indexName}"`;
|
|
804
|
+
}
|
|
739
805
|
async getEvalsByAgentName(agentName, type) {
|
|
740
806
|
try {
|
|
741
|
-
const baseQuery = `SELECT * FROM ${TABLE_EVALS} WHERE agent_name = $1`;
|
|
807
|
+
const baseQuery = `SELECT * FROM ${this.getTableName(TABLE_EVALS)} WHERE agent_name = $1`;
|
|
742
808
|
const typeCondition = type === "test" ? " AND test_info IS NOT NULL AND test_info->>'testPath' IS NOT NULL" : type === "live" ? " AND (test_info IS NULL OR test_info->>'testPath' IS NULL)" : "";
|
|
743
809
|
const query = `${baseQuery}${typeCondition} ORDER BY created_at DESC`;
|
|
744
810
|
const rows = await this.db.manyOrNone(query, [agentName]);
|
|
@@ -832,7 +898,10 @@ var PostgresStore = class extends MastraStorage {
|
|
|
832
898
|
args.push(value);
|
|
833
899
|
}
|
|
834
900
|
}
|
|
835
|
-
const result = await this.db.manyOrNone(
|
|
901
|
+
const result = await this.db.manyOrNone(
|
|
902
|
+
`SELECT * FROM ${this.getTableName(TABLE_TRACES)} ${whereClause} ORDER BY "createdAt" DESC LIMIT ${limit} OFFSET ${offset}`,
|
|
903
|
+
args
|
|
904
|
+
);
|
|
836
905
|
if (!result) {
|
|
837
906
|
return [];
|
|
838
907
|
}
|
|
@@ -853,6 +922,46 @@ var PostgresStore = class extends MastraStorage {
|
|
|
853
922
|
createdAt: row.createdAt
|
|
854
923
|
}));
|
|
855
924
|
}
|
|
925
|
+
async setupSchema() {
|
|
926
|
+
if (!this.schema || this.schemaSetupComplete) {
|
|
927
|
+
return;
|
|
928
|
+
}
|
|
929
|
+
if (!this.setupSchemaPromise) {
|
|
930
|
+
this.setupSchemaPromise = (async () => {
|
|
931
|
+
try {
|
|
932
|
+
const schemaExists = await this.db.oneOrNone(
|
|
933
|
+
`
|
|
934
|
+
SELECT EXISTS (
|
|
935
|
+
SELECT 1 FROM information_schema.schemata
|
|
936
|
+
WHERE schema_name = $1
|
|
937
|
+
)
|
|
938
|
+
`,
|
|
939
|
+
[this.schema]
|
|
940
|
+
);
|
|
941
|
+
if (!schemaExists?.exists) {
|
|
942
|
+
try {
|
|
943
|
+
await this.db.none(`CREATE SCHEMA IF NOT EXISTS ${this.schema}`);
|
|
944
|
+
this.logger.info(`Schema "${this.schema}" created successfully`);
|
|
945
|
+
} catch (error) {
|
|
946
|
+
this.logger.error(`Failed to create schema "${this.schema}"`, { error });
|
|
947
|
+
throw new Error(
|
|
948
|
+
`Unable to create schema "${this.schema}". This requires CREATE privilege on the database. Either create the schema manually or grant CREATE privilege to the user.`
|
|
949
|
+
);
|
|
950
|
+
}
|
|
951
|
+
}
|
|
952
|
+
this.schemaSetupComplete = true;
|
|
953
|
+
this.logger.debug(`Schema "${this.schema}" is ready for use`);
|
|
954
|
+
} catch (error) {
|
|
955
|
+
this.schemaSetupComplete = void 0;
|
|
956
|
+
this.setupSchemaPromise = null;
|
|
957
|
+
throw error;
|
|
958
|
+
} finally {
|
|
959
|
+
this.setupSchemaPromise = null;
|
|
960
|
+
}
|
|
961
|
+
})();
|
|
962
|
+
}
|
|
963
|
+
await this.setupSchemaPromise;
|
|
964
|
+
}
|
|
856
965
|
async createTable({
|
|
857
966
|
tableName,
|
|
858
967
|
schema
|
|
@@ -864,8 +973,11 @@ var PostgresStore = class extends MastraStorage {
|
|
|
864
973
|
if (!def.nullable) constraints.push("NOT NULL");
|
|
865
974
|
return `"${name}" ${def.type.toUpperCase()} ${constraints.join(" ")}`;
|
|
866
975
|
}).join(",\n");
|
|
976
|
+
if (this.schema) {
|
|
977
|
+
await this.setupSchema();
|
|
978
|
+
}
|
|
867
979
|
const sql = `
|
|
868
|
-
CREATE TABLE IF NOT EXISTS ${tableName} (
|
|
980
|
+
CREATE TABLE IF NOT EXISTS ${this.getTableName(tableName)} (
|
|
869
981
|
${columns}
|
|
870
982
|
);
|
|
871
983
|
${tableName === TABLE_WORKFLOW_SNAPSHOT ? `
|
|
@@ -873,7 +985,7 @@ var PostgresStore = class extends MastraStorage {
|
|
|
873
985
|
IF NOT EXISTS (
|
|
874
986
|
SELECT 1 FROM pg_constraint WHERE conname = 'mastra_workflow_snapshot_workflow_name_run_id_key'
|
|
875
987
|
) THEN
|
|
876
|
-
ALTER TABLE ${tableName}
|
|
988
|
+
ALTER TABLE ${this.getTableName(tableName)}
|
|
877
989
|
ADD CONSTRAINT mastra_workflow_snapshot_workflow_name_run_id_key
|
|
878
990
|
UNIQUE (workflow_name, run_id);
|
|
879
991
|
END IF;
|
|
@@ -888,7 +1000,7 @@ var PostgresStore = class extends MastraStorage {
|
|
|
888
1000
|
}
|
|
889
1001
|
async clearTable({ tableName }) {
|
|
890
1002
|
try {
|
|
891
|
-
await this.db.none(`TRUNCATE TABLE ${tableName} CASCADE`);
|
|
1003
|
+
await this.db.none(`TRUNCATE TABLE ${this.getTableName(tableName)} CASCADE`);
|
|
892
1004
|
} catch (error) {
|
|
893
1005
|
console.error(`Error clearing table ${tableName}:`, error);
|
|
894
1006
|
throw error;
|
|
@@ -900,7 +1012,7 @@ var PostgresStore = class extends MastraStorage {
|
|
|
900
1012
|
const values = Object.values(record);
|
|
901
1013
|
const placeholders = values.map((_, i) => `$${i + 1}`).join(", ");
|
|
902
1014
|
await this.db.none(
|
|
903
|
-
`INSERT INTO ${tableName} (${columns.map((c) => `"${c}"`).join(", ")}) VALUES (${placeholders})`,
|
|
1015
|
+
`INSERT INTO ${this.getTableName(tableName)} (${columns.map((c) => `"${c}"`).join(", ")}) VALUES (${placeholders})`,
|
|
904
1016
|
values
|
|
905
1017
|
);
|
|
906
1018
|
} catch (error) {
|
|
@@ -913,7 +1025,10 @@ var PostgresStore = class extends MastraStorage {
|
|
|
913
1025
|
const keyEntries = Object.entries(keys);
|
|
914
1026
|
const conditions = keyEntries.map(([key], index) => `"${key}" = $${index + 1}`).join(" AND ");
|
|
915
1027
|
const values = keyEntries.map(([_, value]) => value);
|
|
916
|
-
const result = await this.db.oneOrNone(
|
|
1028
|
+
const result = await this.db.oneOrNone(
|
|
1029
|
+
`SELECT * FROM ${this.getTableName(tableName)} WHERE ${conditions}`,
|
|
1030
|
+
values
|
|
1031
|
+
);
|
|
917
1032
|
if (!result) {
|
|
918
1033
|
return null;
|
|
919
1034
|
}
|
|
@@ -940,7 +1055,7 @@ var PostgresStore = class extends MastraStorage {
|
|
|
940
1055
|
metadata,
|
|
941
1056
|
"createdAt",
|
|
942
1057
|
"updatedAt"
|
|
943
|
-
FROM
|
|
1058
|
+
FROM ${this.getTableName(TABLE_THREADS)}
|
|
944
1059
|
WHERE id = $1`,
|
|
945
1060
|
[threadId]
|
|
946
1061
|
);
|
|
@@ -968,7 +1083,7 @@ var PostgresStore = class extends MastraStorage {
|
|
|
968
1083
|
metadata,
|
|
969
1084
|
"createdAt",
|
|
970
1085
|
"updatedAt"
|
|
971
|
-
FROM
|
|
1086
|
+
FROM ${this.getTableName(TABLE_THREADS)}
|
|
972
1087
|
WHERE "resourceId" = $1`,
|
|
973
1088
|
[resourceId]
|
|
974
1089
|
);
|
|
@@ -986,7 +1101,7 @@ var PostgresStore = class extends MastraStorage {
|
|
|
986
1101
|
async saveThread({ thread }) {
|
|
987
1102
|
try {
|
|
988
1103
|
await this.db.none(
|
|
989
|
-
`INSERT INTO
|
|
1104
|
+
`INSERT INTO ${this.getTableName(TABLE_THREADS)} (
|
|
990
1105
|
id,
|
|
991
1106
|
"resourceId",
|
|
992
1107
|
title,
|
|
@@ -1030,7 +1145,7 @@ var PostgresStore = class extends MastraStorage {
|
|
|
1030
1145
|
...metadata
|
|
1031
1146
|
};
|
|
1032
1147
|
const thread = await this.db.one(
|
|
1033
|
-
`UPDATE
|
|
1148
|
+
`UPDATE ${this.getTableName(TABLE_THREADS)}
|
|
1034
1149
|
SET title = $1,
|
|
1035
1150
|
metadata = $2,
|
|
1036
1151
|
"updatedAt" = $3
|
|
@@ -1052,8 +1167,8 @@ var PostgresStore = class extends MastraStorage {
|
|
|
1052
1167
|
async deleteThread({ threadId }) {
|
|
1053
1168
|
try {
|
|
1054
1169
|
await this.db.tx(async (t) => {
|
|
1055
|
-
await t.none(`DELETE FROM
|
|
1056
|
-
await t.none(`DELETE FROM
|
|
1170
|
+
await t.none(`DELETE FROM ${this.getTableName(TABLE_MESSAGES)} WHERE thread_id = $1`, [threadId]);
|
|
1171
|
+
await t.none(`DELETE FROM ${this.getTableName(TABLE_THREADS)} WHERE id = $1`, [threadId]);
|
|
1057
1172
|
});
|
|
1058
1173
|
} catch (error) {
|
|
1059
1174
|
console.error("Error deleting thread:", error);
|
|
@@ -1072,7 +1187,7 @@ var PostgresStore = class extends MastraStorage {
|
|
|
1072
1187
|
SELECT
|
|
1073
1188
|
*,
|
|
1074
1189
|
ROW_NUMBER() OVER (ORDER BY "createdAt" DESC) as row_num
|
|
1075
|
-
FROM
|
|
1190
|
+
FROM ${this.getTableName(TABLE_MESSAGES)}
|
|
1076
1191
|
WHERE thread_id = $1
|
|
1077
1192
|
)
|
|
1078
1193
|
SELECT
|
|
@@ -1115,7 +1230,7 @@ var PostgresStore = class extends MastraStorage {
|
|
|
1115
1230
|
type,
|
|
1116
1231
|
"createdAt",
|
|
1117
1232
|
thread_id AS "threadId"
|
|
1118
|
-
FROM
|
|
1233
|
+
FROM ${this.getTableName(TABLE_MESSAGES)}
|
|
1119
1234
|
WHERE thread_id = $1
|
|
1120
1235
|
AND id != ALL($2)
|
|
1121
1236
|
ORDER BY "createdAt" DESC
|
|
@@ -1153,7 +1268,7 @@ var PostgresStore = class extends MastraStorage {
|
|
|
1153
1268
|
await this.db.tx(async (t) => {
|
|
1154
1269
|
for (const message of messages) {
|
|
1155
1270
|
await t.none(
|
|
1156
|
-
`INSERT INTO
|
|
1271
|
+
`INSERT INTO ${this.getTableName(TABLE_MESSAGES)} (id, thread_id, content, "createdAt", role, type)
|
|
1157
1272
|
VALUES ($1, $2, $3, $4, $5, $6)`,
|
|
1158
1273
|
[
|
|
1159
1274
|
message.id,
|
|
@@ -1180,7 +1295,7 @@ var PostgresStore = class extends MastraStorage {
|
|
|
1180
1295
|
try {
|
|
1181
1296
|
const now = (/* @__PURE__ */ new Date()).toISOString();
|
|
1182
1297
|
await this.db.none(
|
|
1183
|
-
`INSERT INTO
|
|
1298
|
+
`INSERT INTO ${this.getTableName(TABLE_WORKFLOW_SNAPSHOT)} (
|
|
1184
1299
|
workflow_name,
|
|
1185
1300
|
run_id,
|
|
1186
1301
|
snapshot,
|
|
@@ -1247,13 +1362,13 @@ var PostgresStore = class extends MastraStorage {
|
|
|
1247
1362
|
let total = 0;
|
|
1248
1363
|
if (limit !== void 0 && offset !== void 0) {
|
|
1249
1364
|
const countResult = await this.db.one(
|
|
1250
|
-
`SELECT COUNT(*) as count FROM ${TABLE_WORKFLOW_SNAPSHOT} ${whereClause}`,
|
|
1365
|
+
`SELECT COUNT(*) as count FROM ${this.getTableName(TABLE_WORKFLOW_SNAPSHOT)} ${whereClause}`,
|
|
1251
1366
|
values
|
|
1252
1367
|
);
|
|
1253
1368
|
total = Number(countResult.count);
|
|
1254
1369
|
}
|
|
1255
1370
|
const query = `
|
|
1256
|
-
SELECT * FROM ${TABLE_WORKFLOW_SNAPSHOT}
|
|
1371
|
+
SELECT * FROM ${this.getTableName(TABLE_WORKFLOW_SNAPSHOT)}
|
|
1257
1372
|
${whereClause}
|
|
1258
1373
|
ORDER BY "createdAt" DESC
|
|
1259
1374
|
${limit !== void 0 && offset !== void 0 ? ` LIMIT $${paramIndex} OFFSET $${paramIndex + 1}` : ""}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mastra/pg",
|
|
3
|
-
"version": "0.2.10-alpha.
|
|
3
|
+
"version": "0.2.10-alpha.5",
|
|
4
4
|
"description": "Postgres provider for Mastra - includes both vector and db storage capabilities",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -24,7 +24,7 @@
|
|
|
24
24
|
"pg": "^8.13.3",
|
|
25
25
|
"pg-promise": "^11.11.0",
|
|
26
26
|
"xxhash-wasm": "^1.1.0",
|
|
27
|
-
"@mastra/core": "^0.8.3-alpha.
|
|
27
|
+
"@mastra/core": "^0.8.3-alpha.4"
|
|
28
28
|
},
|
|
29
29
|
"devDependencies": {
|
|
30
30
|
"@microsoft/api-extractor": "^7.52.1",
|