Files
v3-api/migration.js

59 lines
1.9 KiB
JavaScript

const { MongoClient } = require('mongodb');
async function migrateUsers() {
const v1_uri = 'mongodb://localhost:27017/chatbot-parsian';
const v2_uri = 'mongodb://localhost:27017/chatbot-v2-api';
const clientV1 = new MongoClient(v1_uri);
const clientV2 = new MongoClient(v2_uri);
try {
await clientV1.connect();
await clientV2.connect();
const dbV1 = clientV1.db();
const dbV2 = clientV2.db();
const usersV1 = dbV1.collection('users');
const usersV2 = dbV2.collection('users'); // Assuming the collection name is also 'users' in v2
const cursor = usersV1.find({});
const usersToMigrate = [];
for await (const userV1 of cursor) {
const userV2 = {
mobile: userV1.identity,
username: userV1.identity,
role: userV1.userType || 'user', // Default to 'user' if not specified
nationalCode: '',
otp: null,
createdAt: userV1.createdAt || new Date(),
updatedAt: userV1.updatedAt || new Date(),
family: '',
name: '',
birthDate: '',
address: '',
email: '',
otpAttempts: 0,
otpCreatedAt: null,
// Add any other fields from v1 that map directly to v2, or set default values for new v2 fields
};
usersToMigrate.push(userV2);
}
if (usersToMigrate.length > 0) {
await usersV2.insertMany(usersToMigrate);
console.log(`Successfully migrated ${usersToMigrate.length} users.`);
} else {
console.log('No users found to migrate.');
}
} catch (error) {
console.error('Error during migration:', error);
} finally {
await clientV1.close();
await clientV2.close();
}
}
migrateUsers();