# Model Collection

ModelCollection is a sub class of the Collection, therefore all methods are inherited. The following methods have been updated to use either the model's is method, or the primary key of the model for comparison between models. unique, hasDuplicates, duplicates, diff, only, except, intersect, delete, union, includes. The signature of the before mentioned methods has not changed. In addition, the ModelCollection ensures that only Models are included in the collection. On top of the collection's methods couple of others has been added that are only relevant to model values.

# Methods

# modelKeys

The modelKeys method returns the primary key of the models on a Collection.

import User from '@Models/User';

const modelCollection = await User.get();
modelCollection.modelKeys(); // Collection[1, 2, 3, ...ids]
1
2
3
4

# findByKey

The findByKey method returns the Model or ModelCollection depending on the argument. The method can take the ids as a single argument or as an array or collection. Optionally you may give it a second argument which will be returned is the id is not found in the model collection.

import User from '@Models/User';
import { Model } from '@upfrontjs/framework';

const defaultModel = new Model;
const modelCollection = await User.get();
modelCollection.findByKey(1); // User1
modelCollection.findByKey([1, 2]); // ModelCollection[User1, User2]
modelCollection.findByKey(43, defaultModel); // Model
1
2
3
4
5
6
7
8

# isModelCollection

The isModelCollection static method same as the isCollection method on the collection, is used to evaluate that the given value is a ModelCollection.

import { ModelCollection, Collection } from '@upfrontjs/framework';

const modelCollection = await User.get();
ModelCollection.isModelCollection(modelCollection); // true
ModelCollection.isModelCollection([Model1, Model2]); // false
Collection.isCollection(modelCollection); // true
1
2
3
4
5
6

TIP

The map method returns a ModelCollection if the return of the given callback is a Model, otherwise it returns a Collection.