@riao/dbal
Version:
62 lines • 2.66 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.AutoSeed = void 0;
const comparison_1 = require("../comparison");
const seed_1 = require("./seed");
class AutoSeed extends seed_1.Seed {
constructor() {
super(...arguments);
this.primaryKey = 'id';
}
up() {
return __awaiter(this, void 0, void 0, function* () {
const resolvedRecords = yield Promise.all(this.records.map((record) => __awaiter(this, void 0, void 0, function* () { return typeof record === 'function' ? record.bind(this)() : record; })));
for (const record of resolvedRecords) {
const inserted = yield this.query.insertOne({
table: this.table,
record: record,
ifNotExists: true,
});
yield this.query.insertOne({
table: 'riao_seed',
record: {
name: this.name,
tableName: this.table,
recordId: inserted[this.primaryKey],
},
ifNotExists: true,
});
}
});
}
down() {
return __awaiter(this, void 0, void 0, function* () {
const records = (yield this.query.find({
table: 'riao_seed',
where: { name: this.name },
}));
const recordIds = records.map((record) => record.id);
yield this.query.delete({
table: this.table,
where: { id: (0, comparison_1.inArray)(recordIds) },
});
yield this.query.delete({
table: 'riao_seed',
where: {
name: this.name,
},
});
});
}
}
exports.AutoSeed = AutoSeed;
//# sourceMappingURL=auto-seed.js.map