diff --git a/examples/config-file-usage/src/migrations/1673525773572-first-migration-demo.ts b/examples/config-file-usage/src/migrations/1673525773572-first-migration-demo.ts index 7d1f029..1ce29d3 100644 --- a/examples/config-file-usage/src/migrations/1673525773572-first-migration-demo.ts +++ b/examples/config-file-usage/src/migrations/1673525773572-first-migration-demo.ts @@ -1,8 +1,8 @@ -import getModels from '../models' +import { UserSchema } from '../models/User' +import { Connection } from 'mongoose' -export async function up() { - const { User } = await getModels() - // Write migration here +export async function up(connection: Connection) { + const User = connection.model('User', UserSchema) await User.create([ { firstName: 'John', @@ -15,8 +15,7 @@ export async function up() { ]) } -export async function down() { - const { User } = await getModels() - // Write migration here +export async function down(connection: Connection) { + const User = connection.model('User', UserSchema) await User.deleteMany({ firstName: { $in: ['Jane', 'John'] } }).exec() } diff --git a/examples/config-file-usage/src/models/User.ts b/examples/config-file-usage/src/models/User.ts index c706f80..c5aebfa 100644 --- a/examples/config-file-usage/src/models/User.ts +++ b/examples/config-file-usage/src/models/User.ts @@ -5,7 +5,7 @@ interface IUser { lastName?: string } -const UserSchema = new Schema({ +export const UserSchema = new Schema({ firstName: { type: String, required: true, diff --git a/examples/config-file-usage/src/models/index.ts b/examples/config-file-usage/src/models/index.ts deleted file mode 100644 index 32a2593..0000000 --- a/examples/config-file-usage/src/models/index.ts +++ /dev/null @@ -1,21 +0,0 @@ -import mongoose from 'mongoose' -import mongooseOptions from '../options/mongoose' - -import User from './User' - -const getModels = async () => { - // In case you using mongoose 6 - // https://mongoosejs.com/docs/guide.html#strictQuery - mongoose.set('strictQuery', false) - - // Ensure connection is open so we can run migrations - await mongoose.connect(process.env.MIGRATE_MONGO_URI ?? 'mongodb://localhost/my-db', mongooseOptions) - - // Return models that will be used in migration methods - return { - mongoose, - User, - } -} - -export default getModels diff --git a/src/migrator.ts b/src/migrator.ts index 0ce6e3d..79cbf79 100644 --- a/src/migrator.ts +++ b/src/migrator.ts @@ -422,7 +422,7 @@ class Migrator { } try { - await migrationFunction() + await migrationFunction(this.connection) this.logMigrationStatus(direction, migration.filename) diff --git a/src/template.ts b/src/template.ts index 352aeba..777017e 100644 --- a/src/template.ts +++ b/src/template.ts @@ -1,10 +1,11 @@ -export default `// Import your models here +export default `// Import your schemas here +import { Connection } from 'mongoose' -export async function up (): Promise { +export async function up (connection: Connection): Promise { // Write migration here } -export async function down (): Promise { +export async function down (connection: Connection): Promise { // Write migration here } ` diff --git a/tests/cli.test.ts b/tests/cli.test.ts index 3e7e98c..3f1badd 100644 --- a/tests/cli.test.ts +++ b/tests/cli.test.ts @@ -160,7 +160,7 @@ export interface IExample { type: Date } -export let ExampleSchema = new Schema({ +export const ExampleSchema = new Schema({ name: { type: Schema.Types.String, required: true, @@ -174,40 +174,34 @@ export let ExampleSchema = new Schema({ export default models.User ?? model('Example', ExampleSchema)` const testTemplate = defaultTemplate - .replace( - '// Import your models here', - ` -import Example from './Example.ts' - `, - ) + .replace('// Import your schemas here', `import { ExampleSchema } from './Example.ts'`) .replace( '// Write migration here', - ` - await Example.insertMany([ - { - name: 'test', - type: 1, - }, - { - name: 'test2', - type: 1, - }, - { - name: 'test3', - type: 2, - }, - { - name: 'test4', - type: 2, - } - ])`, + `const Example = connection.model('Example', ExampleSchema) +await Example.insertMany([ + { + name: 'test', + type: 1, + }, + { + name: 'test2', + type: 1, + }, + { + name: 'test3', + type: 2, + }, + { + name: 'test4', + type: 2, + } +])`, ) .replace( '// Write migration here', - ` - await Example.deleteMany({ propertyIsNotDefinedInSchema: 'some-value' }) - await Example.deleteMany({ type: 1 }) - `, + `const Example = connection.model('Example', ExampleSchema) +await Example.deleteMany({ propertyIsNotDefinedInSchema: 'some-value' }) +await Example.deleteMany({ type: 1 })`, ) console.log(testModel) diff --git a/tests/migrator.test.ts b/tests/migrator.test.ts index 9137bcb..dd5e8c7 100644 --- a/tests/migrator.test.ts +++ b/tests/migrator.test.ts @@ -11,18 +11,15 @@ import { getConfig } from '../src/commander' describe('Tests for Migrator class - Programmatic approach', () => { const uri = `${globalThis.__MONGO_URI__}${globalThis.__MONGO_DB_NAME__}` - const migrateUri = process.env.MIGRATE_MONGO_URI // use in getModels in examples/config-file-usage/src/models/index.ts let connection: Connection beforeEach(async () => { - process.env.MIGRATE_MONGO_URI = uri clearDirectory('migrations') connection = await mongoose.createConnection(uri).asPromise() await connection.collection('migrations').deleteMany({}) }) afterEach(async () => { - process.env.MIGRATE_MONGO_URI = migrateUri if (connection.readyState !== 0) { await connection.close() } @@ -468,6 +465,10 @@ describe('Tests for Migrator class - Programmatic approach', () => { const { migrationsInFs } = await migrator.getMigrations() expect(migrationsInFs).toHaveLength(1) + + expect(migrator.connection.readyState).toBe(1) + await migrator.close() + expect(migrator.connection.readyState).toBe(0) }) it('should get migration .js files', async () => { @@ -476,6 +477,10 @@ describe('Tests for Migrator class - Programmatic approach', () => { const { migrationsInFs } = await migrator.getMigrations() expect(migrationsInFs).toHaveLength(1) + + expect(migrator.connection.readyState).toBe(1) + await migrator.close() + expect(migrator.connection.readyState).toBe(0) }) it('should same filename when migrate using .ts files', async () => { @@ -491,6 +496,10 @@ describe('Tests for Migrator class - Programmatic approach', () => { const migrations = await migrator.sync() expect(migrationsInFs[0].filename).toBe(migrations[0].filename) + + expect(migrator.connection.readyState).toBe(1) + await migrator.close() + expect(migrator.connection.readyState).toBe(0) }) it('should same filename when migrate using .js files', async () => { @@ -506,6 +515,10 @@ describe('Tests for Migrator class - Programmatic approach', () => { const migrations = await migrator.sync() expect(migrationsInFs[0].filename).toBe(migrations[0].filename) + + expect(migrator.connection.readyState).toBe(1) + await migrator.close() + expect(migrator.connection.readyState).toBe(0) }) it('should run up/down when migrate using .ts files', async () => { @@ -558,6 +571,10 @@ describe('Tests for Migrator class - Programmatic approach', () => { .find({ firstName: { $in: ['Jane', 'John'] } }) .toArray() expect(deletedUsers).toHaveLength(0) + + expect(migrator.connection.readyState).toBe(1) + await migrator.close() + expect(migrator.connection.readyState).toBe(0) }) it('should run up/down when migrate using .js files', async () => { @@ -610,5 +627,9 @@ describe('Tests for Migrator class - Programmatic approach', () => { .find({ firstName: { $in: ['Jane', 'John'] } }) .toArray() expect(deletedUsers).toHaveLength(0) + + expect(migrator.connection.readyState).toBe(1) + await migrator.close() + expect(migrator.connection.readyState).toBe(0) }) })