@seedcord/plugins 0.6.1 → 0.7.0-next.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/README.md CHANGED
@@ -1,5 +1,5 @@
1
1
  <p align="center">
2
- <img src="https://raw.githubusercontent.com/seedcord/seedcord/main/assets/banner.png" alt="seedcord" width="100%" />
2
+ <img src="https://cdn.seedcord.org/assets/banner.webp" alt="seedcord" width="100%" />
3
3
  </p>
4
4
 
5
5
  ---
package/dist/index.cjs CHANGED
@@ -27,7 +27,10 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
27
27
 
28
28
  //#endregion
29
29
  require("reflect-metadata");
30
- let _seedcord_services_internal = require("@seedcord/services/internal");
30
+ let _seedcord_errors = require("@seedcord/errors");
31
+ let _seedcord_errors_internal = require("@seedcord/errors/internal");
32
+ let _seedcord_services = require("@seedcord/services");
33
+ let _seedcord_utils = require("@seedcord/utils");
31
34
  let chalk = require("chalk");
32
35
  chalk = __toESM(chalk, 1);
33
36
  let envapt = require("envapt");
@@ -41,11 +44,7 @@ let node_path = require("node:path");
41
44
  node_path = __toESM(node_path, 1);
42
45
  let node_url = require("node:url");
43
46
  let node_util = require("node:util");
44
- let _seedcord_utils = require("@seedcord/utils");
45
47
  let kysely_migration = require("kysely/migration");
46
- let seedcord_internal = require("seedcord/internal");
47
- let node_crypto = require("node:crypto");
48
- let _seedcord_services = require("@seedcord/services");
49
48
 
50
49
  //#region src/mongo/decorators/RegisterMongoModel.ts
51
50
  const ModelMetadataKey = Symbol("db:model");
@@ -139,9 +138,9 @@ var MongoService = class {
139
138
  this.core = core;
140
139
  const ctor = this.constructor;
141
140
  const key = Reflect.getMetadata(ServiceMetadataKey, ctor);
142
- if (!key) throw new _seedcord_services_internal.SeedcordError(seedcord.SeedcordErrorCode.PluginMongoServiceDecoratorMissing, [ctor.name]);
141
+ if (!key) throw new _seedcord_errors_internal.SeedcordError(_seedcord_errors.SeedcordErrorCode.PluginMongoServiceDecoratorMissing, [ctor.name]);
143
142
  const model = Reflect.getMetadata(ModelMetadataKey, ctor);
144
- if (!model) throw new _seedcord_services_internal.SeedcordError(seedcord.SeedcordErrorCode.PluginMongoModelDecoratorMissing, [ctor.name]);
143
+ if (!model) throw new _seedcord_errors_internal.SeedcordError(_seedcord_errors.SeedcordErrorCode.PluginMongoModelDecoratorMissing, [ctor.name]);
145
144
  this.model = model;
146
145
  db._register(key, this);
147
146
  }
