Skip to content

Commit

Permalink
feat: New watchlist migration from users MongoDB collection
Browse files Browse the repository at this point in the history
  • Loading branch information
justinhartman committed Jun 4, 2024
1 parent 7013461 commit c786989
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 7 deletions.
27 changes: 20 additions & 7 deletions migrations/20240529104825-create-user-collection.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,43 @@
/**
* User collection migration.
* @module migrations/create-users
* @module migrations/createUsers
* @description This module contains a migration function that creates a 'users' collection in the database.
*/
module.exports = {
/**
* Migrates the database schema by creating a 'users' collection.
*
* @param {import('mongodb').Db} db - The MongoDB database instance.
* @param {import('mongodb').MongoClient} client - The MongoDB client instance.
*
* @returns {Promise<void>} A Promise that resolves when the migration is complete.
* @see https://github.com/seppevs/migrate-mongo/#creating-a-new-migration-script
* @example await db.collection('albums').updateOne({artist: 'The Beatles'}, {$set: {blacklisted: true}});
*/
async up(db, client) {
await db.createCollection('users');
const session = client.startSession();
try {
await session.withTransaction(async () => {
await db.createCollection('users');
});
} finally {
await session.endSession();
}
},

/**
* Migrates the database schema by dropping the 'users' collection.
*
* @param {import('mongodb').Db} db - The MongoDB database instance.
* @param {import('mongodb').MongoClient} client - The MongoDB client instance.
*
* @returns {Promise<void>} A Promise that resolves when the migration is complete.
* @example await db.collection('albums').updateOne({artist: 'The Beatles'}, {$set: {blacklisted: false}});
*/
async down(db, client) {
await db.collection('users').drop();
const session = client.startSession();
try {
await session.withTransaction(async () => {
await db.collection('users').drop();
});
} finally {
await session.endSession();
}
}
};
67 changes: 67 additions & 0 deletions migrations/20240604090347-migrate-watchlist-to-new-collection.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
const mongoose = require('mongoose');

/**
* Watchlist collection migration.
* @module migrations/migrateWatchlists
* @description This module contains a migration function that creates a 'watchlist' collection in the database.
*/
module.exports = {
/**
* This function migrates the 'watchlist' field from 'users' collection to a new 'watchlists' collection.
* @param {Object} db - The MongoDB database instance.
* @param {Object} client - The MongoDB client instance.
* @returns {Promise<void>}
*/
async up(db, client) {
const session = client.startSession();
const users = await db.collection('users').find({ watchlist: { $exists: true, $ne: [] } }).toArray();
const watchlists = users.map(user => ({
userId: user._id,
items: user.watchlist.map(item => ({
_id: new mongoose.Types.ObjectId(),
type: item.type,
imdbId: item.imdbID,
title: item.title,
poster: item.poster,
})),
}));

try {
await session.withTransaction(async () => {
if (watchlists.length > 0) await db.collection('watchlists').insertMany(watchlists);
await db.collection('users').updateMany({}, { $unset: { watchlist: 1 } });
});
} finally {
await session.endSession();
}
},

/**
* This function reverts the migration by moving the 'watchlist' items back to the 'users' collection.
* @param {Object} db - The MongoDB database instance.
* @param {Object} client - The MongoDB client instance.
* @returns {Promise<void>}
*/
async down(db, client) {
const session = client.startSession();
const watchlists = await db.collection('watchlists').find().toArray();

try {
await session.withTransaction(async () => {
for (const watchlist of watchlists) {
await db.collection('users').updateOne(
{ _id: watchlist.userId },
{
$set: {
watchlist: watchlist.items,
},
}
);
}
await db.collection('watchlists').deleteMany({});
});
} finally {
await session.endSession();
}
},
};

0 comments on commit c786989

Please sign in to comment.