Last active
July 1, 2017 07:35
-
-
Save vtanathip/6388752 to your computer and use it in GitHub Desktop.
This is the way to insert initial data into MongoDB via grunt task.
Thx to this plugin: https://github.com/sindresorhus/grunt-shell
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
//this is json array that MongoDB will easier read and import into it | |
//for the record if you didn't use json array MongoDB will read data per line so don't forget to re-format it | |
[ | |
{ name: "Widget 1", desc: "This is Widget 1" }, | |
{ name: "Widget 2", desc: "This is Widget 2" } | |
] |
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
//I assume that you install grunt-shell | |
shell: { | |
options: { // Put this option if you want to see output when run shell script | |
stdout: true, | |
stderr: true, | |
failOnError: true | |
}, | |
//remove exitisting database | |
remove_init_data: { | |
command: 'mongo vCommerce --eval "db.dropDatabase()"' | |
}, | |
//create database/ import data from init_data.json into databse | |
add_init_data: { | |
command: 'mongoimport --db vCommerce --collection initData --type json --file server/init_data.json --jsonArray' | |
} | |
} | |
//grunt prepare-db | |
//run once use anywhere ... this task should run once when you prepare your project | |
//because if you run it again exitisting data you store it will deleted too. | |
grunt.registerTask('prepare-db', [ | |
'shell:remove_init_data', | |
'shell:add_init_data' | |
]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Nicely done! Thanks :)