loopback4-seeder
Version:
Run seeders to populate database
54 lines • 2.32 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.SeedMixin = void 0;
const index_1 = require("../index");
const components_1 = require("../components");
function SeedMixin(superClass) {
return class extends superClass {
constructor(...args) {
super(...args);
this.component(components_1.SeederComponent);
}
async load(args) {
// Get command arguments classes
const filterClasses = args.slice(2);
/* eslint-disable @typescript-eslint/ban-ts-comment */
// A workaround to access protected Application methods
const self = this;
(0, index_1.debug)('[INFO] Loading seeds');
/**
* Application implements `SeedMixinInterface` and have `loadSeeds` method.
*/
// @ts-ignore
if (typeof self.loadSeeds === 'function') {
(0, index_1.debug)('[INFO] Loading seeds from "loadSeeds" function');
// @ts-ignore
await self.loadSeeds(filterClasses);
}
/**
* Seeder as class created and bound to Application.
*/
// Find all tagged bindings as `seeder`
const seederBindings = this.findByTag('seed');
// Filter seeder bindings if filter classes parameter provided from argv
const filteredSeederBindings = filterClasses.length ?
seederBindings.filter(({ key }) => filterClasses.includes(key.replace(`${index_1.SEEDS_NAMESPACE}.`, '').replace('Seeder', ''))) :
seederBindings;
// Loop over filtered/all bindings
for (const s of filteredSeederBindings) {
(0, index_1.debug)(`[INFO] Loading seeds from "${s.key}" class`);
// Get instance of the seeder
const instance = await this.get(s.key);
// Run before seed
await instance.beforeSeed();
// Run seed
await instance.seed();
}
}
async loadByModel(items, repository$, type) {
return (0, index_1.loadByModel)(items, repository$, type);
}
};
}
exports.SeedMixin = SeedMixin;
//# sourceMappingURL=seeder.mixin.js.map