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

sequelize-generator

v0.3.7

Published

Helper to create sequelize objects

Downloads

33

Readme

Sequelize Generator Build Status

===================

Object instantiation for Sequelize models

Let's say you have some complicated relationships:

var ModelChild = sequelize.define("ModelChild", {}),
    ModelFather = sequelize.define("ModelFather", {}),
    ModelMother = sequelize.define("ModelMother", {}),
    ModelPaternalGrandFather = sequelize.define("ModelPaternalGrandFather", {}),
    ModelPaternalGrandMother = sequelize.define("ModelPaternalGrandMother", {}),
    ModelMaternalGrandFather = sequelize.define("ModelMaternalGrandFather", {}),
    ModelMaternalGrandMother = sequelize.define("ModelMaternalGrandMother", {});

    ModelChild.belongsTo(ModelFather);
    ModelChild.belongsTo(ModelMother);

    ModelFather.belongsTo(ModelPaternalGrandFather);
    ModelFather.belongsTo(ModelPaternalGrandMother);

    ModelMother.belongsTo(ModelMaternalGrandFather);
    ModelMother.belongsTo(ModelMaternalGrandMother);

You can, from ModelChild, create every parent level above:

new SequelizeG(ModelChild).then(function (child) {
    return child.getModelFather().then(function (father) {
        assert.strictEqual(father.Model.name, ModelFather.name);
        return father.getModelPaternalGrandFather().then(function (paternalGrandFather) {
            assert.strictEqual(paternalGrandFather.Model.name, ModelPaternalGrandFather.name);

            return father.getModelPaternalGrandMother();
        }).then(function (paternalGrandMother) {
            assert.strictEqual(paternalGrandMother.Model.name, ModelPaternalGrandMother.name);

            return child;
        });
    }).then(function (child) {
        return child.getModelMother().then(function (mother) {
            assert.strictEqual(mother.Model.name, ModelMother.name);
            return mother.getModelMaternalGrandFather().then(function (maternalGrandFather) {
                assert.strictEqual(maternalGrandFather.Model.name, ModelMaternalGrandFather.name);

                return mother.getModelMaternalGrandMother();
            }).then(function (maternalGrandMother) {
                assert.strictEqual(maternalGrandMother.Model.name, ModelMaternalGrandMother.name);

                return null;
            });
        });
    });
});

Phew! That's a lot of promises. This is ok for production code, but for tests, where sequelize-generator is most useful, you have a faster way to access the parent instances:

new SequelizeG(ModelChild).then(function (child) {
    assert.strictEqual(child.generator.ModelFather.Model.name, ModelFather.name);
    assert.strictEqual(child.generator.ModelMother.Model.name, ModelMother.name);

    assert.strictEqual(child.generator.ModelFather.generator.ModelPaternalGrandFather.Model.name, ModelPaternalGrandFather.name);
    assert.strictEqual(child.generator.ModelFather.generator.ModelPaternalGrandMother.Model.name, ModelPaternalGrandMother.name);

    assert.strictEqual(child.generator.ModelMother.generator.ModelMaternalGrandFather.Model.name, ModelMaternalGrandFather.name);
    assert.strictEqual(child.generator.ModelMother.generator.ModelMaternalGrandMother.Model.name, ModelMaternalGrandMother.name);
});

You can tell sequelize-generator to stop creating parents with {ModelName: null}. See test "should stop creating parents if option is set":

var ModelChild = sequelize.define("ModelChild", {}),
    ModelParent = sequelize.define("ModelParent", {}),
    ModelGrandParent = sequelize.define("ModelGrandParent", {});

    ModelChild.belongsTo(ModelParent);
    ModelParent.belongsTo(ModelGrandParent);

new SequelizeG(ModelChild, {
    ModelGrandParent: null
}).then(function (child) {
    assert.strictEqual(child.generator.ModelParent.Model.name, ModelParent.name);
    assert.strictEqual(child.generator.ModelParent.generator.ModelGrandParent, undefined);
});

You can tell sequelize-generator to generate several instances (an array will be returned):

var Model = sequelize.define("Model", {});

new SequelizeG(Model, {
    number: 2
}).then(function (children) {
    var childA = children[0],
        childB = children[1];

    assert.strictEqual(2, children.length);

    assert.strictEqual(childA.Model.name, Model.name);
    assert.strictEqual(childB.Model.name, Model.name);

    assert.notStrictEqual(childA.id, childB.id);
});

You can set attribute values for each of the several instances:

var Model = sequelize.define("Model", {
        name: Sequelize.STRING
    }),
    names = ["Julio", "Gustavo", "Felipe"];

new SequelizeG(Model, {
    number: 3,
    attributes: {
        name: names
    }
}).then(function (children) {
    assert.deepEqual(_.pluck(children, "name"), names);
});

You can set an attribute value by passing a function:

var Model = sequelize.define("Model", {
        name: Sequelize.STRING
    }),
    name = "Julio";

new SequelizeG(Model, {
    attributes: {
        name: function () {
            return name;
        }
    }
}).then(function (instance) {
    assert.strictEqual(name, instance.name);
});

If you want every instance to share a common ancestor, pass {ModelName: "shared"} as an option. See test "should create instances with a shared foreign key, if option is set". The shared option will try to use the first record of ModelName. If one is not found, it will be created, and then found when the second instance attempts to use the shared common ancestor.