-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
23 changed files
with
1,292 additions
and
669 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"extends": "@istanbuljs/nyc-config-typescript", | ||
"all": true, | ||
"check-coverage": true, | ||
"include": [ | ||
"src/**/*.ts", | ||
"providers/**/*.ts" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import { Table } from '@ioc:Adonis/Addons/DynamoDB' | ||
|
||
import { join, extname } from 'path' | ||
import { slash } from '@poppinss/utils' | ||
import { resolveDir, fsReadAll, string } from '@poppinss/utils/build/helpers' | ||
import { BaseCommand } from '@adonisjs/core/build/standalone' | ||
|
||
export default abstract class Base extends BaseCommand { | ||
protected async getFile (filePath: string): Promise<Promise<typeof Table>> { | ||
return new Promise(async (resolve, reject) => { | ||
const path = this.absoluteTablesDirectoryPath() | ||
|
||
try { | ||
resolve( | ||
(await import(join(path, filePath + extname(filePath)))).default | ||
) | ||
} catch (error) { | ||
reject(error) | ||
} | ||
}) | ||
} | ||
|
||
protected getFiles (): Promise<Array<Promise<typeof Table>>> { | ||
return new Promise((resolve, reject) => { | ||
const path = this.absoluteTablesDirectoryPath() | ||
const files = fsReadAll(path) | ||
|
||
try { | ||
resolve( | ||
files.map( | ||
async (file: string) => (await import(join(path, file))).default | ||
) | ||
) | ||
} catch (error) { | ||
reject(error) | ||
} | ||
}) | ||
} | ||
|
||
protected async getTable (filePath: string): Promise<typeof Table> { | ||
return await this.getFile(filePath) | ||
} | ||
|
||
protected async getTables (): Promise<Array<typeof Table>> { | ||
return await Promise.all(await this.getFiles()) | ||
} | ||
|
||
protected getTablesDirectory (): string { | ||
return this.application.namespacesMap.get('dynamodbTables') || 'App/Tables' | ||
} | ||
|
||
protected absoluteTablesDirectoryPath (): string { | ||
const path = this.resolvedNamespace() | ||
|
||
return resolveDir( | ||
this.application.appRoot, | ||
`./${path}` | ||
) | ||
} | ||
|
||
protected resolvedNamespace (): string { | ||
return this.application.resolveNamespaceDirectory('dynamodbTables') || 'app/Tables' | ||
} | ||
|
||
protected normalizeName (name: string): string { | ||
const path = slash(name).split('/') | ||
const transformedName = string.pascalCase(path.pop()!) | ||
|
||
return join(...[...path, transformedName]) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import { Table } from '@ioc:Adonis/Addons/DynamoDB' | ||
|
||
import { args } from '@adonisjs/core/build/standalone' | ||
|
||
import Base from './Base' | ||
|
||
export default class Create extends Base { | ||
public static commandName = 'dynamo:create' | ||
|
||
public static description = 'Create DynamoDB tables on AWS from models' | ||
|
||
public static settings = { | ||
loadApp: true, | ||
stayAlive: true | ||
} | ||
|
||
@args.string({ | ||
description: 'Name/path of the model class', | ||
required: false | ||
}) | ||
public name?: string | ||
|
||
public async run () { | ||
let tables: Array<typeof Table> = [] | ||
|
||
if (this.name) { | ||
tables = [await this.getTable(this.normalizeName(this.name))] | ||
} else { | ||
tables = await this.getTables() | ||
} | ||
|
||
for (const createTable of tables.map(_table => () => this.createTable(_table))) { | ||
await createTable() | ||
} | ||
|
||
await this.exit() | ||
} | ||
|
||
private async createTable (table: typeof Table): Promise<void> { | ||
const spinner = this.logger.await(`Creating table ${table.name}`, undefined, undefined) | ||
|
||
try { | ||
await table.createTable() | ||
|
||
this.logger.logUpdate(this.logger.colors.green(`Table ${table.name} successfully created`)) | ||
} catch (e) { | ||
this.logger.logUpdate(this.logger.colors.red(`Cannot create table ${table.name}.\n${e}`)) | ||
} | ||
|
||
spinner.stop() | ||
} | ||
} |
Oops, something went wrong.