Collections represent arrays of models. They are returned by a hasMany
association, or by one of the ModelClass query methods:
let posts = schema.blogPosts.all();
let posts = schema.blogPosts.find([1, 2, 4]);
let posts = schema.blogPosts.where({published: true});
These methods are available on your collection instances.
Returns the dasherized model name of models in this collection.
let posts = author.blogPosts.all();
posts.modelName; // "blog-post"
Updates each model in the collection (persisting immediately to the db).
let posts = author.blogPosts.all();
posts.update('published', true); // the db was updated for all posts
Saves all models in the collection.
let posts = author.blogPosts.all();
posts.models[0].published = true;
posts.save(); // all posts saved to db
Reloads each model in the collection.
let posts = author.blogPosts.all();
// ...
posts.reload(); // reloads data for each post from the db
Destroys the db record for all models in the collection.
let posts = author.blogPosts.all();
posts.destroy(); // all posts removed from db
Returns a new Collection
with its models
sorted according to the provided compare function
let posts = author.blogPosts.all();
let postsByTitleAsc = posts.sort((a, b) => {
return b.title < a.title;
});
Returns a new Collection
with only the models
that pass the test implemented by the provided function.
let posts = author.blogPosts.all();
let longPosts = posts.filter((postModel) => {
return postModel.wordCount >= 1000;
});