sails-hook-sanseed
v4.0.0
Published
Hook to add items to the base depending on different databases
Downloads
27
Maintainers
Readme
sails-hook-sanseed
About
Hook for sails to add items ('seed') to the database depending on different locations. See faker.js for more details.
Settings
Install
npm install sails-hook-sanseed
Define at config/seed.js
module.exports.seed = {
databases : {
test: {
// A simple model creation
gun: {
scheme: [
{
data: {
name: "revolver",
ammo: 6
}
},
{
data: {
name: "ak-47"
},
faker: {
ammo: "{{random.number}}" // use faker to fill an attribute
},
locale: "es", // set lang, default en
}
],
//If you want to drop before seed
migrate: "drop" //use safe to ignore DBErrors
},
// A model with associations
user: {
scheme: [
{
data: {
username: "juan",
name: "Paco",
last: "Pedro",
password: "de la mar"
},
// Use this to set One-to-Many and One-to-One
oneTo: {
// Use a JSON
favGun: {
//Waterline query
name: "ak-47"
}
},
//Use this to set Many-to-Many
manyTo: {
// Use an Array
guns: [
{
//Waterline query
name: "revolver"
},
{/** */}
],
}
},
{
//More data
}
],
migrate: 'drop'
}
},
//Another database
production: {
// Use Faker.js to fill schemas
user: {
faker: {
format: {
username: "{{internet.userName}}",
names: "{{name.firstName}} {{name.lastName}}",
password: "{{internet.password}}"
},
locale: "es", // set lang, default en
quantity: 10, // set many elements as you wish, default 1
oneTo: {/** */},
manyTo: {/** */}
},
// if you want to try without callback an error
migrate: 'safe'
}
},
serverX: {
//Some data
}
},
//optional
routes: true //Defult routes
}
Use
If sails.seed.routes
is true seed your models with /seed/:database
or /seed/:database/:model
routes, and /drop/:model
or /drop
routes to drop models.
Aviable Functions
Also you have sails.seed
with
seedModel(database, modelName, callback)
Arguments
database
- A string with database name.modelName
- A string with model name.callback(err)
- A callback which is called when all task have finished, or an error occurs.
seedAll(database, callback)
Arguments
database
- A string with database name.callback(err)
- A callback which is called when all task have finished, or an error occurs.
dropModel(modelName, callback)
Arguments
modelName
- A string with model name.callback(err)
- A callback which is called when all task have finished, or an error occurs.
dropAll(callback)
Arguments
callback(err)
- A callback which is called when all task have finished, or an error occurs.
Define at /config/routes.js
:
module.exports.routes{
'get /sanseed/:database': function (req, res, next) {
sails.seed.seedAll(req.params.database, function(err){
if(err){
//do something
}else{
//do something
}
});
}
}
Policies
You have:
seedModel
seedAll
dropModel
dropAll
Define at /config/routes.js
:
module.exports.routes{
'get /sandrop' : [{policy: 'dropAll'}]
}
More
See sails-sanseed-example for more details