Sequenice
sequenice
is a model wrapper for sequelize
which gives you a better and cleaner way to define models. It is based on real JavaScript objects / classes that are automatically turned into sequelize models.
Features
- Auto-loads a model directory
- Attaches helpers to your models
- Turns your
sequenice
models intosequelize
models
Initialization
1. Models folder
sequenice
requires a models folder. Create it anywhere inside your project.
sequelize
2. Initialize Initialize sequelize
the way you are used to, e.g. like this:
var Sequelize = database = "my_database" "my_user" "my_password";
sequenice
3. Initialize Since sequenice
is just a model wrapper for sequelize
, you will have to initialize a new sequenice
instance and pass your sequelize
instance as well as some options to it:
var Sequenice = database modelsDirectory: __dirname + "/models" // The path to your models folder modelsMatch: "**/*.js" // A string, regex or filter function for selecting the correct files modelsAttacher: global // The object you want to attach your models to getterPrefix: "get" // See "Defining models" setterPrefix: "set" // See "Defining models" ;
Defining models
Inside your models folder you create a file for each model which will contain a JavaScript class. Class methods of this class will become class methods of your model, instance methods will become instance methods.
Inside the constructor you have access to a couple of helpers which will define your model.
Here's an example:
{ // Field definitions map; map; // Associations // hasOne, belongsTo, hasMany // Pass the associated model name as a string. map; map; // Hooks // beforeValidate, beforeCreate, ... // Pass the method name as a string. map; // Getters / Setters // Define getterMethods and setterMethods. Pass the variable name // as a string. // If your getterPrefix is "get" and the variable name is "price", // sequenice will try to call the "getPrice" method of your class. map; map; // Validations // Pass the validation method name as a string. map; // Indices mapindex"name" "isPublished" indexName: "NameIsPublished" ; // Model options mapoptions timestamps: false ;} // This will be called before a Product is createdProductprototype { productpublished = true; ;} // Gets called when `myProduct.price` is accessedProductprototype { return "$ " + thisdataValuespriceAsCents / 100;}; // Gets called when `myProduct.price` is set`Productprototype { thisdataValuespriceAsCents = price / 100;}; // ValidationProductprototype { if thisdataValuespriceAsCents > 500 throw "Damn, that shit's expensive!"; };
Since we set modelsAttacher
to global
, we can now access our model via global.User
or just User
.
License
The MIT License (MIT)
Copyright (c) 2013 Sascha Gehlich and FILSH Media GmbH
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.