Skip to content
This repository has been archived by the owner on May 19, 2023. It is now read-only.

Commit

Permalink
feat: fix lint
Browse files Browse the repository at this point in the history
  • Loading branch information
nduchak committed Aug 18, 2020
1 parent cecdf4b commit fd1f396
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 14 deletions.
17 changes: 12 additions & 5 deletions migrations/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@ import { Sequelize } from 'sequelize-typescript'

const logger = loggingFactory('db:migrations')

type UpOptions = string | string[] | Umzug.UpToOptions | Umzug.UpDownMigrationsOptions
type DownOptions = string | string[] | Umzug.DownToOptions | Umzug.UpDownMigrationsOptions

export class Migration {
private umzugIns: Umzug.Umzug
private readonly umzugIns: Umzug.Umzug

constructor (sequelize: Sequelize) {
this.umzugIns = new Umzug({
Expand All @@ -23,12 +26,12 @@ export class Migration {
}

// eslint-disable-next-line require-await
async up (options?: string | string[] | Umzug.UpToOptions | Umzug.UpDownMigrationsOptions): Promise<Umzug.Migration[]> {
async up (options?: UpOptions): Promise<Umzug.Migration[]> {
return this.umzugIns.up(options as any)
}

// eslint-disable-next-line require-await
async down (options?: string | string[] | Umzug.DownToOptions | Umzug.UpDownMigrationsOptions): Promise<Umzug.Migration[]> {
async down (options?: DownOptions): Promise<Umzug.Migration[]> {
return this.umzugIns.down(options as any)
}

Expand All @@ -39,7 +42,9 @@ export class Migration {
// eslint-disable-next-line require-await
async pending (): Promise<Umzug.Migration[]> {
return this.umzugIns.pending().catch(e => {
if (e.code === 'ENOENT') return []
if (e.code === 'ENOENT') {
return []
}
throw e
})
}
Expand All @@ -55,7 +60,9 @@ export default class DbMigration {

static getInstance (sequelize?: Sequelize): Migration {
if (!DbMigration.ins) {
if (!sequelize) throw new Error('You need to provide Sequelize instance')
if (!sequelize) {
throw new Error('You need to provide Sequelize instance')
}
DbMigration.ins = new Migration(sequelize)
}

Expand Down
26 changes: 18 additions & 8 deletions src/cli/db-migration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,9 @@ export default class DbMigrationCommand extends BaseCLICommand {
: `${parsed.base}.sqlite`
)
} else {
if (db[db.length - 1] === '/') throw new Error('Path should include the file name')
if (db[db.length - 1] === '/') {
throw new Error('Path should include the file name')
}
return path.resolve(`${db}${parsed.ext ? '' : '.sqlite'}`)
}
}
Expand Down Expand Up @@ -118,23 +120,31 @@ export default class DbMigrationCommand extends BaseCLICommand {

async run (): Promise<void> {
const { flags: originalFlags } = this.parse(DbMigrationCommand)
const flags = originalFlags as OutputFlags<typeof DbMigrationCommand.flags>
const parsedFlags = originalFlags as OutputFlags<typeof DbMigrationCommand.flags>

if (flags.db) {
config.util.extendDeep(config, { db: `sqlite://${this.resolveDbPath(flags.db)}` })
if (parsedFlags.db) {
config.util.extendDeep(config, { db: `sqlite://${this.resolveDbPath(parsedFlags.db)}` })
}

if (flags.generate) this.generateMigration(flags.generate)
if (parsedFlags.generate) {
this.generateMigration(parsedFlags.generate)
}

// Init database connection
const sequelize = sequelizeFactory()
DbMigration.getInstance(sequelize)

if (!flags.up && !flags.down && !flags.generate) throw new Error('One of \'--generate, --up, --down\' required')
if (!parsedFlags.up && !parsedFlags.down && !parsedFlags.generate) {
throw new Error('One of \'--generate, --up, --down\' required')
}

if (flags.up) await this.migrate(flags.migration, flags)
if (parsedFlags.up) {
await this.migrate(parsedFlags.migration, parsedFlags)
}

if (flags.down) await this.undo(flags.migration, flags)
if (parsedFlags.down) {
await this.undo(parsedFlags.migration, parsedFlags)
}

this.exit()
}
Expand Down
2 changes: 1 addition & 1 deletion src/sequelize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export default async function (app: Application): Promise<void> {

// Run migration
logger.info('Run DB Migrations')
await DbMigration.getInstance(app.get('sequelizeSync')).up()
await DbMigration.getInstance(sequelize).up()

app.setup = function (...args): ReturnType<Application['setup']> {
const result = oldSetup.apply(this, args)
Expand Down

0 comments on commit fd1f396

Please sign in to comment.