UNPKG

57.6 kBJavaScriptView Raw
1'use strict';
2
3const _ = require('lodash');
4const createError = require('create-error');
5
6const Sync = require('./sync');
7const Helpers = require('./helpers');
8const EagerRelation = require('./eager');
9const Errors = require('./errors');
10
11const ModelBase = require('./base/model');
12const Promise = require('./base/promise');
13
14/**
15 * @class Model
16 * @extends ModelBase
17 * @inheritdoc
18 * @classdesc
19 *
20 * Models are simple objects representing individual database rows, specifying
21 * the tableName and any relations to other models. They can be extended with
22 * any domain-specific methods, which can handle components such as validations,
23 * computed properties, and access control.
24 *
25 * @constructor
26 * @description
27 *
28 * When creating an instance of a model, you can pass in the initial values of
29 * the attributes, which will be {@link Model#set set} on the
30 * model. If you define an {@link initialize} function, it will be invoked
31 * when the model is created.
32 *
33 * new Book({
34 * title: "One Thousand and One Nights",
35 * author: "Scheherazade"
36 * });
37 *
38 * In rare cases, if you're looking to get fancy, you may want to override
39 * {@link Model#constructor constructor}, which allows you to replace the
40 * actual constructor function for your model.
41 *
42 * let Book = bookshelf.Model.extend({
43 * tableName: 'documents',
44 * constructor: function() {
45 * bookshelf.Model.apply(this, arguments);
46 * this.on('saving', function(model, attrs, options) {
47 * options.query.where('type', '=', 'book');
48 * });
49 * }
50 * });
51 *
52 * @param {Object} attributes Initial values for this model's attributes.
53 * @param {Object=} options Hash of options.
54 * @param {string=} options.tableName Initial value for {@link Model#tableName tableName}.
55 * @param {Boolean=} [options.hasTimestamps=false]
56 *
57 * Initial value for {@link Model#hasTimestamps hasTimestamps}.
58 *
59 * @param {Boolean} [options.parse=false]
60 *
61 * Convert attributes by {@link Model#parse parse} before being {@link
62 * Model#set set} on the model.
63 *
64 */
65const BookshelfModel = ModelBase.extend(
66 {
67 /**
68 * The `hasOne` relation specifies that this table has exactly one of another
69 * type of object, specified by a foreign key in the other table.
70 *
71 * let Record = bookshelf.Model.extend({
72 * tableName: 'health_records'
73 * });
74 *
75 * let Patient = bookshelf.Model.extend({
76 * tableName: 'patients',
77 * record: function() {
78 * return this.hasOne(Record);
79 * }
80 * });
81 *
82 * // select * from `health_records` where `patient_id` = 1;
83 * new Patient({id: 1}).related('record').fetch().then(function(model) {
84 * // ...
85 * });
86 *
87 * // alternatively, if you don't need the relation loaded on the patient's relations hash:
88 * new Patient({id: 1}).record().fetch().then(function(model) {
89 * // ...
90 * });
91 *
92 * @method Model#hasOne
93 *
94 * @param {Model} Target
95 *
96 * Constructor of {@link Model} targeted by join.
97 *
98 * @param {string=} foreignKey
99 *
100 * ForeignKey in the `Target` model. By default, the `foreignKey` is assumed to
101 * be the singular form of this model's {@link Model#tableName tableName},
102 * followed by `_id` / `_{{{@link Model#idAttribute idAttribute}}}`.
103 *
104 * @param {string=} foreignKeyTarget
105 *
106 * Column in this model's table which `foreignKey` references, if other
107 * than this model's `id` / `{@link Model#idAttribute idAttribute}`.
108 *
109 * @returns {Model}
110 *
111 * The return value will always be a model, even if the relation doesn't exist, but in that
112 * case the relation will be `null` when {@link Model#serialize serializing} the model.
113 */
114 hasOne(Target, foreignKey, foreignKeyTarget) {
115 return this._relation('hasOne', Target, {
116 foreignKey,
117 foreignKeyTarget
118 }).init(this);
119 },
120
121 /**
122 * The `hasMany` relation specifies that this model has one or more rows in
123 * another table which match on this model's primary key.
124 *
125 * let Author = bookshelf.Model.extend({
126 * tableName: 'authors',
127 * books: function() {
128 * return this.hasMany(Book);
129 * }
130 * });
131 *
132 * // select * from `authors` where id = 1
133 * // select * from `books` where author_id = 1
134 * Author.where({id: 1}).fetch({withRelated: ['books']}).then(function(author) {
135 * console.log(JSON.stringify(author.related('books')));
136 * });
137 *
138 * @method Model#hasMany
139 *
140 * @param {Model} Target
141 *
142 * Constructor of {@link Model} targeted by join.
143 *
144 * @param {string=} foreignKey
145 *
146 * ForeignKey in the `Target` model. By default, the foreignKey is assumed to
147 * be the singular form of this model's tableName, followed by `_id` /
148 * `_{{{@link Model#idAttribute idAttribute}}}`.
149 *
150 * @param {string=} foreignKeyTarget
151 *
152 * Column in this model's table which `foreignKey` references, if other
153 * than this model's `id` / `{@link Model#idAttribute idAttribute}`.
154 *
155 * @returns {Collection}
156 */
157 hasMany(Target, foreignKey, foreignKeyTarget) {
158 return this._relation('hasMany', Target, {
159 foreignKey,
160 foreignKeyTarget
161 }).init(this);
162 },
163
164 /**
165 * The `belongsTo` relationship is used when a model is a member of
166 * another `Target` model.
167 *
168 * It can be used in a {@linkplain one-to-one} associations as the inverse
169 * of a {@link Model#hasOne hasOne}. It can also used in {@linkplain
170 * one-to-many} associations as the inverse of a {@link Model#hasMany hasMany}
171 * (and is the one side of that association). In both cases, the {@link
172 * Model#belongsTo belongsTo} relationship is used for a model that is a
173 * member of another Target model, referenced by the foreignKey in the current
174 * model.
175 *
176 * let Book = bookshelf.Model.extend({
177 * tableName: 'books',
178 * author: function() {
179 * return this.belongsTo(Author);
180 * }
181 * });
182 *
183 * // select * from `books` where id = 1
184 * // select * from `authors` where id = book.author_id
185 * Book.where({id: 1}).fetch({withRelated: ['author']}).then(function(book) {
186 * console.log(JSON.stringify(book.related('author')));
187 * });
188 *
189 * @method Model#belongsTo
190 *
191 * @param {Model} Target
192 *
193 * Constructor of {@link Model} targeted by join.
194 *
195 * @param {string=} foreignKey
196 *
197 * ForeignKey in this model. By default, the foreignKey is assumed to
198 * be the singular form of the `Target` model's tableName, followed by `_id` /
199 * `_{{{@link Model#idAttribute idAttribute}}}`.
200 *
201 * @param {string=} foreignKeyTarget
202 *
203 * Column in the `Target` model's table which `foreignKey` references, if other
204 * than `Target` model's `id` / `{@link Model#idAttribute idAttribute}`.
205 *
206 * @returns {Model}
207 *
208 * The return value will always be a model, even if the relation doesn't exist, but in that
209 * case the relation will be `null` when {@link Model#serialize serializing} the model.
210 */
211 belongsTo(Target, foreignKey, foreignKeyTarget) {
212 return this._relation('belongsTo', Target, {
213 foreignKey,
214 foreignKeyTarget
215 }).init(this);
216 },
217
218 /**
219 * Defines a many-to-many relation, where the current model is joined to one
220 * or more of a `Target` model through another table. The default name for
221 * the joining table is the two table names, joined by an underscore, ordered
222 * alphabetically. For example, a `users` table and an `accounts` table would have
223 * a joining table of accounts_users.
224 *
225 * let Account = bookshelf.Model.extend({
226 * tableName: 'accounts'
227 * });
228 *
229 * let User = bookshelf.Model.extend({
230 * tableName: 'users',
231 * allAccounts: function () {
232 * return this.belongsToMany(Account);
233 * },
234 * adminAccounts: function() {
235 * return this.belongsToMany(Account).query({where: {access: 'admin'}});
236 * },
237 * viewAccounts: function() {
238 * return this.belongsToMany(Account).query({where: {access: 'readonly'}});
239 * }
240 * });
241 *
242 * The default key names in the joining table are the singular versions of the
243 * model table names, followed by `_id` /
244 * _{{{@link Model#idAttribute idAttribute}}}. So in the above case, the
245 * columns in the joining table
246 * would be `user_id`, `account_id`, and `access`, which is used as an
247 * example of how dynamic relations can be formed using different contexts.
248 * To customize the keys used in, or the {@link Model#tableName tableName}
249 * used for the join table, you may specify them like so:
250 *
251 * this.belongsToMany(Account, 'users_accounts', 'userid', 'accountid');
252 *
253 * If you wish to create a {@link Model#belongsToMany belongsToMany}
254 * association where the joining table has a primary key, and more information
255 * about the model, you may create a {@link Model#belongsToMany belongsToMany}
256 * {@link Relation#through through} relation:
257 *
258 * let Doctor = bookshelf.Model.extend({
259 * patients: function() {
260 * return this.belongsToMany(Patient).through(Appointment);
261 * }
262 * });
263 *
264 * let Appointment = bookshelf.Model.extend({
265 * patient: function() {
266 * return this.belongsTo(Patient);
267 * },
268 * doctor: function() {
269 * return this.belongsTo(Doctor);
270 * }
271 * });
272 *
273 * let Patient = bookshelf.Model.extend({
274 * doctors: function() {
275 * return this.belongsToMany(Doctor).through(Appointment);
276 * }
277 * });
278 *
279 * Collections returned by a `belongsToMany` relation are decorated with
280 * several pivot helper methods. See {@link Collection#attach attach},
281 * {@link Collection#detach detach}, {@link Collection#updatePivot
282 * updatePivot} and {@link Collection#withPivot withPivot} for more
283 * information.
284 *
285 * @belongsTo Model
286 * @method Model#belongsToMany
287 * @param {Model} Target
288 *
289 * Constructor of {@link Model} targeted by join.
290 *
291 * @param {string=} table
292 *
293 * Name of the joining table. Defaults to the two table names, joined by an
294 * underscore, ordered alphabetically.
295 *
296 * @param {string=} foreignKey
297 *
298 * Foreign key in this model. By default, the `foreignKey` is assumed to
299 * be the singular form of this model's tableName, followed by `_id` /
300 * `_{{{@link Model#idAttribute idAttribute}}}`.
301 *
302 * @param {string=} otherKey
303 *
304 * Foreign key in the `Target` model. By default, the `otherKey` is assumed to
305 * be the singular form of the `Target` model's tableName, followed by `_id` /
306 * `_{{{@link Model#idAttribute idAttribute}}}`.
307 *
308 * @param {string=} foreignKeyTarget
309 *
310 * Column in this model's table which `foreignKey` references, if other
311 * than `id` / `{@link Model#idAttribute idAttribute}`.
312 *
313 * @param {string=} otherKeyTarget
314 *
315 * Column in the `Target` model's table which `otherKey` references, if other
316 * than `Target` model's `id` / `{@link Model#idAttribute idAttribute}`.
317 *
318 * @returns {Collection}
319 */
320 belongsToMany(Target, joinTableName, foreignKey, otherKey, foreignKeyTarget, otherKeyTarget) {
321 return this._relation('belongsToMany', Target, {
322 joinTableName,
323 foreignKey,
324 otherKey,
325 foreignKeyTarget,
326 otherKeyTarget
327 }).init(this);
328 },
329
330 /**
331 * The {@link Model#morphOne morphOne} is used to signify a {@link oneToOne
332 * one-to-one} {@link polymorphicRelation polymorphic relation} with
333 * another `Target` model, where the `name` of the model is used to determine
334 * which database table keys are used. The naming convention requires the
335 * `name` prefix an `_id` and `_type` field in the database. So for the case
336 * below the table names would be `imageable_type` and `imageable_id`. The
337 * `morphValue` may be optionally set to store/retrieve a different value in
338 * the `_type` column than the {@link Model#tableName}.
339 *
340 * let Site = bookshelf.Model.extend({
341 * tableName: 'sites',
342 * photo: function() {
343 * return this.morphOne(Photo, 'imageable');
344 * }
345 * });
346 *
347 * And with custom `columnNames`:
348 *
349 * let Site = bookshelf.Model.extend({
350 * tableName: 'sites',
351 * photo: function() {
352 * return this.morphOne(Photo, 'imageable', ["ImageableType", "ImageableId"]);
353 * }
354 * });
355 *
356 * Note that both `columnNames` and `morphValue` are optional arguments. How
357 * your argument is treated when only one is specified, depends on the type.
358 * If your argument is an array, it will be assumed to contain custom
359 * `columnNames`. If it's not, it will be assumed to indicate a `morphValue`.
360 *
361 * @method Model#morphOne
362 *
363 * @param {Model} Target Constructor of {@link Model} targeted by join.
364 * @param {string=} name Prefix for `_id` and `_type` columns.
365 * @param {(string[])=} columnNames
366 *
367 * Array containing two column names, the first is the `_type`, the second
368 * is the `_id`.
369 *
370 * @param {string=} [morphValue=Target#{@link Model#tableName tableName}]
371 *
372 * The string value associated with this relationship. Stored in the `_type`
373 * column of the polymorphic table. Defaults to `Target#{@link
374 * Model#tableName tableName}`.
375 *
376 * @returns {Model} The related model.
377 */
378 morphOne(Target, name, columnNames, morphValue) {
379 return this._morphOneOrMany(Target, name, columnNames, morphValue, 'morphOne');
380 },
381
382 /**
383 * {@link Model#morphMany morphMany} is essentially the same as a {@link
384 * Model#morphOne morphOne}, but creating a {@link Collection collection}
385 * rather than a {@link Model model} (similar to a {@link Model#hasOne
386 * hasOne} vs. {@link Model#hasMany hasMany} relation).
387 *
388 * {@link Model#morphMany morphMany} is used to signify a {@link oneToMany
389 * one-to-many} or {@link manyToMany many-to-many} {@link polymorphicRelation
390 * polymorphic relation} with another `Target` model, where the `name` of the
391 * model is used to determine which database table keys are used. The naming
392 * convention requires the `name` prefix an `_id` and `_type` field in the
393 * database. So for the case below the table names would be `imageable_type`
394 * and `imageable_id`. The `morphValue` may be optionally set to
395 * store/retrieve a different value in the `_type` column than the `Target`'s
396 * {@link Model#tableName tableName}.
397 *
398 * let Post = bookshelf.Model.extend({
399 * tableName: 'posts',
400 * photos: function() {
401 * return this.morphMany(Photo, 'imageable');
402 * }
403 * });
404 *
405 * And with custom columnNames:
406 *
407 * let Post = bookshelf.Model.extend({
408 * tableName: 'posts',
409 * photos: function() {
410 * return this.morphMany(Photo, 'imageable', ["ImageableType", "ImageableId"]);
411 * }
412 * });
413 *
414 * @method Model#morphMany
415 *
416 * @param {Model} Target Constructor of {@link Model} targeted by join.
417 * @param {string=} name Prefix for `_id` and `_type` columns.
418 * @param {(string[])=} columnNames
419 *
420 * Array containing two column names, the first is the `_type`, the second is the `_id`.
421 *
422 * @param {string=} [morphValue=Target#{@link Model#tableName tablename}]
423 *
424 * The string value associated with this relationship. Stored in the `_type`
425 * column of the polymorphic table. Defaults to `Target`#{@link Model#tableName
426 * tablename}.
427 *
428 * @returns {Collection} A collection of related models.
429 */
430 morphMany(Target, name, columnNames, morphValue) {
431 return this._morphOneOrMany(Target, name, columnNames, morphValue, 'morphMany');
432 },
433
434 /**
435 * The {@link Model#morphTo morphTo} relation is used to specify the inverse
436 * of the {@link Model#morphOne morphOne} or {@link Model#morphMany
437 * morphMany} relations, where the `targets` must be passed to signify which
438 * {@link Model models} are the potential opposite end of the {@link
439 * polymorphicRelation polymorphic relation}.
440 *
441 * let Photo = bookshelf.Model.extend({
442 * tableName: 'photos',
443 * imageable: function() {
444 * return this.morphTo('imageable', Site, Post);
445 * }
446 * });
447 *
448 * And with custom columnNames:
449 *
450 * let Photo = bookshelf.Model.extend({
451 * tableName: 'photos',
452 * imageable: function() {
453 * return this.morphTo('imageable', ["ImageableType", "ImageableId"], Site, Post);
454 * }
455 * });
456 *
457 * And with custom morphValues, the inverse of morphValue of
458 * {@link Model#morphOne morphOne} and {@link Model#morphMany morphMany},
459 * where the `morphValues` may be optionally set to check against a
460 * different value in the `_type` column than the {@link Model#tableName};
461 * for example, a more descriptive name, or a name that betters adheres to
462 * whatever standard you are using for models.
463 *
464 * let Photo = bookshelf.Model.extend({
465 * tableName: 'photos',
466 * imageable: function() {
467 * return this.morphTo('imageable', [Site, "favicon"], [Post, "cover_photo"]);
468 * }
469 * });
470 *
471 * @method Model#morphTo
472 *
473 * @param {string} name Prefix for `_id` and `_type` columns.
474 * @param {(string[])=} columnNames
475 *
476 * Array containing two column names, the first is the `_type`, the second is the `_id`.
477 *
478 * @param {...Model} Target Constructor of {@link Model} targeted by join.
479 *
480 * @returns {Model}
481 */
482 morphTo(morphName) {
483 if (!_.isString(morphName)) throw new Error('The `morphTo` name must be specified.');
484 let columnNames, candidates;
485 if (arguments[1] == null || (Array.isArray(arguments[1]) && _.isString(arguments[1][0]))) {
486 columnNames = arguments[1] || null; // may be `null` or `undefined`
487 candidates = _.drop(arguments, 2);
488 } else {
489 columnNames = null;
490 candidates = _.drop(arguments, 1);
491 }
492
493 candidates = _.map(candidates, (target) => {
494 if (Array.isArray(target)) return target;
495
496 // Set up the morphValue by default as the tableName
497 return [target, _.result(target.prototype, 'tableName')];
498 });
499
500 return this._relation('morphTo', null, {
501 morphName,
502 columnNames,
503 candidates
504 }).init(this);
505 },
506
507 /**
508 * Helps to create dynamic relations between {@link Model models} and {@link
509 * Collection collections}, where a {@link Model#hasOne hasOne}, {@link
510 * Model#hasMany hasMany}, {@link Model#belongsTo belongsTo}, or {@link
511 * Model#belongsToMany belongsToMany} relation may run through a `JoinModel`.
512 *
513 * A good example of where this would be useful is if a book {@link
514 * Model#hasMany hasMany} paragraphs through chapters. Consider the following examples:
515 *
516 *
517 * let Book = bookshelf.Model.extend({
518 * tableName: 'books',
519 *
520 * // Find all paragraphs associated with this book, by
521 * // passing through the "Chapter" model.
522 * paragraphs: function() {
523 * return this.hasMany(Paragraph).through(Chapter);
524 * },
525 *
526 * chapters: function() {
527 * return this.hasMany(Chapter);
528 * }
529 * });
530 *
531 * let Chapter = bookshelf.Model.extend({
532 * tableName: 'chapters',
533 *
534 * paragraphs: function() {
535 * return this.hasMany(Paragraph);
536 * }
537 * });
538 *
539 * let Paragraph = bookshelf.Model.extend({
540 * tableName: 'paragraphs',
541 *
542 * chapter: function() {
543 * return this.belongsTo(Chapter);
544 * },
545 *
546 * // A reverse relation, where we can get the book from the chapter.
547 * book: function() {
548 * return this.belongsTo(Book).through(Chapter);
549 * }
550 * });
551 *
552 * The "through" table creates a pivot model, which it assigns to {@link
553 * Model#pivot model.pivot} after it is created. On {@link Model#toJSON
554 * toJSON}, the pivot model is flattened to values prefixed with
555 * `_pivot_`.
556 *
557 * @method Model#through
558 * @param {Model} Interim Pivot model.
559 * @param {string=} throughForeignKey
560 *
561 * Foreign key in this model. By default, the `foreignKey` is assumed to
562 * be the singular form of the `Target` model's tableName, followed by `_id` /
563 * `_{{{@link Model#idAttribute idAttribute}}}`.
564 *
565 * @param {string=} otherKey
566 *
567 * Foreign key in the `Interim` model. By default, the `otherKey` is assumed to
568 * be the singular form of this model's tableName, followed by `_id` /
569 * `_{{{@link Model#idAttribute idAttribute}}}`.
570 *
571 * @param {string=} throughForeignKeyTarget
572 *
573 * Column in the `Target` model which `throughForeignKey` references, if other
574 * than `Target` model's `id` / `{@link Model#idAttribute idAttribute}`.
575 *
576 * @param {string=} otherKeyTarget
577 *
578 * Column in this model which `otherKey` references, if other
579 * than `id` / `{@link Model#idAttribute idAttribute}`.
580 *
581 * @returns {Collection}
582 */
583 through(Interim, throughForeignKey, otherKey, throughForeignKeyTarget, otherKeyTarget) {
584 return this.relatedData.through(this, Interim, {
585 throughForeignKey,
586 otherKey,
587 throughForeignKeyTarget,
588 otherKeyTarget
589 });
590 },
591
592 /**
593 * @method Model#refresh
594 * @since 0.8.2
595 * @description
596 *
597 * Update the attributes of a model, fetching it by its primary key. If no
598 * attribute matches its {@link Model#idAttribute idAttribute}, then fetch by
599 * all available fields.
600 *
601 * @param {Object} options
602 * A hash of options. See {@link Model#fetch} for details.
603 * @returns {Promise<Model>}
604 * A promise resolving to this model.
605 */
606 refresh(options) {
607 // If this is new, we use all its attributes. Otherwise we just grab the primary key.
608 const attributes = this.isNew() ? this.attributes : _.pick(this.attributes, this.idAttribute);
609 return this._doFetch(attributes, options);
610 },
611
612 /**
613 * Fetches a {@link Model model} from the database, using any {@link
614 * Model#attributes attributes} currently set on the model to form a `select`
615 * query.
616 *
617 * A {@link Model#event:fetching "fetching"} event will be fired just before the
618 * record is fetched; a good place to hook into for validation. {@link
619 * Model#event:fetched "fetched"} event will be fired when a record is
620 * successfully retrieved.
621 *
622 * If you need to constrain the query
623 * performed by fetch, you can call {@link Model#query query} before calling
624 * {@link Model#fetch fetch}.
625 *
626 * // select * from `books` where `ISBN-13` = '9780440180296'
627 * new Book({'ISBN-13': '9780440180296'})
628 * .fetch()
629 * .then(function(model) {
630 * // outputs 'Slaughterhouse Five'
631 * console.log(model.get('title'));
632 * });
633 *
634 * _If you'd like to only fetch specific columns, you may specify a `columns`
635 * property in the `options` for the {@link Model#fetch fetch} call, or use
636 * {@link Model#query query}, tapping into the {@link Knex} {@link
637 * Knex#column column} method to specify which columns will be fetched._
638 *
639 * A single property, or an array of properties can be specified as a value for
640 * the `withRelated` property. You can also execute callbacks on relations
641 * queries (eg. for sorting a relation). The results of these relation queries
642 * will be loaded into a {@link Model#relations relations} property on the
643 * model, may be retrieved with the {@link Model#related related} method, and
644 * will be serialized as properties on a {@link Model#toJSON toJSON} call
645 * unless `{shallow: true}` is passed.
646 *
647 * let Book = bookshelf.Model.extend({
648 * tableName: 'books',
649 * editions: function() {
650 * return this.hasMany(Edition);
651 * },
652 * chapters: function() {
653 * return this.hasMany(Chapter);
654 * },
655 * genre: function() {
656 * return this.belongsTo(Genre);
657 * }
658 * })
659 *
660 * new Book({'ISBN-13': '9780440180296'}).fetch({
661 * withRelated: [
662 * 'genre', 'editions',
663 * { chapters: function(query) { query.orderBy('chapter_number'); }}
664 * ]
665 * }).then(function(book) {
666 * console.log(book.related('genre').toJSON());
667 * console.log(book.related('editions').toJSON());
668 * console.log(book.toJSON());
669 * });
670 *
671 * @method Model#fetch
672 *
673 * @param {Object=} options - Hash of options.
674 * @param {Boolean=} [options.require=false]
675 * Reject the returned response with a {@link Model.NotFoundError
676 * NotFoundError} if results are empty.
677 * @param {string|string[]} [options.columns='*']
678 * Specify columns to be retrieved.
679 * @param {Transaction} [options.transacting]
680 * Optionally run the query in a transaction.
681 * @param {string} [options.lock]
682 * Type of row-level lock to use. Valid options are `forShare` and
683 * `forUpdate`. This only works in conjunction with the `transacting`
684 * option, and requires a database that supports it.
685 * @param {string|Object|mixed[]} [options.withRelated]
686 * Relations to be retrieved with `Model` instance. Either one or more
687 * relation names or objects mapping relation names to query callbacks.
688 *
689 * @fires Model#fetching
690 * @fires Model#fetched
691 *
692 * @throws {Model.NotFoundError}
693 *
694 * @returns {Promise<Model|null>}
695 * A promise resolving to the fetched {@link Model model} or `null` if
696 * none exists.
697 *
698 */
699 fetch(options) {
700 return this._doFetch(this.attributes, options);
701 },
702
703 _doFetch: Promise.method(function(attributes, options) {
704 options = options ? _.clone(options) : {};
705
706 // Run the `first` call on the `sync` object to fetch a single model.
707 return (
708 this.sync(options)
709 .first(attributes)
710 .bind(this)
711
712 // Jump the rest of the chain if the response doesn't exist...
713 .tap(function(response) {
714 if (!response || response.length === 0) {
715 throw new this.constructor.NotFoundError('EmptyResponse');
716 }
717 })
718
719 // Now, load all of the data into the model as necessary.
720 .tap(this._handleResponse)
721
722 // If the "withRelated" is specified, we also need to eager load all of the
723 // data on the model, as a side-effect, before we ultimately jump into the
724 // next step of the model. Since the `columns` are only relevant to the
725 // current level, ensure those are omitted from the options.
726 .tap(function(response) {
727 if (options.withRelated) {
728 return this._handleEager(response, _.omit(options, 'columns'));
729 }
730 })
731
732 .tap(function(response) {
733 /**
734 * Fired after a `fetch` operation. A promise may be returned from the
735 * event handler for async behaviour.
736 *
737 * @event Model#fetched
738 * @param {Model} model
739 * The model firing the event.
740 * @param {Object} response
741 * Knex query response.
742 * @param {Object} options
743 * Options object passed to {@link Model#fetch fetch}.
744 * @returns {Promise}
745 * If the handler returns a promise, `fetch` will wait for it to
746 * be resolved.
747 */
748 return this.triggerThen('fetched', this, response, options);
749 })
750 .return(this)
751 .catch(this.constructor.NotFoundError, function(err) {
752 if (options.require) {
753 throw err;
754 }
755 return null;
756 })
757 );
758 }),
759
760 // Private for now.
761 all() {
762 const collection = this.constructor.collection();
763 collection._knex = this.query().clone();
764 this.resetQuery();
765 if (this.relatedData) collection.relatedData = this.relatedData;
766 return collection;
767 },
768
769 /**
770 * @method Model#count
771 * @since 0.8.2
772 * @description
773 *
774 * Gets the number of matching records in the database, respecting any
775 * previous calls to {@link Model#query}.
776 *
777 * @example
778 *
779 * Duck.where('color', 'blue').count('name')
780 * .then(function(count) { //...
781 *
782 * @param {string} [column='*']
783 * Specify a column to count - rows with null values in this column will be excluded.
784 * @param {Object=} options
785 * Hash of options.
786 * @returns {Promise<Number>}
787 * A promise resolving to the number of matching rows.
788 */
789 count(column, options) {
790 return this.all().count(column, options);
791 },
792
793 /**
794 * Fetches a collection of {@link Model models} from the database, using any
795 * query parameters currently set on the model to form a select query. Returns
796 * a promise, which will resolve with the fetched collection. If you wish to
797 * trigger an error if no models are found, pass {require: true} as one of
798 * the options to the `fetchAll` call.
799 *
800 * If you need to constrain the query performed by fetch, you can call the
801 * {@link Model#query query} method before calling fetch.
802 *
803 * @method Model#fetchAll
804 *
805 * @param {Object=} options - Hash of options.
806 * @param {Boolean=} [options.require=false]
807 *
808 * Rejects the returned promise with an `Collection.EmptyError` if no records are returned.
809 *
810 * @param {Transaction=} options.transacting
811 *
812 * Optionally run the query in a transaction.
813 *
814 * @fires Model#fetching:collection
815 * @fires Model#fetched:collection
816 *
817 * @throws {Collection.EmptyError}
818 *
819 * Rejects the promise in the event of an empty response if the `require: true` option.
820 *
821 * @returns {Promise<Collection>} A promise resolving to the fetched {@link Collection collection}.
822 *
823 */
824 fetchAll(options) {
825 const collection = this.all();
826 return collection
827 .once('fetching', (__, columns, opts) => {
828 /**
829 * Fired before a {@link Model#fetchAll fetchAll} operation. A promise
830 * may be returned from the event handler for async behaviour.
831 *
832 * @event Model#fetching:collection
833 * @param {Collection} collection
834 * The collection that is going to be fetched. At this point it's still empty since the
835 * fetch hasn't happened yet.
836 * @param {string[]} columns
837 * The columns to be retrieved by the query as provided by the underlying query builder.
838 * If the `columns` option is not specified the value of this will usually be an array
839 * with a single string `'tableName.*'`.
840 * @param {Object} options Options object passed to {@link Model#fetchAll fetchAll}.
841 * @returns {Promise}
842 */
843 return this.triggerThen('fetching:collection', collection, columns, opts);
844 })
845 .once('fetched', (__, response, opts) => {
846 /**
847 * Fired after a {@link Model#fetchAll fetchAll} operation. A promise
848 * may be returned from the event handler for async behaviour.
849 *
850 * @event Model#fetched:collection
851 * @param {Collection} collection The collection that has been fetched.
852 * @param {Object} response
853 * The raw response from the underlying query builder. This will be an array with objects
854 * representing each row, similar to the output of a
855 * {@link Model#serialize serialized Model}.
856 * @param {Object} options Options object passed to {@link Model#fetchAll fetchAll}.
857 * @returns {Promise}
858 */
859 return this.triggerThen('fetched:collection', collection, response, opts);
860 })
861 .fetch(options);
862 },
863
864 /**
865 * @method Model#load
866 * @description
867 * The load method takes an array of relations to eager load attributes onto a
868 * {@link Model}, in a similar way that the `withRelated` property works on
869 * {@link Model#fetch fetch}. Dot separated attributes may be used to specify deep
870 * eager loading.
871 *
872 * @example
873 * new Posts().fetch().then(function(collection) {
874 * collection.at(0)
875 * .load(['author', 'content', 'comments.tags'])
876 * .then(function(model) {
877 * JSON.stringify(model);
878 * });
879 * });
880 *
881 * {
882 * title: 'post title',
883 * author: {...},
884 * content: {...},
885 * comments: [
886 * {tags: [...]}, {tags: [...]}
887 * ]
888 * }
889 *
890 * @param {string|Object|mixed[]} relations The relation, or relations, to be loaded.
891 * @param {Object=} options Hash of options.
892 * @param {Transaction=} options.transacting
893 * Optionally run the query in a transaction.
894 * @param {string=} options.lock
895 * Type of row-level lock to use. Valid options are `forShare` and
896 * `forUpdate`. This only works in conjunction with the `transacting`
897 * option, and requires a database that supports it.
898 * @returns {Promise<Model>} A promise resolving to this {@link Model model}.
899 */
900 load: Promise.method(function(relations, options) {
901 const columns = this.format(_.assignIn({}, this.attributes));
902 const withRelated = Array.isArray(relations) ? relations : [relations];
903 return this._handleEager([columns], _.assignIn({}, options, {shallow: true, withRelated})).return(this);
904 }),
905
906 /**
907 * @method Model#save
908 * @description
909 *
910 * This method is used to perform either an insert or update query using the
911 * model's set {@link Model#attributes attributes}.
912 *
913 * If the model {@link Model#isNew isNew}, any {@link Model#defaults defaults}
914 * will be set and an `insert` query will be performed. Otherwise it will
915 * `update` the record with a corresponding ID. It is also possible to
916 * set default attributes on an `update` by passing the `{defaults: true}`
917 * option in the second argument to the `save` call. This will also use the
918 * same {@link Model#defaults defaults} as the `insert` operation.
919 *
920 * The type of operation to perform (either `insert` or `update`) can be
921 * overriden with the `method` option:
922 *
923 * // This forces an insert with the specified id instead of the expected
924 * // update
925 * new Post({name: 'New Article', id: 34})
926 * .save(null, {method: 'insert'})
927 * .then(function(model) {
928 * // ...
929 * });
930 *
931 * If you only wish to update with the params passed to the save, you may pass
932 * a `{patch: true}` option in the second argument to `save`:
933 *
934 * // UPDATE authors SET "bio" = 'Short user bio' WHERE "id" = 1
935 * new Author({id: 1, first_name: 'User'})
936 * .save({bio: 'Short user bio'}, {patch: true})
937 * .then(function(model) {
938 * // ...
939 * });
940 *
941 * Several events fired on the model when saving: a {@link Model#creating
942 * "creating"}, or {@link Model#updating "updating"} event if the model is
943 * being inserted or updated, and a "saving" event in either case. To
944 * prevent saving the model (with validation, etc.), throwing an error inside
945 * one of these event listeners will stop saving the model and reject the
946 * promise. A {@link Model#created "created"}, or {@link Model#"updated"}
947 * event is fired after the model is saved, as well as a {@link Model#saved
948 * "saved"} event either way. If you wish to modify the query when the {@link
949 * Model#saving "saving"} event is fired, the knex query object should is
950 * available in `options.query`.
951 *
952 * // Save with no arguments
953 * Model.forge({id: 5, firstName: 'John', lastName: 'Smith'}).save().then(function() {
954 * //...
955 * });
956 *
957 * // Or add attributes during save
958 * Model.forge({id: 5}).save({firstName: 'John', lastName: 'Smith'}).then(function() {
959 * //...
960 * });
961 *
962 * // Or, if you prefer, for a single attribute
963 * Model.forge({id: 5}).save('name', 'John Smith').then(function() {
964 * //...
965 * });
966 *
967 * @param {string=} key Attribute name.
968 * @param {string=} val Attribute value.
969 * @param {Object=} attrs A hash of attributes.
970 * @param {Object=} options
971 * @param {Transaction=} options.transacting
972 * Optionally run the query in a transaction.
973 * @param {string=} options.method
974 * Explicitly select a save method, either `"update"` or `"insert"`.
975 * @param {Boolean} [options.defaults=false]
976 * Whether to assign or not {@link Model#defaults default} attribute values
977 * on a model when performing an update or create operation.
978 * @param {Boolean} [options.patch=false]
979 * Only save attributes supplied in arguments to `save`.
980 * @param {Boolean} [options.require=true]
981 * Throw a {@link Model.NoRowsUpdatedError} if no records are affected by save.
982 *
983 * @fires Model#saving
984 * @fires Model#creating
985 * @fires Model#updating
986 * @fires Model#created
987 * @fires Model#updated
988 * @fires Model#saved
989 *
990 * @throws {Model.NoRowsUpdatedError}
991 *
992 * @returns {Promise<Model>} A promise resolving to the saved and updated model.
993 */
994 save: Promise.method(function(key, val, options) {
995 let attrs;
996
997 // Handle both `"key", value` and `{key: value}` -style arguments.
998 if (key == null || typeof key === 'object') {
999 attrs = key || {};
1000 options = _.clone(val) || {};
1001 } else {
1002 attrs = {
1003 [key]: val
1004 };
1005 options = options ? _.clone(options) : {};
1006 }
1007
1008 return Promise.bind(this)
1009 .then(function() {
1010 return this.saveMethod(options);
1011 })
1012 .then(function(method) {
1013 // Determine which kind of save we will do: update or insert.
1014 options.method = method;
1015
1016 // If the object is being created, we merge any defaults here rather than
1017 // during object creation.
1018 if (method === 'insert' || options.defaults) {
1019 const defaults = _.result(this, 'defaults');
1020 if (defaults) {
1021 attrs = _.defaultsDeep({}, attrs, this.attributes, defaults);
1022 }
1023 }
1024
1025 // Set the attributes on the model. Note that we do this before adding
1026 // timestamps, as `timestamp` calls `set` internally.
1027 this.set(attrs, {silent: true});
1028
1029 // Now set timestamps if appropriate. Extend `attrs` so that the
1030 // timestamps will be provided for a patch operation.
1031 if (this.hasTimestamps) {
1032 Object.assign(attrs, this.timestamp(options));
1033 }
1034
1035 // If there are any save constraints, set them on the model.
1036 if (this.relatedData && this.relatedData.type !== 'morphTo') {
1037 Helpers.saveConstraints(this, this.relatedData);
1038 }
1039
1040 // Gives access to the `query` object in the `options`, in case we need it
1041 // in any event handlers.
1042 const sync = this.sync(options);
1043 options.query = sync.query;
1044
1045 /**
1046 * Saving event.
1047 *
1048 * Fired before an `insert` or `update` query. A promise may be
1049 * returned from the event handler for async behaviour. Throwing an
1050 * exception from the handler will cancel the save.
1051 *
1052 * @event Model#saving
1053 * @param {Model} model
1054 * The model firing the event. Its attributes are already changed but
1055 * not commited to the database yet.
1056 * @param {Object} attrs
1057 * Attributes that will be inserted or updated.
1058 *
1059 * **Note**: There's currently a bug that leads to attrs only
1060 * containing attributes that were passed as argument to
1061 * {@link Model#save save}. You can work around this by accessing
1062 * `model.changed` which does contain all the attributes that will be
1063 * inserted or updated.
1064 * @param {Object} options Options object passed to {@link Model#save save}.
1065 * @returns {Promise}
1066 */
1067
1068 /**
1069 * Creating event.
1070 *
1071 * Fired before `insert` query. A promise may be
1072 * returned from the event handler for async behaviour. Throwing an
1073 * exception from the handler will cancel the save operation.
1074 *
1075 * @event Model#creating
1076 * @param {Model} model The model firing the event.
1077 * @param {Object} attrs
1078 * Attributes that will be inserted.
1079 *
1080 * **Note**: There's currently a bug that leads to attrs only
1081 * containing attributes that were passed as argument to
1082 * {@link Model#save save}. You can work around this by accessing
1083 * `model.changed` which does contain all the attributes that will be
1084 * inserted.
1085 * @param {Object} options Options object passed to {@link Model#save save}.
1086 * @returns {Promise}
1087 */
1088
1089 /**
1090 * Updating event.
1091 *
1092 * Fired before `update` query. A promise may be
1093 * returned from the event handler for async behaviour. Throwing an
1094 * exception from the handler will cancel the save operation.
1095 *
1096 * @event Model#updating
1097 * @param {Model} model
1098 * The model firing the event. Its attributes are already changed but
1099 * not commited to the database yet.
1100 * @param {Object} attrs
1101 * Attributes that will be updated.
1102 *
1103 * **Note**: There's currently a bug that leads to attrs only
1104 * containing attributes that were passed as argument to
1105 * {@link Model#save save}. You can work around this by accessing
1106 * `model.changed` which does contain all the attributes that will be
1107 * updated.
1108 * @param {Object} options Options object passed to {@link Model#save save}.
1109 * @returns {Promise}
1110 */
1111 return this.triggerThen(method === 'insert' ? 'saving creating' : 'saving updating', this, attrs, options)
1112 .bind(this)
1113 .then(function() {
1114 return sync[options.method](method === 'update' && options.patch ? attrs : this.attributes);
1115 })
1116 .then(function(resp) {
1117 // After a successful database save, the id is updated if the model was created
1118 if (method === 'insert' && this.id == null) {
1119 const updatedCols = {};
1120 updatedCols[this.idAttribute] = this.id = resp[0];
1121 const updatedAttrs = this.parse(updatedCols);
1122 Object.assign(this.attributes, updatedAttrs);
1123 } else if (method === 'update' && resp === 0) {
1124 if (options.require !== false) {
1125 throw new this.constructor.NoRowsUpdatedError('No Rows Updated');
1126 }
1127 }
1128
1129 this._reset();
1130
1131 /**
1132 * Saved event.
1133 *
1134 * Fired after an `insert` or `update` query.
1135 *
1136 * @event Model#saved
1137 * @param {Model} model The model firing the event.
1138 * @param {(Array|Number)} response
1139 * A list containing the id of the newly created model in case of an
1140 * `insert` or a number representing the affected rows in the case of
1141 * an `update` query.
1142 * @param {Object} options Options object passed to {@link Model#save save}.
1143 * @returns {Promise}
1144 */
1145
1146 /**
1147 * Created event.
1148 *
1149 * Fired after an `insert` query.
1150 *
1151 * @event Model#created
1152 * @param {Model} model The model firing the event.
1153 * @param {Array} newId A list containing the id of the newly created model.
1154 * @param {Object} options Options object passed to {@link Model#save save}.
1155 * @returns {Promise}
1156 */
1157
1158 /**
1159 * Updated event.
1160 *
1161 * Fired after an `update` query.
1162 *
1163 * @event Model#updated
1164 * @param {Model} model The model firing the event.
1165 * @param {Number} affectedRows Number of rows affected by the update.
1166 * @param {Object} options Options object passed to {@link Model#save save}.
1167 * @returns {Promise}
1168 */
1169 return this.triggerThen(method === 'insert' ? 'created saved' : 'updated saved', this, resp, options);
1170 });
1171 })
1172 .return(this);
1173 }),
1174
1175 /**
1176 * `destroy` performs a `delete` on the model, using the model's {@link
1177 * Model#idAttribute idAttribute} to constrain the query.
1178 *
1179 * A {@link Model#destroying "destroying"} event is triggered on the model before being
1180 * destroyed. To prevent destroying the model (with validation, etc.), throwing an error
1181 * inside one of these event listeners will stop destroying the model and
1182 * reject the promise.
1183 *
1184 * A {@link Model#destroyed "destroyed"} event is fired after the model's
1185 * removal is completed.
1186 *
1187 * @method Model#destroy
1188 *
1189 * @param {Object=} options Hash of options.
1190 * @param {Transaction=} options.transacting Optionally run the query in a transaction.
1191 * @param {Boolean} [options.require=true]
1192 * Throw a {@link Model.NoRowsDeletedError} if no records are affected by destroy. This is
1193 * the default behavior as of version 0.13.0.
1194 *
1195 * @example
1196 *
1197 * new User({id: 1})
1198 * .destroy()
1199 * .then(function(model) {
1200 * // ...
1201 * });
1202 *
1203 * @fires Model#destroying
1204 * @fires Model#destroyed
1205 *
1206 * @throws {Model.NoRowsDeletedError}
1207 *
1208 * @returns {Promise<Model>} A promise resolving to the destroyed and thus
1209 * empty model, i.e. all attributes are `undefined`.
1210 */
1211 destroy: Promise.method(function(options) {
1212 options = options ? _.clone(options) : {};
1213 const sync = this.sync(options);
1214 options.query = sync.query;
1215 return Promise.bind(this)
1216 .then(function() {
1217 /**
1218 * Destroying event.
1219 *
1220 * Fired before a `delete` query. A promise may be returned from the event
1221 * handler for async behaviour. Throwing an exception from the handler
1222 * will reject the promise and cancel the deletion.
1223 *
1224 * @event Model#destroying
1225 * @param {Model} model The model firing the event.
1226 * @param {Object} options Options object passed to {@link Model#destroy destroy}.
1227 * @returns {Promise}
1228 */
1229 return this.triggerThen('destroying', this, options);
1230 })
1231 .then(function() {
1232 return sync.del();
1233 })
1234 .then(function(affectedRows) {
1235 if (options.require !== false && affectedRows === 0) {
1236 throw new this.constructor.NoRowsDeletedError('No Rows Deleted');
1237 }
1238
1239 this._previousAttributes = _.clone(this.attributes);
1240 this.clear();
1241
1242 /**
1243 * Destroyed event.
1244 *
1245 * Fired after a `delete` query. A promise may be returned from the event
1246 * handler for async behaviour.
1247 *
1248 * @event Model#destroyed
1249 * @param {Model} model The model firing the event.
1250 * @param {Object} options Options object passed to {@link Model#destroy destroy}.
1251 * @returns {Promise}
1252 */
1253 return this.triggerThen('destroyed', this, options);
1254 })
1255 .then(this._reset);
1256 }),
1257
1258 /**
1259 * Used to reset the internal state of the current query builder instance.
1260 * This method is called internally each time a database action is completed
1261 * by {@link Sync}
1262 *
1263 * @method Model#resetQuery
1264 * @returns {Model} Self, this method is chainable.
1265 */
1266 resetQuery() {
1267 this._knex = null;
1268 return this;
1269 },
1270
1271 /**
1272 * The `query` method is used to tap into the underlying Knex query builder
1273 * instance for the current model. If called with no arguments, it will
1274 * return the query builder directly. Otherwise, it will call the specified
1275 * method on the query builder, applying any additional arguments from the
1276 * `model.query` call. If the method argument is a function, it will be
1277 * called with the Knex query builder as the context and the first argument,
1278 * returning the current model.
1279 *
1280 * @example
1281 *
1282 * model
1283 * .query('where', 'other_id', '=', '5')
1284 * .fetch()
1285 * .then(function(model) {
1286 * // ...
1287 * });
1288 *
1289 * model
1290 * .query({where: {other_id: '5'}, orWhere: {key: 'value'}})
1291 * .fetch()
1292 * .then(function(model) {
1293 * // ...
1294 * });
1295 *
1296 * model.query(function(qb) {
1297 * qb.where('other_person', 'LIKE', '%Demo').orWhere('other_id', '>', 10);
1298 * }).fetch()
1299 * .then(function(model) {
1300 * // ...
1301 * });
1302 *
1303 * let qb = model.query();
1304 * qb.where({id: 1}).select().then(function(resp) {
1305 * // ...
1306 * });
1307 *
1308 * @method Model#query
1309 * @param {function|Object|...string=} arguments The query method.
1310 * @returns {Model|QueryBuilder}
1311 * Will return this model or, if called with no arguments, the underlying query builder.
1312 *
1313 * @see {@link http://knexjs.org/#Builder Knex `QueryBuilder`}
1314 */
1315 query() {
1316 return Helpers.query(this, Array.from(arguments));
1317 },
1318
1319 /**
1320 * The where method is used as convenience for the most common {@link
1321 * Model#query query} method, adding a where clause to the builder. Any
1322 * additional knex methods may be accessed using {@link Model#query query}.
1323 *
1324 * Accepts either key, value syntax, or a hash of attributes.
1325 *
1326 * @example
1327 *
1328 * model.where('favorite_color', '<>', 'green').fetch().then(function() { //...
1329 * // or
1330 * model.where('favorite_color', 'red').fetch().then(function() { //...
1331 * // or
1332 * model.where({favorite_color: 'red', shoe_size: 12}).fetch().then(function() { //...
1333 *
1334 * @method Model#where
1335 * @param {Object|...string} method
1336 *
1337 * Either `key, [operator], value` syntax, or a hash of attributes to
1338 * match. Note that these must be formatted as they are in the database,
1339 * not how they are stored after {@link Model#parse}.
1340 *
1341 * @returns {Model} Self, this method is chainable.
1342 *
1343 * @see Model#query
1344 */
1345 where() {
1346 return this.query.apply(this, ['where'].concat(Array.from(arguments)));
1347 },
1348
1349 /**
1350 * @method Model#orderBy
1351 * @since 0.9.3
1352 * @description
1353 *
1354 * Specifies the column to sort on and sort order.
1355 *
1356 * The order parameter is optional, and defaults to 'ASC'. You may
1357 * also specify 'DESC' order by prepending a hyphen to the sort column
1358 * name. `orderBy("date", 'DESC')` is the same as `orderBy("-date")`.
1359 *
1360 * Unless specified using dot notation (i.e., "table.column"), the default
1361 * table will be the table name of the model `orderBy` was called on.
1362 *
1363 * @example
1364 *
1365 * Car.forge().orderBy('color', 'ASC').fetchAll()
1366 * .then(function (rows) { // ...
1367 *
1368 * @param sort {string}
1369 * Column to sort on
1370 * @param order {string}
1371 * Ascending ('ASC') or descending ('DESC') order
1372 */
1373 orderBy() {
1374 return Helpers.orderBy.apply(null, [this].concat(Array.from(arguments)));
1375 },
1376
1377 /* Ensure that QueryBuilder is copied on clone. */
1378 clone() {
1379 // This needs to use the direct apply method because the spread operator
1380 // incorrectly converts to `clone.apply(ModelBase.prototype, arguments)`
1381 // instead of `apply(this, arguments)`
1382 const cloned = BookshelfModel.__super__.clone.apply(this, arguments);
1383 if (this._knex != null) {
1384 cloned._knex = cloned._builder(this._knex.clone());
1385 }
1386 return cloned;
1387 },
1388
1389 /**
1390 * Creates and returns a new Bookshelf.Sync instance.
1391 *
1392 * @method Model#sync
1393 * @private
1394 * @returns Sync
1395 */
1396 sync(options) {
1397 return new Sync(this, options);
1398 },
1399
1400 /**
1401 * Helper for setting up the `morphOne` or `morphMany` relations.
1402 *
1403 * @method Model#_morphOneOrMany
1404 * @private
1405 */
1406 _morphOneOrMany(Target, morphName, columnNames, morphValue, type) {
1407 if (!Array.isArray(columnNames)) {
1408 // Shift by one place
1409 morphValue = columnNames;
1410 columnNames = null;
1411 }
1412 if (!morphName || !Target) throw new Error('The polymorphic `name` and `Target` are required.');
1413
1414 return this._relation(type, Target, {
1415 morphName: morphName,
1416 morphValue: morphValue,
1417 columnNames: columnNames
1418 }).init(this);
1419 },
1420
1421 /**
1422 * @name Model#_handleResponse
1423 * @private
1424 * @description
1425 *
1426 * Handles the response data for the model, returning from the model's fetch call.
1427 *
1428 * @param {Object} Response from Knex query.
1429 *
1430 * @todo: need to check on Backbone's status there, ticket #2636
1431 * @todo: {silent: true, parse: true}, for parity with collection#set
1432 */
1433 _handleResponse(response) {
1434 const relatedData = this.relatedData;
1435
1436 this.set(this.parse(response[0]), {silent: true})
1437 .formatTimestamps()
1438 ._reset();
1439 this._previousAttributes = _.cloneDeep(this.attributes);
1440
1441 if (relatedData && relatedData.isJoined()) {
1442 relatedData.parsePivot([this]);
1443 }
1444 },
1445
1446 /**
1447 * @name Model#_handleEager
1448 * @private
1449 * @description
1450 *
1451 * Handles the related data loading on the model.
1452 *
1453 * @param {Object} Response from Knex query.
1454 */
1455 _handleEager(response, options) {
1456 return new EagerRelation([this], response, this).fetch(options);
1457 }
1458 },
1459 {
1460 extended(child) {
1461 /**
1462 * @class Model.NotFoundError
1463 * @description
1464 *
1465 * Thrown when no records are found by {@link Model#fetch fetch} or
1466 * {@link Model#refresh} when called with the
1467 * `{require: true}` option.
1468 */
1469 child.NotFoundError = createError(this.NotFoundError);
1470
1471 /**
1472 * @class Model.NoRowsUpdatedError
1473 * @description
1474 *
1475 * Thrown when no records are saved by {@link Model#save save}
1476 * unless called with the `{require: false}` option.
1477 */
1478 child.NoRowsUpdatedError = createError(this.NoRowsUpdatedError);
1479
1480 /**
1481 * @class Model.NoRowsDeletedError
1482 * @description
1483 *
1484 * Thrown when no record is deleted by {@link Model#destroy destroy}
1485 * if called with the `{require: true}` option.
1486 */
1487 child.NoRowsDeletedError = createError(this.NoRowsDeletedError);
1488 }
1489 }
1490);
1491
1492BookshelfModel.NotFoundError = Errors.NotFoundError;
1493BookshelfModel.NoRowsUpdatedError = Errors.NoRowsUpdatedError;
1494BookshelfModel.NoRowsDeletedError = Errors.NoRowsDeletedError;
1495
1496module.exports = BookshelfModel;