Last active
June 4, 2020 18:38
-
-
Save l337quez/1c1c14c6baa0ae5c5787994a89b1e944 to your computer and use it in GitHub Desktop.
Script que permite poblar una colecion de una base de datos de Mongo usando mongoose. El script hace la conexion con mongo y comienza a poblar la coleccion
This file contains hidden or 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
// Create by Ronal https://ronaldl337.wordpress.com/ | |
// Poblar coleccion de mongo usando Mongoose | |
'use strict' | |
const mongoose = require('mongoose') | |
// Agregamos la URI con el nombre de la base de datos al final | |
const uri = 'mongodb://localhost/comidas' | |
mongoose.connect(uri, { useNewUrlParser: true, useUnifiedTopology: true }); | |
const db =mongoose.createConnection(uri, { useNewUrlParser: true, useUnifiedTopology: true }); | |
// Creamos el esquema | |
const Schema = mongoose.Schema | |
console.log("STARTING SCRIPT"); | |
// Creamos el Schema | |
const testSchema = new Schema({ | |
entity: String, | |
scope: Number, | |
actionType: Number, | |
}, { | |
versionKey: false | |
}); // con versionKey eliminamos __v:0 | |
// Creamos el Modelo a partir del Schema | |
// Cambie el nombre de la coleccion 'test' | |
const Test = db.model('test', testSchema) | |
// BORRAMOS TODA LA COLECCION | |
//db.collection('test').drop() | |
function permiso(action, scope, entity){ | |
return { | |
"entity": entities[entity], | |
"scope":scope, | |
"actionType":action | |
} | |
} | |
var contador= 0; | |
var entities = [ "combo", "detailTypeStore","food","cereal","dairy","drink","fruit","nut","protein","sauce","vegetable","typefood","typestore","user","store"] | |
console.log(" TAMAÑO DE ENTITES") | |
console.log(entities.length) | |
for (var entity = 0; entity < entities.length; entity++) { | |
for (var scope = 1; scope < 4; scope++) { | |
for (var action = 1; action < 5; action++) { | |
console.log((permiso(action, scope, entity))) | |
// vamos guardandado cada objeto en un documento | |
const test = new Test(permiso(action, scope, entity)) | |
//test.save() | |
Test.create(test) | |
} | |
} | |
contador=contador+1; | |
console.log("Se han agregado " + contador + " grupos de entidades") | |
} | |
// salimos del script 2 segundos despues de haber insertado los documentos | |
setTimeout((function() { | |
return process.exit(1); | |
}), 2000); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment