Skip to content

Commit 132bec1

Browse files
committed
feat(yargs): add table-name and attribute-name cli options
The options will overwrite the default DynamoDB tableName and primaryKey attribute options
1 parent 75199cd commit 132bec1

File tree

6 files changed

+58
-35
lines changed

6 files changed

+58
-35
lines changed

src/commands/migrate-undo-all.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { CommandModule } from 'yargs';
22

3-
import { defaultMigrator } from '../core/migrator';
4-
import { BaseCliOptions, baseOptions } from '../core/yargs';
3+
import { BaseCliOptions, baseHandler, baseOptions } from '../core/yargs';
54
import logger from '../helpers/logger';
65

76
interface CliOptions extends BaseCliOptions {
@@ -16,21 +15,21 @@ export default {
1615
type: 'string'
1716
}),
1817

19-
handler: async (args) => {
18+
handler: baseHandler(async (args, migrator) => {
2019
try {
21-
const migrations = await defaultMigrator.executed();
20+
const migrations = await migrator.executed();
2221

2322
if (migrations.length === 0) {
2423
logger.log('No executed migrations found.');
2524

2625
process.exit(0);
2726
}
2827

29-
await defaultMigrator.down({ to: args.to || 0 });
28+
await migrator.down({ to: args.to || 0 });
3029
} catch (e) {
3130
logger.error(e);
3231
}
3332

3433
process.exit(0);
35-
}
34+
})
3635
} as CommandModule<CliOptions, CliOptions>;

src/commands/migrate-undo.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { CommandModule } from 'yargs';
22

3-
import { defaultMigrator } from '../core/migrator';
4-
import { BaseCliOptions, baseOptions } from '../core/yargs';
3+
import { BaseCliOptions, baseHandler, baseOptions } from '../core/yargs';
54
import logger from '../helpers/logger';
65

76
interface CliOptions extends BaseCliOptions {
@@ -15,9 +14,9 @@ export default {
1514
type: 'string'
1615
}),
1716

18-
handler: async (args) => {
17+
handler: baseHandler(async (args, migrator) => {
1918
try {
20-
const migrations = await defaultMigrator.executed();
19+
const migrations = await migrator.executed();
2120

2221
if (migrations.length === 0) {
2322
logger.log('No executed migrations found.');
@@ -26,14 +25,14 @@ export default {
2625
}
2726

2827
if (args.name) {
29-
await defaultMigrator.down(args.name);
28+
await migrator.down(args.name);
3029
} else {
31-
await defaultMigrator.down();
30+
await migrator.down();
3231
}
3332
} catch (e) {
3433
logger.error(e);
3534
}
3635

3736
process.exit(0);
38-
}
37+
})
3938
} as CommandModule<CliOptions, CliOptions>;

src/commands/migrate.ts

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { CommandModule } from 'yargs';
22

3-
import { defaultMigrator } from '../core/migrator';
4-
import { BaseCliOptions, baseOptions } from '../core/yargs';
3+
import { Migrator } from '../core/migrator';
4+
import { BaseCliOptions, baseHandler, baseOptions } from '../core/yargs';
55
import logger from '../helpers/logger';
66

77
interface CliOptions extends BaseCliOptions {
@@ -20,26 +20,26 @@ export default {
2020
type: 'string'
2121
}),
2222

23-
handler: async function (args) {
23+
handler: baseHandler(async (args, migrator) => {
2424
const command = args._[0];
2525

2626
switch (command) {
2727
case 'migrate':
28-
await migrate(args);
28+
await migrate(args, migrator);
2929
break;
3030
case 'migrate:status':
31-
await migrationStatus();
31+
await migrationStatus(migrator);
3232
break;
3333
}
3434

3535
process.exit(0);
36-
}
36+
})
3737
} as CommandModule<CliOptions, CliOptions>;
3838

