Security News
The Risks of Misguided Research in Supply Chain Security
Snyk's use of malicious npm packages for research raises ethical concerns, highlighting risks in public deployment, data exfiltration, and unauthorized testing.
sequelize-generator
Advanced tools
===================
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.
FAQs
Helper to create sequelize objects
The npm package sequelize-generator receives a total of 1 weekly downloads. As such, sequelize-generator popularity was classified as not popular.
We found that sequelize-generator demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Snyk's use of malicious npm packages for research raises ethical concerns, highlighting risks in public deployment, data exfiltration, and unauthorized testing.
Research
Security News
Socket researchers found several malicious npm packages typosquatting Chalk and Chokidar, targeting Node.js developers with kill switches and data theft.
Security News
pnpm 10 blocks lifecycle scripts by default to improve security, addressing supply chain attack risks but sparking debate over compatibility and workflow changes.