Skip to content

Instantly share code, notes, and snippets.

@sharepointoscar
Last active March 23, 2025 00:02
Show Gist options
  • Save sharepointoscar/30c5d5a398b34b2e8c82c19ad3ddd89f to your computer and use it in GitHub Desktop.
Save sharepointoscar/30c5d5a398b34b2e8c82c19ad3ddd89f to your computer and use it in GitHub Desktop.
/**
* Seed Function
* (sails.config.bootstrap)
*
* A function that runs just before your Sails app gets lifted.
* > Need more flexibility? You can also create a hook.
*
* For more information on seeding your app with fake data, check out:
* https://sailsjs.com/config/bootstrap
*/
module.exports.bootstrap = async function() {
// Import dependencies
var path = require('path');
// This bootstrap version indicates what version of fake data we're dealing with here.
var BOOTSTRAP_VERSION = 1;
// This path indicates where to store/look for the JSON file that tracks the "last run bootstrap info"
var bootstrapLastRunInfoPath = path.resolve(sails.config.appPath, '.tmp/bootstrap-version.json');
// Whether or not to continue doing the stuff in this file (i.e. wiping and regenerating data)
// depends on some factors:
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// If the bootstrap version has been changed, then run the seed data again
if (await sails.helpers.fs.exists(bootstrapLastRunInfoPath)) {
let lastRunBootstrapInfo = await sails.helpers.fs.readJson(bootstrapLastRunInfoPath);
if (lastRunBootstrapInfo.version === BOOTSTRAP_VERSION) {
sails.log('Skipping v'+BOOTSTRAP_VERSION+' bootstrap seed data because it was already run');
return;
}
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// Otherwise, we're running the bootstrap for the first time, or it's a new version
sails.log('Running v'+BOOTSTRAP_VERSION+' bootstrap seed data...');
// Add seed data below
// -----------------
// Example for a Book model:
await Book.createEach([
{ title: 'The Great Gatsby', author: 'F. Scott Fitzgerald', year: 1925 },
{ title: 'To Kill a Mockingbird', author: 'Harper Lee', year: 1960 },
{ title: '1984', author: 'George Orwell', year: 1949 },
{ title: 'The Catcher in the Rye', author: 'J.D. Salinger', year: 1951 },
]);
// Save the bootstrap version
await sails.helpers.fs.writeJson.with({
destination: bootstrapLastRunInfoPath,
json: { version: BOOTSTRAP_VERSION },
force: true
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment