-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14 from hellivan/develop
New release
- Loading branch information
Showing
9 changed files
with
307 additions
and
150 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,15 @@ | ||
export { AbstractMigrator, VersionInformation, VersionStorage } from './abstract-migrator'; | ||
export { CollectionMigrationHandler } from './mongodb-collection-migrator'; | ||
export { ModelMigrationHandler } from './mongoose-model-migrator'; | ||
export { | ||
CollectionMigrationHandler, | ||
CollectionMigratorOptions, | ||
migrateCollection | ||
} from './mongodb-collection-migrator'; | ||
export { ModelMigrationHandler, ModelMigratorOptions, migrateModel } from './mongoose-model-migrator'; | ||
migrateCollection, | ||
readCollectionVersion, | ||
writeCollectionVersion | ||
} from './mongodb-collection-versioning-utils'; | ||
export { | ||
ModelMigratorOptions, | ||
migrateModel, | ||
readModelVersion, | ||
writeModelVersion | ||
} from './mongoose-model-versioning-utils'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { MongodbCollectionVersionStorage } from './mongodb-collection-version-storage'; | ||
import { VersionInformation } from './abstract-migrator'; | ||
|
||
class VersionCollectionMock { | ||
private versionInformation: VersionInformation | null; | ||
|
||
constructor(versionInformation?: VersionInformation) { | ||
this.versionInformation = versionInformation ?? null; | ||
} | ||
|
||
public async findOne(_filter: unknown): Promise<VersionInformation | null> { | ||
return this.versionInformation; | ||
} | ||
public async insertOne(versionInformation: VersionInformation): Promise<void> { | ||
this.versionInformation = versionInformation; | ||
} | ||
public async findOneAndUpdate(filter: unknown, updateOptions: { $set: VersionInformation }): Promise<void> { | ||
this.versionInformation = { ...this.versionInformation, ...updateOptions.$set }; | ||
} | ||
} | ||
|
||
describe('MongodbCollectionVersionStorage', () => { | ||
test('read version should return the current version information', async () => { | ||
const currentVersion = { current: 1, updated: new Date() }; | ||
const collectionMock = new VersionCollectionMock(currentVersion); | ||
const versionStorage = new MongodbCollectionVersionStorage(collectionMock as any); | ||
const findOneSpy = jest.spyOn(collectionMock, 'findOne'); | ||
|
||
const resultVersion = await versionStorage.readVersion(); | ||
expect(resultVersion).toEqual(currentVersion); | ||
expect(findOneSpy).toHaveBeenCalledWith({}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { Db } from 'mongodb'; | ||
|
||
import { VersionInformation } from './abstract-migrator'; | ||
import { CollectionMigrationHandler, CollectionMigrator } from './mongodb-collection-migrator'; | ||
import { MongodbCollectionVersionStorage } from './mongodb-collection-version-storage'; | ||
import { getGlobalMongooseConnectionDb } from './utils'; | ||
|
||
export interface CollectionMigratorOptions { | ||
db?: Db; | ||
versionCollectionName?: string; | ||
} | ||
|
||
async function sanitizeCollectionMigratorOptions( | ||
collectionName: string, | ||
options?: CollectionMigratorOptions | ||
): Promise<Required<CollectionMigratorOptions>> { | ||
const versionCollectionName = options?.versionCollectionName || `${collectionName}.version`; | ||
const db = options?.db ?? (await getGlobalMongooseConnectionDb()); | ||
return { db, versionCollectionName }; | ||
} | ||
|
||
export async function migrateCollection( | ||
collectionName: string, | ||
version: number, | ||
migrationHandler: CollectionMigrationHandler, | ||
options?: CollectionMigratorOptions | ||
): Promise<void> { | ||
const { db, versionCollectionName } = await sanitizeCollectionMigratorOptions(collectionName, options); | ||
const migrator = new CollectionMigrator(db, collectionName, versionCollectionName, migrationHandler); | ||
await migrator.migrate(version); | ||
} | ||
|
||
export async function writeCollectionVersion( | ||
collectionName: string, | ||
version: number, | ||
options?: CollectionMigratorOptions | ||
): Promise<VersionInformation> { | ||
const { db, versionCollectionName } = await sanitizeCollectionMigratorOptions(collectionName, options); | ||
const versionStorage = new MongodbCollectionVersionStorage(db.collection(versionCollectionName)); | ||
return versionStorage.writeVersion(version); | ||
} | ||
|
||
export async function readCollectionVersion( | ||
collectionName: string, | ||
options?: CollectionMigratorOptions | ||
): Promise<VersionInformation | null> { | ||
const { db, versionCollectionName } = await sanitizeCollectionMigratorOptions(collectionName, options); | ||
const versionStorage = new MongodbCollectionVersionStorage(db.collection(versionCollectionName)); | ||
return versionStorage.readVersion(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import { Document, Model } from 'mongoose'; | ||
import { Db } from 'mongodb'; | ||
|
||
import { ModelMigrationHandler, ModelMigrator } from './mongoose-model-migrator'; | ||
import { getGlobalMongooseConnectionDb } from './utils'; | ||
import { VersionInformation } from './abstract-migrator'; | ||
import { MongodbCollectionVersionStorage } from './mongodb-collection-version-storage'; | ||
|
||
export interface ModelMigratorOptions { | ||
db?: Db; | ||
versionCollectionName?: string; | ||
} | ||
|
||
async function sanitizeModelMigratorOptions<TModelDocument extends Document>( | ||
model: Model<TModelDocument>, | ||
options?: ModelMigratorOptions | ||
): Promise<Required<ModelMigratorOptions>> { | ||
const versionCollectionName = options?.versionCollectionName || `${model.collection.collectionName}.version`; | ||
const db = options?.db ?? (await getGlobalMongooseConnectionDb()); | ||
return { db, versionCollectionName }; | ||
} | ||
|
||
export async function migrateModel<TModelDocument extends Document>( | ||
model: Model<TModelDocument>, | ||
version: number, | ||
migrationHandler: ModelMigrationHandler<TModelDocument>, | ||
options?: ModelMigratorOptions | ||
): Promise<void> { | ||
const { db, versionCollectionName } = await sanitizeModelMigratorOptions(model, options); | ||
const migrator = new ModelMigrator(db, model, versionCollectionName, migrationHandler); | ||
await migrator.migrate(version); | ||
} | ||
|
||
export async function writeModelVersion<TModelDocument extends Document>( | ||
model: Model<TModelDocument>, | ||
version: number, | ||
options?: ModelMigratorOptions | ||
): Promise<VersionInformation> { | ||
const { db, versionCollectionName } = await sanitizeModelMigratorOptions(model, options); | ||
const versionStorage = new MongodbCollectionVersionStorage(db.collection(versionCollectionName)); | ||
return versionStorage.writeVersion(version); | ||
} | ||
|
||
export async function readModelVersion<TModelDocument extends Document>( | ||
model: Model<TModelDocument>, | ||
options?: ModelMigratorOptions | ||
): Promise<VersionInformation | null> { | ||
const { db, versionCollectionName } = await sanitizeModelMigratorOptions(model, options); | ||
const versionStorage = new MongodbCollectionVersionStorage(db.collection(versionCollectionName)); | ||
return versionStorage.readVersion(); | ||
} |
Oops, something went wrong.