-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmigration.js
40 lines (37 loc) · 1.45 KB
/
migration.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
const mongoose = require('mongoose')
const Category = require('./models/category')
const CategorySeeds = require('./seeds/category')
const uri = "mongodb://qasim:[email protected]:27017,abdulla-shard-00-01.eftvp.mongodb.net:27017,abdulla-shard-00-02.eftvp.mongodb.net:27017/ioffer?ssl=true&replicaSet=abdulla-shard-0&authSource=admin&retryWrites=true&w=majority";
mongoose.connect(uri);
mongoose.Promise = global.Promise;
let array = [];
let keyarray = [];
(async () => {
mongoose.connection.once('open', async () => {
await Category.deleteMany({});
console.log(' 🍃 connected to mongoDB mLab', CategorySeeds);
await convertToArray(CategorySeeds);
console.log('keyarray:',keyarray)
console.log('array:',array)
})
})()
async function convertToArray(object = null, parentCategory = null, level = 0) {
for (const [key, value] of Object.entries(object)) {
let parentCategoryId = parentCategory ? parentCategory.id : null
let newCategory = new Category({
title: key,
level,
parentCategory: parentCategoryId,
subCategories: []
})
if (value !== {}) {
await convertToArray(value, newCategory, level + 1);
}
if (parentCategory) {
parentCategory.subCategories.push(newCategory.id)
}
keyarray.push(key);
array.push(newCategory)
await newCategory.save()
}
}