The "fixtures" app provides a way for you to define data to quickly load to and
unload from the database. Any app can define fixtures in a fixtures.js
file
in the app directory. More on how to write these is further down.
Fixtures are identified by name, which you must reference if using the CLI commands to load or unload fixtures.
This is the command to load a named fixture into the database (if it is already loaded, nothing will happen):
node . fixtures load <fixture_name>
This is the command to unload a named fixture from the database (if it is not currently loaded, nothing will happen):
node . fixtures unload <fixture_name>
The "fixtures" app defines two models that are for internal use in tracking fixtures: "Fixture" and "FixtureRecord". These are used to track which fixtures have been loaded and what objects in the database belong to what fixtures.
You can write fixtures in your custom app in a fixtures.js
file. That file
should export an object mapping fixture names to fixture objects.
A fixture object should have the following keys:
ensureFixtures
: An optional array of fixture names that should be loaded before this one.before
: An optional async function taking no arguments that is called before loading beings.beforeUnload
: An optional async function taking no arguments that is called before unloading beings.beforeSaveEach
: An optional async
function taking the created object and model name that is called and awaited before the object is first saved to the database.beforeDeleteEach
: An optional async
function taking the created object and model name that is called and awaited before the object is deleted (when unloading the fixture).records
: An array of records, more on this below.afterSaveEach
: An optional async function taking the created object and model name that is called and awaited after the object first saved to the database.after
: An optional async function taking a list of created objects that is called after the fixture is loaded.afterUnload
: An optional async function taking no arguments that is called after the fixture is unloaded.Each object in records
should have the following keys:
model
: The name of the model that an object should be created for.tag
: An optional string, unique to the fixture, that identifies the specific record. A later update of Carolina may make use of this for making references.attributes
: An object of initial attributes. These attributes will be passed to the constructor of the given model.beforeSave
: An optional async method taking the created object that is called and awaited before the object is saved.afterSave
: An optional async method taking the created object that is called and awaited after the object is saved.For an example, here is the fixtures.js
defined by the "auth" app, which
loads users with pony names into the database:
const App = require('@carolina/core');
const pony_users = {
ensureFixtures: [],
beforeDeleteEach: async(obj, modelName) => { console.log(`Deleting ${modelName}.`); },
records: [
{
model: 'User',
tag: 'applejack',
attributes: {
username: 'Applejack',
isAdmin: false
},
beforeSave: async (obj) => {
await App.$auth.setUserPassword(obj, 'orange123');
}
},
{
model: 'User',
tag: 'pinkiepie',
attributes: {
username: 'PinkiePie',
isAdmin: false
},
beforeSave: async (obj) => {
await App.$auth.setUserPassword(obj, 'pink123');
}
},
{
model: 'User',
tag: 'rarity',
attributes: {
username: 'Rarity',
isAdmin: false
},
beforeSave: async (obj) => {
await App.$auth.setUserPassword(obj, 'white123');
}
},
{
model: 'User',
tag: 'twilightsparkle',
attributes: {
username: 'TwilightSparkle',
isAdmin: false
},
beforeSave: async (obj) => {
await App.$auth.setUserPassword(obj, 'purple123');
}
},
{
model: 'User',
tag: 'rainbowdash',
attributes: {
username: 'RainbowDash',
isAdmin: false
},
beforeSave: async (obj) => {
await App.$auth.setUserPassword(obj, 'blue123');
}
},
{
model: 'User',
tag: 'fluttershy',
attributes: {
username: 'Fluttershy',
isAdmin: false
},
beforeSave: async (obj) => {
await App.$auth.setUserPassword(obj, 'yellow123');
}
}
],
afterSaveEach: async (obj, modelName) => { console.log(`New ${modelName} created.`); },
};
module.exports = {
pony_users
};