npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

sequenice

v0.2.1

Published

An object-oriented model definition alternative for `sequelize`

Downloads

5

Readme

Sequenice Build Status

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 into sequelize models

Initialization

1. Models folder

sequenice requires a models folder. Create it anywhere inside your project.

2. Initialize sequelize

Initialize sequelize the way you are used to, e.g. like this:

var Sequelize = require("sequelize")
  , database = new Sequelize("my_database", "my_user", "my_password");

3. Initialize sequenice

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 = require("sequenice")
  , new 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:

function Product(map) {
  // Field definitions
  map.field("name", s.STRING);
  map.field("isPublished", s.BOOLEAN, { defaultValue: false });

  // Associations
  // hasOne, belongsTo, hasMany
  // Pass the associated model name as a string.
  map.hasMany("Variants");
  map.belongsTo("Category");

  // Hooks
  // beforeValidate, beforeCreate, ...
  // Pass the method name as a string.
  map.beforeCreate("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.
  map.get("price");
  map.set("price");

  // Validations
  // Pass the validation method name as a string.
  map.validates("cheap");

  // Indices
  map.index(["name", "isPublished"], { indexName: "NameIsPublished" });

  // Model options
  map.options({
    timestamps: false
  });
}

// This will be called before a Product is created
Product.prototype.publish = function (product, callback) {
  product.published = true;
  callback();
}

// Gets called when `myProduct.price` is accessed
Product.prototype.getPrice = function () {
  return "$ " + (this.dataValues.priceAsCents / 100).toFixed(2);
};

// Gets called when `myProduct.price` is set`
Product.prototype.setPrice = function (price) {
  this.dataValues.priceAsCents = price / 100;
};

// Validation
Product.prototype.cheap = function () {
  if (this.dataValues.priceAsCents > 500) {
    throw new Error("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.