@orion-js/migrations 4.1.10 → 4.1.12
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +1 -1
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +6 -2
- package/dist/index.d.ts +6 -2
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/package.json +8 -8
package/dist/index.cjs
CHANGED
|
@@ -214,7 +214,7 @@ function loadMigrations(migrationServices, options) {
|
|
|
214
214
|
if (options == null ? void 0 : options.omitJob) return migrations;
|
|
215
215
|
(0, import_dogs.startWorkers)({
|
|
216
216
|
cooldownPeriod: 1e3,
|
|
217
|
-
|
|
217
|
+
defaultLockTime: 1e3 * 60 * 20,
|
|
218
218
|
// 20 min
|
|
219
219
|
workersCount: 1,
|
|
220
220
|
pollInterval: 10 * 1e3,
|
package/dist/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts","../src/Schema.ts","../src/Repo.ts","../src/loadMigrations/index.ts","../src/service/index.ts","../src/MigrationsService.ts"],"sourcesContent":["export * from './Schema'\nexport * from './Repo'\nexport * from './loadMigrations'\nexport * from './service'\nexport * from './MigrationsService'\n","import {TypedId, typedId} from '@orion-js/mongodb'\nimport {InferSchemaType, schemaWithName} from '@orion-js/schema'\n\nexport type MigrationId = TypedId<'scnmg'>\n\nexport const MigrationSchema = schemaWithName('Migration', {\n _id: {\n type: typedId('scnmg'),\n },\n name: {\n type: String,\n },\n completedAt: {\n type: Date,\n },\n})\n\nexport type MigrationType = InferSchemaType<typeof MigrationSchema>\n","import {createCollection} from '@orion-js/mongodb'\nimport {MigrationSchema} from './Schema'\nimport {Service} from '@orion-js/services'\n\n@Service()\nexport class MigrationsRepo {\n public collection = createCollection({\n name: 'orionjs.migrations',\n schema: MigrationSchema,\n indexes: [],\n })\n\n async getCompletedMigrationNames() {\n const migrations = await this.collection.find().toArray()\n return migrations.map(m => m.name)\n }\n\n async saveCompletedMigration(name: string) {\n await this.collection.insertOne({\n name,\n completedAt: new Date(),\n })\n }\n}\n","import {defineJob, startWorkers} from '@orion-js/dogs'\nimport {getMigrationsFromServices} from '../service'\nimport {getInstance} from '@orion-js/services'\nimport {MigrationsService} from '../MigrationsService'\n\nexport interface Options {\n lockTime?: number\n omitJob?: boolean\n}\n\nexport function loadMigrations(migrationServices: any[], options?: Options) {\n const migrations = getMigrationsFromServices(migrationServices)\n if (options?.omitJob) return migrations\n\n startWorkers({\n cooldownPeriod: 1000,\n
|
|
1
|
+
{"version":3,"sources":["../src/index.ts","../src/Schema.ts","../src/Repo.ts","../src/loadMigrations/index.ts","../src/service/index.ts","../src/MigrationsService.ts"],"sourcesContent":["export * from './Schema'\nexport * from './Repo'\nexport * from './loadMigrations'\nexport * from './service'\nexport * from './MigrationsService'\n","import {TypedId, typedId} from '@orion-js/mongodb'\nimport {InferSchemaType, schemaWithName} from '@orion-js/schema'\n\nexport type MigrationId = TypedId<'scnmg'>\n\nexport const MigrationSchema = schemaWithName('Migration', {\n _id: {\n type: typedId('scnmg'),\n },\n name: {\n type: String,\n },\n completedAt: {\n type: Date,\n },\n})\n\nexport type MigrationType = InferSchemaType<typeof MigrationSchema>\n","import {createCollection} from '@orion-js/mongodb'\nimport {MigrationSchema} from './Schema'\nimport {Service} from '@orion-js/services'\n\n@Service()\nexport class MigrationsRepo {\n public collection = createCollection({\n name: 'orionjs.migrations',\n schema: MigrationSchema,\n indexes: [],\n })\n\n async getCompletedMigrationNames() {\n const migrations = await this.collection.find().toArray()\n return migrations.map(m => m.name)\n }\n\n async saveCompletedMigration(name: string) {\n await this.collection.insertOne({\n name,\n completedAt: new Date(),\n })\n }\n}\n","import {defineJob, startWorkers} from '@orion-js/dogs'\nimport {getMigrationsFromServices} from '../service'\nimport {getInstance} from '@orion-js/services'\nimport {MigrationsService} from '../MigrationsService'\n\nexport interface Options {\n lockTime?: number\n omitJob?: boolean\n}\n\nexport function loadMigrations(migrationServices: any[], options?: Options) {\n const migrations = getMigrationsFromServices(migrationServices)\n if (options?.omitJob) return migrations\n\n startWorkers({\n cooldownPeriod: 1000,\n defaultLockTime: 1000 * 60 * 20, // 20 min\n workersCount: 1,\n pollInterval: 10 * 1000,\n jobs: {\n orionjsRunMigrations: defineJob({\n type: 'recurrent',\n runEvery: 30 * 1000,\n async resolve(_params, context) {\n const instance = getInstance(MigrationsService)\n await instance.runMigrations(migrations, context)\n },\n }),\n },\n })\n\n return migrations\n}\n","import {ExecutionContext} from '@orion-js/dogs'\nimport {getInstance} from '@orion-js/services'\nimport {Service} from '@orion-js/services'\n\nexport interface MigrationServiceOptions {\n name: string\n useMongoTransactions: false\n}\n\n// Define metadata storage using WeakMaps\nconst serviceMetadata = new WeakMap<any, {_serviceType: string; options: MigrationServiceOptions}>()\n\nexport function MigrationService(options: MigrationServiceOptions) {\n return (target: any, context: ClassDecoratorContext<any>) => {\n Service()(target, context)\n\n context.addInitializer(function (this) {\n serviceMetadata.set(this, {_serviceType: 'migrations', options: options})\n })\n }\n}\n\nexport type MigrationExecutable = {\n runMigration(context: ExecutionContext): Promise<void>\n} & MigrationServiceOptions\n\nexport function getMigrationsFromServices(services: any[]): MigrationExecutable[] {\n return services.map(service => {\n const instance = getInstance(service)\n const options = serviceMetadata.get(instance.constructor)\n if (!options._serviceType || options._serviceType !== 'migrations') {\n throw new Error(`Service ${service.name} is not a migration service`)\n }\n\n return {\n ...options.options,\n runMigration: async (context: ExecutionContext) => {\n const instance = getInstance(service) as any\n return await instance.runMigration(context)\n },\n }\n })\n}\n","import {Inject, Service} from '@orion-js/services'\nimport type {MigrationsRepo as MigrationsRepoType} from './Repo'\nimport {MigrationsRepo} from './Repo'\nimport {logger} from '@orion-js/logger'\nimport {MigrationExecutable} from './service'\nimport {ExecutionContext} from '@orion-js/dogs'\n\n@Service()\nexport class MigrationsService {\n @Inject(() => MigrationsRepo)\n private migrationsRepo: MigrationsRepoType\n\n async getNextMigration(migrationsList: MigrationExecutable[]) {\n const completedNames = await this.migrationsRepo.getCompletedMigrationNames()\n\n for (const migrationExecutable of migrationsList) {\n if (completedNames.includes(migrationExecutable.name)) continue\n return migrationExecutable\n }\n }\n\n async runMigrations(migrationsList: MigrationExecutable[], context: ExecutionContext) {\n const next = await this.getNextMigration(migrationsList)\n if (!next) return\n\n logger.info('[orionjs/migrations] Running migration...', {name: next.name})\n\n if (next.useMongoTransactions) {\n await this.runAsTransaction(next.runMigration, context)\n } else {\n await this.runMigration(next.runMigration, context)\n }\n\n logger.info('[orionjs/migrations] Migration executed correctly', {name: next.name})\n\n await this.migrationsRepo.saveCompletedMigration(next.name)\n\n await this.runMigrations(migrationsList, context)\n }\n\n async runMigration(\n func: (context: ExecutionContext) => Promise<void>,\n context: ExecutionContext,\n ) {\n try {\n await func(context)\n } catch (error) {\n logger.error('[orionjs/migrations] Error running migration', {error})\n throw error\n }\n }\n\n async runAsTransaction(\n func: (context: ExecutionContext) => Promise<void>,\n context: ExecutionContext,\n ) {\n const {client} = this.migrationsRepo.collection.client\n const session = client.startSession()\n\n await session.withTransaction(async () => {\n try {\n await func(context)\n } catch (error) {\n logger.error('[orionjs/migrations] Error running migration, will abort transaction', {\n error,\n })\n throw error\n }\n })\n\n session.endSession()\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,qBAA+B;AAC/B,oBAA8C;AAIvC,IAAM,sBAAkB,8BAAe,aAAa;AAAA,EACzD,KAAK;AAAA,IACH,UAAM,wBAAQ,OAAO;AAAA,EACvB;AAAA,EACA,MAAM;AAAA,IACJ,MAAM;AAAA,EACR;AAAA,EACA,aAAa;AAAA,IACX,MAAM;AAAA,EACR;AACF,CAAC;;;ACfD,IAAAA,kBAA+B;AAE/B,sBAAsB;AAFtB;AAIA,kCAAC,yBAAQ;A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;AACd,6BAAO,MAAM,wEAAwE;AAAA,UACnF;AAAA,QACF,CAAC;AACD,cAAM;AAAA,MACR;AAAA,IACF,CAAC;AAED,YAAQ,WAAW;AAAA,EACrB;AACF;AAhEOA,SAAA;AAEL,kBAAAA,QAAA,GAAQ,kBADR,qBADW;AAAA,oBAAN,kBAAAA,QAAA,wBADP,+BACa;AAAN,kBAAAA,QAAA,GAAM;;;AFEN,SAAS,eAAe,mBAA0B,SAAmB;AAC1E,QAAM,aAAa,0BAA0B,iBAAiB;AAC9D,MAAI,mCAAS,QAAS,QAAO;AAE7B,gCAAa;AAAA,IACX,gBAAgB;AAAA,IAChB,iBAAiB,MAAO,KAAK;AAAA;AAAA,IAC7B,cAAc;AAAA,IACd,cAAc,KAAK;AAAA,IACnB,MAAM;AAAA,MACJ,0BAAsB,uBAAU;AAAA,QAC9B,MAAM;AAAA,QACN,UAAU,KAAK;AAAA,QACf,MAAM,QAAQ,SAAS,SAAS;AAC9B,gBAAM,eAAW,8BAAY,iBAAiB;AAC9C,gBAAM,SAAS,cAAc,YAAY,OAAO;AAAA,QAClD;AAAA,MACF,CAAC;AAAA,IACH;AAAA,EACF,CAAC;AAED,SAAO;AACT;","names":["import_mongodb","import_services","instance","import_services","import_services","_init"]}
|
package/dist/index.d.cts
CHANGED
|
@@ -7,7 +7,9 @@ import { ExecutionContext } from '@orion-js/dogs';
|
|
|
7
7
|
type MigrationId = TypedId<'scnmg'>;
|
|
8
8
|
declare const MigrationSchema: {
|
|
9
9
|
_id: {
|
|
10
|
-
type: _orion_js_schema.FieldType<`scnmg-${string}
|
|
10
|
+
type: _orion_js_schema.FieldType<`scnmg-${string}`> & {
|
|
11
|
+
generateId: () => `scnmg-${string}`;
|
|
12
|
+
};
|
|
11
13
|
};
|
|
12
14
|
name: {
|
|
13
15
|
type: StringConstructor;
|
|
@@ -21,7 +23,9 @@ type MigrationType = InferSchemaType<typeof MigrationSchema>;
|
|
|
21
23
|
declare class MigrationsRepo {
|
|
22
24
|
collection: _orion_js_mongodb.Collection<_orion_js_mongodb.InferSchemaTypeWithId<{
|
|
23
25
|
_id: {
|
|
24
|
-
type: _orion_js_schema.FieldType<`scnmg-${string}
|
|
26
|
+
type: _orion_js_schema.FieldType<`scnmg-${string}`> & {
|
|
27
|
+
generateId: () => `scnmg-${string}`;
|
|
28
|
+
};
|
|
25
29
|
};
|
|
26
30
|
name: {
|
|
27
31
|
type: StringConstructor;
|
package/dist/index.d.ts
CHANGED
|
@@ -7,7 +7,9 @@ import { ExecutionContext } from '@orion-js/dogs';
|
|
|
7
7
|
type MigrationId = TypedId<'scnmg'>;
|
|
8
8
|
declare const MigrationSchema: {
|
|
9
9
|
_id: {
|
|
10
|
-
type: _orion_js_schema.FieldType<`scnmg-${string}
|
|
10
|
+
type: _orion_js_schema.FieldType<`scnmg-${string}`> & {
|
|
11
|
+
generateId: () => `scnmg-${string}`;
|
|
12
|
+
};
|
|
11
13
|
};
|
|
12
14
|
name: {
|
|
13
15
|
type: StringConstructor;
|
|
@@ -21,7 +23,9 @@ type MigrationType = InferSchemaType<typeof MigrationSchema>;
|
|
|
21
23
|
declare class MigrationsRepo {
|
|
22
24
|
collection: _orion_js_mongodb.Collection<_orion_js_mongodb.InferSchemaTypeWithId<{
|
|
23
25
|
_id: {
|
|
24
|
-
type: _orion_js_schema.FieldType<`scnmg-${string}
|
|
26
|
+
type: _orion_js_schema.FieldType<`scnmg-${string}`> & {
|
|
27
|
+
generateId: () => `scnmg-${string}`;
|
|
28
|
+
};
|
|
25
29
|
};
|
|
26
30
|
name: {
|
|
27
31
|
type: StringConstructor;
|
package/dist/index.js
CHANGED
|
@@ -187,7 +187,7 @@ function loadMigrations(migrationServices, options) {
|
|
|
187
187
|
if (options == null ? void 0 : options.omitJob) return migrations;
|
|
188
188
|
startWorkers({
|
|
189
189
|
cooldownPeriod: 1e3,
|
|
190
|
-
|
|
190
|
+
defaultLockTime: 1e3 * 60 * 20,
|
|
191
191
|
// 20 min
|
|
192
192
|
workersCount: 1,
|
|
193
193
|
pollInterval: 10 * 1e3,
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/Schema.ts","../src/Repo.ts","../src/loadMigrations/index.ts","../src/service/index.ts","../src/MigrationsService.ts"],"sourcesContent":["import {TypedId, typedId} from '@orion-js/mongodb'\nimport {InferSchemaType, schemaWithName} from '@orion-js/schema'\n\nexport type MigrationId = TypedId<'scnmg'>\n\nexport const MigrationSchema = schemaWithName('Migration', {\n _id: {\n type: typedId('scnmg'),\n },\n name: {\n type: String,\n },\n completedAt: {\n type: Date,\n },\n})\n\nexport type MigrationType = InferSchemaType<typeof MigrationSchema>\n","import {createCollection} from '@orion-js/mongodb'\nimport {MigrationSchema} from './Schema'\nimport {Service} from '@orion-js/services'\n\n@Service()\nexport class MigrationsRepo {\n public collection = createCollection({\n name: 'orionjs.migrations',\n schema: MigrationSchema,\n indexes: [],\n })\n\n async getCompletedMigrationNames() {\n const migrations = await this.collection.find().toArray()\n return migrations.map(m => m.name)\n }\n\n async saveCompletedMigration(name: string) {\n await this.collection.insertOne({\n name,\n completedAt: new Date(),\n })\n }\n}\n","import {defineJob, startWorkers} from '@orion-js/dogs'\nimport {getMigrationsFromServices} from '../service'\nimport {getInstance} from '@orion-js/services'\nimport {MigrationsService} from '../MigrationsService'\n\nexport interface Options {\n lockTime?: number\n omitJob?: boolean\n}\n\nexport function loadMigrations(migrationServices: any[], options?: Options) {\n const migrations = getMigrationsFromServices(migrationServices)\n if (options?.omitJob) return migrations\n\n startWorkers({\n cooldownPeriod: 1000,\n
|
|
1
|
+
{"version":3,"sources":["../src/Schema.ts","../src/Repo.ts","../src/loadMigrations/index.ts","../src/service/index.ts","../src/MigrationsService.ts"],"sourcesContent":["import {TypedId, typedId} from '@orion-js/mongodb'\nimport {InferSchemaType, schemaWithName} from '@orion-js/schema'\n\nexport type MigrationId = TypedId<'scnmg'>\n\nexport const MigrationSchema = schemaWithName('Migration', {\n _id: {\n type: typedId('scnmg'),\n },\n name: {\n type: String,\n },\n completedAt: {\n type: Date,\n },\n})\n\nexport type MigrationType = InferSchemaType<typeof MigrationSchema>\n","import {createCollection} from '@orion-js/mongodb'\nimport {MigrationSchema} from './Schema'\nimport {Service} from '@orion-js/services'\n\n@Service()\nexport class MigrationsRepo {\n public collection = createCollection({\n name: 'orionjs.migrations',\n schema: MigrationSchema,\n indexes: [],\n })\n\n async getCompletedMigrationNames() {\n const migrations = await this.collection.find().toArray()\n return migrations.map(m => m.name)\n }\n\n async saveCompletedMigration(name: string) {\n await this.collection.insertOne({\n name,\n completedAt: new Date(),\n })\n }\n}\n","import {defineJob, startWorkers} from '@orion-js/dogs'\nimport {getMigrationsFromServices} from '../service'\nimport {getInstance} from '@orion-js/services'\nimport {MigrationsService} from '../MigrationsService'\n\nexport interface Options {\n lockTime?: number\n omitJob?: boolean\n}\n\nexport function loadMigrations(migrationServices: any[], options?: Options) {\n const migrations = getMigrationsFromServices(migrationServices)\n if (options?.omitJob) return migrations\n\n startWorkers({\n cooldownPeriod: 1000,\n defaultLockTime: 1000 * 60 * 20, // 20 min\n workersCount: 1,\n pollInterval: 10 * 1000,\n jobs: {\n orionjsRunMigrations: defineJob({\n type: 'recurrent',\n runEvery: 30 * 1000,\n async resolve(_params, context) {\n const instance = getInstance(MigrationsService)\n await instance.runMigrations(migrations, context)\n },\n }),\n },\n })\n\n return migrations\n}\n","import {ExecutionContext} from '@orion-js/dogs'\nimport {getInstance} from '@orion-js/services'\nimport {Service} from '@orion-js/services'\n\nexport interface MigrationServiceOptions {\n name: string\n useMongoTransactions: false\n}\n\n// Define metadata storage using WeakMaps\nconst serviceMetadata = new WeakMap<any, {_serviceType: string; options: MigrationServiceOptions}>()\n\nexport function MigrationService(options: MigrationServiceOptions) {\n return (target: any, context: ClassDecoratorContext<any>) => {\n Service()(target, context)\n\n context.addInitializer(function (this) {\n serviceMetadata.set(this, {_serviceType: 'migrations', options: options})\n })\n }\n}\n\nexport type MigrationExecutable = {\n runMigration(context: ExecutionContext): Promise<void>\n} & MigrationServiceOptions\n\nexport function getMigrationsFromServices(services: any[]): MigrationExecutable[] {\n return services.map(service => {\n const instance = getInstance(service)\n const options = serviceMetadata.get(instance.constructor)\n if (!options._serviceType || options._serviceType !== 'migrations') {\n throw new Error(`Service ${service.name} is not a migration service`)\n }\n\n return {\n ...options.options,\n runMigration: async (context: ExecutionContext) => {\n const instance = getInstance(service) as any\n return await instance.runMigration(context)\n },\n }\n })\n}\n","import {Inject, Service} from '@orion-js/services'\nimport type {MigrationsRepo as MigrationsRepoType} from './Repo'\nimport {MigrationsRepo} from './Repo'\nimport {logger} from '@orion-js/logger'\nimport {MigrationExecutable} from './service'\nimport {ExecutionContext} from '@orion-js/dogs'\n\n@Service()\nexport class MigrationsService {\n @Inject(() => MigrationsRepo)\n private migrationsRepo: MigrationsRepoType\n\n async getNextMigration(migrationsList: MigrationExecutable[]) {\n const completedNames = await this.migrationsRepo.getCompletedMigrationNames()\n\n for (const migrationExecutable of migrationsList) {\n if (completedNames.includes(migrationExecutable.name)) continue\n return migrationExecutable\n }\n }\n\n async runMigrations(migrationsList: MigrationExecutable[], context: ExecutionContext) {\n const next = await this.getNextMigration(migrationsList)\n if (!next) return\n\n logger.info('[orionjs/migrations] Running migration...', {name: next.name})\n\n if (next.useMongoTransactions) {\n await this.runAsTransaction(next.runMigration, context)\n } else {\n await this.runMigration(next.runMigration, context)\n }\n\n logger.info('[orionjs/migrations] Migration executed correctly', {name: next.name})\n\n await this.migrationsRepo.saveCompletedMigration(next.name)\n\n await this.runMigrations(migrationsList, context)\n }\n\n async runMigration(\n func: (context: ExecutionContext) => Promise<void>,\n context: ExecutionContext,\n ) {\n try {\n await func(context)\n } catch (error) {\n logger.error('[orionjs/migrations] Error running migration', {error})\n throw error\n }\n }\n\n async runAsTransaction(\n func: (context: ExecutionContext) => Promise<void>,\n context: ExecutionContext,\n ) {\n const {client} = this.migrationsRepo.collection.client\n const session = client.startSession()\n\n await session.withTransaction(async () => {\n try {\n await func(context)\n } catch (error) {\n logger.error('[orionjs/migrations] Error running migration, will abort transaction', {\n error,\n })\n throw error\n }\n })\n\n session.endSession()\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,SAAiB,eAAc;AAC/B,SAAyB,sBAAqB;AAIvC,IAAM,kBAAkB,eAAe,aAAa;AAAA,EACzD,KAAK;AAAA,IACH,MAAM,QAAQ,OAAO;AAAA,EACvB;AAAA,EACA,MAAM;AAAA,IACJ,MAAM;AAAA,EACR;AAAA,EACA,aAAa;AAAA,IACX,MAAM;AAAA,EACR;AACF,CAAC;;;ACfD,SAAQ,wBAAuB;AAE/B,SAAQ,eAAc;AAFtB;AAIA,8BAAC,QAAQ;AACF,IAAM,iBAAN,MAAqB;AAAA,EACnB,aAAa,iBAAiB;AAAA,IACnC,MAAM;AAAA,IACN,QAAQ;AAAA,IACR,SAAS,CAAC;AAAA,EACZ,CAAC;AAAA,EAED,MAAM,6BAA6B;AACjC,UAAM,aAAa,MAAM,KAAK,WAAW,KAAK,EAAE,QAAQ;AACxD,WAAO,WAAW,IAAI,OAAK,EAAE,IAAI;AAAA,EACnC;AAAA,EAEA,MAAM,uBAAuB,MAAc;AACzC,UAAM,KAAK,WAAW,UAAU;AAAA,MAC9B;AAAA,MACA,aAAa,oBAAI,KAAK;AAAA,IACxB,CAAC;AAAA,EACH;AACF;AAlBO;AAAM,iBAAN,8CADP,4BACa;AAAN,4BAAM;;;ACLb,SAAQ,WAAW,oBAAmB;;;ACCtC,SAAQ,mBAAkB;AAC1B,SAAQ,WAAAA,gBAAc;AAQtB,IAAM,kBAAkB,oBAAI,QAAuE;AAE5F,SAAS,iBAAiB,SAAkC;AACjE,SAAO,CAAC,QAAa,YAAwC;AAC3D,IAAAA,SAAQ,EAAE,QAAQ,OAAO;AAEzB,YAAQ,eAAe,WAAgB;AACrC,sBAAgB,IAAI,MAAM,EAAC,cAAc,cAAc,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H;AAAA,EACF,CAAC;AAED,SAAO;AACT;","names":["Service","instance","getInstance","Service","_init","Service","getInstance"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@orion-js/migrations",
|
|
3
|
-
"version": "4.1.
|
|
3
|
+
"version": "4.1.12",
|
|
4
4
|
"main": "./dist/index.cjs",
|
|
5
5
|
"types": "./dist/index.d.ts",
|
|
6
6
|
"files": [
|
|
@@ -9,23 +9,23 @@
|
|
|
9
9
|
"author": "nicolaslopezj",
|
|
10
10
|
"license": "MIT",
|
|
11
11
|
"peerDependencies": {
|
|
12
|
-
"@orion-js/dogs": "4.1.
|
|
12
|
+
"@orion-js/dogs": "4.1.12",
|
|
13
13
|
"@orion-js/logger": "4.1.4",
|
|
14
|
-
"@orion-js/mongodb": "4.1.
|
|
14
|
+
"@orion-js/mongodb": "4.1.10"
|
|
15
15
|
},
|
|
16
16
|
"dependencies": {
|
|
17
|
-
"@orion-js/helpers": "4.1.4",
|
|
18
|
-
"@orion-js/services": "4.1.4",
|
|
19
17
|
"@orion-js/schema": "4.1.4",
|
|
20
|
-
"@orion-js/
|
|
18
|
+
"@orion-js/helpers": "4.1.4",
|
|
19
|
+
"@orion-js/typed-model": "4.1.7",
|
|
20
|
+
"@orion-js/services": "4.1.4"
|
|
21
21
|
},
|
|
22
22
|
"devDependencies": {
|
|
23
23
|
"@types/node": "^18.0.0",
|
|
24
24
|
"tsup": "^8.0.1",
|
|
25
25
|
"typescript": "^5.4.5",
|
|
26
26
|
"vitest": "^3.0.8",
|
|
27
|
-
"@orion-js/mongodb": "4.1.
|
|
28
|
-
"@orion-js/dogs": "4.1.
|
|
27
|
+
"@orion-js/mongodb": "4.1.10",
|
|
28
|
+
"@orion-js/dogs": "4.1.12"
|
|
29
29
|
},
|
|
30
30
|
"publishConfig": {
|
|
31
31
|
"access": "public"
|