Let's assume we have an Sequelize model called Person
—how can we create an instance of Person
, without saving it into the database?
This will create the record in the database:
Person.create({ name: "Alice" }).then(..., ...);
How to create a model instance without creating a record for it into the database?
The use-case is for creating multiple documents:
myFamily.addPersons([person1, person2, ...]);
When I try to pass raw objects into that array, an error appears: val.replace is not a function
and as suggested here, the reason could be the fact I pass raw objects.
While I'm interested to know how to solve the problem, I'd want to know how to create Sequelize model instances, without saving them in the database.
Let's assume we have an Sequelize model called Person
—how can we create an instance of Person
, without saving it into the database?
This will create the record in the database:
Person.create({ name: "Alice" }).then(..., ...);
How to create a model instance without creating a record for it into the database?
The use-case is for creating multiple documents:
myFamily.addPersons([person1, person2, ...]);
When I try to pass raw objects into that array, an error appears: val.replace is not a function
and as suggested here, the reason could be the fact I pass raw objects.
While I'm interested to know how to solve the problem, I'd want to know how to create Sequelize model instances, without saving them in the database.
Share Improve this question asked Mar 27, 2017 at 13:15 Ionică BizăuIonică Bizău 113k93 gold badges307 silver badges487 bronze badges1 Answer
Reset to default 19If you build
one, it won't be saved into the db:
var person = Person.build({ name: "Alice" });
Or you can just instantiate the Person
class:
const person = new Person({ name: "Alice" })