UNPKG

@mikro-orm/migrations

Version:

TypeScript ORM for Node.js based on Data Mapper, Unit of Work and Identity Map patterns. Supports MongoDB, MySQL, PostgreSQL and SQLite databases as well as usage with vanilla JavaScript.

50 lines (49 loc) 1.37 kB
/** Base class for SQL database migrations. Extend this class and implement `up()` (and optionally `down()`). */ export class Migration { driver; config; #queries = []; ctx; #em; constructor(driver, config) { this.driver = driver; this.config = config; } down() { throw new Error('This migration cannot be reverted'); } isTransactional() { return true; } addSql(sql) { this.#queries.push(sql); } reset() { this.#queries.length = 0; this.ctx = undefined; } setTransactionContext(ctx) { this.ctx = ctx; } /** * Executes a raw SQL query. Accepts a string SQL, `raw()` SQL fragment, or a native query builder instance. * The `params` parameter is respected only if you use string SQL in the first parameter. */ async execute(sql, params) { return this.driver.execute(sql, params, 'all', this.ctx); } /** * Creates a cached `EntityManager` instance for this migration, which will respect * the current transaction context. */ getEntityManager() { if (!this.#em) { this.#em = this.driver.createEntityManager(); this.#em.setTransactionContext(this.ctx); } return this.#em; } getQueries() { return this.#queries; } }