@mastra/pg 0.13.3 → 0.14.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.
package/dist/index.js CHANGED
@@ -2288,6 +2288,7 @@ var StoreOperationsPG = class extends StoreOperations {
2288
2288
  }
2289
2289
  };
2290
2290
  function transformScoreRow(row) {
2291
+ console.log(`row is`, JSON.stringify(row, null, 2));
2291
2292
  return {
2292
2293
  ...row,
2293
2294
  input: safelyParseJSON(row.input),
@@ -2323,7 +2324,7 @@ var ScoresPG = class extends ScoresStorage {
2323
2324
  `SELECT * FROM ${getTableName({ indexName: TABLE_SCORERS, schemaName: this.schema })} WHERE id = $1`,
2324
2325
  [id]
2325
2326
  );
2326
- return transformScoreRow(result);
2327
+ return result ? transformScoreRow(result) : null;
2327
2328
  } catch (error) {
2328
2329
  throw new MastraError(
2329
2330
  {
@@ -2337,12 +2338,31 @@ var ScoresPG = class extends ScoresStorage {
2337
2338
  }
2338
2339
  async getScoresByScorerId({
2339
2340
  scorerId,
2340
- pagination
2341
+ pagination,
2342
+ entityId,
2343
+ entityType,
2344
+ source
2341
2345
  }) {
2342
2346
  try {
2347
+ const conditions = [`"scorerId" = $1`];
2348
+ const queryParams = [scorerId];
2349
+ let paramIndex = 2;
2350
+ if (entityId) {
2351
+ conditions.push(`"entityId" = $${paramIndex++}`);
2352
+ queryParams.push(entityId);
2353
+ }
2354
+ if (entityType) {
2355
+ conditions.push(`"entityType" = $${paramIndex++}`);
2356
+ queryParams.push(entityType);
2357
+ }
2358
+ if (source) {
2359
+ conditions.push(`"source" = $${paramIndex++}`);
2360
+ queryParams.push(source);
2361
+ }
2362
+ const whereClause = conditions.join(" AND ");
2343
2363
  const total = await this.client.oneOrNone(
2344
- `SELECT COUNT(*) FROM ${getTableName({ indexName: TABLE_SCORERS, schemaName: this.schema })} WHERE "scorerId" = $1`,
2345
- [scorerId]
2364
+ `SELECT COUNT(*) FROM ${getTableName({ indexName: TABLE_SCORERS, schemaName: this.schema })} WHERE ${whereClause}`,
2365
+ queryParams
2346
2366
  );
2347
2367
  if (total?.count === "0" || !total?.count) {
2348
2368
  return {
@@ -2356,8 +2376,8 @@ var ScoresPG = class extends ScoresStorage {
2356
2376
  };
2357
2377
  }
2358
2378
  const result = await this.client.manyOrNone(
2359
- `SELECT * FROM ${getTableName({ indexName: TABLE_SCORERS, schemaName: this.schema })} WHERE "scorerId" = $1 LIMIT $2 OFFSET $3`,
2360
- [scorerId, pagination.perPage, pagination.page * pagination.perPage]
2379
+ `SELECT * FROM ${getTableName({ indexName: TABLE_SCORERS, schemaName: this.schema })} WHERE ${whereClause} ORDER BY "createdAt" DESC LIMIT $${paramIndex++} OFFSET $${paramIndex++}`,
2380
+ [...queryParams, pagination.perPage, pagination.page * pagination.perPage]
2361
2381
  );
2362
2382
  return {
2363
2383
  pagination: {
@@ -2381,7 +2401,7 @@ var ScoresPG = class extends ScoresStorage {
2381
2401
  }
2382
2402
  async saveScore(score) {
2383
2403
  try {
2384
- const scoreId = crypto.randomUUID();
2404
+ const id = crypto.randomUUID();
2385
2405
  const {
2386
2406
  scorer,
2387
2407
  preprocessStepResult,
@@ -2394,10 +2414,11 @@ var ScoresPG = class extends ScoresStorage {
2394
2414
  entity,
2395
2415
  ...rest
2396
2416
  } = score;
2417
+ console.log(`saving score with id: ${id}`);
2397
2418
  await this.operations.insert({
2398
2419
  tableName: TABLE_SCORERS,
2399
2420
  record: {
2400
- id: scoreId,
2421
+ id,
2401
2422
  ...rest,
2402
2423
  input: JSON.stringify(input) || "",
2403
2424
  output: JSON.stringify(output) || "",
@@ -2412,7 +2433,7 @@ var ScoresPG = class extends ScoresStorage {
2412
2433
  updatedAt: (/* @__PURE__ */ new Date()).toISOString()
2413
2434
  }
2414
2435
  });
2415
- const scoreFromDb = await this.getScoreById({ id: scoreId });
2436
+ const scoreFromDb = await this.getScoreById({ id });
2416
2437
  return { score: scoreFromDb };
2417
2438
  } catch (error) {
2418
2439
  throw new MastraError(
@@ -2848,10 +2869,11 @@ var WorkflowsPG = class extends WorkflowsStorage {
2848
2869
 
2849
2870
  // src/storage/index.ts
2850
2871
  var PostgresStore = class extends MastraStorage {
2851
- db;
2852
- pgp;
2853
- client;
2872
+ #db;
2873
+ #pgp;
2874
+ #config;
2854
2875
  schema;
2876
+ isConnected = false;
2855
2877
  stores;
2856
2878
  constructor(config) {
2857
2879
  try {
@@ -2872,10 +2894,11 @@ var PostgresStore = class extends MastraStorage {
2872
2894
  }
2873
2895
  }
2874
2896
  super({ name: "PostgresStore" });
2875
- this.pgp = pgPromise();
2876
2897
  this.schema = config.schemaName || "public";
2877
- this.db = this.pgp(
2878
- `connectionString` in config ? { connectionString: config.connectionString } : {
2898
+ this.#config = {
2899
+ max: config.max,
2900
+ idleTimeoutMillis: config.idleTimeoutMillis,
2901
+ ...`connectionString` in config ? { connectionString: config.connectionString } : {
2879
2902
  host: config.host,
2880
2903
  port: config.port,
2881
2904
  database: config.database,
@@ -2883,14 +2906,33 @@ var PostgresStore = class extends MastraStorage {
2883
2906
  password: config.password,
2884
2907
  ssl: config.ssl
2885
2908
  }
2909
+ };
2910
+ this.stores = {};
2911
+ } catch (e) {
2912
+ throw new MastraError(
2913
+ {
2914
+ id: "MASTRA_STORAGE_PG_STORE_INITIALIZATION_FAILED",
2915
+ domain: ErrorDomain.STORAGE,
2916
+ category: ErrorCategory.USER
2917
+ },
2918
+ e
2886
2919
  );
2887
- this.client = this.db;
2888
- const operations = new StoreOperationsPG({ client: this.client, schemaName: this.schema });
2889
- const scores = new ScoresPG({ client: this.client, operations, schema: this.schema });
2890
- const traces = new TracesPG({ client: this.client, operations, schema: this.schema });
2891
- const workflows = new WorkflowsPG({ client: this.client, operations, schema: this.schema });
2892
- const legacyEvals = new LegacyEvalsPG({ client: this.client, schema: this.schema });
2893
- const memory = new MemoryPG({ client: this.client, schema: this.schema, operations });
2920
+ }
2921
+ }
2922
+ async init() {
2923
+ if (this.isConnected) {
2924
+ return;
2925
+ }
2926
+ try {
2927
+ this.isConnected = true;
2928
+ this.#pgp = pgPromise();
2929
+ this.#db = this.#pgp(this.#config);
2930
+ const operations = new StoreOperationsPG({ client: this.#db, schemaName: this.schema });
2931
+ const scores = new ScoresPG({ client: this.#db, operations, schema: this.schema });
2932
+ const traces = new TracesPG({ client: this.#db, operations, schema: this.schema });
2933
+ const workflows = new WorkflowsPG({ client: this.#db, operations, schema: this.schema });
2934
+ const legacyEvals = new LegacyEvalsPG({ client: this.#db, schema: this.schema });
2935
+ const memory = new MemoryPG({ client: this.#db, schema: this.schema, operations });
2894
2936
  this.stores = {
2895
2937
  operations,
2896
2938
  scores,
@@ -2899,17 +2941,31 @@ var PostgresStore = class extends MastraStorage {
2899
2941
  legacyEvals,
2900
2942
  memory
2901
2943
  };
2902
- } catch (e) {
2944
+ await super.init();
2945
+ } catch (error) {
2946
+ this.isConnected = false;
2903
2947
  throw new MastraError(
2904
2948
  {
2905
- id: "MASTRA_STORAGE_PG_STORE_INITIALIZATION_FAILED",
2949
+ id: "MASTRA_STORAGE_POSTGRES_STORE_INIT_FAILED",
2906
2950
  domain: ErrorDomain.STORAGE,
2907
- category: ErrorCategory.USER
2951
+ category: ErrorCategory.THIRD_PARTY
2908
2952
  },
2909
- e
2953
+ error
2910
2954
  );
2911
2955
  }
2912
2956
  }
2957
+ get db() {
2958
+ if (!this.#db) {
2959
+ throw new Error(`PostgresStore: Store is not initialized, please call "init()" first.`);
2960
+ }
2961
+ return this.#db;
2962
+ }
2963
+ get pgp() {
2964
+ if (!this.#pgp) {
2965
+ throw new Error(`PostgresStore: Store is not initialized, please call "init()" first.`);
2966
+ }
2967
+ return this.#pgp;
2968
+ }
2913
2969
  get supports() {
2914
2970
  return {
2915
2971
  selectByIncludeResourceScope: true,
@@ -3066,10 +3122,13 @@ var PostgresStore = class extends MastraStorage {
3066
3122
  return this.stores.scores.getScoreById({ id: _id });
3067
3123
  }
3068
3124
  async getScoresByScorerId({
3069
- scorerId: _scorerId,
3070
- pagination: _pagination
3125
+ scorerId,
3126
+ pagination,
3127
+ entityId,
3128
+ entityType,
3129
+ source
3071
3130
  }) {
3072
- return this.stores.scores.getScoresByScorerId({ scorerId: _scorerId, pagination: _pagination });
3131
+ return this.stores.scores.getScoresByScorerId({ scorerId, pagination, entityId, entityType, source });
3073
3132
  }
3074
3133
  async saveScore(_score) {
3075
3134
  return this.stores.scores.saveScore(_score);