@mastra/pg 1.9.0-alpha.0 → 1.9.1-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.
package/dist/index.js CHANGED
@@ -2474,36 +2474,39 @@ var PgDB = class extends MastraBase {
2474
2474
  return getDefaultValue(type);
2475
2475
  }
2476
2476
  }
2477
- async insert({ tableName, record }) {
2478
- try {
2479
- this.addTimestampZColumns(record);
2480
- const filteredRecord = await this.filterRecordToKnownColumns(tableName, record);
2481
- const schemaName = getSchemaName(this.schemaName);
2482
- const columns = Object.keys(filteredRecord).map((col) => parseSqlIdentifier(col, "column name"));
2483
- if (columns.length === 0) return;
2484
- const values = this.prepareValuesForInsert(filteredRecord, tableName);
2485
- const placeholders = values.map((_, i) => `$${i + 1}`).join(", ");
2486
- const fullTableName = getTableName({ indexName: tableName, schemaName });
2487
- const columnList = columns.map((c) => `"${c}"`).join(", ");
2488
- if (tableName === TABLE_SPANS) {
2489
- const updateColumns = columns.filter((c) => c !== "traceId" && c !== "spanId");
2490
- if (updateColumns.length > 0) {
2491
- const updateClause = updateColumns.map((c) => `"${c}" = EXCLUDED."${c}"`).join(", ");
2492
- await this.client.none(
2493
- `INSERT INTO ${fullTableName} (${columnList}) VALUES (${placeholders})
2477
+ async executeInsert(client, { tableName, record }) {
2478
+ this.addTimestampZColumns(record);
2479
+ const filteredRecord = await this.filterRecordToKnownColumns(tableName, record);
2480
+ const schemaName = getSchemaName(this.schemaName);
2481
+ const columns = Object.keys(filteredRecord).map((col) => parseSqlIdentifier(col, "column name"));
2482
+ if (columns.length === 0) return;
2483
+ const values = this.prepareValuesForInsert(filteredRecord, tableName);
2484
+ const placeholders = values.map((_, i) => `$${i + 1}`).join(", ");
2485
+ const fullTableName = getTableName({ indexName: tableName, schemaName });
2486
+ const columnList = columns.map((c) => `"${c}"`).join(", ");
2487
+ if (tableName === TABLE_SPANS) {
2488
+ const updateColumns = columns.filter((c) => c !== "traceId" && c !== "spanId");
2489
+ if (updateColumns.length > 0) {
2490
+ const updateClause = updateColumns.map((c) => `"${c}" = EXCLUDED."${c}"`).join(", ");
2491
+ await client.none(
2492
+ `INSERT INTO ${fullTableName} (${columnList}) VALUES (${placeholders})
2494
2493
  ON CONFLICT ("traceId", "spanId") DO UPDATE SET ${updateClause}`,
2495
- values
2496
- );
2497
- } else {
2498
- await this.client.none(
2499
- `INSERT INTO ${fullTableName} (${columnList}) VALUES (${placeholders})
2500
- ON CONFLICT ("traceId", "spanId") DO NOTHING`,
2501
- values
2502
- );
2503
- }
2494
+ values
2495
+ );
2504
2496
  } else {
2505
- await this.client.none(`INSERT INTO ${fullTableName} (${columnList}) VALUES (${placeholders})`, values);
2497
+ await client.none(
2498
+ `INSERT INTO ${fullTableName} (${columnList}) VALUES (${placeholders})
2499
+ ON CONFLICT ("traceId", "spanId") DO NOTHING`,
2500
+ values
2501
+ );
2506
2502
  }
2503
+ } else {
2504
+ await client.none(`INSERT INTO ${fullTableName} (${columnList}) VALUES (${placeholders})`, values);
2505
+ }
2506
+ }
2507
+ async insert({ tableName, record }) {
2508
+ try {
2509
+ await this.executeInsert(this.client, { tableName, record });
2507
2510
  } catch (error) {
2508
2511
  throw new MastraError(
2509
2512
  {
@@ -2978,13 +2981,12 @@ Note: This migration may take some time for large tables.
2978
2981
  }
2979
2982
  async batchInsert({ tableName, records }) {
2980
2983
  try {
2981
- await this.client.query("BEGIN");
2982
- for (const record of records) {
2983
- await this.insert({ tableName, record });
2984
- }
2985
- await this.client.query("COMMIT");
2984
+ await this.client.tx(async (tx) => {
2985
+ for (const record of records) {
2986
+ await this.executeInsert(tx, { tableName, record });
2987
+ }
2988
+ });
2986
2989
  } catch (error) {
2987
- await this.client.query("ROLLBACK");
2988
2990
  throw new MastraError(
2989
2991
  {
2990
2992
  id: createStorageErrorId("PG", "BATCH_INSERT", "FAILED"),
@@ -3262,30 +3264,7 @@ Note: This migration may take some time for large tables.
3262
3264
  data
3263
3265
  }) {
3264
3266
  try {
3265
- const filteredData = await this.filterRecordToKnownColumns(tableName, data);
3266
- if (Object.keys(filteredData).length === 0) return;
3267
- const setColumns = [];
3268
- const setValues = [];
3269
- let paramIndex = 1;
3270
- Object.entries(filteredData).forEach(([key, value]) => {
3271
- const parsedKey = parseSqlIdentifier(key, "column name");
3272
- setColumns.push(`"${parsedKey}" = $${paramIndex++}`);
3273
- setValues.push(this.prepareValue(value, key, tableName));
3274
- });
3275
- const whereConditions = [];
3276
- const whereValues = [];
3277
- Object.entries(keys).forEach(([key, value]) => {
3278
- const parsedKey = parseSqlIdentifier(key, "column name");
3279
- whereConditions.push(`"${parsedKey}" = $${paramIndex++}`);
3280
- whereValues.push(this.prepareValue(value, key, tableName));
3281
- });
3282
- const tableName_ = getTableName({
3283
- indexName: tableName,
3284
- schemaName: getSchemaName(this.schemaName)
3285
- });
3286
- const sql = `UPDATE ${tableName_} SET ${setColumns.join(", ")} WHERE ${whereConditions.join(" AND ")}`;
3287
- const values = [...setValues, ...whereValues];
3288
- await this.client.none(sql, values);
3267
+ await this.executeUpdate(this.client, { tableName, keys, data });
3289
3268
  } catch (error) {
3290
3269
  throw new MastraError(
3291
3270
  {
@@ -3305,13 +3284,12 @@ Note: This migration may take some time for large tables.
3305
3284
  updates
3306
3285
  }) {
3307
3286
  try {
3308
- await this.client.query("BEGIN");
3309
- for (const { keys, data } of updates) {
3310
- await this.update({ tableName, keys, data });
3311
- }
3312
- await this.client.query("COMMIT");
3287
+ await this.client.tx(async (tx) => {
3288
+ for (const { keys, data } of updates) {
3289
+ await this.executeUpdate(tx, { tableName, keys, data });
3290
+ }
3291
+ });
3313
3292
  } catch (error) {
3314
- await this.client.query("ROLLBACK");
3315
3293
  throw new MastraError(
3316
3294
  {
3317
3295
  id: createStorageErrorId("PG", "BATCH_UPDATE", "FAILED"),
@@ -3370,6 +3348,36 @@ Note: This migration may take some time for large tables.
3370
3348
  async deleteData({ tableName }) {
3371
3349
  return this.clearTable({ tableName });
3372
3350
  }
3351
+ async executeUpdate(client, {
3352
+ tableName,
3353
+ keys,
3354
+ data
3355
+ }) {
3356
+ const filteredData = await this.filterRecordToKnownColumns(tableName, data);
3357
+ if (Object.keys(filteredData).length === 0) return;
3358
+ const setColumns = [];
3359
+ const setValues = [];
3360
+ let paramIndex = 1;
3361
+ Object.entries(filteredData).forEach(([key, value]) => {
3362
+ const parsedKey = parseSqlIdentifier(key, "column name");
3363
+ setColumns.push(`"${parsedKey}" = $${paramIndex++}`);
3364
+ setValues.push(this.prepareValue(value, key, tableName));
3365
+ });
3366
+ const whereConditions = [];
3367
+ const whereValues = [];
3368
+ Object.entries(keys).forEach(([key, value]) => {
3369
+ const parsedKey = parseSqlIdentifier(key, "column name");
3370
+ whereConditions.push(`"${parsedKey}" = $${paramIndex++}`);
3371
+ whereValues.push(this.prepareValue(value, key, tableName));
3372
+ });
3373
+ const tableName_ = getTableName({
3374
+ indexName: tableName,
3375
+ schemaName: getSchemaName(this.schemaName)
3376
+ });
3377
+ const sql = `UPDATE ${tableName_} SET ${setColumns.join(", ")} WHERE ${whereConditions.join(" AND ")}`;
3378
+ const values = [...setValues, ...whereValues];
3379
+ await client.none(sql, values);
3380
+ }
3373
3381
  };
3374
3382
  function getSchemaName2(schema) {
3375
3383
  return schema ? `"${parseSqlIdentifier(schema, "schema name")}"` : void 0;
@@ -5283,6 +5291,16 @@ var ExperimentsPG = class _ExperimentsPG extends ExperimentsStorage {
5283
5291
  async init() {
5284
5292
  await this.#db.createTable({ tableName: TABLE_EXPERIMENTS, schema: EXPERIMENTS_SCHEMA });
5285
5293
  await this.#db.createTable({ tableName: TABLE_EXPERIMENT_RESULTS, schema: EXPERIMENT_RESULTS_SCHEMA });
5294
+ await this.#db.alterTable({
5295
+ tableName: TABLE_EXPERIMENTS,
5296
+ schema: EXPERIMENTS_SCHEMA,
5297
+ ifNotExists: ["agentVersion"]
5298
+ });
5299
+ await this.#db.alterTable({
5300
+ tableName: TABLE_EXPERIMENT_RESULTS,
5301
+ schema: EXPERIMENT_RESULTS_SCHEMA,
5302
+ ifNotExists: ["status", "tags"]
5303
+ });
5286
5304
  await this.createDefaultIndexes();
5287
5305
  await this.createCustomIndexes();
5288
5306
  }