@mastra/pg 0.13.3-alpha.0 → 0.14.0-alpha.0

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.
@@ -1,4 +1,4 @@
1
1
 
2
- > @mastra/pg@0.13.3-alpha.0 build /home/runner/work/mastra/mastra/stores/pg
2
+ > @mastra/pg@0.14.0-alpha.0 build /home/runner/work/mastra/mastra/stores/pg
3
3
  > tsup --silent --config tsup.config.ts
4
4
 
package/CHANGELOG.md CHANGED
@@ -1,5 +1,29 @@
1
1
  # @mastra/pg
2
2
 
3
+ ## 0.14.0-alpha.0
4
+
5
+ ### Minor Changes
6
+
7
+ - 79d34ce: improve cloudflare workers compatibility
8
+
9
+ ### Patch Changes
10
+
11
+ - b32c50d: Filter scores by source
12
+ - Updated dependencies [d5330bf]
13
+ - Updated dependencies [a239d41]
14
+ - Updated dependencies [b32c50d]
15
+ - Updated dependencies [121a3f8]
16
+ - Updated dependencies [ec510e7]
17
+ - @mastra/core@0.13.2-alpha.2
18
+
19
+ ## 0.13.3
20
+
21
+ ### Patch Changes
22
+
23
+ - 3e49b7a: Fix PostgreSQL vector metadata filtering for Memory system - changed default minScore to -1 to include all similarity results and added comprehensive metadata filtering tests
24
+ - Updated dependencies [cd0042e]
25
+ - @mastra/core@0.13.1
26
+
3
27
  ## 0.13.3-alpha.0
4
28
 
5
29
  ### Patch Changes
package/dist/index.cjs CHANGED
@@ -2296,6 +2296,7 @@ var StoreOperationsPG = class extends storage.StoreOperations {
2296
2296
  }
2297
2297
  };
