@mastra/pg 1.11.0-alpha.0 → 1.11.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
@@ -11733,18 +11733,22 @@ function rowToTrigger(row) {
11733
11733
  if (metadata !== void 0) trigger.metadata = metadata;
11734
11734
  return trigger;
11735
11735
  }
11736
- var SchedulesPG = class extends SchedulesStorage {
11736
+ var SchedulesPG = class _SchedulesPG extends SchedulesStorage {
11737
11737
  #db;
11738
11738
  #client;
11739
11739
  #schema;
11740
+ #skipDefaultIndexes;
11741
+ #indexes;
11740
11742
  /** Tables managed by this domain */
11741
11743
  static MANAGED_TABLES = [TABLE_SCHEDULES, TABLE_SCHEDULE_TRIGGERS];
11742
11744
  constructor(config) {
11743
11745
  super();
11744
- const { client, schemaName, skipDefaultIndexes } = resolvePgConfig(config);
11746
+ const { client, schemaName, skipDefaultIndexes, indexes } = resolvePgConfig(config);
11745
11747
  this.#client = client;
11746
11748
  this.#db = new PgDB({ client, schemaName, skipDefaultIndexes });
11747
11749
  this.#schema = schemaName || "public";
11750
+ this.#skipDefaultIndexes = skipDefaultIndexes;
11751
+ this.#indexes = indexes?.filter((idx) => _SchedulesPG.MANAGED_TABLES.includes(idx.table));
11748
11752
  }
11749
11753
  async init() {
11750
11754
  await this.#db.createTable({
@@ -11755,22 +11759,79 @@ var SchedulesPG = class extends SchedulesStorage {
11755
11759
  tableName: TABLE_SCHEDULE_TRIGGERS,
11756
11760
  schema: TABLE_SCHEMAS[TABLE_SCHEDULE_TRIGGERS]
11757
11761
  });
11762
+ await this.createDefaultIndexes();
11763
+ await this.createCustomIndexes();
11758
11764
  }
11759
- static getExportDDL(schemaName) {
11765
+ /**
11766
+ * Returns default index definitions for the schedules domain.
11767
+ * @param schemaPrefix - Prefix for index names (e.g. "my_schema_" or "")
11768
+ */
11769
+ static getDefaultIndexDefs(schemaPrefix) {
11760
11770
  return [
11771
+ {
11772
+ name: `${schemaPrefix}idx_mastra_schedules_status_next_fire`,
11773
+ table: TABLE_SCHEDULES,
11774
+ columns: ["status", "next_fire_at"]
11775
+ },
11776
+ {
11777
+ name: `${schemaPrefix}idx_mastra_schedule_triggers_schedule_fire`,
11778
+ table: TABLE_SCHEDULE_TRIGGERS,
11779
+ columns: ["schedule_id", "actual_fire_at DESC"]
11780
+ }
11781
+ ];
11782
+ }
11783
+ getDefaultIndexDefinitions() {
11784
+ const schemaPrefix = this.#schema !== "public" ? `${this.#schema}_` : "";
11785
+ return _SchedulesPG.getDefaultIndexDefs(schemaPrefix);
11786
+ }
11787
+ async createDefaultIndexes() {
11788
+ if (this.#skipDefaultIndexes) {
11789
+ return;
11790
+ }
11791
+ for (const indexDef of this.getDefaultIndexDefinitions()) {
11792
+ try {
11793
+ await this.#db.createIndex(indexDef);
11794
+ } catch (error) {
11795
+ this.logger?.warn?.(`Failed to create index ${indexDef.name}:`, error);
11796
+ }
11797
+ }
11798
+ }
11799
+ async createCustomIndexes() {
11800
+ if (!this.#indexes || this.#indexes.length === 0) {
11801
+ return;
11802
+ }
11803
+ for (const indexDef of this.#indexes) {
11804
+ try {
11805
+ await this.#db.createIndex(indexDef);
11806
+ } catch (error) {
11807
+ this.logger?.warn?.(`Failed to create custom index ${indexDef.name}:`, error);
11808
+ }
11809
+ }
11810
+ }
11811
+ static getExportDDL(schemaName) {
11812
+ const statements = [];
11813
+ const parsedSchema = schemaName ? parseSqlIdentifier(schemaName, "schema name") : "";
11814
+ const schemaPrefix = parsedSchema && parsedSchema !== "public" ? `${parsedSchema}_` : "";
11815
+ statements.push(
11761
11816
  generateTableSQL({
11762
11817
  tableName: TABLE_SCHEDULES,
11763
11818
  schema: TABLE_SCHEMAS[TABLE_SCHEDULES],
11764
11819
  schemaName,
11765
11820
  includeAllConstraints: true
11766
- }),
11821
+ })
11822
+ );
11823
+ statements.push(
11767
11824
  generateTableSQL({
11768
11825
  tableName: TABLE_SCHEDULE_TRIGGERS,
11769
11826
  schema: TABLE_SCHEMAS[TABLE_SCHEDULE_TRIGGERS],
11770
11827
  schemaName,
11771
11828
  includeAllConstraints: true
11772
11829
  })
11773
- ];
11830
+ );
11831
+ for (const idx of _SchedulesPG.getDefaultIndexDefs(schemaPrefix)) {
11832
+ statements.push(generateIndexSQL(idx, schemaName));
11833
+ }
11834
+ return statements;
11774
11835
  }
11775
11836
  async dangerouslyClearAll() {
11776
11837
  await this.#db.clearTable({ tableName: TABLE_SCHEDULE_TRIGGERS });