@smallprod/models
Version:
47 lines (46 loc) • 1.44 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.SeedRow = void 0;
const migrationtype_1 = __importDefault(require("./migrationtype"));
class SeedTable extends migrationtype_1.default {
constructor(tableName) {
super(tableName, 'seed');
this.rows = [];
this.clear = false;
this.addRow = () => {
const row = new SeedRow();
this.rows.push(row);
return row;
};
this.clearTable = () => {
this.clear = true;
};
this.execute = async (model) => {
if (this.clear) {
await model.delete(this.tableName, []);
}
else {
await this.rows.reduce(async (prev, cur) => {
await prev;
const attributes = cur.getData();
await model.insert(this.tableName, attributes);
}, Promise.resolve());
}
};
}
}
exports.default = SeedTable;
class SeedRow {
constructor() {
this.data = [];
this.add = (columnName, value) => {
this.data.push({ value, column: columnName });
return this;
};
this.getData = () => this.data;
}
}
exports.SeedRow = SeedRow;