@mastra/pg 1.13.0 → 1.13.3-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/CHANGELOG.md +20 -0
- package/dist/docs/SKILL.md +1 -1
- package/dist/docs/assets/SOURCE_MAP.json +1 -1
- package/dist/index.cjs +170 -4
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +170 -4
- package/dist/index.js.map +1 -1
- package/dist/storage/client.d.ts +58 -0
- package/dist/storage/client.d.ts.map +1 -1
- package/dist/storage/domains/skills/index.d.ts.map +1 -1
- package/dist/storage/index.d.ts.map +1 -1
- package/package.json +16 -10
package/dist/index.js
CHANGED
|
@@ -14,6 +14,7 @@ import { createRequire } from 'module';
|
|
|
14
14
|
import { MessageList } from '@mastra/core/agent';
|
|
15
15
|
import { coreFeatures } from '@mastra/core/features';
|
|
16
16
|
import { saveScorePayloadSchema } from '@mastra/core/evals';
|
|
17
|
+
import { skillSnapshotFieldValuesEqual } from '@mastra/core/storage/domains/skills';
|
|
17
18
|
|
|
18
19
|
var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
|
|
19
20
|
get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
|
|
@@ -2139,6 +2140,161 @@ var TransactionClient = class {
|
|
|
2139
2140
|
return Promise.all(promises);
|
|
2140
2141
|
}
|
|
2141
2142
|
};
|
|
2143
|
+
var PinnedClientAdapter = class {
|
|
2144
|
+
constructor($pool, pinnedClient) {
|
|
2145
|
+
this.$pool = $pool;
|
|
2146
|
+
this.pinnedClient = pinnedClient;
|
|
2147
|
+
}
|
|
2148
|
+
$pool;
|
|
2149
|
+
pinnedClient;
|
|
2150
|
+
/**
|
|
2151
|
+
* Serialization tail. Domain init() methods fire via Promise.all in
|
|
2152
|
+
* MastraCompositeStore.#runInit(), so without our own gate every domain's
|
|
2153
|
+
* query() lands on the same PoolClient concurrently. pg@8 queues those
|
|
2154
|
+
* internally and emits a deprecation warning; pg@9 will throw. Chaining
|
|
2155
|
+
* each new query off the previous one's settlement (success or failure)
|
|
2156
|
+
* gives us deterministic FIFO ordering at the adapter layer and removes
|
|
2157
|
+
* the reliance on pg's internal queue.
|
|
2158
|
+
*/
|
|
2159
|
+
#tail = Promise.resolve();
|
|
2160
|
+
/**
|
|
2161
|
+
* Run `fn` after any previously enqueued work on this pinned client has
|
|
2162
|
+
* settled. Failures in earlier calls don't poison the queue — we always
|
|
2163
|
+
* resume on the next caller's turn.
|
|
2164
|
+
*/
|
|
2165
|
+
#enqueue(fn) {
|
|
2166
|
+
const next = this.#tail.then(fn, fn);
|
|
2167
|
+
this.#tail = next.catch(() => void 0);
|
|
2168
|
+
return next;
|
|
2169
|
+
}
|
|
2170
|
+
connect() {
|
|
2171
|
+
throw new Error(
|
|
2172
|
+
"PinnedClientAdapter.connect() is not supported during PostgresStore.init(). All DDL must flow through the pinned client."
|
|
2173
|
+
);
|
|
2174
|
+
}
|
|
2175
|
+
none(query, values) {
|
|
2176
|
+
return this.#enqueue(async () => {
|
|
2177
|
+
await this.pinnedClient.query(query, values);
|
|
2178
|
+
return null;
|
|
2179
|
+
});
|
|
2180
|
+
}
|
|
2181
|
+
one(query, values) {
|
|
2182
|
+
return this.#enqueue(async () => {
|
|
2183
|
+
const result = await this.pinnedClient.query(query, values);
|
|
2184
|
+
if (result.rows.length === 0) {
|
|
2185
|
+
throw new Error(`No data returned from query: ${truncateQuery(query)}`);
|
|
2186
|
+
}
|
|
2187
|
+
if (result.rows.length > 1) {
|
|
2188
|
+
throw new Error(`Multiple rows returned when one was expected: ${truncateQuery(query)}`);
|
|
2189
|
+
}
|
|
2190
|
+
return result.rows[0];
|
|
2191
|
+
});
|
|
2192
|
+
}
|
|
2193
|
+
oneOrNone(query, values) {
|
|
2194
|
+
return this.#enqueue(async () => {
|
|
2195
|
+
const result = await this.pinnedClient.query(query, values);
|
|
2196
|
+
if (result.rows.length === 0) {
|
|
2197
|
+
return null;
|
|
2198
|
+
}
|
|
2199
|
+
if (result.rows.length > 1) {
|
|
2200
|
+
throw new Error(`Multiple rows returned when one or none was expected: ${truncateQuery(query)}`);
|
|
2201
|
+
}
|
|
2202
|
+
return result.rows[0];
|
|
2203
|
+
});
|
|
2204
|
+
}
|
|
2205
|
+
any(query, values) {
|
|
2206
|
+
return this.#enqueue(async () => {
|
|
2207
|
+
const result = await this.pinnedClient.query(query, values);
|
|
2208
|
+
return result.rows;
|
|
2209
|
+
});
|
|
2210
|
+
}
|
|
2211
|
+
manyOrNone(query, values) {
|
|
2212
|
+
return this.any(query, values);
|
|
2213
|
+
}
|
|
2214
|
+
many(query, values) {
|
|
2215
|
+
return this.#enqueue(async () => {
|
|
2216
|
+
const result = await this.pinnedClient.query(query, values);
|
|
2217
|
+
if (result.rows.length === 0) {
|
|
2218
|
+
throw new Error(`No data returned from query: ${truncateQuery(query)}`);
|
|
2219
|
+
}
|
|
2220
|
+
return result.rows;
|
|
2221
|
+
});
|
|
2222
|
+
}
|
|
2223
|
+
query(query, values) {
|
|
2224
|
+
return this.#enqueue(() => this.pinnedClient.query(query, values));
|
|
2225
|
+
}
|
|
2226
|
+
tx(callback) {
|
|
2227
|
+
return this.#enqueue(async () => {
|
|
2228
|
+
await this.pinnedClient.query("BEGIN");
|
|
2229
|
+
try {
|
|
2230
|
+
const result = await callback(new TransactionClient(this.pinnedClient));
|
|
2231
|
+
await this.pinnedClient.query("COMMIT");
|
|
2232
|
+
return result;
|
|
2233
|
+
} catch (error) {
|
|
2234
|
+
try {
|
|
2235
|
+
await this.pinnedClient.query("ROLLBACK");
|
|
2236
|
+
} catch (rollbackError) {
|
|
2237
|
+
console.error("Transaction rollback failed:", rollbackError);
|
|
2238
|
+
}
|
|
2239
|
+
throw error;
|
|
2240
|
+
}
|
|
2241
|
+
});
|
|
2242
|
+
}
|
|
2243
|
+
};
|
|
2244
|
+
var RoutingDbClient = class {
|
|
2245
|
+
#base;
|
|
2246
|
+
#pinned = null;
|
|
2247
|
+
constructor(base) {
|
|
2248
|
+
this.#base = base;
|
|
2249
|
+
}
|
|
2250
|
+
/** Returns the currently active client (pinned if set, otherwise base). */
|
|
2251
|
+
get active() {
|
|
2252
|
+
return this.#pinned ?? this.#base;
|
|
2253
|
+
}
|
|
2254
|
+
/**
|
|
2255
|
+
* Pin a DbClient so all subsequent calls route through it until unpinned.
|
|
2256
|
+
* Throws if a client is already pinned to avoid silent overrides.
|
|
2257
|
+
*/
|
|
2258
|
+
pin(client) {
|
|
2259
|
+
if (this.#pinned) {
|
|
2260
|
+
throw new Error("RoutingDbClient already has a pinned client");
|
|
2261
|
+
}
|
|
2262
|
+
this.#pinned = client;
|
|
2263
|
+
}
|
|
2264
|
+
unpin() {
|
|
2265
|
+
this.#pinned = null;
|
|
2266
|
+
}
|
|
2267
|
+
get $pool() {
|
|
2268
|
+
return this.#base.$pool;
|
|
2269
|
+
}
|
|
2270
|
+
connect() {
|
|
2271
|
+
return this.active.connect();
|
|
2272
|
+
}
|
|
2273
|
+
none(query, values) {
|
|
2274
|
+
return this.active.none(query, values);
|
|
2275
|
+
}
|
|
2276
|
+
one(query, values) {
|
|
2277
|
+
return this.active.one(query, values);
|
|
2278
|
+
}
|
|
2279
|
+
oneOrNone(query, values) {
|
|
2280
|
+
return this.active.oneOrNone(query, values);
|
|
2281
|
+
}
|
|
2282
|
+
any(query, values) {
|
|
2283
|
+
return this.active.any(query, values);
|
|
2284
|
+
}
|
|
2285
|
+
manyOrNone(query, values) {
|
|
2286
|
+
return this.active.manyOrNone(query, values);
|
|
2287
|
+
}
|
|
2288
|
+
many(query, values) {
|
|
2289
|
+
return this.active.many(query, values);
|
|
2290
|
+
}
|
|
2291
|
+
query(query, values) {
|
|
2292
|
+
return this.active.query(query, values);
|
|
2293
|
+
}
|
|
2294
|
+
tx(callback) {
|
|
2295
|
+
return this.active.tx(callback);
|
|
2296
|
+
}
|
|
2297
|
+
};
|
|
2142
2298
|
|
|
2143
2299
|
// src/storage/db/constraint-utils.ts
|
|
2144
2300
|
var POSTGRES_IDENTIFIER_MAX_LENGTH = 63;
|
|
@@ -17037,7 +17193,10 @@ var SkillsPG = class _SkillsPG extends SkillsStorage {
|
|
|
17037
17193
|
} = latestVersion;
|
|
17038
17194
|
const newConfig = { ...latestConfig, ...configFields };
|
|
17039
17195
|
const changedFields = SNAPSHOT_FIELDS5.filter(
|
|
17040
|
-
(field) => field in configFields &&
|
|
17196
|
+
(field) => field in configFields && !skillSnapshotFieldValuesEqual(
|
|
17197
|
+
configFields[field],
|
|
17198
|
+
latestConfig[field]
|
|
17199
|
+
)
|
|
17041
17200
|
);
|
|
17042
17201
|
if (changedFields.length > 0) {
|
|
17043
17202
|
versionCreated = true;
|
|
@@ -18981,6 +19140,8 @@ function exportSchemas(schemaName) {
|
|
|
18981
19140
|
}
|
|
18982
19141
|
var PostgresStore = class extends MastraCompositeStore {
|
|
18983
19142
|
#pool;
|
|
19143
|
+
// Narrowed to RoutingDbClient so init()'s pin/unpin path is type-checked.
|
|
19144
|
+
// The public `db` getter still exposes it as DbClient.
|
|
18984
19145
|
#db;
|
|
18985
19146
|
#ownsPool;
|
|
18986
19147
|
#poolClosed = false;
|
|
@@ -18999,7 +19160,7 @@ var PostgresStore = class extends MastraCompositeStore {
|
|
|
18999
19160
|
this.#pool = this.createPool(config);
|
|
19000
19161
|
this.#ownsPool = true;
|
|
19001
19162
|
}
|
|
19002
|
-
this.#db = new PoolAdapter(this.#pool);
|
|
19163
|
+
this.#db = new RoutingDbClient(new PoolAdapter(this.#pool));
|
|
19003
19164
|
const domainConfig = {
|
|
19004
19165
|
client: this.#db,
|
|
19005
19166
|
schemaName: this.schema,
|
|
@@ -19055,11 +19216,13 @@ var PostgresStore = class extends MastraCompositeStore {
|
|
|
19055
19216
|
if (this.isInitialized) {
|
|
19056
19217
|
return;
|
|
19057
19218
|
}
|
|
19219
|
+
const pinnedClient = await this.#pool.connect();
|
|
19220
|
+
const pinned = new PinnedClientAdapter(this.#pool, pinnedClient);
|
|
19058
19221
|
try {
|
|
19059
|
-
this.
|
|
19222
|
+
this.#db.pin(pinned);
|
|
19060
19223
|
await super.init();
|
|
19224
|
+
this.isInitialized = true;
|
|
19061
19225
|
} catch (error) {
|
|
19062
|
-
this.isInitialized = false;
|
|
19063
19226
|
if (error instanceof MastraError) {
|
|
19064
19227
|
throw error;
|
|
19065
19228
|
}
|
|
@@ -19071,6 +19234,9 @@ var PostgresStore = class extends MastraCompositeStore {
|
|
|
19071
19234
|
},
|
|
19072
19235
|
error
|
|
19073
19236
|
);
|
|
19237
|
+
} finally {
|
|
19238
|
+
this.#db.unpin();
|
|
19239
|
+
pinnedClient.release();
|
|
19074
19240
|
}
|
|
19075
19241
|
}
|
|
19076
19242
|
/**
|