MCPcopy
hub / github.com/sequelize/sequelize / get

Method get

lib/associations/has-many.js:173–241  ·  view source on GitHub ↗

* Get everything currently associated with this, using an optional where clause. * * @param {Model|Array<Model>} instances source instances * @param {object} [options] find options * @param {object} [options.where] An optional where clause to limit the associated models * @param {stri

(instances, options = {})

Source from the content-addressed store, hash-verified

171 * @returns {Promise<Array<Model>>}
172 */
173 async get(instances, options = {}) {
174 const where = {};
175
176 let Model = this.target;
177 let instance;
178 let values;
179
180 if (!Array.isArray(instances)) {
181 instance = instances;
182 instances = undefined;
183 }
184
185 options = { ...options };
186
187 if (this.scope) {
188 Object.assign(where, this.scope);
189 }
190
191 if (instances) {
192 values = instances.map(_instance => _instance.get(this.sourceKey, { raw: true }));
193
194 if (options.limit && instances.length > 1) {
195 options.groupedLimit = {
196 limit: options.limit,
197 on: this, // association
198 values
199 };
200
201 delete options.limit;
202 } else {
203 where[this.foreignKey] = {
204 [Op.in]: values
205 };
206 delete options.groupedLimit;
207 }
208 } else {
209 where[this.foreignKey] = instance.get(this.sourceKey, { raw: true });
210 }
211
212 options.where = options.where ?
213 { [Op.and]: [where, options.where] } :
214 where;
215
216 if (Object.prototype.hasOwnProperty.call(options, 'scope')) {
217 if (!options.scope) {
218 Model = Model.unscoped();
219 } else {
220 Model = Model.scope(options.scope);
221 }
222 }
223
224 if (Object.prototype.hasOwnProperty.call(options, 'schema')) {
225 Model = Model.schema(options.schema, options.schemaDelimiter);
226 }
227
228 const results = await Model.findAll(options);
229 if (instance) return results;
230

Callers 3

countMethod · 0.95
hasMethod · 0.95
setMethod · 0.95

Calls 5

unscopedMethod · 0.80
scopeMethod · 0.80
schemaMethod · 0.80
findAllMethod · 0.80
getMethod · 0.65

Tested by

no test coverage detected