.fetchOrCreate()

Fetches the entries that match the provided filter directly from the JSON file. If no entry is found, creates and pushes a new one with the provided data into the collection.

fetchOrCreate(filter, data)

Parameter

Description

filter

Callback

Function to test each element of the array. Return a value that coerces to true to keep the element, or to false otherwise

data

Data

Entry's data

Returns

Description

Entries that match the filter or a single entry if the array length is equal to 1 and a filter is provided

// Note: Users is a collection.

Users.create({ name: 'Richard' }); // { name: 'Richard' }

Users.fetch(); // [{ name: 'Richard' }]
Users.fetch(u => u.name === 'Henry'); // null

Users.fetchOrCreate(u => u.name === 'Henry', { name: 'Henry' }); // { name: 'Henry' }
Users.fetch(); // [{ name: 'Richard' }, { name: 'Henry' }]

Last updated