39-
async function migrate(args: CliOptions) {
39+
async function migrate(args: CliOptions, migrator: Migrator) {
4040
try {
41-
const migrations = await defaultMigrator.pending();
42-
const options: Partial<CliOptions> = {};
41+
const migrations = await migrator.pending();
42+
const options = {} as CliOptions;
4343

4444
if (migrations.length === 0) {
4545
logger.log('No migrations were executed, database schema was already up to date.');
@@ -60,21 +60,21 @@ async function migrate(args: CliOptions) {
6060
options.from = args.from;
6161
}
6262

63-
await defaultMigrator.up(options);
63+
await migrator.up(options);
6464
} catch (e) {
6565
logger.error(e);
6666
}
6767
}
6868

69-
async function migrationStatus() {
69+
async function migrationStatus(migrator: Migrator) {
7070
try {
71-
const executedMigrations = await defaultMigrator.executed();
71+
const executedMigrations = await migrator.executed();
7272

7373
executedMigrations.forEach(migration => {
7474
logger.log('up', migration.file);
7575
});
7676

77-
const pendingMigrations = await defaultMigrator.pending();
77+
const pendingMigrations = await migrator.pending();
7878

7979
pendingMigrations.forEach(migration => {
8080
logger.log('down', migration.file);

src/commands/migration-generate.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@ import clc from 'cli-color';
22
import path from 'path';
33
import { CommandModule } from 'yargs';
44

5-
import { defaultMigrator } from '../core/migrator';
6-
import { BaseCliOptions, baseOptions } from '../core/yargs';
5+
import { BaseCliOptions, baseHandler, baseOptions } from '../core/yargs';
76
import logger from '../helpers/logger';
87

98
interface CliOptions extends BaseCliOptions {
@@ -18,8 +17,8 @@ export default {
1817
demandOption: true
1918
}),
2019

21-
handler: async (args) => {
22-
await defaultMigrator.generate(args.name);
20+
handler: baseHandler(async (args, migrator) => {
21+
await migrator.generate(args.name);
2322

2423
logger.log(
2524
'New migration was created at',
@@ -28,5 +27,5 @@ export default {
2827
);
2928

3029
process.exit(0);
31-
}
30+
})
3231
} as CommandModule<CliOptions, CliOptions>;

src/core/migrator.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export {DynamoDBStorage};
1111
export interface MigratorOptions {
1212
dynamodb?: DocumentClient;
1313
tableName?: string;
14+
attributeName?: string;
1415
}
1516

1617
export class Migrator {
@@ -22,13 +23,15 @@ export class Migrator {
2223
* @param options
2324
* @param options.dynamodb - a DynamoDB document client instance
2425
* @param options.tableName - a name of migration table in DynamoDB
26+
* @param options.attributeName - name of the table primaryKey attribute in DynamoDB
2527
*/
26-
constructor({ dynamodb, tableName }: MigratorOptions = {}) {
28+
constructor({ dynamodb, tableName, attributeName }: MigratorOptions = {}) {
2729
dynamodb = dynamodb || new DocumentClient();
2830
tableName = tableName || 'migrations';
31+
attributeName = attributeName || 'name';
2932

3033
this.umzug = new Umzug({
31-
storage: new DynamoDBStorage({ dynamodb, tableName }),
34+
storage: new DynamoDBStorage({ dynamodb, tableName, attributeName }),
3235
migrations: {
3336
params: [dynamodb],
3437
},

src/core/yargs.ts

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
import { Argv } from 'yargs';
1+
import { Arguments, Argv } from 'yargs';
2+
3+
import { Migrator } from './migrator';
24

35
export interface BaseCliOptions {
46
'options-path': string;
@@ -7,6 +9,8 @@ export interface BaseCliOptions {
79
'secret-access-key': string;
810
'region': string;
911
'endpoint-url': string;
12+
'table-name': string;
13+
'attribute-name': string;
1014
}
1115

1216
export function baseOptions(yargs: Argv<BaseCliOptions>) {
@@ -33,7 +37,26 @@ export function baseOptions(yargs: Argv<BaseCliOptions>) {
3337
type: 'string'
3438
})
3539
.option('endpoint-url', {
36-
describe: 'The DynamoDB endpoint url to use',
40+
describe: 'The DynamoDB endpoint url to use. The DynamoDB local instance url could be specified here.',
41+
type: 'string'
42+
})
43+
.option('table-name', {
44+
describe: 'The DynamoDB table name. `migrations` is default.',
45+
type: 'string'
46+
})
47+
.option('attribute-name', {
48+
describe: 'The DynamoDB primaryKey attribute name. `name` is default.',
3749
type: 'string'
3850
});
3951
}
52+
53+
export function baseHandler<T extends BaseCliOptions>(callback: (args: Arguments<T>, migrator: Migrator) => void) {
54+
return (args: Arguments<T>): void => {
55+
const migrator = new Migrator({
56+
tableName: args['table-name'],
57+
attributeName: args['attribute-name'],
58+
});
59+
60+
callback(args, migrator);
61+
};
62+
}

0 commit comments

Comments
 (0)