Schema
The primary use of the Schema class is to use it to find Models and Collections via the Model class methods.
The Schema is most often accessed via the first parameter to a route handler:
this.get('posts', schema => {
return schema.posts.where({ isAdmin: false });
});It is also available from the .schema property of a server instance:
server.schema.users.create({ name: 'Yehuda' });To work with the Model or Collection returned from one of the methods below, refer to the instance methods in the API docs for the Model and Collection classes.
Properties
db: Object
Returns Mirage's database. See the Db docs for the db's API.
Methods
all(type: any): any
Return all models in the database.
let posts = blogPosts.all();
// [post:1, post:2, ...]associationsFor(modelName: String): Object
Returns an object containing the associations registered for the model of the given modelName.
For example, given this configuration
import { Server, Model, hasMany, belongsTo } from 'miragejs'
let server = new Server({
models: {
user: Model,
article: Model.extend({
fineAuthor: belongsTo("user"),
comments: hasMany()
}),
comment: Model
}
})each of the following would return empty objects
server.schema.associationsFor('user')
// {}
server.schema.associationsFor('comment')
// {}but the associations for the article would return
server.schema.associationsFor('article')
// {
// fineAuthor: BelongsToAssociation,
// comments: HasManyAssociation
// }Check out the docs on the Association class to see what fields are available for each association.
create(type: any, attrs: any): any
Create a new model instance with attributes attrs, and insert it into the database.
let post = blogPosts.create({title: 'Lorem ipsum'});
post.title; // Lorem ipsum
post.id; // 1
post.isNew(); // falsefind(type: any, ids: any): any
Return one or many models in the database by id.
let post = blogPosts.find(1);
let posts = blogPosts.find([1, 3, 4]);findBy(type: any, attributeName: any): any
Returns the first model in the database that matches the key-value pairs in attrs. Note that a string comparison is used.
let post = blogPosts.findBy({ published: true });
let post = blogPosts.findBy({ authorId: 1, published: false });
let post = blogPosts.findBy({ author: janeSmith, featured: true });This will return null if the schema doesn't have any matching record.
findOrCreateBy(type: any, attributeName: any): any
Returns the first model in the database that matches the key-value pairs in attrs, or creates a record with the attributes if one is not found.
// Find the first published blog post, or create a new one.
let post = blogPosts.findOrCreateBy({ published: true });first(type: any): any
Returns the first model in the database.
let post = blogPosts.first();N.B. This will return null if the schema doesn't contain any records.
new(type: any, attrs: any): any
Create a new unsaved model instance with attributes attrs.
let post = blogPosts.new({ title: 'Lorem ipsum' });
post.title; // Lorem ipsum
post.id; // null
post.isNew(); // truenone(type: any): any
Return an empty collection of type type.
where(type: any, query: any): any
Return an ORM/Collection, which represents an array of models from the database matching query.
If query is an object, its key-value pairs will be compared against records using string comparison.
query can also be a compare function.
let posts = blogPosts.where({ published: true });
let posts = blogPosts.where(post => post.published === true);