mongoose-fixture-csv
v0.2.1
Published
Import entity form csv file to mongodb using mongoose scheme.
Downloads
8
Readme
mongoose-fixture-csv
Parse CSV file and it will make mongoose model to save on Mongodb.
npm install mongoose-fixture-csv
Using
You fixture file
const fixtureCSV = require("mongoose-fixture-csv");
const mongoose = require("mongoose");
// Models
const Usuarios = require("./models/usuario");
const csvModel = [{ csv: "usuarios.csv", model: Usuarios }];
mongoose.connect(
"mongodb://localhost/fixture-test",
{ useCreateIndex: true, useNewUrlParser: true },
err => {
if (err) { return console.error(err); }
else {
Usuarios.deleteMany({}).then(() =>
fixtureCSV(csvModel, { basePath: __dirname + "/csv/", delimiter: ";" })
.catch(err => {
console.error(err.message);
})
.then(objectIDs => {
console.log(objectIDs);
})
.finally(() => {
mongoose.disconnect();
});
)
}
}
);
Mongoose Model
const schema = new Schema({
name: String,
mail: String,
password: String
});
schema.pre("save", function(next) {
if (this.isModified("password")) {
this.password = crypto
.createHash("sha256")
.update(this.password)
.digest("hex");
}
next();
});
module.exports = mongoose.model("Usuarios", schema);
CSV File
|name|mail|password| |:---|:---|:-------| |Matias|[email protected]|123456| |Ezequiel|[email protected]|123456|
Fixture Options
options = {
showSave: false,
delimiter: ";",
basePath: undefined,
skipUndefined: true
}
Sub object
Mongoose Models
const mongoose = require("mongoose");
const schema = mongoose.Schema({
name: String,
pets: [
{
name: String,
age: Number
}
]
});
module.exports = mongoose.model("Person", schema);
CSV File
For pets
|id|name|age|person_id| |:-|:---|:--|:--------| |1|pet_id_1_p0|10|0| |2|pet_id_2_p1|20|1| |3|pet_id_3_p1|30|1| |4|pet_id_4_p0|40|0|
For person
|name|id| |:---|:-| |persona_id_0|0| |persona_id_1|1| |persona_id_2|2|
You fixture file
const fixtureCSV = require("./../index");
const mongoose = require("mongoose");
// Models
const csvModel = [
{
csv: "person.csv",
model: require("./models/person"),
csvFieldId: "id", // default index
pets: {
csv: "pets.csv",
csvFieldId: "id",
ref: "person_id"
}
}
];
mongoose.connect(
"mongodb://localhost/fixture-test",
{ useCreateIndex: true, useNewUrlParser: true },
err => {
if (err) { return console.error(err); }
else {
fixtureCSV(csvModel, { basePath: __dirname + "/csv/" })
.catch(err => { console.error(err.message); return; })
.finally(() => { mongoose.disconnect();});
}
}
);
With sub models
const fixtureCSV = require("mongoose-fixture-csv");
const Group = require("./../../../models/grupo_componente");
fixtureCSV([{ csv, model: require("./../../../models/componente") }], {
basePath: __dirname + "/"
}).then(objectIDs => {
new Group({ nombre: "Grupos", componentes: objectIDs.componentes }).save((err, general) => {
if(err) { console.error(err) }
else { console.log(general) }
});
});