@mastra/pg 0.17.2 → 0.17.3-alpha.1

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.d.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  export * from './vector/index.js';
2
2
  export * from './storage/index.js';
3
+ export type { PostgresConfig, PostgresStoreConfig, PgVectorConfig } from './shared/config.js';
3
4
  export { PGVECTOR_PROMPT } from './vector/prompt.js';
4
5
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,UAAU,CAAC;AACzB,cAAc,WAAW,CAAC;AAC1B,OAAO,EAAE,eAAe,EAAE,MAAM,iBAAiB,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,UAAU,CAAC;AACzB,cAAc,WAAW,CAAC;AAC1B,YAAY,EAAE,cAAc,EAAE,mBAAmB,EAAE,cAAc,EAAE,MAAM,iBAAiB,CAAC;AAC3F,OAAO,EAAE,eAAe,EAAE,MAAM,iBAAiB,CAAC"}
package/dist/index.js CHANGED
@@ -2,7 +2,7 @@ import { MastraError, ErrorCategory, ErrorDomain } from '@mastra/core/error';
2
2
  import { parseSqlIdentifier, parseFieldKey } from '@mastra/core/utils';
3
3
  import { MastraVector } from '@mastra/core/vector';
4
4
  import { Mutex } from 'async-mutex';
5
- import pg from 'pg';
5
+ import * as pg from 'pg';
6
6
  import xxhash from 'xxhash-wasm';
7
7
  import { BaseFilterTranslator } from '@mastra/core/vector/filter';
8
8
  import { MastraStorage, StoreOperations, TABLE_SCHEMAS, TABLE_WORKFLOW_SNAPSHOT, TABLE_AI_SPANS, TABLE_THREADS, TABLE_MESSAGES, TABLE_TRACES, TABLE_EVALS, TABLE_SCORERS, ScoresStorage, TracesStorage, safelyParseJSON, WorkflowsStorage, LegacyEvalsStorage, MemoryStorage, resolveMessageLimit, TABLE_RESOURCES, ObservabilityStorage } from '@mastra/core/storage';
@@ -11,6 +11,39 @@ import { MessageList } from '@mastra/core/agent';
11
11
  import { saveScorePayloadSchema } from '@mastra/core/scores';
12
12
 
13
13
  // src/vector/index.ts
14
+
15
+ // src/shared/config.ts
16
+ var isConnectionStringConfig = (cfg) => {
17
+ return "connectionString" in cfg;
18
+ };
19
+ var isHostConfig = (cfg) => {
20
+ return "host" in cfg && "database" in cfg && "user" in cfg && "password" in cfg;
21
+ };
22
+ var isCloudSqlConfig = (cfg) => {
23
+ return "stream" in cfg || "password" in cfg && typeof cfg.password === "function";
24
+ };
25
+ var validateConfig = (name, config) => {
26
+ if (isConnectionStringConfig(config)) {
27
+ if (!config.connectionString || typeof config.connectionString !== "string" || config.connectionString.trim() === "") {
28
+ throw new Error(
29
+ `${name}: connectionString must be provided and cannot be empty. Passing an empty string may cause fallback to local Postgres defaults.`
30
+ );
31
+ }
32
+ } else if (isCloudSqlConfig(config)) ; else if (isHostConfig(config)) {
33
+ const required = ["host", "database", "user", "password"];
34
+ for (const key of required) {
35
+ if (!config[key] || typeof config[key] !== "string" || config[key].trim() === "") {
36
+ throw new Error(
37
+ `${name}: ${key} must be provided and cannot be empty. Passing an empty string may cause fallback to local Postgres defaults.`
38
+ );
39
+ }
40
+ }
41
+ } else {
42
+ throw new Error(
43
+ `${name}: invalid config. Provide either {connectionString}, {host,port,database,user,password}, or a pg ClientConfig (e.g., Cloud SQL connector with \`stream\`).`
44
+ );
45
+ }
46
+ };
14
47
  var PGFilterTranslator = class extends BaseFilterTranslator {
15
48
  getSupportedOperators() {
16
49
  return {
@@ -367,29 +400,46 @@ var PgVector = class extends MastraVector {
367
400
  vectorExtensionSchema = null;
368
401
  schemaSetupComplete = void 0;
369
402
  cacheWarmupPromise = null;
370
- constructor({
371
- connectionString,
372
- schemaName,
373
- pgPoolOptions
374
- }) {
403
+ constructor(config) {
375
404
  try {
376
- if (!connectionString || connectionString.trim() === "") {
377
- throw new Error(
378
- "PgVector: connectionString must be provided and cannot be empty. Passing an empty string may cause fallback to local Postgres defaults."
379
- );
380
- }
405
+ validateConfig("PgVector", config);
381
406
  super();
382
- this.schema = schemaName;
383
- const basePool = new pg.Pool({
384
- connectionString,
385
- max: 20,
386
- // Maximum number of clients in the pool
387
- idleTimeoutMillis: 3e4,
388
- // Close idle connections after 30 seconds
389
- connectionTimeoutMillis: 2e3,
390
- // Fail fast if can't connect
391
- ...pgPoolOptions
392
- });
407
+ this.schema = config.schemaName;
408
+ let poolConfig;
409
+ if (isConnectionStringConfig(config)) {
410
+ poolConfig = {
411
+ connectionString: config.connectionString,
412
+ ssl: config.ssl,
413
+ max: config.max ?? 20,
414
+ idleTimeoutMillis: config.idleTimeoutMillis ?? 3e4,
415
+ connectionTimeoutMillis: 2e3,
416
+ ...config.pgPoolOptions
417
+ };
418
+ } else if (isCloudSqlConfig(config)) {
419
+ poolConfig = {
420
+ ...config,
421
+ max: config.max ?? 20,
422
+ idleTimeoutMillis: config.idleTimeoutMillis ?? 3e4,
423
+ connectionTimeoutMillis: 2e3,
424
+ ...config.pgPoolOptions
425
+ };
426
+ } else if (isHostConfig(config)) {
427
+ poolConfig = {
428
+ host: config.host,
429
+ port: config.port,
430
+ database: config.database,
431
+ user: config.user,
432
+ password: config.password,
433
+ ssl: config.ssl,
434
+ max: config.max ?? 20,
435
+ idleTimeoutMillis: config.idleTimeoutMillis ?? 3e4,
436
+ connectionTimeoutMillis: 2e3,
437
+ ...config.pgPoolOptions
438
+ };
439
+ } else {
440
+ throw new Error("PgVector: invalid configuration provided");
441
+ }
442
+ const basePool = new pg.Pool(poolConfig);
393
443
  const telemetry = this.__getTelemetry();
394
444
  this.pool = telemetry?.traceClass(basePool, {
395
445
  spanNamePrefix: "pg-vector",
@@ -423,7 +473,7 @@ var PgVector = class extends MastraVector {
423
473
  domain: ErrorDomain.MASTRA_VECTOR,
424
474
  category: ErrorCategory.THIRD_PARTY,
425
475
  details: {
426
- schemaName: schemaName ?? ""
476
+ schemaName: "schemaName" in config ? config.schemaName ?? "" : ""
427
477
  }
428
478
  },
429
479
  error
@@ -4052,36 +4102,8 @@ var PostgresStore = class extends MastraStorage {
4052
4102
  isConnected = false;
4053
4103
  stores;
4054
4104
  constructor(config) {
4055
- const isConnectionStringConfig = (cfg) => {
4056
- return "connectionString" in cfg;
4057
- };
4058
- const isHostConfig = (cfg) => {
4059
- return "host" in cfg && "database" in cfg && "user" in cfg && "password" in cfg;
4060
- };
4061
- const isCloudSqlConfig = (cfg) => {
4062
- return "stream" in cfg || "password" in cfg && typeof cfg.password === "function";
4063
- };
4064
4105
  try {
4065
- if (isConnectionStringConfig(config)) {
4066
- if (!config.connectionString || typeof config.connectionString !== "string" || config.connectionString.trim() === "") {
4067
- throw new Error(
4068
- "PostgresStore: connectionString must be provided and cannot be empty. Passing an empty string may cause fallback to local Postgres defaults."
4069
- );
4070
- }
4071
- } else if (isCloudSqlConfig(config)) ; else if (isHostConfig(config)) {
4072
- const required = ["host", "database", "user", "password"];
4073
- for (const key of required) {
4074
- if (!config[key] || typeof config[key] !== "string" || config[key].trim() === "") {
4075
- throw new Error(
4076
- `PostgresStore: ${key} must be provided and cannot be empty. Passing an empty string may cause fallback to local Postgres defaults.`
4077
- );
4078
- }
4079
- }
4080
- } else {
4081
- throw new Error(
4082
- "PostgresStore: invalid config. Provide either {connectionString}, {host,port,database,user,password}, or a pg ClientConfig (e.g., Cloud SQL connector with `stream`)."
4083
- );
4084
- }
4106
+ validateConfig("PostgresStore", config);
4085
4107
  super({ name: "PostgresStore" });
4086
4108
  this.schema = config.schemaName || "public";
4087
4109
  if (isConnectionStringConfig(config)) {