@mastra/pg 1.17.0-alpha.1 → 1.17.0-alpha.4
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/CHANGELOG.md +46 -0
- package/dist/docs/SKILL.md +1 -1
- package/dist/docs/assets/SOURCE_MAP.json +1 -1
- package/dist/index.cjs +48 -26
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +48 -26
- package/dist/index.js.map +1 -1
- package/dist/storage/factory-storage.d.ts +1 -0
- package/dist/storage/factory-storage.d.ts.map +1 -1
- package/dist/storage/test-utils.d.ts.map +1 -1
- package/package.json +4 -4
package/dist/index.js
CHANGED
|
@@ -7302,7 +7302,7 @@ var FavoritesPG = class _FavoritesPG extends FavoritesStorage {
|
|
|
7302
7302
|
}
|
|
7303
7303
|
const fullFavoritesTable = getTableName2({ indexName: TABLE_FAVORITES, schemaName: getSchemaName2(schemaName) });
|
|
7304
7304
|
statements.push(
|
|
7305
|
-
`CREATE INDEX IF NOT EXISTS idx_favorites_entity ON ${fullFavoritesTable} ("entityType", "entityId")
|
|
7305
|
+
`CREATE INDEX IF NOT EXISTS idx_favorites_entity ON ${fullFavoritesTable} ("entityType", "entityId");`
|
|
7306
7306
|
);
|
|
7307
7307
|
return statements;
|
|
7308
7308
|
}
|
|
@@ -20231,10 +20231,12 @@ function hashAdvisoryLockKey(key) {
|
|
|
20231
20231
|
return [digest.readInt32BE(0), digest.readInt32BE(4)];
|
|
20232
20232
|
}
|
|
20233
20233
|
var PgFactoryStorageOps = class {
|
|
20234
|
-
#
|
|
20234
|
+
#queryable;
|
|
20235
|
+
#transactionClient;
|
|
20235
20236
|
#schemas;
|
|
20236
|
-
constructor(
|
|
20237
|
-
this.#
|
|
20237
|
+
constructor(queryable, schemas, transactionClient) {
|
|
20238
|
+
this.#queryable = queryable;
|
|
20239
|
+
this.#transactionClient = transactionClient;
|
|
20238
20240
|
this.#schemas = schemas;
|
|
20239
20241
|
}
|
|
20240
20242
|
#schema(collection) {
|
|
@@ -20382,11 +20384,11 @@ var PgFactoryStorageOps = class {
|
|
|
20382
20384
|
return result.rows.map((raw) => this.#deserializeRow(schema, raw));
|
|
20383
20385
|
}
|
|
20384
20386
|
async findOne(collection, where) {
|
|
20385
|
-
const rows = await this.#select(this.#
|
|
20387
|
+
const rows = await this.#select(this.#queryable, collection, where, { limit: 1 });
|
|
20386
20388
|
return rows[0] ?? null;
|
|
20387
20389
|
}
|
|
20388
20390
|
async findMany(collection, where, opts) {
|
|
20389
|
-
return this.#select(this.#
|
|
20391
|
+
return this.#select(this.#queryable, collection, where, opts);
|
|
20390
20392
|
}
|
|
20391
20393
|
async insertOne(collection, row) {
|
|
20392
20394
|
const schema = this.#schema(collection);
|
|
@@ -20400,7 +20402,7 @@ var PgFactoryStorageOps = class {
|
|
|
20400
20402
|
const sql = `INSERT INTO "${schema.name}" (${columns.map((c) => `"${c}"`).join(", ")}) VALUES (${columns.map((_, i) => `$${i + 1}`).join(", ")}) RETURNING *`;
|
|
20401
20403
|
const args = columns.map((column) => this.#serialize(this.#column(schema, column), values[column]));
|
|
20402
20404
|
try {
|
|
20403
|
-
const result = await this.#
|
|
20405
|
+
const result = await this.#queryable.query(sql, args);
|
|
20404
20406
|
return this.#deserializeRow(schema, result.rows[0]);
|
|
20405
20407
|
} catch (error) {
|
|
20406
20408
|
if (isUniqueViolation(error)) throw new UniqueViolationError(collection, { cause: error });
|
|
@@ -20438,7 +20440,7 @@ var PgFactoryStorageOps = class {
|
|
|
20438
20440
|
throw lastError ?? new Error(`PgFactoryStorage: upsert into '${collection}' did not converge`);
|
|
20439
20441
|
}
|
|
20440
20442
|
async updateMany(collection, where, set) {
|
|
20441
|
-
return this.#updateMany(this.#
|
|
20443
|
+
return this.#updateMany(this.#queryable, collection, where, set);
|
|
20442
20444
|
}
|
|
20443
20445
|
async #updateMany(queryable, collection, where, set) {
|
|
20444
20446
|
const schema = this.#schema(collection);
|
|
@@ -20453,30 +20455,30 @@ var PgFactoryStorageOps = class {
|
|
|
20453
20455
|
async deleteMany(collection, where) {
|
|
20454
20456
|
const schema = this.#schema(collection);
|
|
20455
20457
|
const filter = this.#buildWhere(schema, where);
|
|
20456
|
-
const result = await this.#
|
|
20458
|
+
const result = await this.#queryable.query(`DELETE FROM "${schema.name}" WHERE ${filter.sql}`, filter.args);
|
|
20457
20459
|
return result.rowCount ?? 0;
|
|
20458
20460
|
}
|
|
20459
20461
|
async updateAtomic(collection, where, fn) {
|
|
20460
|
-
const
|
|
20461
|
-
|
|
20462
|
-
|
|
20462
|
+
const run = async (client2) => {
|
|
20463
|
+
const schema = this.#schema(collection);
|
|
20464
|
+
const pk = primaryKeyOf(schema);
|
|
20465
|
+
const rows = await this.#select(client2, collection, where, { limit: 1 }, true);
|
|
20466
|
+
const row = rows[0];
|
|
20467
|
+
if (!row) return null;
|
|
20468
|
+
const patch = await fn(row);
|
|
20469
|
+
if (patch === null) return row;
|
|
20470
|
+
const pkWhere = { [pk]: row[pk] };
|
|
20471
|
+
await this.#updateMany(client2, collection, pkWhere, patch);
|
|
20472
|
+
const updated = await this.#select(client2, collection, pkWhere, { limit: 1 });
|
|
20473
|
+
return updated[0] ?? null;
|
|
20474
|
+
};
|
|
20475
|
+
if (this.#transactionClient) return run(this.#transactionClient);
|
|
20476
|
+
const pool = this.#queryable;
|
|
20477
|
+
const client = await pool.connect();
|
|
20463
20478
|
try {
|
|
20464
20479
|
await client.query("BEGIN");
|
|
20465
20480
|
try {
|
|
20466
|
-
const
|
|
20467
|
-
const row = rows[0];
|
|
20468
|
-
if (!row) {
|
|
20469
|
-
await client.query("COMMIT");
|
|
20470
|
-
return null;
|
|
20471
|
-
}
|
|
20472
|
-
const patch = await fn(row);
|
|
20473
|
-
let result = row;
|
|
20474
|
-
if (patch !== null) {
|
|
20475
|
-
const pkWhere = { [pk]: row[pk] };
|
|
20476
|
-
await this.#updateMany(client, collection, pkWhere, patch);
|
|
20477
|
-
const updated = await this.#select(client, collection, pkWhere, { limit: 1 });
|
|
20478
|
-
result = updated[0] ?? null;
|
|
20479
|
-
}
|
|
20481
|
+
const result = await run(client);
|
|
20480
20482
|
await client.query("COMMIT");
|
|
20481
20483
|
return result;
|
|
20482
20484
|
} catch (error) {
|
|
@@ -20522,6 +20524,22 @@ var PgFactoryStorage = class extends FactoryStorage {
|
|
|
20522
20524
|
async initStorage() {
|
|
20523
20525
|
await this.#pool.query("SELECT 1");
|
|
20524
20526
|
}
|
|
20527
|
+
async withTransaction(fn) {
|
|
20528
|
+
const client = await this.#pool.connect();
|
|
20529
|
+
try {
|
|
20530
|
+
await client.query("BEGIN");
|
|
20531
|
+
try {
|
|
20532
|
+
const result = await fn(new PgFactoryStorageOps(client, this.#schemas, client));
|
|
20533
|
+
await client.query("COMMIT");
|
|
20534
|
+
return result;
|
|
20535
|
+
} catch (error) {
|
|
20536
|
+
await client.query("ROLLBACK");
|
|
20537
|
+
throw error;
|
|
20538
|
+
}
|
|
20539
|
+
} finally {
|
|
20540
|
+
client.release();
|
|
20541
|
+
}
|
|
20542
|
+
}
|
|
20525
20543
|
async ensureCollections(schemas) {
|
|
20526
20544
|
for (const schema of schemas) {
|
|
20527
20545
|
await this.#ensureCollection(schema);
|
|
@@ -20578,6 +20596,10 @@ var PgFactoryStorage = class extends FactoryStorage {
|
|
|
20578
20596
|
for (const [name, spec] of Object.entries(schema.columns)) {
|
|
20579
20597
|
await this.#pool.query(`ALTER TABLE "${schema.name}" ADD COLUMN IF NOT EXISTS ${this.#columnDdl(name, spec)}`);
|
|
20580
20598
|
}
|
|
20599
|
+
for (const [name, spec] of Object.entries(schema.columns)) {
|
|
20600
|
+
if (!spec.nullable || spec.type === "uuid-pk" || spec.primaryKey) continue;
|
|
20601
|
+
await this.#pool.query(`ALTER TABLE "${schema.name}" ALTER COLUMN "${name}" DROP NOT NULL`);
|
|
20602
|
+
}
|
|
20581
20603
|
for (const index of schema.uniqueIndexes ?? []) {
|
|
20582
20604
|
assertIdentifier("index", index.name);
|
|
20583
20605
|
index.columns.forEach((column) => assertIdentifier("column", column));
|