# Store

To understand how the store works, first read the explanation.


# get

The get method accepts several different signatures:

# get(typeName, key, field)

store.get('Book', 123, 'title');
// returns: "a string"

Returns the value of the field for the requested entity. If the type of the field is a scalar type (e.g. Int, String, etc.) the value is returned. If the type of the field is a type (ie. a Book or User), a reference is returned:

// assuming the schema is `type Book { owner: User }`

store.get('Book', 123, 'owner');
// returns: { $ref: { typeName: 'User', key: 456 } }

# get(typeName, key)

If a field is not specified as the third argument, a reference is returned.

store.get('Book', 123);
// returns: { $ref: { typeName: 'Book', key: 123 } }

# get(ref, field)

A reference can be used as first argument to get values for that reference:

const $book = store.get('Book', 123);

store.get($book, 'title');
// returns: "a string"

# get(typeName)

When calling get with only a typeName, a new ref is returned:

store.get('Book');
// returns: { $ref: { typeName: 'Book', key: 123 } }

The key will be generated via the mock of the type of the id field.

This is equivalent to an insert without any values.

# Nested get

Instead of providing a string for the field, an array can be used to traverse the graph:

store.get('Book', 123, ['owner', 'username']);

is the equivalent of:

const $owner = store.get('Book', 123, 'owner');
store.get($owner, 'username');

# set

The set method accepts several different signatures:

# set(typeName, key, field, value)

Set a field value in the store for the given type, store key and field name:

store.set('Book', 123, 'title', 'some title');

store.get('Book', 123, 'title');
// returns: "some title"

If the field you're setting is a type (ie. Book or User), a reference can be provided:

const $user = store.get('User', 456);
store.set('Book', 123, 'owner', $user);

# set(ref, field, value)

Just like get, a reference can be used as first argument to set values:

const $book = store.get('Book', 123);
store.set($book, 'title', 'a better title');

# Provide multiple values

Multiple values can be passed using an object:

store.set('Book', 123, { title: 'The title', author: 'John Doe' });

// or using a reference
const $book = store.get('Book', 123);
store.set($book, { title: 'The title', author: 'John Doe' });

# Nested set

A nested object can be passed to deeply set values:

store.set('Book', 123, {
  owner: {
    name: 'that guy'
  }
})

# find

The find method allows to store to be queried, but only fixtures. An array of references are returned.

store.find(typeName, predicate, opts)

# Arguments

Name Type Description
typeName string The type to find
predicate (instance) => boolean or {} (optional) A predicate function or object for each instance
opts {} (optional) Options

Options

  • fixturesOnly: Return only instances from fixtures (default: true).

# find example

Assuming the following fixtures:

const fixtures = {
  Book: [
    { id: 1, title: 'The amazing story' },
    { id: 2, title: 'A great story' },
    { id: 3, title: 'Greatest letters assembled into words' }
  ]
}
store.find('Book')
// returns: an array of reference to the `Book` fixtures

A predicate function can be also be passed:

store.find('Book', (book) => book.title.includes('story'));

Or a predicate object:

store.find('Book', { title: 'A great story' });

The following code tries to access fields that are not included in the fixtures, which will produce unstable results.

store.find('Book', (book) => book.author.includes('Shakespeare'));

This code attempts to get the author field. There is no guarantee that this field will exist, as it doesn't have values set in the fixtures.


# findOne

findOne is like find, but only returns the first result, or null if no results.


# insert

Insert an instance into the store. Returns a reference to the inserted instance.

store.insert(typeName, values);
// returns: a reference to the inserted instance

# Arguments

Name Type Description
typeName string The type to find
values object The values of the instance to insert

# has

Check if an instance or instance field exists in the store. Returns a boolean.

store.has(typeName, id, field);
// returns: boolean

# Arguments

Name Type Description
typeName string The type to find
id mixed The id of the instance
field string (optional) The field of the instance

# reset

This method will reset the store (and erase the cache).

store.reset();