最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to create a Sequelize model instance, without saving it in the database? - Stack Overflow

programmeradmin6浏览0评论

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 badges
Add a ment  | 

1 Answer 1

Reset to default 19

If 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" })

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论