Skip to content

Instantly share code, notes, and snippets.

@y8
Forked from l337quez/migracion.js
Last active June 4, 2020 16:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save y8/4d0d3549f90572ee66067f12b7a4c5e9 to your computer and use it in GitHub Desktop.
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
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