.fetchOrCreate()
Fetches the first entry that matches 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)
Returns
Description
First entry that matches the filter or, if no entry is found, the newly created one
// Note: Users is a collection.
Users.create({ name: 'Richard' }); // { name: 'Richard' }
Users.fetchAll(); // [{ name: 'Richard' }]
Users.fetch(u => u.name === 'Henry'); // null
Users.fetchOrCreate(u => u.name === 'Henry', { name: 'Henry' }); // { name: 'Henry' }
Users.fetchAll(); // [{ name: 'Richard' }, { name: 'Henry' }]
Last updated
Was this helpful?