2298
2298
  function transformScoreRow(row) {
2299
+ console.log(`row is`, JSON.stringify(row, null, 2));
2299
2300
  return {
2300
2301
  ...row,
2301
2302
  input: storage.safelyParseJSON(row.input),
@@ -2331,7 +2332,7 @@ var ScoresPG = class extends storage.ScoresStorage {
2331
2332
  `SELECT * FROM ${getTableName({ indexName: storage.TABLE_SCORERS, schemaName: this.schema })} WHERE id = $1`,
2332
2333
  [id]
2333
2334
  );
2334
- return transformScoreRow(result);
2335
+ return result ? transformScoreRow(result) : null;
2335
2336
  } catch (error$1) {
2336
2337
  throw new error.MastraError(
2337
2338
  {
@@ -2345,12 +2346,31 @@ var ScoresPG = class extends storage.ScoresStorage {
2345
2346
  }
2346
2347
  async getScoresByScorerId({
2347
2348
  scorerId,
2348
- pagination
2349
+ pagination,
2350
+ entityId,
2351
+ entityType,
2352
+ source
2349
2353
  }) {
2350
2354
  try {
2355
+ const conditions = [`"scorerId" = $1`];
2356
+ const queryParams = [scorerId];
2357
+ let paramIndex = 2;
2358
+ if (entityId) {
2359
+ conditions.push(`"entityId" = $${paramIndex++}`);
2360
+ queryParams.push(entityId);
2361
+ }
2362
+ if (entityType) {
2363
+ conditions.push(`"entityType" = $${paramIndex++}`);
2364
+ queryParams.push(entityType);
2365
+ }
2366
+ if (source) {
2367
+ conditions.push(`"source" = $${paramIndex++}`);
2368
+ queryParams.push(source);
2369
+ }
2370
+ const whereClause = conditions.join(" AND ");
2351
2371
  const total = await this.client.oneOrNone(
2352
- `SELECT COUNT(*) FROM ${getTableName({ indexName: storage.TABLE_SCORERS, schemaName: this.schema })} WHERE "scorerId" = $1`,
2353
- [scorerId]
2372
+ `SELECT COUNT(*) FROM ${getTableName({ indexName: storage.TABLE_SCORERS, schemaName: this.schema })} WHERE ${whereClause}`,
2373
+ queryParams
2354
2374
  );
2355
2375
  if (total?.count === "0" || !total?.count) {
2356
2376
  return {
@@ -2364,8 +2384,8 @@ var ScoresPG = class extends storage.ScoresStorage {
2364
2384
  };
2365
2385
  }
2366
2386
  const result = await this.client.manyOrNone(
2367
- `SELECT * FROM ${getTableName({ indexName: storage.TABLE_SCORERS, schemaName: this.schema })} WHERE "scorerId" = $1 LIMIT $2 OFFSET $3`,
2368
- [scorerId, pagination.perPage, pagination.page * pagination.perPage]
2387
+ `SELECT * FROM ${getTableName({ indexName: storage.TABLE_SCORERS, schemaName: this.schema })} WHERE ${whereClause} ORDER BY "createdAt" DESC LIMIT $${paramIndex++} OFFSET $${paramIndex++}`,
2388
+ [...queryParams, pagination.perPage, pagination.page * pagination.perPage]
2369
2389
  );
2370
2390
  return {
2371
2391
  pagination: {
@@ -2389,7 +2409,7 @@ var ScoresPG = class extends storage.ScoresStorage {
2389
2409
  }
2390
2410
  async saveScore(score) {
2391
2411
  try {
2392
- const scoreId = crypto.randomUUID();
2412
+ const id = crypto.randomUUID();
2393
2413
  const {
2394
2414
  scorer,
2395
2415
  preprocessStepResult,
@@ -2402,10 +2422,11 @@ var ScoresPG = class extends storage.ScoresStorage {
2402
2422
  entity,
2403
2423
  ...rest
2404
2424
  } = score;
2425
+ console.log(`saving score with id: ${id}`);
2405
2426
  await this.operations.insert({
2406
2427
  tableName: storage.TABLE_SCORERS,
2407
2428
  record: {
2408
- id: scoreId,
2429
+ id,
2409
2430
  ...rest,
2410
2431
  input: JSON.stringify(input) || "",
2411
2432
  output: JSON.stringify(output) || "",
@@ -2420,7 +2441,7 @@ var ScoresPG = class extends storage.ScoresStorage {
2420
2441
  updatedAt: (/* @__PURE__ */ new Date()).toISOString()
2421
2442
  }
2422
2443
  });
2423
- const scoreFromDb = await this.getScoreById({ id: scoreId });
2444
+ const scoreFromDb = await this.getScoreById({ id });
2424
2445
  return { score: scoreFromDb };
2425
2446
  } catch (error$1) {
2426
2447
  throw new error.MastraError(
@@ -2856,10 +2877,11 @@ var WorkflowsPG = class extends storage.WorkflowsStorage {
2856
2877
 
2857
2878
  // src/storage/index.ts
2858
2879
  var PostgresStore = class extends storage.MastraStorage {
2859
- db;
2860
- pgp;
2861
- client;
2880
+ #db;
2881
+ #pgp;
2882
+ #config;
2862
2883
  schema;
2884
+ isConnected = false;
2863
2885
  stores;
2864
2886
  constructor(config) {
2865
2887
  try {
@@ -2880,10 +2902,11 @@ var PostgresStore = class extends storage.MastraStorage {
2880
2902
  }
2881
2903
  }
2882
2904
  super({ name: "PostgresStore" });
2883
- this.pgp = pgPromise__default.default();
2884
2905
  this.schema = config.schemaName || "public";
2885
- this.db = this.pgp(
2886
- `connectionString` in config ? { connectionString: config.connectionString } : {
2906
+ this.#config = {
2907
+ max: config.max,
2908
+ idleTimeoutMillis: config.idleTimeoutMillis,
2909
+ ...`connectionString` in config ? { connectionString: config.connectionString } : {
2887
2910
  host: config.host,
2888
2911
  port: config.port,
2889
2912
  database: config.database,
@@ -2891,14 +2914,33 @@ var PostgresStore = class extends storage.MastraStorage {
2891
2914
  password: config.password,
2892
2915
  ssl: config.ssl
2893
2916
  }
2917
+ };
2918
+ this.stores = {};
2919
+ } catch (e) {
2920
+ throw new error.MastraError(
2921
+ {
2922
+ id: "MASTRA_STORAGE_PG_STORE_INITIALIZATION_FAILED",
2923
+ domain: error.ErrorDomain.STORAGE,
2924
+ category: error.ErrorCategory.USER
2925
+ },
2926
+ e
2894
2927
  );
2895
- this.client = this.db;
2896
- const operations = new StoreOperationsPG({ client: this.client, schemaName: this.schema });
2897
- const scores = new ScoresPG({ client: this.client, operations, schema: this.schema });
2898
- const traces = new TracesPG({ client: this.client, operations, schema: this.schema });
2899
- const workflows = new WorkflowsPG({ client: this.client, operations, schema: this.schema });
2900
- const legacyEvals = new LegacyEvalsPG({ client: this.client, schema: this.schema });
2901
- const memory = new MemoryPG({ client: this.client, schema: this.schema, operations });
2928
+ }
2929
+ }
2930
+ async init() {
2931
+ if (this.isConnected) {
2932
+ return;
2933
+ }
2934
+ try {
2935
+ this.isConnected = true;
2936
+ this.#pgp = pgPromise__default.default();
2937
+ this.#db = this.#pgp(this.#config);
2938
+ const operations = new StoreOperationsPG({ client: this.#db, schemaName: this.schema });
2939
+ const scores = new ScoresPG({ client: this.#db, operations, schema: this.schema });
2940
+ const traces = new TracesPG({ client: this.#db, operations, schema: this.schema });
2941
+ const workflows = new WorkflowsPG({ client: this.#db, operations, schema: this.schema });
2942
+ const legacyEvals = new LegacyEvalsPG({ client: this.#db, schema: this.schema });
2943
+ const memory = new MemoryPG({ client: this.#db, schema: this.schema, operations });
2902
2944
  this.stores = {
2903
2945
  operations,
2904
2946
  scores,
@@ -2907,17 +2949,31 @@ var PostgresStore = class extends storage.MastraStorage {
2907
2949
  legacyEvals,
2908
2950
  memory
2909
2951
  };
2910
- } catch (e) {
2952
+ await super.init();
2953
+ } catch (error$1) {
2954
+ this.isConnected = false;
2911
2955
  throw new error.MastraError(
2912
2956
  {
2913
- id: "MASTRA_STORAGE_PG_STORE_INITIALIZATION_FAILED",
2957
+ id: "MASTRA_STORAGE_POSTGRES_STORE_INIT_FAILED",
2914
2958
  domain: error.ErrorDomain.STORAGE,
2915
- category: error.ErrorCategory.USER
2959
+ category: error.ErrorCategory.THIRD_PARTY
2916
2960
  },
2917
- e
2961
+ error$1
2918
2962
  );
2919
2963
  }
2920
2964
  }
2965
+ get db() {
2966
+ if (!this.#db) {
2967
+ throw new Error(`PostgresStore: Store is not initialized, please call "init()" first.`);
2968
+ }
2969
+ return this.#db;
2970
+ }
2971
+ get pgp() {
2972
+ if (!this.#pgp) {
2973
+ throw new Error(`PostgresStore: Store is not initialized, please call "init()" first.`);
2974
+ }
2975
+ return this.#pgp;
2976
+ }
2921
2977
  get supports() {
2922
2978
  return {
2923
2979
  selectByIncludeResourceScope: true,
@@ -3074,10 +3130,13 @@ var PostgresStore = class extends storage.MastraStorage {
3074
3130
  return this.stores.scores.getScoreById({ id: _id });
3075
3131
  }
3076
3132
  async getScoresByScorerId({
3077
- scorerId: _scorerId,
3078
- pagination: _pagination
3133
+ scorerId,
3134
+ pagination,
3135
+ entityId,
3136
+ entityType,
3137
+ source
3079
3138
  }) {
3080
- return this.stores.scores.getScoresByScorerId({ scorerId: _scorerId, pagination: _pagination });
3139
+ return this.stores.scores.getScoresByScorerId({ scorerId, pagination, entityId, entityType, source });
3081
3140
  }
3082
3141
  async saveScore(_score) {
3083
3142
  return this.stores.scores.saveScore(_score);