@@ -158,7 +157,7 @@ var MongoService = class {
158
157
  var Mongo = class extends seedcord.Plugin {
159
158
  core;
160
159
  options;
161
- logger = new seedcord.Logger("Mongo");
160
+ logger = new _seedcord_services.Logger("Mongo");
162
161
  isInitialised = false;
163
162
  servicesReady = false;
164
163
  uri;
@@ -170,7 +169,7 @@ var Mongo = class extends seedcord.Plugin {
170
169
  * a plugin that starts in an earlier phase).
171
170
  */
172
171
  get services() {
173
- if (!this.servicesReady) throw new _seedcord_services_internal.SeedcordError(seedcord.SeedcordErrorCode.PluginMongoServicesNotReady);
172
+ if (!this.servicesReady) throw new _seedcord_errors_internal.SeedcordError(_seedcord_errors.SeedcordErrorCode.PluginMongoServicesNotReady);
174
173
  return this._services;
175
174
  }
176
175
  hmrHandler;
@@ -179,7 +178,7 @@ var Mongo = class extends seedcord.Plugin {
179
178
  this.core = core;
180
179
  this.options = options;
181
180
  this.uri = options.uri;
182
- this.core.shutdown.addTask(seedcord.ShutdownPhase.ExternalResources, "stop-database", async () => await this.stop(), this.options.timeout);
181
+ this.core.shutdown.addTask(_seedcord_services.ShutdownPhase.ExternalResources, "stop-database", async () => await this.stop(), this.options.timeout);
183
182
  if (!envapt.Envapter.isDevelopment) return;
184
183
  this.hmrHandler = new seedcord.HmrModuleHandler({
185
184
  handlersDir: this.options.dir,
@@ -221,12 +220,12 @@ var Mongo = class extends seedcord.Plugin {
221
220
  tls: true,
222
221
  ssl: true
223
222
  },
224
- ...(0, seedcord.keepDefined)(this.options.connectionOptions ?? {})
223
+ ...(0, _seedcord_utils.keepDefined)(this.options.connectionOptions ?? {})
225
224
  }).then((conn) => {
226
225
  this.logger.info(chalk.default.green.bold(`Connected to MongoDB: ${chalk.default.magenta.bold(conn.connection.name)}`));
227
226
  return conn;
228
227
  }).catch((err) => {
229
- throw new _seedcord_services_internal.SeedcordError(seedcord.SeedcordErrorCode.PluginMongoConnectionFailed, [this.options.name], { cause: err });
228
+ throw new _seedcord_errors_internal.SeedcordError(_seedcord_errors.SeedcordErrorCode.PluginMongoConnectionFailed, [this.options.name], { cause: err });
230
229
  });
231
230
  }
232
231
  clearModels() {
@@ -241,13 +240,13 @@ var Mongo = class extends seedcord.Plugin {
241
240
  if (!this.connection) return;
242
241
  await this.connection.disconnect().then(() => this.logger.info(chalk.default.red.bold("Disconnected from MongoDB"))).catch((err) => {
243
242
  this.logger.error(`Could not disconnect from MongoDB: ${err.message}`);
244
- throw new _seedcord_services_internal.SeedcordError(seedcord.SeedcordErrorCode.PluginMongoDisconnectFailed, { cause: err });
243
+ throw new _seedcord_errors_internal.SeedcordError(_seedcord_errors.SeedcordErrorCode.PluginMongoDisconnectFailed, { cause: err });
245
244
  });
246
245
  }
247
246
  async loadServices() {
248
247
  const servicesDir = this.options.dir;
249
248
  this.logger.info(chalk.default.bold(servicesDir));
250
- await (0, seedcord.traverseDirectory)(servicesDir, (fullPath, rel, mod) => {
249
+ await (0, _seedcord_utils.traverseDirectory)(servicesDir, (fullPath, rel, mod) => {
251
250
  for (const Service of Object.values(mod)) if (this.isServiceClass(Service)) {
252
251
  this.initializeService(Service, rel);
253
252
  this.hmrHandler?.trackHandler(fullPath, Service);
@@ -347,9 +346,9 @@ var KpgService = class {
347
346
  this.core = core;
348
347
  const ctor = this.constructor;
349
348
  const key = Reflect.getMetadata(PgServiceMetadataKey, ctor);
350
- if (!key) throw new _seedcord_services_internal.SeedcordError(seedcord.SeedcordErrorCode.PluginKpgServiceDecoratorMissing, [ctor.name]);
349
+ if (!key) throw new _seedcord_errors_internal.SeedcordError(_seedcord_errors.SeedcordErrorCode.PluginKpgServiceDecoratorMissing, [ctor.name]);
351
350
  const table = Reflect.getMetadata(PgTableMetadataKey, ctor);
352
- if (!table) throw new _seedcord_services_internal.SeedcordError(seedcord.SeedcordErrorCode.PluginKpgServiceTableMissing, [ctor.name]);
351
+ if (!table) throw new _seedcord_errors_internal.SeedcordError(_seedcord_errors.SeedcordErrorCode.PluginKpgServiceTableMissing, [ctor.name]);
353
352
  this.table = table;
354
353
  this.kysely._register(key, this);
355
354
  }
@@ -497,7 +496,7 @@ var KpgMigrationManager = class {
497
496
  case "up":
498
497
  case "down": {
499
498
  const stepCount = steps ?? 1;
500
- if (!Number.isInteger(stepCount) || stepCount < 0) throw new _seedcord_services_internal.SeedcordRangeError(seedcord.SeedcordErrorCode.PluginKpgInvalidStepCount);
499
+ if (!Number.isInteger(stepCount) || stepCount < 0) throw new _seedcord_errors_internal.SeedcordRangeError(_seedcord_errors.SeedcordErrorCode.PluginKpgInvalidStepCount);
501
500
  if (stepCount === 0) {
502
501
  this.logMigrationResults([]);
503
502
  return;
@@ -506,7 +505,7 @@ var KpgMigrationManager = class {
506
505
  await this.runStepwise(stepCount, direction, runner);
507
506
  return;
508
507
  }
509
- default: throw new _seedcord_services_internal.SeedcordError(seedcord.SeedcordErrorCode.PluginKpgUnknownDirection, [direction]);
508
+ default: throw new _seedcord_errors_internal.SeedcordError(_seedcord_errors.SeedcordErrorCode.PluginKpgUnknownDirection, [direction]);
510
509
  }
511
510
  }
512
511
  async migrateUp(options) {
@@ -597,14 +596,14 @@ var KpgMigrationManager = class {
597
596
  return this.createModuleProvider([resolvedTarget]);
598
597
  }
599
598
  const label = Array.isArray(target) ? target.join(", ") : target;
600
- throw new _seedcord_services_internal.SeedcordError(seedcord.SeedcordErrorCode.PluginKpgUnresolvedMigrationsPath, [label]);
599
+ throw new _seedcord_errors_internal.SeedcordError(_seedcord_errors.SeedcordErrorCode.PluginKpgUnresolvedMigrationsPath, [label]);
601
600
  }
602
601
  async createModuleProvider(files) {
603
- if (files.length === 0) throw new _seedcord_services_internal.SeedcordError(seedcord.SeedcordErrorCode.PluginKpgNoMigrationFiles);
602
+ if (files.length === 0) throw new _seedcord_errors_internal.SeedcordError(_seedcord_errors.SeedcordErrorCode.PluginKpgNoMigrationFiles);
604
603
  const comparator = this.ctx.config.nameComparator ?? ((nameA, nameB) => nameA.localeCompare(nameB));
605
604
  const sorted = (await Promise.all(files.map(async (filePath) => {
606
605
  const mod = await import((0, node_url.pathToFileURL)(filePath).href);
607
- if (!this.isMigrationModule(mod)) throw new _seedcord_services_internal.SeedcordError(seedcord.SeedcordErrorCode.PluginKpgInvalidMigrationModule, [filePath]);
606
+ if (!this.isMigrationModule(mod)) throw new _seedcord_errors_internal.SeedcordError(_seedcord_errors.SeedcordErrorCode.PluginKpgInvalidMigrationModule, [filePath]);
608
607
  const { up, down } = mod;
609
608
  return [node_path.default.basename(filePath), {
610
609
  async up(db) {
@@ -658,7 +657,7 @@ var KpgMigrationManager = class {
658
657
  const message = error instanceof Error ? error.message : (0, node_util.inspect)(error);
659
658
  this.ctx.logger.error(`Migration failure: ${message}`);
660
659
  if (error instanceof Error) throw error;
661
- throw new _seedcord_services_internal.SeedcordError(seedcord.SeedcordErrorCode.PluginKpgNonErrorFailure, [message]);
660
+ throw new _seedcord_errors_internal.SeedcordError(_seedcord_errors.SeedcordErrorCode.PluginKpgNonErrorFailure, [message]);
662
661
  }
663
662
  isMigrationModule(value) {
664
663
  if (!value || typeof value !== "object") return false;
@@ -691,7 +690,7 @@ var KpgServiceRegistry = class {
691
690
  }
692
691
  async loadFromDirectory(dir) {
693
692
  this.logger.info(chalk.default.bold(dir));
694
- await (0, seedcord.traverseDirectory)(dir, (fullPath, rel, mod) => {
693
+ await (0, _seedcord_utils.traverseDirectory)(dir, (fullPath, rel, mod) => {
695
694
  for (const Service of Object.values(mod)) if (this.isServiceClass(Service)) {
696
695
  this.initializeService(Service, rel);
697
696
  this.plugin.trackServiceFile(fullPath, Service);
@@ -723,7 +722,7 @@ var KpgServiceRegistry = class {
723
722
  var KyselyPg = class extends seedcord.Plugin {
724
723
  core;
725
724
  options;
726
- logger = new seedcord.Logger("KyselyPg");
725
+ logger = new _seedcord_services.Logger("KyselyPg");
727
726
  isInitialised = false;
728
727
  servicesReady = false;
729
728
  pool = null;
@@ -737,7 +736,7 @@ var KyselyPg = class extends seedcord.Plugin {
737
736
  * Map of all services registered with the plugin, keyed by their decorator name.
738
737
  */
739
738
  get services() {
740
- if (!this.servicesReady) throw new _seedcord_services_internal.SeedcordError(seedcord.SeedcordErrorCode.PluginKpgServicesNotReady);
739
+ if (!this.servicesReady) throw new _seedcord_errors_internal.SeedcordError(_seedcord_errors.SeedcordErrorCode.PluginKpgServicesNotReady);
741
740
  return this.serviceRegistry.map;
742
741
  }
743
742
  constructor(core, options) {
@@ -746,7 +745,7 @@ var KyselyPg = class extends seedcord.Plugin {
746
745
  this.options = options;
747
746
  this.serviceRegistry = new KpgServiceRegistry(this, core, this.logger);
748
747
  this.databaseBootstrapper = new KpgDatabaseBootstrapper(this.logger);
749
- this.core.shutdown.addTask(seedcord.ShutdownPhase.ExternalResources, "stop-kyselypg", async () => await this.stop(), this.options.timeout);
748
+ this.core.shutdown.addTask(_seedcord_services.ShutdownPhase.ExternalResources, "stop-kyselypg", async () => await this.stop(), this.options.timeout);
750
749
  if (!envapt.Envapter.isDevelopment) return;
751
750
  const relPaths = this.options.migrations.path;
752
751
  super.registerCriticalFiles(Array.isArray(relPaths) ? relPaths : [relPaths]);
@@ -797,7 +796,7 @@ var KyselyPg = class extends seedcord.Plugin {
797
796
  await this.testPoolConnection(pool);
798
797
  this.connection = new kysely.Kysely({
799
798
  dialect: new kysely.PostgresDialect({ pool }),
800
- ...(0, seedcord.keepDefined)(this.options.kysely ?? {})
799
+ ...(0, _seedcord_utils.keepDefined)(this.options.kysely ?? {})
801
800
  });
802
801
  this.migrationManager = new KpgMigrationManager({
803
802
  db: this.connection,
@@ -825,12 +824,12 @@ var KyselyPg = class extends seedcord.Plugin {
825
824
  this.logger.info(chalk.default.gray("Closing Postgres pool."));
826
825
  await pool.end().catch((err) => {
827
826
  this.logger.error(`Could not close pg pool: ${err.message}`);
828
- throw new _seedcord_services_internal.SeedcordError(seedcord.SeedcordErrorCode.PluginKpgDisconnectFailed, { cause: err });
827
+ throw new _seedcord_errors_internal.SeedcordError(_seedcord_errors.SeedcordErrorCode.PluginKpgDisconnectFailed, { cause: err });
829
828
  });
830
829
  this.logger.info(chalk.default.red.bold("Disconnected from Postgres"));
831
830
  }
832
831
  /**
833
- * Runs migrations using the supplied options or defaults to `latest`.
832
+ * Runs migrations using the supplied options.
834
833
  *
835
834
  * @param options - Target migration or direction overrides
836
835
  */
@@ -928,68 +927,10 @@ var KyselyPg = class extends seedcord.Plugin {
928
927
  }
929
928
  };
930
929
 
931
- //#endregion
932
- //#region src/shared/throwDatabaseError.ts
933
- const logger = new _seedcord_services.Logger("DatabaseError");
934
- /**
935
- * Wraps an unknown error in a {@link DatabaseError} with a generated UUID for correlation, then
936
- * throws it. Used by `@WrapDatabaseError` to normalize raw database failures.
937
- *
938
- * @param error - The original error or value
939
- * @param message - Fallback message used when `error` is not an `Error`
940
- * @throws A {@link DatabaseError} carrying the message and a fresh UUID
941
- *
942
- * @internal
943
- */
944
- function throwDatabaseError(error, message) {
945
- logger.error("Throwing DatabaseError", error instanceof Error ? error.name : String(error));
946
- throw new seedcord_internal.DatabaseError(error instanceof Error ? error.message : message, (0, node_crypto.randomUUID)());
947
- }
948
-
949
- //#endregion
950
- //#region src/shared/WrapDatabaseError.ts
951
- /**
952
- * Catches and wraps database operation errors.
953
- *
954
- * Wraps non-CustomError exceptions in DatabaseError instances
955
- * with UUID tracking. Should be applied to database service methods.
956
- *
957
- * @typeParam TypeReturn - The return type of the decorated method
958
- * @param errorMessage - Message to include when wrapping errors
959
- * @decorator
960
- * @example
961
- * ```typescript
962
- * class UserService extends MongoService<IUser> {
963
- * \@WrapDatabaseError('Failed to find user')
964
- * async findById(id: string) {
965
- * return this.model.findById(id);
966
- * }
967
- * }
968
- * ```
969
- *
970
- * @see {@link DatabaseError}
971
- * @see {@link CustomError}
972
- * @see {@link MongoService}
973
- */
974
- function WrapDatabaseError(errorMessage) {
975
- return function(_target, _propertyKey, descriptor) {
976
- const originalMethod = descriptor.value;
977
- descriptor.value = async function(...args) {
978
- if (!originalMethod) throw new seedcord_internal.SeedcordError(seedcord.SeedcordErrorCode.DecoratorMethodNotFound);
979
- try {
980
- return await originalMethod.apply(this, args);
981
- } catch (error) {
982
- if (!(error instanceof seedcord.CustomError)) throwDatabaseError(error, errorMessage);
983
- else throw error;
984
- }
985
- };
986
- };
987
- }
988
-
989
930
  //#endregion
990
931
  //#region src/index.ts
991
932
  /** Package version */
992
- const version = "0.6.1";
933
+ const version = "0.7.0-next.0";
993
934
 
994
935
  //#endregion
995
936
  exports.KpgService = KpgService;
@@ -999,6 +940,5 @@ exports.MongoService = MongoService;
999
940
  exports.RegisterKpgService = RegisterKpgService;
1000
941
  exports.RegisterMongoModel = RegisterMongoModel;
1001
942
  exports.RegisterMongoService = RegisterMongoService;
1002
- exports.WrapDatabaseError = WrapDatabaseError;
1003
943
  exports.version = version;
1004
944
  //# sourceMappingURL=index.cjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.cjs","names":["SeedcordError","SeedcordErrorCode","Plugin","Logger","SeedcordError","SeedcordErrorCode","ShutdownPhase","Envapter","HmrModuleHandler","SeedcordError","SeedcordErrorCode","Pool","NO_MIGRATIONS","SeedcordRangeError","SeedcordErrorCode","SeedcordError","Migrator","fs","FileMigrationProvider","path","Plugin","Logger","SeedcordError","SeedcordErrorCode","ShutdownPhase","Envapter","HmrModuleHandler","Kysely","PostgresDialect","Pool","Logger","DatabaseError","SeedcordError","SeedcordErrorCode","CustomError"],"sources":["../src/mongo/decorators/RegisterMongoModel.ts","../src/mongo/decorators/RegisterMongoService.ts","../src/mongo/MongoService.ts","../src/mongo/Mongo.ts","../src/kysely-pg/decorators/RegisterKpgService.ts","../src/kysely-pg/KpgService.ts","../src/kysely-pg/KpgDatabaseBootstrapper.ts","../src/kysely-pg/KpgMigrationManager.ts","../src/kysely-pg/KpgServiceRegistry.ts","../src/kysely-pg/KyselyPg.ts","../src/shared/throwDatabaseError.ts","../src/shared/WrapDatabaseError.ts","../src/index.ts"],"sourcesContent":["import mongoose from 'mongoose';\n\nimport type { MongoServiceKeys } from '../types/MongoServices';\n\nexport const ModelMetadataKey = Symbol('db:model');\n\n/**\n * Associates a Mongoose model with a database service\n *\n * Creates a Mongoose model from the decorated static schema property and stores it\n * for service registration. The model becomes available as `this.model` in the service.\n * Must be applied to a `public static schema` property in the service class.\n *\n * @typeParam TService - The service key type\n * @param collection - Collection name for the Mongoose model\n * @decorator\n * @example\n * ```typescript\n * \\@RegisterMongoService('users')\n * export class Users extends MongoService<IUser> {\n * \\@RegisterMongoModel('users')\n * public static schema = new mongoose.Schema<IUser>({\n * username: { type: String, required: true, unique: true }\n * });\n * }\n * ```\n */\nexport function RegisterMongoModel<TService extends MongoServiceKeys>(collection: TService) {\n return <\n SchemaObj extends Record<KeyOfSchema, mongoose.Schema>,\n KeyOfSchema extends keyof SchemaObj & (string | symbol)\n >(\n target: SchemaObj,\n propertyKey: KeyOfSchema\n ): void => {\n const schema = target[propertyKey];\n const name = String(collection);\n const model = mongoose.model(name, schema);\n Reflect.defineMetadata(ModelMetadataKey, model, target);\n };\n}\n","import type { MongoService } from '../MongoService';\nimport type { MongoServiceKeys } from '../types/MongoServices';\nimport type { Constructor } from 'type-fest';\n\nexport const ServiceMetadataKey = Symbol('db:serviceKey');\n\n/**\n * Registers a database service with a typed key\n *\n * Associates a service class with a key for dependency injection.\n * The service becomes available via `core.db.services[key]`.\n *\n * @typeParam TService - The service key type\n * @param key - Service key for registration and type-safe access\n * @decorator\n * @example\n * ```typescript\n * \\@RegisterMongoService('users')\n * export class Users<Doc extends IUser = IUser> extends MongoService<Doc> {\n * // Some code\n * }\n * ```\n */\nexport function RegisterMongoService<TService extends MongoServiceKeys>(key: TService) {\n return <DatabaseCtor extends Constructor<unknown> & { prototype: MongoService }>(ctor: DatabaseCtor): void => {\n Reflect.defineMetadata(ServiceMetadataKey, key, ctor);\n };\n}\n","import { SeedcordError } from '@seedcord/services/internal';\nimport { SeedcordErrorCode } from 'seedcord';\n\nimport { ModelMetadataKey } from './decorators/RegisterMongoModel';\nimport { ServiceMetadataKey } from './decorators/RegisterMongoService';\n\nimport type { Mongo } from './Mongo';\nimport type { MongoDocument } from './types/MongoDocument';\nimport type { TypedConstructor } from '@seedcord/types';\nimport type mongoose from 'mongoose';\nimport type { Core } from 'seedcord';\n\n/**\n * Base class for MongoDB service layers\n *\n * Provides typed access to MongoDB collections through Mongoose models.\n * Services are automatically registered with the Mongo plugin when instantiated.\n *\n * @typeParam Doc - The document type this service manages\n * @example\n * ```typescript\n * \\@RegisterMongoService('users')\n * export class Users extends MongoService<IUser> {\n * \\@RegisterMongoModel('users')\n * public static schema = new mongoose.Schema<IUser>({\n * username: { type: String, required: true, unique: true }\n * });\n *\n * // Custom methods here\n * public async findByUsername(username: string) {\n * return this.model.findOne({ username });\n * }\n * }\n * ```\n */\nexport abstract class MongoService<Doc extends MongoDocument = MongoDocument> {\n public readonly model: mongoose.Model<Doc>;\n\n public constructor(\n protected readonly db: Mongo,\n protected readonly core: Core\n ) {\n const ctor = this.constructor;\n\n const key = Reflect.getMetadata(ServiceMetadataKey, ctor) as string | undefined;\n if (!key) {\n throw new SeedcordError(SeedcordErrorCode.PluginMongoServiceDecoratorMissing, [ctor.name]);\n }\n\n const model = Reflect.getMetadata(ModelMetadataKey, ctor) as mongoose.Model<Doc> | undefined;\n if (!model) {\n throw new SeedcordError(SeedcordErrorCode.PluginMongoModelDecoratorMissing, [ctor.name]);\n }\n\n this.model = model;\n\n db._register(key, this);\n }\n}\n\n/** Constructor type for {@link MongoService} classes */\nexport type MongoServiceConstructor = TypedConstructor<typeof MongoService>;\n","import 'reflect-metadata';\n\nimport { SeedcordError } from '@seedcord/services/internal';\nimport chalk from 'chalk';\nimport { Envapter } from 'envapt';\nimport mongoose from 'mongoose';\nimport {\n HmrModuleHandler,\n keepDefined,\n Logger,\n Plugin,\n SeedcordErrorCode,\n ShutdownPhase,\n traverseDirectory\n} from 'seedcord';\n\nimport { ModelMetadataKey } from './decorators/RegisterMongoModel';\nimport { ServiceMetadataKey } from './decorators/RegisterMongoService';\nimport { MongoService } from './MongoService';\n\nimport type { MongoServiceConstructor } from './MongoService';\nimport type { MongoOptions } from './types/MongoOptions';\nimport type { MongoServices } from './types/MongoServices';\nimport type { HmrUpdateEvent } from '@seedcord/types/internal';\nimport type { Mongoose } from 'mongoose';\nimport type { Core } from 'seedcord';\n\ninterface MongoArtifact {\n key?: string;\n modelName?: string;\n}\n\n/**\n * MongoDB integration plugin for Seedcord.\n *\n * Manages MongoDB connections, service loading, and provides type-safe\n * access to database services through service registration decorators.\n */\nexport class Mongo extends Plugin {\n public readonly logger = new Logger('Mongo');\n private isInitialised = false;\n private servicesReady = false;\n private readonly uri: string;\n\n private readonly _services: Record<string, unknown> = {};\n\n /**\n * Map of all loaded services. Keys come from `@RegisterMongoService('key')`.\n *\n * @throws A {@link SeedcordError} if accessed before the plugin finishes initializing (e.g. from\n * a plugin that starts in an earlier phase).\n */\n public get services(): MongoServices {\n if (!this.servicesReady) {\n throw new SeedcordError(SeedcordErrorCode.PluginMongoServicesNotReady);\n }\n // MongoServices is augmented per-consumer via declaration merging; the registry holds the\n // instances opaquely and exposes the generated map shape at this public boundary.\n return this._services;\n }\n\n /** Exposed Mongoose instance once `init` completes. */\n declare public connection: Mongoose;\n private readonly hmrHandler?: HmrModuleHandler<MongoServiceConstructor, void, MongoArtifact>;\n\n constructor(\n public readonly core: Core,\n private readonly options: MongoOptions\n ) {\n super(core);\n this.uri = options.uri;\n\n this.core.shutdown.addTask(\n ShutdownPhase.ExternalResources,\n 'stop-database',\n async () => await this.stop(),\n this.options.timeout\n );\n\n if (!Envapter.isDevelopment) return;\n this.hmrHandler = new HmrModuleHandler({\n handlersDir: this.options.dir,\n isHandler: this.isServiceClass.bind(this),\n registerHandler: this.initializeService.bind(this),\n unregisterHandler: this.unregister.bind(this),\n getArtifacts: this.getArtifacts.bind(this),\n logger: this.logger,\n name: 'Mongo'\n });\n }\n\n private getArtifacts(ctor: MongoServiceConstructor): MongoArtifact {\n const key = Reflect.getMetadata(ServiceMetadataKey, ctor) as string | undefined;\n const model = Reflect.getMetadata(ModelMetadataKey, ctor) as mongoose.Model<unknown> | undefined;\n return {\n ...(key ? { key } : {}),\n ...(model?.modelName ? { modelName: model.modelName } : {})\n };\n }\n\n /** @internal For use in dev mode */\n public override async onHmr(event: HmrUpdateEvent): Promise<void> {\n await this.hmrHandler?.handle(event);\n }\n\n public async init(): Promise<void> {\n if (this.isInitialised) return;\n this.isInitialised = true;\n\n await this.connect();\n await this.loadServices();\n this.servicesReady = true;\n }\n\n public async stop(): Promise<void> {\n await this.disconnect();\n }\n\n private async connect(): Promise<void> {\n this.clearModels();\n this.connection = await mongoose\n .connect(this.uri, {\n dbName: this.options.name,\n ...(Envapter.isProduction && { tls: true, ssl: true }),\n ...keepDefined(this.options.connectionOptions ?? {})\n })\n .then((conn) => {\n this.logger.info(chalk.green.bold(`Connected to MongoDB: ${chalk.magenta.bold(conn.connection.name)}`));\n return conn;\n })\n .catch((err) => {\n throw new SeedcordError(SeedcordErrorCode.PluginMongoConnectionFailed, [this.options.name], {\n cause: err\n });\n });\n }\n\n private clearModels(): void {\n const modelNames = Object.keys(mongoose.models);\n if (modelNames.length > 0) {\n this.logger.debug(`Clearing ${modelNames.length} mongoose models`);\n for (const name of modelNames) mongoose.deleteModel(name);\n }\n }\n\n private async disconnect(): Promise<void> {\n this.clearModels();\n\n // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition -- connect() may have failed before assigning, so there is nothing to disconnect\n if (!this.connection) return;\n\n await this.connection\n .disconnect()\n .then(() => this.logger.info(chalk.red.bold('Disconnected from MongoDB')))\n .catch((err) => {\n this.logger.error(`Could not disconnect from MongoDB: ${(err as Error).message}`);\n throw new SeedcordError(SeedcordErrorCode.PluginMongoDisconnectFailed, { cause: err });\n });\n }\n\n private async loadServices(): Promise<void> {\n const servicesDir = this.options.dir;\n this.logger.info(chalk.bold(servicesDir));\n\n await traverseDirectory(\n servicesDir,\n (fullPath, rel, mod) => {\n for (const Service of Object.values(mod)) {\n if (this.isServiceClass(Service)) {\n this.initializeService(Service, rel);\n this.hmrHandler?.trackHandler(fullPath, Service);\n }\n }\n },\n this.logger\n );\n\n this.logger.utils.list(\n [`${chalk.magenta(Object.keys(this._services).length)} services`],\n chalk.bold.green('Loaded')\n );\n }\n\n private initializeService(Service: MongoServiceConstructor, relativePath: string): void {\n const instance = new Service(this, this.core);\n this.logger.utils.registration(instance.constructor.name, relativePath);\n }\n\n private isServiceClass(obj: unknown): obj is MongoServiceConstructor {\n return (\n typeof obj === 'function' &&\n obj.prototype instanceof MongoService &&\n Reflect.hasMetadata(ServiceMetadataKey, obj)\n );\n }\n\n /**\n * Register hook used by decorated services.\n *\n * @internal\n */\n _register(key: string, instance: unknown): void {\n this._services[key] = instance;\n }\n\n private unregister(Service: MongoServiceConstructor, artifacts?: { key?: string; modelName?: string }): void {\n const key = artifacts?.key ?? (Reflect.getMetadata(ServiceMetadataKey, Service) as string | undefined);\n const modelName =\n artifacts?.modelName ??\n (Reflect.getMetadata(ModelMetadataKey, Service) as mongoose.Model<unknown> | undefined)?.modelName;\n\n if (key && this._services[key]) {\n // eslint-disable-next-line @typescript-eslint/no-dynamic-delete -- key is a runtime service name, not a static property\n delete this._services[key];\n }\n\n if (modelName) {\n mongoose.deleteModel(modelName);\n }\n }\n}\n","import type { KpgServiceRegistrationOptions } from '../types/KpgServiceRegistrationOptions';\nimport type { KpgServices, KpgServiceKeys } from '../types/KpgServices';\nimport type { Constructor } from 'type-fest';\n\nexport const PgServiceMetadataKey = Symbol('db:pgServiceKey');\nexport const PgTableMetadataKey = Symbol('db:pgTable');\n\n/**\n *\n * Registers a Kysely PG service with the specified key and options.\n *\n * Associates a service class with a key for dependency injection.\n * The service becomes available via `core.db.services[key]`.\n *\n * @typeParam TKey - The service key type\n * @param key - Service key for registration and type-safe access\n * @param options - Additional registration options\n * @decorator\n * @example\n * ```typescript\n * \\@RegisterKpgService('users', { table: 'app_users' })\n * export class UsersService extends KpgService<{ users: IUser }, 'users'> {\n * // Some code\n * }\n * ```\n *\n * @see {@link KpgService}\n */\nexport function RegisterKpgService<TKey extends KpgServiceKeys>(key: TKey, options?: KpgServiceRegistrationOptions) {\n return <Ctor extends Constructor<KpgServices[TKey]>>(ctor: Ctor): void => {\n Reflect.defineMetadata(PgServiceMetadataKey, key, ctor);\n\n const tableName = options?.table ?? String(key);\n Reflect.defineMetadata(PgTableMetadataKey, tableName, ctor);\n };\n}\n","import { SeedcordError } from '@seedcord/services/internal';\nimport { SeedcordErrorCode } from 'seedcord';\n\nimport { PgServiceMetadataKey, PgTableMetadataKey } from './decorators/RegisterKpgService';\n\nimport type { KyselyPg } from './KyselyPg';\nimport type { Kysely } from 'kysely';\nimport type { Core, TypedConstructor } from 'seedcord';\nimport type { LiteralUnion } from 'type-fest';\n\n/**\n * Base class for KyselyPg services.\n *\n * Provides a small, typed shim around the shared Kysely instance and ensures\n * that subclasses have been decorated with `@RegisterKpgService`.\n *\n * @typeParam Database - The database shape used by Kysely (tables as keys).\n * @typeParam TTable - The specific table key from `Database` this service works with.\n *\n * @example\n * ```typescript\n * \\@RegisterKpgService('users')\n * export class UsersService extends KpgService<ImportedDatabaseInterface, 'users'> {\n * public async findById(id: string) {\n * return this.entity\n * .selectFrom(this.table)\n * .selectAll().where('id', '=', id)\n * .executeTakeFirst();\n * }\n * }\n *\n * // Usage inside handlers:\n * const user = await this.core.db.services.users.findById('abc');\n * ```\n */\nexport abstract class KpgService<Database extends object, TTable extends LiteralUnion<keyof Database, string>> {\n public readonly table: TTable;\n\n public constructor(\n protected readonly kysely: KyselyPg<Database>,\n protected readonly core: Core\n ) {\n const ctor = this.constructor;\n\n const key = Reflect.getMetadata(PgServiceMetadataKey, ctor) as string | undefined;\n if (!key) {\n throw new SeedcordError(SeedcordErrorCode.PluginKpgServiceDecoratorMissing, [ctor.name]);\n }\n\n const table = Reflect.getMetadata(PgTableMetadataKey, ctor) as TTable | undefined;\n\n // This check should always pass since TTable is derived from the key if a table is not provided explicitly.\n if (!table) {\n throw new SeedcordError(SeedcordErrorCode.PluginKpgServiceTableMissing, [ctor.name]);\n }\n\n this.table = table;\n this.kysely._register(key, this);\n }\n\n /**\n * Shared Kysely instance used to interact with the Postgres database.\n */\n public get db(): Kysely<Database> {\n return this.kysely.connection;\n }\n}\n\n/** Constructor type for {@link KpgService} classes */\nexport type KyselyServiceConstructor<Database extends object = object> = TypedConstructor<\n typeof KpgService<Database, keyof Database & string>\n>;\n","import chalk from 'chalk';\nimport { Pool, type PoolConfig } from 'pg';\n\nimport type { Logger } from 'seedcord';\n\n/**\n * Ensures the target Postgres database exists, creating it if missing.\n */\nexport class KpgDatabaseBootstrapper {\n private static readonly ADMIN_DB = 'postgres';\n private static readonly DATABASE_EXISTS_SQL =\n 'SELECT EXISTS (SELECT 1 FROM pg_database WHERE datname = $1) AS \"exists\"';\n\n constructor(private readonly logger: Logger) {}\n\n public resolveDatabaseName(config: PoolConfig): string | null {\n return KpgDatabaseBootstrapper.parseDatabaseName(config);\n }\n\n public resolveDatabaseFromPool(pool: Pool): string | null {\n const config: PoolConfig = {};\n\n const { options } = pool;\n\n if (typeof options.database === 'string') {\n config.database = options.database;\n }\n\n if (typeof options.connectionString === 'string') {\n config.connectionString = options.connectionString;\n }\n\n return this.resolveDatabaseName(config);\n }\n\n public async ensure(baseConfig: PoolConfig): Promise<void> {\n const targetDb = this.resolveDatabaseName(baseConfig);\n if (!targetDb) {\n this.logger.info(chalk.gray('Skipping database existence check (no database specified).'));\n return;\n }\n\n if (targetDb === KpgDatabaseBootstrapper.ADMIN_DB) {\n this.logger.info(chalk.gray('Target database is postgres; skipping creation.'));\n return;\n }\n\n const adminConfig = this.buildAdminConfig(baseConfig);\n if (!adminConfig) {\n this.logger.warn(`Unable to derive admin connection when ensuring database ${targetDb}`);\n return;\n }\n\n this.logger.info(chalk.gray(`Ensuring database ${chalk.yellow(targetDb)} exists...`));\n\n const adminPool = new Pool(adminConfig);\n\n try {\n const exists = await this.databaseExists(adminPool, targetDb);\n if (exists) {\n this.logger.info(chalk.gray(`Database ${chalk.yellow(targetDb)} already exists.`));\n return;\n }\n\n await this.createDatabase(adminPool, targetDb);\n this.logger.info(chalk.green(`Created database ${chalk.bold(targetDb)}.`));\n } catch (error) {\n const err = error instanceof Error ? error : new Error(String(error));\n this.logger.error(`Failed to ensure database ${targetDb}: ${err.message}`);\n throw err;\n } finally {\n await adminPool.end();\n }\n }\n\n private buildAdminConfig(baseConfig: PoolConfig): PoolConfig | null {\n const adminConfig: PoolConfig = { ...baseConfig };\n\n const { connectionString } = adminConfig;\n if (connectionString) {\n const connection = KpgDatabaseBootstrapper.applyDatabaseToConnectionString(\n connectionString,\n KpgDatabaseBootstrapper.ADMIN_DB\n );\n if (!connection) return null;\n adminConfig.connectionString = connection;\n }\n\n adminConfig.database = KpgDatabaseBootstrapper.ADMIN_DB;\n return adminConfig;\n }\n\n private async databaseExists(pool: Pool, database: string): Promise<boolean> {\n const client = await pool.connect();\n try {\n const { rows } = await client.query<{ exists: boolean }>(KpgDatabaseBootstrapper.DATABASE_EXISTS_SQL, [\n database\n ]);\n return Boolean(rows[0]?.exists);\n } finally {\n client.release();\n }\n }\n\n private async createDatabase(pool: Pool, database: string): Promise<void> {\n const client = await pool.connect();\n try {\n const createSql = `CREATE DATABASE ${KpgDatabaseBootstrapper.escapeIdentifier(database)}`;\n await client.query(createSql);\n } finally {\n client.release();\n }\n }\n\n private static parseDatabaseName(config: PoolConfig): string | null {\n if (typeof config.database === 'string' && config.database.trim().length > 0) {\n return config.database.trim();\n }\n\n const connectionString = config.connectionString;\n if (!connectionString) return null;\n\n try {\n const url = new URL(connectionString);\n const pathname = url.pathname.replace(/^\\//, '');\n if (!pathname) return null;\n const [candidate] = pathname.split('/');\n return candidate ? decodeURIComponent(candidate) : null;\n } catch {\n return null;\n }\n }\n\n private static applyDatabaseToConnectionString(connectionString: string, database: string): string | null {\n try {\n const url = new URL(connectionString);\n url.pathname = `/${encodeURIComponent(database)}`;\n return url.toString();\n } catch {\n return null;\n }\n }\n\n private static escapeIdentifier(identifier: string): string {\n return `\"${identifier.replace(/\"/g, '\"\"')}\"`;\n }\n}\n","import { promises as fs } from 'node:fs';\nimport path from 'node:path';\nimport { pathToFileURL } from 'node:url';\nimport { inspect } from 'node:util';\n\nimport { SeedcordError, SeedcordRangeError } from '@seedcord/services/internal';\nimport { keepDefined } from '@seedcord/utils';\nimport chalk from 'chalk';\nimport { FileMigrationProvider, Migrator, NO_MIGRATIONS } from 'kysely/migration';\nimport { SeedcordErrorCode } from 'seedcord';\n\nimport type {\n MigrationManagerContext,\n MigrationModule,\n MigrationOptions,\n StepMigrationOptions\n} from './types/KpgMigration';\nimport type {\n Migration,\n MigrationInfo,\n MigrationProvider,\n MigrationResult,\n MigrationResultSet\n} from 'kysely/migration';\nimport type { Stats } from 'node:fs';\n\n/**\n * Migration tooling for KyselyPg.\n *\n * @sealed\n */\nexport class KpgMigrationManager<Database extends object> {\n constructor(private readonly ctx: MigrationManagerContext<Database>) {}\n\n public async migrate(options?: MigrationOptions): Promise<void> {\n const { target, direction = 'latest', steps } = options ?? {};\n\n if (typeof target !== 'undefined') {\n const label = target === NO_MIGRATIONS ? 'NO_MIGRATIONS' : target;\n await this.runMigration((migrator) => migrator.migrateTo(target), `Migrating to ${chalk.yellow(label)}...`);\n return;\n }\n\n switch (direction) {\n case 'latest':\n await this.runMigration((migrator) => migrator.migrateToLatest());\n return;\n case 'up':\n case 'down': {\n const stepCount = steps ?? 1;\n if (!Number.isInteger(stepCount) || stepCount < 0) {\n throw new SeedcordRangeError(SeedcordErrorCode.PluginKpgInvalidStepCount);\n }\n\n if (stepCount === 0) {\n this.logMigrationResults([]);\n return;\n }\n\n const runner =\n direction === 'up'\n ? (migrator: Migrator) => migrator.migrateUp()\n : (migrator: Migrator) => migrator.migrateDown();\n await this.runStepwise(stepCount, direction, runner);\n return;\n }\n default:\n throw new SeedcordError(SeedcordErrorCode.PluginKpgUnknownDirection, [direction]);\n }\n }\n\n public async migrateUp(options?: StepMigrationOptions): Promise<void> {\n if (typeof options?.steps === 'undefined') {\n await this.migrate({ direction: 'up' });\n return;\n }\n\n await this.migrate({ direction: 'up', steps: options.steps });\n }\n\n public async migrateDown(options?: StepMigrationOptions): Promise<void> {\n if (typeof options?.steps === 'undefined') {\n await this.migrate({ direction: 'down' });\n return;\n }\n\n await this.migrate({ direction: 'down', steps: options.steps });\n }\n\n public async listMigrations(): Promise<readonly MigrationInfo[]> {\n const migrator = await this.createMigrator();\n return migrator.getMigrations();\n }\n\n private async runMigration(\n runner: (migrator: Migrator) => Promise<MigrationResultSet>,\n runningMessage = 'Running migrations...'\n ): Promise<void> {\n this.ctx.logger.info(chalk.gray('Preparing migrations...'));\n const migrator = await this.createMigrator();\n\n this.ctx.logger.info(chalk.gray(runningMessage));\n const { error, results } = await runner(migrator);\n\n this.logMigrationResults(results ?? []);\n\n if (error) {\n this.handleMigrationError(error);\n }\n }\n\n private async runStepwise(\n steps: number,\n direction: 'up' | 'down',\n runner: (migrator: Migrator) => Promise<MigrationResultSet>\n ): Promise<void> {\n this.ctx.logger.info(chalk.gray('Preparing migrations...'));\n const migrator = await this.createMigrator();\n\n const directionLabel = direction === 'up' ? 'Running' : 'Reverting';\n const countLabel = steps === 1 ? 'one migration' : `${chalk.yellow(String(steps))} migrations`;\n this.ctx.logger.info(chalk.gray(`${directionLabel} ${countLabel}...`));\n\n const aggregated: MigrationResult[] = [];\n let encounteredError: unknown;\n\n for (let index = 0; index < steps; index += 1) {\n const { error, results } = await runner(migrator);\n\n if (results?.length) {\n aggregated.push(...results);\n }\n\n if (error) {\n encounteredError = error;\n break;\n }\n\n if (!results?.length) {\n break;\n }\n }\n\n this.logMigrationResults(aggregated);\n\n if (encounteredError) {\n this.handleMigrationError(encounteredError);\n }\n }\n\n private async createMigrator(): Promise<Migrator> {\n const provider = await this.getMigrationProvider();\n const { config } = this.ctx;\n\n return new Migrator({\n db: this.ctx.db,\n provider,\n allowUnorderedMigrations: config.allowUnorderedMigrations ?? false,\n ...keepDefined(config, 'migrationTableName', 'migrationLockTableName', 'migrationTableSchema')\n });\n }\n\n private async getMigrationProvider(): Promise<MigrationProvider> {\n const { path: target } = this.ctx.config;\n const resolvedTarget = Array.isArray(target)\n ? target.map((entry) => this.resolvePath(entry))\n : this.resolvePath(target);\n\n if (Array.isArray(resolvedTarget)) {\n this.logMigrationFiles(resolvedTarget);\n return this.createModuleProvider(resolvedTarget);\n }\n\n let migrationStat: Stats | undefined;\n try {\n migrationStat = await fs.stat(resolvedTarget);\n } catch {\n migrationStat = undefined;\n }\n\n if (migrationStat?.isDirectory()) {\n const directory = this.relativePath(resolvedTarget);\n this.ctx.logger.info(chalk.gray(`Loading migrations directory ${chalk.yellow(directory)}`));\n return new FileMigrationProvider({ fs, path, migrationFolder: resolvedTarget });\n }\n\n if (migrationStat?.isFile() ?? true) {\n this.logMigrationFiles([resolvedTarget]);\n return this.createModuleProvider([resolvedTarget]);\n }\n\n const label = Array.isArray(target) ? target.join(', ') : target;\n throw new SeedcordError(SeedcordErrorCode.PluginKpgUnresolvedMigrationsPath, [label]);\n }\n\n private async createModuleProvider(files: string[]): Promise<MigrationProvider> {\n if (files.length === 0) {\n throw new SeedcordError(SeedcordErrorCode.PluginKpgNoMigrationFiles);\n }\n\n const comparator =\n this.ctx.config.nameComparator ?? ((nameA: string, nameB: string) => nameA.localeCompare(nameB));\n\n const entries = await Promise.all(\n files.map(async (filePath) => {\n const moduleUrl = pathToFileURL(filePath).href;\n const mod: unknown = await import(moduleUrl);\n\n if (!this.isMigrationModule(mod)) {\n throw new SeedcordError(SeedcordErrorCode.PluginKpgInvalidMigrationModule, [filePath]);\n }\n\n const { up, down } = mod;\n\n const name = path.basename(filePath);\n\n const migration: Migration = {\n async up(db) {\n await up(db);\n },\n async down(db) {\n await down(db);\n }\n };\n\n return [name, migration] as const;\n })\n );\n\n const sorted = entries.sort(([a], [b]) => comparator(a, b));\n this.logPreparedMigrations(sorted);\n\n return {\n getMigrations: () => Promise.resolve(Object.fromEntries(sorted))\n } satisfies MigrationProvider;\n }\n\n private logMigrationFiles(files: readonly string[]): void {\n if (!files.length) return;\n\n this.ctx.logger.info('Loading migration file(s):');\n for (const file of files) {\n this.ctx.logger.utils.item(`${chalk.yellow(this.relativePath(file))}`);\n }\n }\n\n private logPreparedMigrations(entries: readonly (readonly [string, Migration])[]): void {\n if (!entries.length) return;\n\n this.ctx.logger.info('Prepared migrations:');\n for (const [name] of entries) {\n this.ctx.logger.utils.item(`${chalk.green(name)}`);\n }\n }\n\n private logMigrationResults(results: readonly MigrationResult[]): void {\n if (!results.length) {\n this.ctx.logger.info(chalk.gray('No migrations executed.'));\n return;\n }\n\n this.ctx.logger.info('Migration results:');\n\n for (const result of results) {\n if (result.status === 'Success') {\n this.ctx.logger.info(`${chalk.green('✓')} ${chalk.bold(result.migrationName)}`);\n continue;\n }\n\n if (result.status === 'Error') {\n this.ctx.logger.error(`${chalk.red('✗')} ${chalk.bold(result.migrationName)}`);\n continue;\n }\n\n this.ctx.logger.info(`${chalk.yellow('•')} ${chalk.bold(result.migrationName)} ${chalk.gray('(skipped)')}`);\n }\n }\n\n private relativePath(filePath: string): string {\n const relative = path.relative(this.ctx.baseDir, filePath);\n return relative.startsWith('..') ? filePath : relative;\n }\n\n private resolvePath(target: string): string {\n if (path.isAbsolute(target)) return target;\n return path.resolve(this.ctx.baseDir, target);\n }\n\n private handleMigrationError(error: unknown): never {\n const message = error instanceof Error ? error.message : inspect(error);\n this.ctx.logger.error(`Migration failure: ${message}`);\n\n if (error instanceof Error) {\n throw error;\n }\n\n throw new SeedcordError(SeedcordErrorCode.PluginKpgNonErrorFailure, [message]);\n }\n\n private isMigrationModule(value: unknown): value is MigrationModule {\n if (!value || typeof value !== 'object') return false;\n if (!('up' in value) || !('down' in value)) return false;\n\n const { up, down } = value;\n\n return typeof up === 'function' && typeof down === 'function';\n }\n}\n","import chalk from 'chalk';\nimport { traverseDirectory } from 'seedcord';\n\nimport { PgServiceMetadataKey } from './decorators/RegisterKpgService';\nimport { KpgService } from './KpgService';\n\nimport type { KyselyServiceConstructor } from './KpgService';\nimport type { KyselyArtifact, KyselyPg } from './KyselyPg';\nimport type { KpgServices } from './types/KpgServices';\nimport type { Core, Logger } from 'seedcord';\n\n/**\n * Discovers and registers Postgres services for the plugin.\n */\nexport class KpgServiceRegistry<Database extends object> {\n private readonly services: Record<string, unknown> = Object.create(null) as Record<string, unknown>;\n\n constructor(\n private readonly plugin: KyselyPg<Database>,\n private readonly core: Core,\n private readonly logger: Logger\n ) {}\n\n public get map(): KpgServices {\n // KpgServices is augmented per-consumer via declaration merging; the registry holds the\n // instances opaquely and exposes the generated map shape at this boundary.\n return this.services;\n }\n\n public register(key: string, instance: unknown): void {\n this.services[key] = instance;\n }\n\n public async loadFromDirectory(dir: string): Promise<void> {\n this.logger.info(chalk.bold(dir));\n\n await traverseDirectory(\n dir,\n (fullPath, rel, mod) => {\n for (const Service of Object.values(mod)) {\n if (this.isServiceClass(Service)) {\n this.initializeService(Service, rel);\n this.plugin.trackServiceFile(fullPath, Service);\n }\n }\n },\n this.logger\n );\n\n this.logger.utils.list(\n [`${chalk.magenta(Object.keys(this.services).length)} services`],\n chalk.bold.green('Loaded')\n );\n }\n\n public unregister(Service: KyselyServiceConstructor<Database>, artifacts?: KyselyArtifact): void {\n const key = artifacts?.key ?? (Reflect.getMetadata(PgServiceMetadataKey, Service) as string | undefined);\n if (key && this.services[key]) {\n // eslint-disable-next-line @typescript-eslint/no-dynamic-delete -- key is a runtime service name, not a static property\n delete this.services[key];\n }\n }\n\n public initializeService(Service: KyselyServiceConstructor<Database>, relativePath: string): void {\n const instance = new Service(this.plugin, this.core);\n this.logger.utils.registration(instance.constructor.name, relativePath);\n }\n\n public isServiceClass(obj: unknown): obj is KyselyServiceConstructor<Database> {\n return (\n typeof obj === 'function' &&\n obj.prototype instanceof KpgService &&\n Reflect.hasMetadata(PgServiceMetadataKey, obj)\n );\n }\n}\n","import 'reflect-metadata';\n\nimport { SeedcordError } from '@seedcord/services/internal';\nimport chalk from 'chalk';\nimport { Envapter } from 'envapt';\nimport { Kysely, PostgresDialect } from 'kysely';\nimport { Pool, type PoolConfig, type PoolClient } from 'pg';\nimport { HmrModuleHandler, keepDefined, Logger, Plugin, SeedcordErrorCode, ShutdownPhase } from 'seedcord';\n\nimport { PgServiceMetadataKey } from './decorators/RegisterKpgService';\nimport { KpgDatabaseBootstrapper } from './KpgDatabaseBootstrapper';\nimport { KpgMigrationManager } from './KpgMigrationManager';\nimport { KpgServiceRegistry } from './KpgServiceRegistry';\n\nimport type { KyselyServiceConstructor } from './KpgService';\nimport type { MigrationOptions, StepMigrationOptions } from './types/KpgMigration';\nimport type { KpgOptions } from './types/KpgOptions';\nimport type { KpgServices } from './types/KpgServices';\nimport type { HmrUpdateEvent } from '@seedcord/types/internal';\nimport type { MigrationInfo } from 'kysely/migration';\nimport type { Core } from 'seedcord';\n\nexport interface KyselyArtifact {\n key?: string;\n}\n\n/**\n * Postgres plugin using Kysely.\n *\n * Handles setting up the connection pool, applying migrations, and\n * registering decorated services so they can be resolved from the core.\n */\nexport class KyselyPg<Database extends object> extends Plugin {\n public readonly logger = new Logger('KyselyPg');\n private isInitialised = false;\n private servicesReady = false;\n\n /** Exposed Kysely instance once `init` completes. */\n declare public connection: Kysely<Database>;\n private pool: Pool | null = null;\n private onConnectHandler: ((client: PoolClient) => void) | null = null;\n private migrationManager: KpgMigrationManager<Database> | null = null;\n private readonly serviceRegistry: KpgServiceRegistry<Database>;\n private readonly databaseBootstrapper: KpgDatabaseBootstrapper;\n private databaseName: string | null = null;\n private readonly hmrHandler?: HmrModuleHandler<KyselyServiceConstructor<Database>, void, KyselyArtifact>;\n\n /**\n * Map of all services registered with the plugin, keyed by their decorator name.\n */\n public get services(): KpgServices {\n if (!this.servicesReady) {\n throw new SeedcordError(SeedcordErrorCode.PluginKpgServicesNotReady);\n }\n return this.serviceRegistry.map;\n }\n\n constructor(\n public readonly core: Core,\n private readonly options: KpgOptions\n ) {\n super(core);\n this.serviceRegistry = new KpgServiceRegistry(this, core, this.logger);\n this.databaseBootstrapper = new KpgDatabaseBootstrapper(this.logger);\n this.core.shutdown.addTask(\n ShutdownPhase.ExternalResources,\n 'stop-kyselypg',\n async () => await this.stop(),\n this.options.timeout\n );\n\n if (!Envapter.isDevelopment) return; // HMR only in development\n\n const relPaths = this.options.migrations.path;\n super.registerCriticalFiles(Array.isArray(relPaths) ? relPaths : [relPaths]);\n\n this.hmrHandler = new HmrModuleHandler({\n handlersDir: this.options.dir,\n isHandler: this.serviceRegistry.isServiceClass.bind(this.serviceRegistry),\n registerHandler: this.serviceRegistry.initializeService.bind(this.serviceRegistry),\n unregisterHandler: this.serviceRegistry.unregister.bind(this.serviceRegistry),\n getArtifacts: this.getArtifacts.bind(this),\n logger: this.logger,\n name: 'KyselyPg'\n });\n }\n\n private getArtifacts(ctor: KyselyServiceConstructor<Database>): KyselyArtifact {\n const key = Reflect.getMetadata(PgServiceMetadataKey, ctor) as string | undefined;\n return key ? { key } : {};\n }\n\n /** @internal For use in dev mode */\n public override async onHmr(event: HmrUpdateEvent): Promise<void> {\n await this.hmrHandler?.handle(event);\n }\n\n /**\n * Connects to Postgres, runs any startup migrations, and loads decorated services.\n *\n * Safe to call multiple times; subsequent calls exit early.\n */\n public async init(): Promise<void> {\n if (this.isInitialised) return;\n this.isInitialised = true;\n\n await this.connect();\n\n const startupConfig = this.options.migrations.onStartup;\n if (startupConfig !== false) {\n if (startupConfig && typeof startupConfig !== 'boolean') {\n await this.migrate(startupConfig);\n } else {\n await this.migrate();\n }\n }\n await this.serviceRegistry.loadFromDirectory(this.options.dir);\n this.servicesReady = true;\n }\n\n /**\n * Tears down the connection pool and clears the migration manager reference.\n */\n public async stop(): Promise<void> {\n await this.disconnect();\n }\n\n private async connect(): Promise<void> {\n const pool = await this.resolvePool();\n this.pool = pool;\n\n this.registerOnConnectStatements(pool, this.options.onConnectSQL);\n\n try {\n await this.testPoolConnection(pool);\n\n this.connection = new Kysely<Database>({\n dialect: new PostgresDialect({ pool }),\n ...keepDefined(this.options.kysely ?? {})\n });\n\n this.migrationManager = new KpgMigrationManager({\n db: this.connection,\n logger: this.logger,\n config: this.options.migrations,\n baseDir: process.cwd()\n });\n\n const dbLabel = this.databaseName ?? 'unknown';\n this.logger.info(`Connected to Postgres database ${chalk.bold.magenta(dbLabel)}`);\n } catch (err) {\n const error = err instanceof Error ? err : new Error(String(err));\n this.logger.error(`Could not connect to Postgres: ${error.message}`);\n throw error;\n }\n }\n\n private async disconnect(): Promise<void> {\n const pool = this.pool;\n if (!pool) return;\n\n if (this.onConnectHandler) {\n pool.removeListener('connect', this.onConnectHandler);\n this.onConnectHandler = null;\n }\n\n this.pool = null;\n this.migrationManager = null;\n\n this.logger.info(chalk.gray('Closing Postgres pool.'));\n await pool.end().catch((err) => {\n this.logger.error(`Could not close pg pool: ${(err as Error).message}`);\n throw new SeedcordError(SeedcordErrorCode.PluginKpgDisconnectFailed, { cause: err });\n });\n this.logger.info(chalk.red.bold('Disconnected from Postgres'));\n }\n\n /**\n * Runs migrations using the supplied options or defaults to `latest`.\n *\n * @param options - Target migration or direction overrides\n */\n public async migrate(options?: MigrationOptions): Promise<void> {\n await this.getMigrationManager().migrate(options);\n }\n\n /**\n * Runs a single upwards migration step unless a custom count is provided.\n *\n * @param options - Optional configuration for step-based execution\n */\n public async migrateUp(options?: StepMigrationOptions): Promise<void> {\n await this.getMigrationManager().migrateUp(options);\n }\n\n /**\n * Runs a single downwards migration step unless a custom count is provided.\n *\n * @param options - Optional configuration for step-based execution\n */\n public async migrateDown(options?: StepMigrationOptions): Promise<void> {\n await this.getMigrationManager().migrateDown(options);\n }\n\n /**\n * Lists every migration registered with the manager along with its execution state.\n */\n public listMigrations(): Promise<readonly MigrationInfo[]> {\n return this.getMigrationManager().listMigrations();\n }\n\n /**\n * Lists unapplied migrations.\n */\n public async listPendingMigrations(): Promise<MigrationInfo[]> {\n const all = await this.listMigrations();\n return all.filter((m) => !m.executedAt);\n }\n\n private getMigrationManager(): KpgMigrationManager<Database> {\n if (this.migrationManager) return this.migrationManager;\n\n const manager = new KpgMigrationManager({\n db: this.connection,\n logger: this.logger,\n config: this.options.migrations,\n baseDir: process.cwd()\n });\n\n this.migrationManager = manager;\n return manager;\n }\n\n /**\n * Register hook used by decorated services.\n *\n * @internal\n */\n _register(key: string, instance: unknown): void {\n this.serviceRegistry.register(key, instance);\n }\n\n /**\n * Tracks a service file with the HMR handler so dev reloads can swap it. No-op outside dev.\n *\n * @internal Lets {@link KpgServiceRegistry} reach the dev-only HMR handler without poking a\n * private field.\n */\n public trackServiceFile(filePath: string, ctor: KyselyServiceConstructor<Database>): void {\n this.hmrHandler?.trackHandler(filePath, ctor);\n }\n\n private async resolvePool(): Promise<Pool> {\n const { pool: providedPool, connectionString } = this.options;\n\n if (providedPool instanceof Pool) {\n this.logger.info(chalk.gray('Reusing provided Postgres pool instance.'));\n this.databaseName = this.databaseBootstrapper.resolveDatabaseFromPool(providedPool);\n return providedPool;\n }\n\n const baseConfig = this.createPoolConfig(providedPool, connectionString);\n await this.databaseBootstrapper.ensure(baseConfig);\n this.databaseName = this.databaseBootstrapper.resolveDatabaseName(baseConfig);\n\n this.logger.info(chalk.gray('Creating new Postgres pool.'));\n return new Pool(baseConfig);\n }\n\n private createPoolConfig(poolConfig?: PoolConfig, connectionString?: string): PoolConfig {\n const config: PoolConfig = poolConfig ? { ...poolConfig } : {};\n\n if (connectionString) {\n config.connectionString = connectionString;\n }\n\n if (this.options.forceInsecureSSL) {\n config.ssl = { rejectUnauthorized: false };\n }\n\n return config;\n }\n\n private registerOnConnectStatements(pool: Pool, statements?: string[]): void {\n if (!statements?.length) return;\n\n const queuedStatements = [...statements];\n const handler = (client: PoolClient): void => {\n void (async () => {\n for (const sql of queuedStatements) {\n await client.query(sql);\n }\n })().catch((err) => this.logger.error('Failed to run onConnect SQL', err));\n };\n\n this.onConnectHandler = handler;\n pool.on('connect', handler);\n }\n\n private async testPoolConnection(pool: Pool): Promise<void> {\n const client = await pool.connect();\n client.release();\n }\n}\n","import { randomUUID } from 'node:crypto';\n\nimport { Logger } from '@seedcord/services';\nimport { DatabaseError } from 'seedcord/internal';\n\nconst logger = new Logger('DatabaseError');\n\n/**\n * Wraps an unknown error in a {@link DatabaseError} with a generated UUID for correlation, then\n * throws it. Used by `@WrapDatabaseError` to normalize raw database failures.\n *\n * @param error - The original error or value\n * @param message - Fallback message used when `error` is not an `Error`\n * @throws A {@link DatabaseError} carrying the message and a fresh UUID\n *\n * @internal\n */\nexport function throwDatabaseError(error: unknown, message: string): never {\n logger.error('Throwing DatabaseError', error instanceof Error ? error.name : String(error));\n const errorMessage = error instanceof Error ? error.message : message;\n throw new DatabaseError(errorMessage, randomUUID());\n}\n","import { CustomError, SeedcordErrorCode } from 'seedcord';\nimport { SeedcordError } from 'seedcord/internal';\n\nimport { throwDatabaseError } from './throwDatabaseError';\n\n/**\n * Catches and wraps database operation errors.\n *\n * Wraps non-CustomError exceptions in DatabaseError instances\n * with UUID tracking. Should be applied to database service methods.\n *\n * @typeParam TypeReturn - The return type of the decorated method\n * @param errorMessage - Message to include when wrapping errors\n * @decorator\n * @example\n * ```typescript\n * class UserService extends MongoService<IUser> {\n * \\@WrapDatabaseError('Failed to find user')\n * async findById(id: string) {\n * return this.model.findById(id);\n * }\n * }\n * ```\n *\n * @see {@link DatabaseError}\n * @see {@link CustomError}\n * @see {@link MongoService}\n */\nexport function WrapDatabaseError<TypeReturn>(errorMessage: string) {\n return function (\n _target: unknown,\n _propertyKey: string,\n descriptor: TypedPropertyDescriptor<(...args: any[]) => Promise<TypeReturn>>\n ): void {\n const originalMethod = descriptor.value;\n\n descriptor.value = async function (...args: any[]): Promise<TypeReturn> {\n if (!originalMethod) {\n throw new SeedcordError(SeedcordErrorCode.DecoratorMethodNotFound);\n }\n\n try {\n return await originalMethod.apply(this, args);\n } catch (error) {\n if (!(error instanceof CustomError)) {\n throwDatabaseError(error, errorMessage);\n } else {\n throw error;\n }\n }\n };\n };\n}\n","export * from './mongo';\nexport * from './kysely-pg';\nexport * from './shared';\n\n/** Package version */\nexport const version = process.env.PACKAGE_VERSION ?? '0.0.0';\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAIA,MAAa,mBAAmB,OAAO,UAAU;;;;;;;;;;;;;;;;;;;;;;AAuBjD,SAAgB,mBAAsD,YAAsB;CACxF,QAII,QACA,gBACO;EACP,MAAM,SAAS,OAAO;EACtB,MAAM,OAAO,OAAO,UAAU;EAC9B,MAAM,QAAQ,iBAAS,MAAM,MAAM,MAAM;EACzC,QAAQ,eAAe,kBAAkB,OAAO,MAAM;CAC1D;AACJ;;;;ACpCA,MAAa,qBAAqB,OAAO,eAAe;;;;;;;;;;;;;;;;;;AAmBxD,SAAgB,qBAAwD,KAAe;CACnF,QAAiF,SAA6B;EAC1G,QAAQ,eAAe,oBAAoB,KAAK,IAAI;CACxD;AACJ;;;;;;;;;;;;;;;;;;;;;;;;;;;ACQA,IAAsB,eAAtB,MAA8E;CAInD;CACA;CAJvB,AAAgB;CAEhB,AAAO,YACH,AAAmB,IACnB,AAAmB,MACrB;EAFqB;EACA;EAEnB,MAAM,OAAO,KAAK;EAElB,MAAM,MAAM,QAAQ,YAAY,oBAAoB,IAAI;EACxD,IAAI,CAAC,KACD,MAAM,IAAIA,0CAAcC,2BAAkB,oCAAoC,CAAC,KAAK,IAAI,CAAC;EAG7F,MAAM,QAAQ,QAAQ,YAAY,kBAAkB,IAAI;EACxD,IAAI,CAAC,OACD,MAAM,IAAID,0C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b,OAAO,MAAM,EAAE,oBAAoB,MAAM;EAG7C,OAAO;CACX;CAEA,AAAQ,4BAA4B,MAAY,YAA6B;EACzE,IAAI,CAAC,YAAY,QAAQ;EAEzB,MAAM,mBAAmB,CAAC,GAAG,UAAU;EACvC,MAAM,WAAW,WAA6B;GAC1C,CAAM,YAAY;IACd,KAAK,MAAM,OAAO,kBACd,MAAM,OAAO,MAAM,GAAG;GAE9B,GAAG,EAAE,OAAO,QAAQ,KAAK,OAAO,MAAM,+BAA+B,GAAG,CAAC;EAC7E;EAEA,KAAK,mBAAmB;EACxB,KAAK,GAAG,WAAW,OAAO;CAC9B;CAEA,MAAc,mBAAmB,MAA2B;EAExD,OADqB,KAAK,QAAQ,GAC3B,QAAQ;CACnB;AACJ;;;;AC1SA,MAAM,SAAS,IAAIC,0BAAO,eAAe;;;;;;;;;;;AAYzC,SAAgB,mBAAmB,OAAgB,SAAwB;CACvE,OAAO,MAAM,0BAA0B,iBAAiB,QAAQ,MAAM,OAAO,OAAO,KAAK,CAAC;CAE1F,MAAM,IAAIC,gCADW,iBAAiB,QAAQ,MAAM,UAAU,qCACb,CAAC;AACtD;;;;;;;;;;;;;;;;;;;;;;;;;;;ACOA,SAAgB,kBAA8B,cAAsB;CAChE,OAAO,SACH,SACA,cACA,YACI;EACJ,MAAM,iBAAiB,WAAW;EAElC,WAAW,QAAQ,eAAgB,GAAG,MAAkC;GACpE,IAAI,CAAC,gBACD,MAAM,IAAIC,gCAAcC,2BAAkB,uBAAuB;GAGrE,IAAI;IACA,OAAO,MAAM,eAAe,MAAM,MAAM,IAAI;GAChD,SAAS,OAAO;IACZ,IAAI,EAAE,iBAAiBC,uBACnB,mBAAmB,OAAO,YAAY;SAEtC,MAAM;GAEd;EACJ;CACJ;AACJ;;;;;AC/CA,MAAa"}
1
+ {"version":3,"file":"index.cjs","names":["SeedcordError","SeedcordErrorCode","Plugin","Logger","SeedcordError","SeedcordErrorCode","ShutdownPhase","Envapter","HmrModuleHandler","SeedcordError","SeedcordErrorCode","Pool","NO_MIGRATIONS","SeedcordRangeError","SeedcordErrorCode","SeedcordError","Migrator","fs","FileMigrationProvider","path","Plugin","Logger","SeedcordError","SeedcordErrorCode","ShutdownPhase","Envapter","HmrModuleHandler","Kysely","PostgresDialect","Pool"],"sources":["../src/mongo/decorators/RegisterMongoModel.ts","../src/mongo/decorators/RegisterMongoService.ts","../src/mongo/MongoService.ts","../src/mongo/Mongo.ts","../src/kysely-pg/decorators/RegisterKpgService.ts","../src/kysely-pg/KpgService.ts","../src/kysely-pg/KpgDatabaseBootstrapper.ts","../src/kysely-pg/KpgMigrationManager.ts","../src/kysely-pg/KpgServiceRegistry.ts","../src/kysely-pg/KyselyPg.ts","../src/index.ts"],"sourcesContent":["import mongoose from 'mongoose';\n\nimport type { MongoServiceKeys } from '../types/MongoServices';\n\nexport const ModelMetadataKey = Symbol('db:model');\n\n/**\n * Associates a Mongoose model with a database service\n *\n * Creates a Mongoose model from the decorated static schema property and stores it\n * for service registration. The model becomes available as `this.model` in the service.\n * Must be applied to a `public static schema` property in the service class.\n *\n * @typeParam TService - The service key type\n * @param collection - Collection name for the Mongoose model\n * @decorator\n * @example\n * ```typescript\n * \\@RegisterMongoService('users')\n * export class Users extends MongoService<IUser> {\n * \\@RegisterMongoModel('users')\n * public static schema = new mongoose.Schema<IUser>({\n * username: { type: String, required: true, unique: true }\n * });\n * }\n * ```\n */\nexport function RegisterMongoModel<TService extends MongoServiceKeys>(collection: TService) {\n return <\n SchemaObj extends Record<KeyOfSchema, mongoose.Schema>,\n KeyOfSchema extends keyof SchemaObj & (string | symbol)\n >(\n target: SchemaObj,\n propertyKey: KeyOfSchema\n ): void => {\n const schema = target[propertyKey];\n const name = String(collection);\n const model = mongoose.model(name, schema);\n Reflect.defineMetadata(ModelMetadataKey, model, target);\n };\n}\n","import type { MongoService } from '../MongoService';\nimport type { MongoServiceKeys } from '../types/MongoServices';\nimport type { Constructor } from 'type-fest';\n\nexport const ServiceMetadataKey = Symbol('db:serviceKey');\n\n/**\n * Registers a database service with a typed key\n *\n * Associates a service class with a key for dependency injection.\n * The service becomes available via `core.db.services[key]`.\n *\n * @typeParam TService - The service key type\n * @param key - Service key for registration and type-safe access\n * @decorator\n * @example\n * ```typescript\n * \\@RegisterMongoService('users')\n * export class Users<Doc extends IUser = IUser> extends MongoService<Doc> {\n * // Some code\n * }\n * ```\n */\nexport function RegisterMongoService<TService extends MongoServiceKeys>(key: TService) {\n return <DatabaseCtor extends Constructor<unknown> & { prototype: MongoService }>(ctor: DatabaseCtor): void => {\n Reflect.defineMetadata(ServiceMetadataKey, key, ctor);\n };\n}\n","import { SeedcordErrorCode } from '@seedcord/errors';\nimport { SeedcordError } from '@seedcord/errors/internal';\n\nimport { ModelMetadataKey } from './decorators/RegisterMongoModel';\nimport { ServiceMetadataKey } from './decorators/RegisterMongoService';\n\nimport type { Mongo } from './Mongo';\nimport type { MongoDocument } from './types/MongoDocument';\nimport type { TypedConstructor } from '@seedcord/types';\nimport type mongoose from 'mongoose';\nimport type { Core } from 'seedcord';\n\n/**\n * Base class for MongoDB service layers\n *\n * Provides typed access to MongoDB collections through Mongoose models.\n * Services are automatically registered with the Mongo plugin when instantiated.\n *\n * @typeParam Doc - The document type this service manages\n * @example\n * ```typescript\n * \\@RegisterMongoService('users')\n * export class Users extends MongoService<IUser> {\n * \\@RegisterMongoModel('users')\n * public static schema = new mongoose.Schema<IUser>({\n * username: { type: String, required: true, unique: true }\n * });\n *\n * // Custom methods here\n * public async findByUsername(username: string) {\n * return this.model.findOne({ username });\n * }\n * }\n * ```\n */\nexport abstract class MongoService<Doc extends MongoDocument = MongoDocument> {\n public readonly model: mongoose.Model<Doc>;\n\n public constructor(\n protected readonly db: Mongo,\n protected readonly core: Core\n ) {\n const ctor = this.constructor;\n\n const key = Reflect.getMetadata(ServiceMetadataKey, ctor) as string | undefined;\n if (!key) {\n throw new SeedcordError(SeedcordErrorCode.PluginMongoServiceDecoratorMissing, [ctor.name]);\n }\n\n const model = Reflect.getMetadata(ModelMetadataKey, ctor) as mongoose.Model<Doc> | undefined;\n if (!model) {\n throw new SeedcordError(SeedcordErrorCode.PluginMongoModelDecoratorMissing, [ctor.name]);\n }\n\n this.model = model;\n\n db._register(key, this);\n }\n}\n\n/** Constructor type for {@link MongoService} classes */\nexport type MongoServiceConstructor = TypedConstructor<typeof MongoService>;\n","import 'reflect-metadata';\n\nimport { SeedcordErrorCode } from '@seedcord/errors';\nimport { SeedcordError } from '@seedcord/errors/internal';\nimport { Logger, ShutdownPhase } from '@seedcord/services';\nimport { keepDefined, traverseDirectory } from '@seedcord/utils';\nimport chalk from 'chalk';\nimport { Envapter } from 'envapt';\nimport mongoose from 'mongoose';\nimport { HmrModuleHandler, Plugin } from 'seedcord';\n\nimport { ModelMetadataKey } from './decorators/RegisterMongoModel';\nimport { ServiceMetadataKey } from './decorators/RegisterMongoService';\nimport { MongoService } from './MongoService';\n\nimport type { MongoServiceConstructor } from './MongoService';\nimport type { MongoOptions } from './types/MongoOptions';\nimport type { MongoServices } from './types/MongoServices';\nimport type { HmrUpdateEvent } from '@seedcord/types/internal';\nimport type { Mongoose } from 'mongoose';\nimport type { Core } from 'seedcord';\n\ninterface MongoArtifact {\n key?: string;\n modelName?: string;\n}\n\n/**\n * MongoDB integration plugin for Seedcord.\n *\n * Manages MongoDB connections, service loading, and provides type-safe\n * access to database services through service registration decorators.\n */\nexport class Mongo extends Plugin {\n public readonly logger = new Logger('Mongo');\n private isInitialised = false;\n private servicesReady = false;\n private readonly uri: string;\n\n private readonly _services: Record<string, unknown> = {};\n\n /**\n * Map of all loaded services. Keys come from `@RegisterMongoService('key')`.\n *\n * @throws A {@link SeedcordError} if accessed before the plugin finishes initializing (e.g. from\n * a plugin that starts in an earlier phase).\n */\n public get services(): MongoServices {\n if (!this.servicesReady) {\n throw new SeedcordError(SeedcordErrorCode.PluginMongoServicesNotReady);\n }\n // MongoServices is augmented per-consumer via declaration merging; the registry holds the\n // instances opaquely and exposes the generated map shape at this public boundary.\n return this._services;\n }\n\n /** Exposed Mongoose instance once `init` completes. */\n declare public connection: Mongoose;\n private readonly hmrHandler?: HmrModuleHandler<MongoServiceConstructor, void, MongoArtifact>;\n\n constructor(\n public readonly core: Core,\n private readonly options: MongoOptions\n ) {\n super(core);\n this.uri = options.uri;\n\n this.core.shutdown.addTask(\n ShutdownPhase.ExternalResources,\n 'stop-database',\n async () => await this.stop(),\n this.options.timeout\n );\n\n if (!Envapter.isDevelopment) return;\n this.hmrHandler = new HmrModuleHandler({\n handlersDir: this.options.dir,\n isHandler: this.isServiceClass.bind(this),\n registerHandler: this.initializeService.bind(this),\n unregisterHandler: this.unregister.bind(this),\n getArtifacts: this.getArtifacts.bind(this),\n logger: this.logger,\n name: 'Mongo'\n });\n }\n\n private getArtifacts(ctor: MongoServiceConstructor): MongoArtifact {\n const key = Reflect.getMetadata(ServiceMetadataKey, ctor) as string | undefined;\n const model = Reflect.getMetadata(ModelMetadataKey, ctor) as mongoose.Model<unknown> | undefined;\n return {\n ...(key ? { key } : {}),\n ...(model?.modelName ? { modelName: model.modelName } : {})\n };\n }\n\n /** @internal For use in dev mode */\n public override async onHmr(event: HmrUpdateEvent): Promise<void> {\n await this.hmrHandler?.handle(event);\n }\n\n public async init(): Promise<void> {\n if (this.isInitialised) return;\n this.isInitialised = true;\n\n await this.connect();\n await this.loadServices();\n this.servicesReady = true;\n }\n\n public async stop(): Promise<void> {\n await this.disconnect();\n }\n\n private async connect(): Promise<void> {\n this.clearModels();\n this.connection = await mongoose\n .connect(this.uri, {\n dbName: this.options.name,\n ...(Envapter.isProduction && { tls: true, ssl: true }),\n ...keepDefined(this.options.connectionOptions ?? {})\n })\n .then((conn) => {\n this.logger.info(chalk.green.bold(`Connected to MongoDB: ${chalk.magenta.bold(conn.connection.name)}`));\n return conn;\n })\n .catch((err) => {\n throw new SeedcordError(SeedcordErrorCode.PluginMongoConnectionFailed, [this.options.name], {\n cause: err\n });\n });\n }\n\n private clearModels(): void {\n const modelNames = Object.keys(mongoose.models);\n if (modelNames.length > 0) {\n this.logger.debug(`Clearing ${modelNames.length} mongoose models`);\n for (const name of modelNames) mongoose.deleteModel(name);\n }\n }\n\n private async disconnect(): Promise<void> {\n this.clearModels();\n\n // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition -- connect() may have failed before assigning, so there is nothing to disconnect\n if (!this.connection) return;\n\n await this.connection\n .disconnect()\n .then(() => this.logger.info(chalk.red.bold('Disconnected from MongoDB')))\n .catch((err) => {\n this.logger.error(`Could not disconnect from MongoDB: ${(err as Error).message}`);\n throw new SeedcordError(SeedcordErrorCode.PluginMongoDisconnectFailed, { cause: err });\n });\n }\n\n private async loadServices(): Promise<void> {\n const servicesDir = this.options.dir;\n this.logger.info(chalk.bold(servicesDir));\n\n await traverseDirectory(\n servicesDir,\n (fullPath, rel, mod) => {\n for (const Service of Object.values(mod)) {\n if (this.isServiceClass(Service)) {\n this.initializeService(Service, rel);\n this.hmrHandler?.trackHandler(fullPath, Service);\n }\n }\n },\n this.logger\n );\n\n this.logger.utils.list(\n [`${chalk.magenta(Object.keys(this._services).length)} services`],\n chalk.bold.green('Loaded')\n );\n }\n\n private initializeService(Service: MongoServiceConstructor, relativePath: string): void {\n const instance = new Service(this, this.core);\n this.logger.utils.registration(instance.constructor.name, relativePath);\n }\n\n private isServiceClass(obj: unknown): obj is MongoServiceConstructor {\n return (\n typeof obj === 'function' &&\n obj.prototype instanceof MongoService &&\n Reflect.hasMetadata(ServiceMetadataKey, obj)\n );\n }\n\n /**\n * Register hook used by decorated services.\n *\n * @internal\n */\n _register(key: string, instance: unknown): void {\n this._services[key] = instance;\n }\n\n private unregister(Service: MongoServiceConstructor, artifacts?: { key?: string; modelName?: string }): void {\n const key = artifacts?.key ?? (Reflect.getMetadata(ServiceMetadataKey, Service) as string | undefined);\n const modelName =\n artifacts?.modelName ??\n (Reflect.getMetadata(ModelMetadataKey, Service) as mongoose.Model<unknown> | undefined)?.modelName;\n\n if (key && this._services[key]) {\n // eslint-disable-next-line @typescript-eslint/no-dynamic-delete -- key is a runtime service name, not a static property\n delete this._services[key];\n }\n\n if (modelName) {\n mongoose.deleteModel(modelName);\n }\n }\n}\n","import type { KpgServiceRegistrationOptions } from '../types/KpgServiceRegistrationOptions';\nimport type { KpgServices, KpgServiceKeys } from '../types/KpgServices';\nimport type { Constructor } from 'type-fest';\n\nexport const PgServiceMetadataKey = Symbol('db:pgServiceKey');\nexport const PgTableMetadataKey = Symbol('db:pgTable');\n\n/**\n *\n * Registers a Kysely PG service with the specified key and options.\n *\n * Associates a service class with a key for dependency injection.\n * The service becomes available via `core.db.services[key]`.\n *\n * @typeParam TKey - The service key type\n * @param key - Service key for registration and type-safe access\n * @param options - Additional registration options\n * @decorator\n * @example\n * ```typescript\n * \\@RegisterKpgService('users', { table: 'app_users' })\n * export class UsersService extends KpgService<{ users: IUser }, 'users'> {\n * // Some code\n * }\n * ```\n *\n * @see {@link KpgService}\n */\nexport function RegisterKpgService<TKey extends KpgServiceKeys>(key: TKey, options?: KpgServiceRegistrationOptions) {\n return <Ctor extends Constructor<KpgServices[TKey]>>(ctor: Ctor): void => {\n Reflect.defineMetadata(PgServiceMetadataKey, key, ctor);\n\n const tableName = options?.table ?? String(key);\n Reflect.defineMetadata(PgTableMetadataKey, tableName, ctor);\n };\n}\n","import { SeedcordErrorCode } from '@seedcord/errors';\nimport { SeedcordError } from '@seedcord/errors/internal';\n\nimport { PgServiceMetadataKey, PgTableMetadataKey } from './decorators/RegisterKpgService';\n\nimport type { KyselyPg } from './KyselyPg';\nimport type { TypedConstructor } from '@seedcord/types';\nimport type { Kysely } from 'kysely';\nimport type { Core } from 'seedcord';\nimport type { LiteralUnion } from 'type-fest';\n\n/**\n * Base class for KyselyPg services.\n *\n * Provides a small, typed shim around the shared Kysely instance and ensures\n * that subclasses have been decorated with `@RegisterKpgService`.\n *\n * @typeParam Database - The database shape used by Kysely (tables as keys).\n * @typeParam TTable - The specific table key from `Database` this service works with.\n *\n * @example\n * ```typescript\n * \\@RegisterKpgService('users')\n * export class UsersService extends KpgService<ImportedDatabaseInterface, 'users'> {\n * public async findById(id: string) {\n * return this.entity\n * .selectFrom(this.table)\n * .selectAll().where('id', '=', id)\n * .executeTakeFirst();\n * }\n * }\n *\n * // Usage inside handlers:\n * const user = await this.core.db.services.users.findById('abc');\n * ```\n */\nexport abstract class KpgService<Database extends object, TTable extends LiteralUnion<keyof Database, string>> {\n public readonly table: TTable;\n\n public constructor(\n protected readonly kysely: KyselyPg<Database>,\n protected readonly core: Core\n ) {\n const ctor = this.constructor;\n\n const key = Reflect.getMetadata(PgServiceMetadataKey, ctor) as string | undefined;\n if (!key) {\n throw new SeedcordError(SeedcordErrorCode.PluginKpgServiceDecoratorMissing, [ctor.name]);\n }\n\n const table = Reflect.getMetadata(PgTableMetadataKey, ctor) as TTable | undefined;\n\n // This check should always pass since TTable is derived from the key if a table is not provided explicitly.\n if (!table) {\n throw new SeedcordError(SeedcordErrorCode.PluginKpgServiceTableMissing, [ctor.name]);\n }\n\n this.table = table;\n this.kysely._register(key, this);\n }\n\n /**\n * Shared Kysely instance used to interact with the Postgres database.\n */\n public get db(): Kysely<Database> {\n return this.kysely.connection;\n }\n}\n\n/** Constructor type for {@link KpgService} classes */\nexport type KyselyServiceConstructor<Database extends object = object> = TypedConstructor<\n typeof KpgService<Database, keyof Database & string>\n>;\n","import chalk from 'chalk';\nimport { Pool, type PoolConfig } from 'pg';\n\nimport type { Logger } from '@seedcord/services';\n\n/**\n * Ensures the target Postgres database exists, creating it if missing.\n */\nexport class KpgDatabaseBootstrapper {\n private static readonly ADMIN_DB = 'postgres';\n private static readonly DATABASE_EXISTS_SQL =\n 'SELECT EXISTS (SELECT 1 FROM pg_database WHERE datname = $1) AS \"exists\"';\n\n constructor(private readonly logger: Logger) {}\n\n public resolveDatabaseName(config: PoolConfig): string | null {\n return KpgDatabaseBootstrapper.parseDatabaseName(config);\n }\n\n public resolveDatabaseFromPool(pool: Pool): string | null {\n const config: PoolConfig = {};\n\n const { options } = pool;\n\n if (typeof options.database === 'string') {\n config.database = options.database;\n }\n\n if (typeof options.connectionString === 'string') {\n config.connectionString = options.connectionString;\n }\n\n return this.resolveDatabaseName(config);\n }\n\n public async ensure(baseConfig: PoolConfig): Promise<void> {\n const targetDb = this.resolveDatabaseName(baseConfig);\n if (!targetDb) {\n this.logger.info(chalk.gray('Skipping database existence check (no database specified).'));\n return;\n }\n\n if (targetDb === KpgDatabaseBootstrapper.ADMIN_DB) {\n this.logger.info(chalk.gray('Target database is postgres; skipping creation.'));\n return;\n }\n\n const adminConfig = this.buildAdminConfig(baseConfig);\n if (!adminConfig) {\n this.logger.warn(`Unable to derive admin connection when ensuring database ${targetDb}`);\n return;\n }\n\n this.logger.info(chalk.gray(`Ensuring database ${chalk.yellow(targetDb)} exists...`));\n\n const adminPool = new Pool(adminConfig);\n\n try {\n const exists = await this.databaseExists(adminPool, targetDb);\n if (exists) {\n this.logger.info(chalk.gray(`Database ${chalk.yellow(targetDb)} already exists.`));\n return;\n }\n\n await this.createDatabase(adminPool, targetDb);\n this.logger.info(chalk.green(`Created database ${chalk.bold(targetDb)}.`));\n } catch (error) {\n const err = error instanceof Error ? error : new Error(String(error));\n this.logger.error(`Failed to ensure database ${targetDb}: ${err.message}`);\n throw err;\n } finally {\n await adminPool.end();\n }\n }\n\n private buildAdminConfig(baseConfig: PoolConfig): PoolConfig | null {\n const adminConfig: PoolConfig = { ...baseConfig };\n\n const { connectionString } = adminConfig;\n if (connectionString) {\n const connection = KpgDatabaseBootstrapper.applyDatabaseToConnectionString(\n connectionString,\n KpgDatabaseBootstrapper.ADMIN_DB\n );\n if (!connection) return null;\n adminConfig.connectionString = connection;\n }\n\n adminConfig.database = KpgDatabaseBootstrapper.ADMIN_DB;\n return adminConfig;\n }\n\n private async databaseExists(pool: Pool, database: string): Promise<boolean> {\n const client = await pool.connect();\n try {\n const { rows } = await client.query<{ exists: boolean }>(KpgDatabaseBootstrapper.DATABASE_EXISTS_SQL, [\n database\n ]);\n return Boolean(rows[0]?.exists);\n } finally {\n client.release();\n }\n }\n\n private async createDatabase(pool: Pool, database: string): Promise<void> {\n const client = await pool.connect();\n try {\n const createSql = `CREATE DATABASE ${KpgDatabaseBootstrapper.escapeIdentifier(database)}`;\n await client.query(createSql);\n } finally {\n client.release();\n }\n }\n\n private static parseDatabaseName(config: PoolConfig): string | null {\n if (typeof config.database === 'string' && config.database.trim().length > 0) {\n return config.database.trim();\n }\n\n const connectionString = config.connectionString;\n if (!connectionString) return null;\n\n try {\n const url = new URL(connectionString);\n const pathname = url.pathname.replace(/^\\//, '');\n if (!pathname) return null;\n const [candidate] = pathname.split('/');\n return candidate ? decodeURIComponent(candidate) : null;\n } catch {\n return null;\n }\n }\n\n private static applyDatabaseToConnectionString(connectionString: string, database: string): string | null {\n try {\n const url = new URL(connectionString);\n url.pathname = `/${encodeURIComponent(database)}`;\n return url.toString();\n } catch {\n return null;\n }\n }\n\n private static escapeIdentifier(identifier: string): string {\n return `\"${identifier.replace(/\"/g, '\"\"')}\"`;\n }\n}\n","import { promises as fs } from 'node:fs';\nimport path from 'node:path';\nimport { pathToFileURL } from 'node:url';\nimport { inspect } from 'node:util';\n\nimport { SeedcordErrorCode } from '@seedcord/errors';\nimport { SeedcordError, SeedcordRangeError } from '@seedcord/errors/internal';\nimport { keepDefined } from '@seedcord/utils';\nimport chalk from 'chalk';\nimport { FileMigrationProvider, Migrator, NO_MIGRATIONS } from 'kysely/migration';\n\nimport type {\n MigrationManagerContext,\n MigrationModule,\n MigrationOptions,\n StepMigrationOptions\n} from './types/KpgMigration';\nimport type {\n Migration,\n MigrationInfo,\n MigrationProvider,\n MigrationResult,\n MigrationResultSet\n} from 'kysely/migration';\nimport type { Stats } from 'node:fs';\n\n/**\n * Migration tooling for KyselyPg.\n *\n * @sealed\n */\nexport class KpgMigrationManager<Database extends object> {\n constructor(private readonly ctx: MigrationManagerContext<Database>) {}\n\n public async migrate(options?: MigrationOptions): Promise<void> {\n const { target, direction = 'latest', steps } = options ?? {};\n\n if (typeof target !== 'undefined') {\n const label = target === NO_MIGRATIONS ? 'NO_MIGRATIONS' : target;\n await this.runMigration((migrator) => migrator.migrateTo(target), `Migrating to ${chalk.yellow(label)}...`);\n return;\n }\n\n switch (direction) {\n case 'latest':\n await this.runMigration((migrator) => migrator.migrateToLatest());\n return;\n case 'up':\n case 'down': {\n const stepCount = steps ?? 1;\n if (!Number.isInteger(stepCount) || stepCount < 0) {\n throw new SeedcordRangeError(SeedcordErrorCode.PluginKpgInvalidStepCount);\n }\n\n if (stepCount === 0) {\n this.logMigrationResults([]);\n return;\n }\n\n const runner =\n direction === 'up'\n ? (migrator: Migrator) => migrator.migrateUp()\n : (migrator: Migrator) => migrator.migrateDown();\n await this.runStepwise(stepCount, direction, runner);\n return;\n }\n default:\n throw new SeedcordError(SeedcordErrorCode.PluginKpgUnknownDirection, [direction]);\n }\n }\n\n public async migrateUp(options?: StepMigrationOptions): Promise<void> {\n if (typeof options?.steps === 'undefined') {\n await this.migrate({ direction: 'up' });\n return;\n }\n\n await this.migrate({ direction: 'up', steps: options.steps });\n }\n\n public async migrateDown(options?: StepMigrationOptions): Promise<void> {\n if (typeof options?.steps === 'undefined') {\n await this.migrate({ direction: 'down' });\n return;\n }\n\n await this.migrate({ direction: 'down', steps: options.steps });\n }\n\n public async listMigrations(): Promise<readonly MigrationInfo[]> {\n const migrator = await this.createMigrator();\n return migrator.getMigrations();\n }\n\n private async runMigration(\n runner: (migrator: Migrator) => Promise<MigrationResultSet>,\n runningMessage = 'Running migrations...'\n ): Promise<void> {\n this.ctx.logger.info(chalk.gray('Preparing migrations...'));\n const migrator = await this.createMigrator();\n\n this.ctx.logger.info(chalk.gray(runningMessage));\n const { error, results } = await runner(migrator);\n\n this.logMigrationResults(results ?? []);\n\n if (error) {\n this.handleMigrationError(error);\n }\n }\n\n private async runStepwise(\n steps: number,\n direction: 'up' | 'down',\n runner: (migrator: Migrator) => Promise<MigrationResultSet>\n ): Promise<void> {\n this.ctx.logger.info(chalk.gray('Preparing migrations...'));\n const migrator = await this.createMigrator();\n\n const directionLabel = direction === 'up' ? 'Running' : 'Reverting';\n const countLabel = steps === 1 ? 'one migration' : `${chalk.yellow(String(steps))} migrations`;\n this.ctx.logger.info(chalk.gray(`${directionLabel} ${countLabel}...`));\n\n const aggregated: MigrationResult[] = [];\n let encounteredError: unknown;\n\n for (let index = 0; index < steps; index += 1) {\n const { error, results } = await runner(migrator);\n\n if (results?.length) {\n aggregated.push(...results);\n }\n\n if (error) {\n encounteredError = error;\n break;\n }\n\n if (!results?.length) {\n break;\n }\n }\n\n this.logMigrationResults(aggregated);\n\n if (encounteredError) {\n this.handleMigrationError(encounteredError);\n }\n }\n\n private async createMigrator(): Promise<Migrator> {\n const provider = await this.getMigrationProvider();\n const { config } = this.ctx;\n\n return new Migrator({\n db: this.ctx.db,\n provider,\n allowUnorderedMigrations: config.allowUnorderedMigrations ?? false,\n ...keepDefined(config, 'migrationTableName', 'migrationLockTableName', 'migrationTableSchema')\n });\n }\n\n private async getMigrationProvider(): Promise<MigrationProvider> {\n const { path: target } = this.ctx.config;\n const resolvedTarget = Array.isArray(target)\n ? target.map((entry) => this.resolvePath(entry))\n : this.resolvePath(target);\n\n if (Array.isArray(resolvedTarget)) {\n this.logMigrationFiles(resolvedTarget);\n return this.createModuleProvider(resolvedTarget);\n }\n\n let migrationStat: Stats | undefined;\n try {\n migrationStat = await fs.stat(resolvedTarget);\n } catch {\n migrationStat = undefined;\n }\n\n if (migrationStat?.isDirectory()) {\n const directory = this.relativePath(resolvedTarget);\n this.ctx.logger.info(chalk.gray(`Loading migrations directory ${chalk.yellow(directory)}`));\n return new FileMigrationProvider({ fs, path, migrationFolder: resolvedTarget });\n }\n\n if (migrationStat?.isFile() ?? true) {\n this.logMigrationFiles([resolvedTarget]);\n return this.createModuleProvider([resolvedTarget]);\n }\n\n const label = Array.isArray(target) ? target.join(', ') : target;\n throw new SeedcordError(SeedcordErrorCode.PluginKpgUnresolvedMigrationsPath, [label]);\n }\n\n private async createModuleProvider(files: string[]): Promise<MigrationProvider> {\n if (files.length === 0) {\n throw new SeedcordError(SeedcordErrorCode.PluginKpgNoMigrationFiles);\n }\n\n const comparator =\n this.ctx.config.nameComparator ?? ((nameA: string, nameB: string) => nameA.localeCompare(nameB));\n\n const entries = await Promise.all(\n files.map(async (filePath) => {\n const moduleUrl = pathToFileURL(filePath).href;\n const mod: unknown = await import(moduleUrl);\n\n if (!this.isMigrationModule(mod)) {\n throw new SeedcordError(SeedcordErrorCode.PluginKpgInvalidMigrationModule, [filePath]);\n }\n\n const { up, down } = mod;\n\n const name = path.basename(filePath);\n\n const migration: Migration = {\n async up(db) {\n await up(db);\n },\n async down(db) {\n await down(db);\n }\n };\n\n return [name, migration] as const;\n })\n );\n\n const sorted = entries.sort(([a], [b]) => comparator(a, b));\n this.logPreparedMigrations(sorted);\n\n return {\n getMigrations: () => Promise.resolve(Object.fromEntries(sorted))\n } satisfies MigrationProvider;\n }\n\n private logMigrationFiles(files: readonly string[]): void {\n if (!files.length) return;\n\n this.ctx.logger.info('Loading migration file(s):');\n for (const file of files) {\n this.ctx.logger.utils.item(`${chalk.yellow(this.relativePath(file))}`);\n }\n }\n\n private logPreparedMigrations(entries: readonly (readonly [string, Migration])[]): void {\n if (!entries.length) return;\n\n this.ctx.logger.info('Prepared migrations:');\n for (const [name] of entries) {\n this.ctx.logger.utils.item(`${chalk.green(name)}`);\n }\n }\n\n private logMigrationResults(results: readonly MigrationResult[]): void {\n if (!results.length) {\n this.ctx.logger.info(chalk.gray('No migrations executed.'));\n return;\n }\n\n this.ctx.logger.info('Migration results:');\n\n for (const result of results) {\n if (result.status === 'Success') {\n this.ctx.logger.info(`${chalk.green('✓')} ${chalk.bold(result.migrationName)}`);\n continue;\n }\n\n if (result.status === 'Error') {\n this.ctx.logger.error(`${chalk.red('✗')} ${chalk.bold(result.migrationName)}`);\n continue;\n }\n\n this.ctx.logger.info(`${chalk.yellow('•')} ${chalk.bold(result.migrationName)} ${chalk.gray('(skipped)')}`);\n }\n }\n\n private relativePath(filePath: string): string {\n const relative = path.relative(this.ctx.baseDir, filePath);\n return relative.startsWith('..') ? filePath : relative;\n }\n\n private resolvePath(target: string): string {\n if (path.isAbsolute(target)) return target;\n return path.resolve(this.ctx.baseDir, target);\n }\n\n private handleMigrationError(error: unknown): never {\n const message = error instanceof Error ? error.message : inspect(error);\n this.ctx.logger.error(`Migration failure: ${message}`);\n\n if (error instanceof Error) {\n throw error;\n }\n\n throw new SeedcordError(SeedcordErrorCode.PluginKpgNonErrorFailure, [message]);\n }\n\n private isMigrationModule(value: unknown): value is MigrationModule {\n if (!value || typeof value !== 'object') return false;\n if (!('up' in value) || !('down' in value)) return false;\n\n const { up, down } = value;\n\n return typeof up === 'function' && typeof down === 'function';\n }\n}\n","import { traverseDirectory } from '@seedcord/utils';\nimport chalk from 'chalk';\n\nimport { PgServiceMetadataKey } from './decorators/RegisterKpgService';\nimport { KpgService } from './KpgService';\n\nimport type { KyselyServiceConstructor } from './KpgService';\nimport type { KyselyArtifact, KyselyPg } from './KyselyPg';\nimport type { KpgServices } from './types/KpgServices';\nimport type { Logger } from '@seedcord/services';\nimport type { Core } from 'seedcord';\n\n/**\n * Discovers and registers Postgres services for the plugin.\n */\nexport class KpgServiceRegistry<Database extends object> {\n private readonly services: Record<string, unknown> = Object.create(null) as Record<string, unknown>;\n\n constructor(\n private readonly plugin: KyselyPg<Database>,\n private readonly core: Core,\n private readonly logger: Logger\n ) {}\n\n public get map(): KpgServices {\n // KpgServices is augmented per-consumer via declaration merging; the registry holds the\n // instances opaquely and exposes the generated map shape at this boundary.\n return this.services;\n }\n\n public register(key: string, instance: unknown): void {\n this.services[key] = instance;\n }\n\n public async loadFromDirectory(dir: string): Promise<void> {\n this.logger.info(chalk.bold(dir));\n\n await traverseDirectory(\n dir,\n (fullPath, rel, mod) => {\n for (const Service of Object.values(mod)) {\n if (this.isServiceClass(Service)) {\n this.initializeService(Service, rel);\n this.plugin.trackServiceFile(fullPath, Service);\n }\n }\n },\n this.logger\n );\n\n this.logger.utils.list(\n [`${chalk.magenta(Object.keys(this.services).length)} services`],\n chalk.bold.green('Loaded')\n );\n }\n\n public unregister(Service: KyselyServiceConstructor<Database>, artifacts?: KyselyArtifact): void {\n const key = artifacts?.key ?? (Reflect.getMetadata(PgServiceMetadataKey, Service) as string | undefined);\n if (key && this.services[key]) {\n // eslint-disable-next-line @typescript-eslint/no-dynamic-delete -- key is a runtime service name, not a static property\n delete this.services[key];\n }\n }\n\n public initializeService(Service: KyselyServiceConstructor<Database>, relativePath: string): void {\n const instance = new Service(this.plugin, this.core);\n this.logger.utils.registration(instance.constructor.name, relativePath);\n }\n\n public isServiceClass(obj: unknown): obj is KyselyServiceConstructor<Database> {\n return (\n typeof obj === 'function' &&\n obj.prototype instanceof KpgService &&\n Reflect.hasMetadata(PgServiceMetadataKey, obj)\n );\n }\n}\n","import 'reflect-metadata';\n\nimport { SeedcordErrorCode } from '@seedcord/errors';\nimport { SeedcordError } from '@seedcord/errors/internal';\nimport { Logger, ShutdownPhase } from '@seedcord/services';\nimport { keepDefined } from '@seedcord/utils';\nimport chalk from 'chalk';\nimport { Envapter } from 'envapt';\nimport { Kysely, PostgresDialect } from 'kysely';\nimport { Pool, type PoolConfig, type PoolClient } from 'pg';\nimport { HmrModuleHandler, Plugin } from 'seedcord';\n\nimport { PgServiceMetadataKey } from './decorators/RegisterKpgService';\nimport { KpgDatabaseBootstrapper } from './KpgDatabaseBootstrapper';\nimport { KpgMigrationManager } from './KpgMigrationManager';\nimport { KpgServiceRegistry } from './KpgServiceRegistry';\n\nimport type { KyselyServiceConstructor } from './KpgService';\nimport type { MigrationOptions, StepMigrationOptions } from './types/KpgMigration';\nimport type { KpgOptions } from './types/KpgOptions';\nimport type { KpgServices } from './types/KpgServices';\nimport type { HmrUpdateEvent } from '@seedcord/types/internal';\nimport type { MigrationInfo } from 'kysely/migration';\nimport type { Core } from 'seedcord';\n\nexport interface KyselyArtifact {\n key?: string;\n}\n\n/**\n * Postgres plugin using Kysely.\n *\n * Handles setting up the connection pool, applying migrations, and\n * registering decorated services so they can be resolved from the core.\n */\nexport class KyselyPg<Database extends object> extends Plugin {\n public readonly logger = new Logger('KyselyPg');\n private isInitialised = false;\n private servicesReady = false;\n\n /** Exposed Kysely instance once `init` completes. */\n declare public connection: Kysely<Database>;\n private pool: Pool | null = null;\n private onConnectHandler: ((client: PoolClient) => void) | null = null;\n private migrationManager: KpgMigrationManager<Database> | null = null;\n private readonly serviceRegistry: KpgServiceRegistry<Database>;\n private readonly databaseBootstrapper: KpgDatabaseBootstrapper;\n private databaseName: string | null = null;\n private readonly hmrHandler?: HmrModuleHandler<KyselyServiceConstructor<Database>, void, KyselyArtifact>;\n\n /**\n * Map of all services registered with the plugin, keyed by their decorator name.\n */\n public get services(): KpgServices {\n if (!this.servicesReady) {\n throw new SeedcordError(SeedcordErrorCode.PluginKpgServicesNotReady);\n }\n return this.serviceRegistry.map;\n }\n\n constructor(\n public readonly core: Core,\n private readonly options: KpgOptions\n ) {\n super(core);\n this.serviceRegistry = new KpgServiceRegistry(this, core, this.logger);\n this.databaseBootstrapper = new KpgDatabaseBootstrapper(this.logger);\n this.core.shutdown.addTask(\n ShutdownPhase.ExternalResources,\n 'stop-kyselypg',\n async () => await this.stop(),\n this.options.timeout\n );\n\n if (!Envapter.isDevelopment) return; // HMR only in development\n\n const relPaths = this.options.migrations.path;\n super.registerCriticalFiles(Array.isArray(relPaths) ? relPaths : [relPaths]);\n\n this.hmrHandler = new HmrModuleHandler({\n handlersDir: this.options.dir,\n isHandler: this.serviceRegistry.isServiceClass.bind(this.serviceRegistry),\n registerHandler: this.serviceRegistry.initializeService.bind(this.serviceRegistry),\n unregisterHandler: this.serviceRegistry.unregister.bind(this.serviceRegistry),\n getArtifacts: this.getArtifacts.bind(this),\n logger: this.logger,\n name: 'KyselyPg'\n });\n }\n\n private getArtifacts(ctor: KyselyServiceConstructor<Database>): KyselyArtifact {\n const key = Reflect.getMetadata(PgServiceMetadataKey, ctor) as string | undefined;\n return key ? { key } : {};\n }\n\n /** @internal For use in dev mode */\n public override async onHmr(event: HmrUpdateEvent): Promise<void> {\n await this.hmrHandler?.handle(event);\n }\n\n /**\n * Connects to Postgres, runs any startup migrations, and loads decorated services.\n *\n * Safe to call multiple times; subsequent calls exit early.\n */\n public async init(): Promise<void> {\n if (this.isInitialised) return;\n this.isInitialised = true;\n\n await this.connect();\n\n const startupConfig = this.options.migrations.onStartup;\n if (startupConfig !== false) {\n if (startupConfig && typeof startupConfig !== 'boolean') {\n await this.migrate(startupConfig);\n } else {\n await this.migrate();\n }\n }\n await this.serviceRegistry.loadFromDirectory(this.options.dir);\n this.servicesReady = true;\n }\n\n /**\n * Tears down the connection pool and clears the migration manager reference.\n */\n public async stop(): Promise<void> {\n await this.disconnect();\n }\n\n private async connect(): Promise<void> {\n const pool = await this.resolvePool();\n this.pool = pool;\n\n this.registerOnConnectStatements(pool, this.options.onConnectSQL);\n\n try {\n await this.testPoolConnection(pool);\n\n this.connection = new Kysely<Database>({\n dialect: new PostgresDialect({ pool }),\n ...keepDefined(this.options.kysely ?? {})\n });\n\n this.migrationManager = new KpgMigrationManager({\n db: this.connection,\n logger: this.logger,\n config: this.options.migrations,\n baseDir: process.cwd()\n });\n\n const dbLabel = this.databaseName ?? 'unknown';\n this.logger.info(`Connected to Postgres database ${chalk.bold.magenta(dbLabel)}`);\n } catch (err) {\n const error = err instanceof Error ? err : new Error(String(err));\n this.logger.error(`Could not connect to Postgres: ${error.message}`);\n throw error;\n }\n }\n\n private async disconnect(): Promise<void> {\n const pool = this.pool;\n if (!pool) return;\n\n if (this.onConnectHandler) {\n pool.removeListener('connect', this.onConnectHandler);\n this.onConnectHandler = null;\n }\n\n this.pool = null;\n this.migrationManager = null;\n\n this.logger.info(chalk.gray('Closing Postgres pool.'));\n await pool.end().catch((err) => {\n this.logger.error(`Could not close pg pool: ${(err as Error).message}`);\n throw new SeedcordError(SeedcordErrorCode.PluginKpgDisconnectFailed, { cause: err });\n });\n this.logger.info(chalk.red.bold('Disconnected from Postgres'));\n }\n\n /**\n * Runs migrations using the supplied options.\n *\n * @param options - Target migration or direction overrides\n */\n public async migrate(options?: MigrationOptions): Promise<void> {\n await this.getMigrationManager().migrate(options);\n }\n\n /**\n * Runs a single upwards migration step unless a custom count is provided.\n *\n * @param options - Optional configuration for step-based execution\n */\n public async migrateUp(options?: StepMigrationOptions): Promise<void> {\n await this.getMigrationManager().migrateUp(options);\n }\n\n /**\n * Runs a single downwards migration step unless a custom count is provided.\n *\n * @param options - Optional configuration for step-based execution\n */\n public async migrateDown(options?: StepMigrationOptions): Promise<void> {\n await this.getMigrationManager().migrateDown(options);\n }\n\n /**\n * Lists every migration registered with the manager along with its execution state.\n */\n public listMigrations(): Promise<readonly MigrationInfo[]> {\n return this.getMigrationManager().listMigrations();\n }\n\n /**\n * Lists unapplied migrations.\n */\n public async listPendingMigrations(): Promise<MigrationInfo[]> {\n const all = await this.listMigrations();\n return all.filter((m) => !m.executedAt);\n }\n\n private getMigrationManager(): KpgMigrationManager<Database> {\n if (this.migrationManager) return this.migrationManager;\n\n const manager = new KpgMigrationManager({\n db: this.connection,\n logger: this.logger,\n config: this.options.migrations,\n baseDir: process.cwd()\n });\n\n this.migrationManager = manager;\n return manager;\n }\n\n /**\n * Register hook used by decorated services.\n *\n * @internal\n */\n _register(key: string, instance: unknown): void {\n this.serviceRegistry.register(key, instance);\n }\n\n /**\n * Tracks a service file with the HMR handler so dev reloads can swap it. No-op outside dev.\n *\n * @internal Lets {@link KpgServiceRegistry} reach the dev-only HMR handler without poking a\n * private field.\n */\n public trackServiceFile(filePath: string, ctor: KyselyServiceConstructor<Database>): void {\n this.hmrHandler?.trackHandler(filePath, ctor);\n }\n\n private async resolvePool(): Promise<Pool> {\n const { pool: providedPool, connectionString } = this.options;\n\n if (providedPool instanceof Pool) {\n this.logger.info(chalk.gray('Reusing provided Postgres pool instance.'));\n this.databaseName = this.databaseBootstrapper.resolveDatabaseFromPool(providedPool);\n return providedPool;\n }\n\n const baseConfig = this.createPoolConfig(providedPool, connectionString);\n await this.databaseBootstrapper.ensure(baseConfig);\n this.databaseName = this.databaseBootstrapper.resolveDatabaseName(baseConfig);\n\n this.logger.info(chalk.gray('Creating new Postgres pool.'));\n return new Pool(baseConfig);\n }\n\n private createPoolConfig(poolConfig?: PoolConfig, connectionString?: string): PoolConfig {\n const config: PoolConfig = poolConfig ? { ...poolConfig } : {};\n\n if (connectionString) {\n config.connectionString = connectionString;\n }\n\n if (this.options.forceInsecureSSL) {\n config.ssl = { rejectUnauthorized: false };\n }\n\n return config;\n }\n\n private registerOnConnectStatements(pool: Pool, statements?: string[]): void {\n if (!statements?.length) return;\n\n const queuedStatements = [...statements];\n const handler = (client: PoolClient): void => {\n void (async () => {\n for (const sql of queuedStatements) {\n await client.query(sql);\n }\n })().catch((err) => this.logger.error('Failed to run onConnect SQL', err));\n };\n\n this.onConnectHandler = handler;\n pool.on('connect', handler);\n }\n\n private async testPoolConnection(pool: Pool): Promise<void> {\n const client = await pool.connect();\n client.release();\n }\n}\n","export * from './mongo';\nexport * from './kysely-pg';\n\n/** Package version */\nexport const version = process.env.PACKAGE_VERSION ?? '0.0.0';\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAIA,MAAa,mBAAmB,OAAO,UAAU;;;;;;;;;;;;;;;;;;;;;;AAuBjD,SAAgB,mBAAsD,YAAsB;CACxF,QAII,QACA,gBACO;EACP,MAAM,SAAS,OAAO;EACtB,MAAM,OAAO,OAAO,UAAU;EAC9B,MAAM,QAAQ,iBAAS,MAAM,MAAM,MAAM;EACzC,QAAQ,eAAe,kBAAkB,OAAO,MAAM;CAC1D;AACJ;;;;ACpCA,MAAa,qBAAqB,OAAO,eAAe;;;;;;;;;;;;;;;;;;AAmBxD,SAAgB,qBAAwD,KAAe;CACnF,QAAiF,SAA6B;EAC1G,QAAQ,eAAe,oBAAoB,KAAK,IAAI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