UNPKG

nawr

Version:
42 lines (38 loc) 988 B
const { once } = require('lodash') const createMigrationTable = once(async transaction => { return transaction.query(` create table if not exists migration ( name text primary key, date timestamptz not null default now() ) `) }) class Storage { constructor({ transaction, client }) { this.transaction = transaction this.client = client } async logMigration(name) { await createMigrationTable(this.client) return this.transaction.query( 'INSERT INTO migration (name) VALUES(:name);', { name } ) } async unlogMigration(name) { return this.transaction.query('DELETE FROM migration WHERE name = :name;', { name }) } async executed() { await createMigrationTable(this.client) return this.client .query(`select name from migration order by date;`) .then(({ records }) => { return records.map(record => record.name) }) } } module.exports = Storage