-
-
Save y8/4d0d3549f90572ee66067f12b7a4c5e9 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
mongoose.connect('mongodb://localhost:27017/myapp', { useNewUrlParser: true }) | |
const modelSchema = new Schema( | |
{ | |
entity: String, | |
scope: Number, | |
actionType: Number | |
}, | |
{ versionKey: false } | |
) | |
const Test = mongoose.model('Test', modelSchema) | |
function permiso (action, scope, entity) { | |
return { | |
entity: entity, | |
scope: scope, | |
actionType: action | |
} | |
} | |
var contador = 0 | |
var entities = ['user', 'store'] | |
for (const entity of entities) { | |
for (var scope = 1; scope < 4; scope++) { | |
for (var action = 1; action < 5; action++) { | |
const attributes = permiso(action, scope, entity) | |
const doc = new Test(attributes) | |
doc.save(function (err) { | |
if (err) return console.error(`Can't save '${attributes}': ${err}`) | |
console.log(`Saved: ${attributes}`) | |
}) | |
} | |
} | |
contador++ | |
console.log(`Se han agregado ${contador} grupos de entidades`) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment