MCPcopy
hub / github.com/sequelize/sequelize / constructor

Method constructor

src/model.js:85–143  ·  view source on GitHub ↗

* Builds a new model instance. * * @param {object} [values={}] an object of key value pairs * @param {object} [options] instance construction options * @param {boolean} [options.raw=false] If set to true, values will ignore field and virtual setters. * @param {boolean} [options.isNe

(values = {}, options = {})

Source from the content-addressed store, hash-verified

83 * @param {Array} [options.include] an array of include options - Used to build prefetched/included model instances. See `set`
84 */
85 constructor(values = {}, options = {}) {
86 if (!this.constructor._overwrittenAttributesChecked) {
87 this.constructor._overwrittenAttributesChecked = true;
88
89 // setTimeout is hacky but necessary.
90 // Public Class Fields declared by descendants of this class
91 // will not be available until after their call to super, so after
92 // this constructor is done running.
93 setTimeout(() => {
94 const overwrittenAttributes = [];
95 for (const key of Object.keys(this.constructor._attributeManipulation)) {
96 if (Object.prototype.hasOwnProperty.call(this, key)) {
97 overwrittenAttributes.push(key);
98 }
99 }
100
101 if (overwrittenAttributes.length > 0) {
102 logger.warn(`Model ${JSON.stringify(this.constructor.name)} is declaring public class fields for attribute(s): ${overwrittenAttributes.map(attr => JSON.stringify(attr)).join(', ')}.` +
103 '\nThese class fields are shadowing Sequelize\'s attribute getters & setters.' +
104 '\nSee https://sequelize.org/main/manual/model-basics.html#caveat-with-public-class-fields');
105 }
106 }, 0);
107 }
108
109 options = {
110 isNewRecord: true,
111 _schema: this.constructor._schema,
112 _schemaDelimiter: this.constructor._schemaDelimiter,
113 ...options
114 };
115
116 if (options.attributes) {
117 options.attributes = options.attributes.map(attribute => Array.isArray(attribute) ? attribute[1] : attribute);
118 }
119
120 if (!options.includeValidated) {
121 this.constructor._conformIncludes(options, this.constructor);
122 if (options.include) {
123 this.constructor._expandIncludeAll(options);
124 this.constructor._validateIncludedElements(options);
125 }
126 }
127
128 this.dataValues = {};
129 this._previousDataValues = {};
130 this.uniqno = 1;
131 this._changed = new Set();
132 this._options = options;
133
134 /**
135 * Returns true if this instance has not yet been persisted to the database
136 *
137 * @property isNewRecord
138 * @returns {boolean}
139 */
140 this.isNewRecord = options.isNewRecord;
141
142 this._initValues(values, options);

Callers

nothing calls this directly

Calls 6

_initValuesMethod · 0.95
_conformIncludesMethod · 0.80
_expandIncludeAllMethod · 0.80
warnMethod · 0.65
stringifyMethod · 0.65

Tested by

no test coverage detected