-
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.
feat: New watchlist migration from users MongoDB collection
- Loading branch information
1 parent
7013461
commit c786989
Showing
2 changed files
with
87 additions
and
7 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
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
67
migrations/20240604090347-migrate-watchlist-to-new-collection.js
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,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(); | ||
} | ||
}, | ||
}; |