A nice model wrapper for
sequenice is a model wrapper for
- Auto-loads a model directory
- Attaches helpers to your models
- Turns your
sequenice requires a models folder. Create it anywhere inside your project.
sequelize the way you are used to, e.g. like this:
var Sequelize = require"sequelize"database = "my_database" "my_user" "my_password";
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 = require"sequenice"databasemodelsDirectory: __dirname + "/models" // The path to your models foldermodelsMatch: "**/*.js" // A string, regex or filter function for selecting the correct filesmodelsAttacher: global // The object you want to attach your models togetterPrefix: "get" // See "Defining models"setterPrefix: "set" // See "Defining models";
Inside the constructor you have access to a couple of helpers which will define your model.
Here's an example:
// Field definitionsmapfield"name" sSTRING;mapfield"isPublished" sBOOLEAN defaultValue: false ;// Associations// hasOne, belongsTo, hasMany// Pass the associated model name as a string.maphasMany"Variants";mapbelongsTo"Category";// Hooks// beforeValidate, beforeCreate, ...// Pass the method name as a string.mapbeforeCreate"publish";// 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.mapget"price";mapset"price";// Validations// Pass the validation method name as a string.mapvalidates"cheap";// Indicesmapindex"name" "isPublished" indexName: "NameIsPublished" ;// Model optionsmapoptionstimestamps: false;// This will be called before a Product is createdproductpublished = true;callback;// Gets called when `myProduct.price` is accessedreturn "$ " + thisdataValuespriceAsCents / 100toFixed2;;// Gets called when `myProduct.price` is set`thisdataValuespriceAsCents = price / 100;;// Validationif thisdataValuespriceAsCents > 500throw "Damn, that shit's expensive!";;
Since we set
global, we can now access our model via
global.User or just
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.