Skip to content

Instantly share code, notes, and snippets.

@muhammadfaizan
Last active October 24, 2017 10:16
Show Gist options
  • Save muhammadfaizan/dcb96c968c4231e66c0e1544f6b6b06a to your computer and use it in GitHub Desktop.
Save muhammadfaizan/dcb96c968c4231e66c0e1544f6b6b06a to your computer and use it in GitHub Desktop.
To seed value with migration

Title

Function of migration to populate data in database with reference data.

Summary

  • up and down functions uploads and removes data from database respectively
/*
* Copyright (c) 2017
*
* Author: Muhammad Faizan
*/
require('dotenv').config();
const { MongoClient, ObjectID } = require('mongodb');
const url = process.env.DB_URI;
const documents = require('./industrytypes.json');
const connect = dbUrl => new Promise((resolve, reject) => {
MongoClient.connect(dbUrl, (err, db) => {
if (err) reject(err);
else resolve(db);
});
});
const createCollection = (db, collectionName) => new Promise((resolve, reject) => {
db.createCollection(collectionName, (err, collection) => {
if (err) reject(err);
else resolve(collection);
});
});
const task = (db, collection, docs) => {
if (!db || !collection || !docs) {
throw new Error('fields missing');
}
const bulk = collection.initializeUnorderedBulkOp();
docs.forEach((_doc) => {
const doc = _doc;
if (doc._id && typeof doc._id === 'string') {
doc._id = ObjectID(doc._id);
}
if (doc.parent && typeof doc.parent === 'string') {
doc.parent = ObjectID(doc.parent);
}
bulk.insert(doc);
});
return new Promise((resolve, reject) => {
bulk.execute((err, result) => {
if (err) reject(err);
else resolve(result);
});
});
};
exports.up = async function up(next) {
try {
const db = await connect(url);
const collection = await createCollection(db, 'industrytypes');
await task(db, collection, documents);
next();
} catch (err) {
throw err;
}
};
exports.down = function down(next) {
next();
};
[
{
"_id": "59d7155c9e9d0b30b44d3678",
"name": "Agriculture, Forestry, Fishing & Hunting",
"industryCode": "11",
"level" : 1
},
{
"_id": "59d7155c9e9d0b30b44d3679",
"name": "Animal Production",
"parent": "59d7155c9e9d0b30b44d3678",
"industryCode": "111",
"level" : 2
},
{
"_id": "59d7155c9e9d0b30b44d367a",
"name": "Crop Production",
"parent": "59d7155c9e9d0b30b44d3678",
"industryCode": "112",
"level" : 2
},
{
"_id": "59d7155c9e9d0b30b44d367b",
"name": "Forestry, Logging",
"parent": "59d7155c9e9d0b30b44d3678",
"industryCode": "113",
"level" : 2
},
{
"_id": "59d7155c9e9d0b30b44d367c",
"name": "Fishing, Hunting & Trapping",
"parent": "59d7155c9e9d0b30b44d3678",
"industryCode": "114",
"level" : 2
},
{
"_id": "59d7155c9e9d0b30b44d367d",
"name": "Agricultural Support Activities",
"parent": "59d7155c9e9d0b30b44d3678",
"industryCode": "115",
"level" : 2
},
{
"_id": "59d7155c9e9d0b30b44d3681",
"name": "Mining",
"industryCode": "21",
"level" : 1
},
{
"_id": "59d7155c9e9d0b30b44d3682",
"name": "Oil and Gas Extraction",
"parent": "59d7155c9e9d0b30b44d3681",
"industryCode": "2111",
"level" : 2
},
{
"_id": "59d7155c9e9d0b30b44d3683",
"name": "Coal Mining",
"parent": "59d7155c9e9d0b30b44d3681",
"industryCode": "2121",
"level" : 2
},
{
"_id": "59d7155c9e9d0b30b44d3684",
"name": "Metal Ore Mining",
"parent": "59d7155c9e9d0b30b44d3681",
"industryCode": "2122",
"level" : 2
},
{
"_id": "59d7155c9e9d0b30b44d3685",
"name": "Nonmetallic Mineral Mining and Quarrying",
"parent": "59d7155c9e9d0b30b44d3681",
"industryCode": "2123",
"level" : 2
},
{
"_id": "59d7155c9e9d0b30b44d3686",
"name": "Support Activities for Oil Operations",
"parent": "59d7155c9e9d0b30b44d3681",
"industryCode": "213111",
"level" : 2
},
{
"_id": "59d7155c9e9d0b30b44d3687",
"name": "Support Activities for Gas Operations",
"parent": "59d7155c9e9d0b30b44d3681",
"industryCode": "213112",
"level" : 2
},
{
"_id": "59d7155c9e9d0b30b44d3688",
"name": "Support Activities for Solid Mineral Operations",
"parent": "59d7155c9e9d0b30b44d3681",
"industryCode": "213113",
"level" : 2
},
{
"_id": "59d7155c9e9d0b30b44d3691",
"name": "Utilities",
"industryCode": "22",
"level" : 1
},
{
"_id": "59d7155c9e9d0b30b44d3692",
"name": "Electric Power Generation, Transmission and Distribution",
"parent": "59d7155c9e9d0b30b44d3691",
"industryCode": "2211",
"level" : 2
},
{
"_id": "59d7155c9e9d0b30b44d3693",
"name": "Natural Gas Distribution",
"parent": "59d7155c9e9d0b30b44d3691",
"industryCode": "2212",
"level" : 2
},
{
"_id": "59d7155c9e9d0b30b44d3694",
"name": "Water, Sewage and Other Systems",
"parent": "59d7155c9e9d0b30b44d3691",
"industryCode": "2213",
"level" : 2
},
{
"_id": "59d7155c9e9d0b30b44d36a1",
"name": "Construction",
"industryCode": "23",
"level" : 1
},
{
"_id": "59d7155c9e9d0b30b44d36a2",
"name": "Construction of Buildings",
"parent": "59d7155c9e9d0b30b44d36a1",
"industryCode": "236",
"level" : 2
},
{
"_id": "59d7155c9e9d0b30b44d36a3",
"name": "Heavy and Civil Engineering Construction",
"parent": "59d7155c9e9d0b30b44d36a1",
"industryCode": "237",
"level" : 2
},
{
"_id": "59d7155c9e9d0b30b44d36a4",
"name": "Specialty Trade Contractors",
"parent": "59d7155c9e9d0b30b44d36a1",
"industryCode": "238",
"level" : 2
},
{
"_id": "59d7155c9e9d0b30b44d36b1",
"name": "Health Care and Social Assistance",
"industryCode": "62",
"level" : 1
},
{
"_id": "59d7155c9e9d0b30b44d36b2",
"name": "Offices of Physicians",
"parent": "59d7155c9e9d0b30b44d36b1",
"industryCode": "6212",
"level" : 2
},
{
"_id": "59d7155c9e9d0b30b44d36b3",
"name": "Offices of Dentists and Other Health Practitioners",
"parent": "59d7155c9e9d0b30b44d36b1",
"industryCode": "6212",
"level" : 2
},
{
"_id": "59d7155c9e9d0b30b44d36b4",
"name": "Medical and Diagnostics Laboratories",
"parent": "59d7155c9e9d0b30b44d36b1",
"industryCode": "6215",
"level" : 2
},
{
"_id": "59d7155c9e9d0b30b44d36b5",
"name": "Home Health Care Services",
"parent": "59d7155c9e9d0b30b44d36b1",
"industryCode": "6216",
"level" : 2
},
{
"_id": "59d7155c9e9d0b30b44d36b6",
"name": "Outpatient Care Centers",
"parent": "59d7155c9e9d0b30b44d36b1",
"industryCode": "6214",
"level" : 2
},
{
"_id": "59d7155c9e9d0b30b44d36b7",
"name": "Other Ambulatory Health Care Services",
"parent": "59d7155c9e9d0b30b44d36b1",
"industryCode": "6219",
"level" : 2
},
{
"_id": "59d7155c9e9d0b30b44d36b8",
"name": "General Medical and Surgical Hospitals",
"parent": "59d7155c9e9d0b30b44d36b1",
"industryCode": "6221",
"level" : 2
},
{
"_id": "59d7155c9e9d0b30b44d36b9",
"name": "Psychiatric, Substance Abuse and Specialty Hospitals",
"parent": "59d7155c9e9d0b30b44d36b1",
"industryCode": "6222",
"level" : 2
},
{
"_id": "59d7155c9e9d0b30b44d36ba",
"name": "Nursing and Residential Care Facilities",
"parent": "59d7155c9e9d0b30b44d36b1",
"industryCode": "623",
"level" : 2
},
{
"_id": "59d7155c9e9d0b30b44d36bb",
"name": "Social Assistance",
"parent": "59d7155c9e9d0b30b44d36b1",
"industryCode": "624",
"level" : 2
},
{
"_id": "59d7155c9e9d0b30b44d36bc",
"name": "Child Day Care Services",
"parent": "59d7155c9e9d0b30b44d36b1",
"industryCode": "6244",
"level" : 2
}
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment