-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpopulatedb.js
127 lines (109 loc) · 2.91 KB
/
populatedb.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#! /usr/bin/env node
console.log('This script populated some test data into the database\n\n')
// Get arguments passed on command line
var userArgs = process.argv.slice(2);
/*
if (!userArgs[0].startsWith('mongodb')) {
console.log('ERROR: You need to specify a valid mongodb URL as the first argument');
return
}
*/
const async = require('async')
const Album = require('./models/album')
const Composer = require('./models/composer')
const Genre = require('./models/genre')
const Lyricist = require('./models/lyricist')
const Singers = require('./models/singers')
const Song = require('./models/song')
var mongoose = require('mongoose');
var mongoDB = userArgs[0];
mongoose.connect(mongoDB, { useNewUrlParser: true, useUnifiedTopology: true });
mongoose.Promise = global.Promise;
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'MongoDB connection error:'));
let songs = []
let albums = []
function songCreate(song_name, language, composer_name, lyricist_name, singer_names, album_name, genre_name, cb) {
songDetail = {
song_name: song_name,
language: language
}
if(composer_name) {
songDetail.composer_name = composer_name
}
if(lyricist_name) {
songDetail.lyricist_name = lyricist_name
}
if(singer_names) {
songDetail.singer_names = singer_names
}
if(album_name) {
songDetail.album_name = album_name
}
if(singer_names) {
songDetail.genre_name = genre_name
}
let song = new Song(songDetail)
song.save(function(err) {
if(err) {
cb(err, null)
return
}
console.log('New song: ' + song)
songs.push(song)
cb(null, song)
})
}
function albumCreate(album_name, release_year, composer_name) {
albumDetail = {
album_name: album_name
}
if(release_year){
albumDetail.release_year = release_year
}
if (composer_name) {
albumDetail.composer_name = composer_name
}
let album = new Album(albumDetail)
album.save(function(err) {
if(err) {
cb(err, null)
return
}
console.log('New album: ' + album)
album.push(album)
cb(null, album)
})
}
function fillSongs(cb) {
async.series([
function(callback) {
songCreate('Rehna tu', 'Hindi', 'AR Rahman', 'Prasoon Joshi', ['AR Rahman', 'Benny Dayal', 'Tanvi'], 'Delhi-6', 'Romantic', callback)
},
function(callback) {
songCreate('Dil Se Re', 'Hindi', 'AR Rahman', 'Mehboob', ['AR Rahman'], 'Dil Se', 'Romantic', callback)
},
function(callback) {
songCreate('The Magic Flute', 'Instrumental', 'Mozart', 'Mozart', ['Mozart'], 'Magic Flute', 'ABC', callback)
},
], cb)
}
function fillAlbums(cb) {
async.series([
function(callback) {
albumCreate('Rehna Tu', 2009, 'AR Rahman', callback)
},
function(callback) {
albumCreate('Dil Se', 1998, 'AR Rahman', callback)
}
],cb )
}
async.series([
fillSongs,
fillAlbums
],
function(err, ressults) {
if(err) { console.log('FINAL ERR: \n' +err) }
else { console.log('CURRENT INFO: \nSongs:' + songs + '\nAlbums:' +albums) }
mongoose.connection.close()
})