Giter VIP home page Giter VIP logo

sails-hook-sanseed's Introduction

sails-hook-sanseed

NPM Version NPM Downloads Build Status Code Climate Issue Count bitHound Overall Score bitHound Dependencies bitHound Dev Dependencies bitHound Code Codacy Badge

About

Hook for sails to add items ('seed') to the database depending on different locations. See faker.js for more details.

NPM

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

sails-hook-sanseed's People

Contributors

sanjorgek avatar jordimo avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.