diff --git a/README.md b/README.md index a8583e1..1c605e7 100644 --- a/README.md +++ b/README.md @@ -111,8 +111,6 @@ EXAMPLES $ pg-god db-create --databaseName=bank-db --password=123 --port=5433 --host=a.example.com --userName=beer ``` -_See code: [src/commands/db-create.ts](https://github.com/ivawzh/pg-god/blob/v1.0.2/src/commands/db-create.ts)_ - ## `pg-god db-drop` drop a database @@ -122,7 +120,7 @@ USAGE $ pg-god db-drop OPTIONS - -e, --errorIfNonExist [default: false] whether throw error if DB doesn't exists + -e, --errorIfNonExist [default: false] whether throw error if DB doesn't exist -h, --help show CLI help -h, --host=host [default: localhost] DB host -i, --initialDb=initialDb [default: postgres] Initial DB name @@ -140,8 +138,6 @@ EXAMPLES $ pg-god db-drop --databaseName=bank-db --password=123 --port=5433 --host=a.example.com --userName=beer ``` -_See code: [src/commands/db-drop.ts](https://github.com/ivawzh/pg-god/blob/v1.0.2/src/commands/db-drop.ts)_ - ## `pg-god help [COMMAND]` display help for pg-god diff --git a/package.json b/package.json index 1d1cb6f..b8add21 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "pg-god", "description": "Tiny library that helps create and kill PostgreSQL database.", - "version": "1.0.3", + "version": "1.0.5", "author": "ivan.wang @ivawzh", "bin": { "pg-god": "./bin/run" diff --git a/src/commands/db-create.ts b/src/commands/db-create.ts index 1dd3879..b0f63dd 100644 --- a/src/commands/db-create.ts +++ b/src/commands/db-create.ts @@ -37,7 +37,7 @@ export default class DbCreate extends Command { } = this.parse(DbCreate) try { - cli.action.start(`🦾 Start to create database '${databaseName}'`) + cli.action.start(`😇 Start to create database '${databaseName}'`) createDatabase( { databaseName, errorIfExist }, diff --git a/src/commands/db-drop.ts b/src/commands/db-drop.ts index cd9e3a3..6c7e0c3 100644 --- a/src/commands/db-drop.ts +++ b/src/commands/db-drop.ts @@ -15,7 +15,7 @@ export default class DbDrop extends Command { static flags = { help: flags.help({char: 'h'}), databaseName: flags.string({char: 'n', required: true, description: 'name of DB attempt to drop', env: 'DB_NAME'}), - errorIfNonExist: flags.boolean({char: 'e', default: false, description: "[default: false] whether throw error if DB doesn't exists", env: 'DB_ERROR_IF_NON_EXIST'}), + errorIfNonExist: flags.boolean({char: 'e', default: false, description: "[default: false] whether throw error if DB doesn't exist", env: 'DB_ERROR_IF_NON_EXIST'}), userName: flags.string({char: 'u', default: 'postgres', description: 'DB user name', env: 'DB_USERNAME'}), initialDb: flags.string({char: 'i', default: 'postgres', description: 'Initial DB name', env: 'DB_INITIAL'}), port: flags.integer({char: 'p', default: 5432, description: 'DB port, default `5432`', env: 'DB_PORT'}), @@ -37,7 +37,7 @@ export default class DbDrop extends Command { } = this.parse(DbDrop) try { - cli.action.start(`🦾 Start to drop database '${databaseName}'`) + cli.action.start(`😇 Start to drop database '${databaseName}'`) dropDatabase( { databaseName, errorIfNonExist }, diff --git a/src/error.ts b/src/error.ts index 8ae2922..ac163bf 100644 --- a/src/error.ts +++ b/src/error.ts @@ -23,7 +23,7 @@ export const errorProtocol = { }, } -export class PgDbGodError implements Error { +export class PgGodError implements Error { constructor( readonly name: string, readonly message: string, @@ -31,8 +31,8 @@ export class PgDbGodError implements Error { readonly stack?: string ) {} - public static fromPgError(pgError: Error & { code?: string }): PgDbGodError { - return new PgDbGodError( + public static fromPgError(pgError: Error & { code?: string }): PgGodError { + return new PgGodError( // Until resolution of index issue: https://github.com/Microsoft/TypeScript/issues/14951#issuecomment-291617624 // @ts-ignore errorProtocol[pgError.code]?.name || 'PDG_ERR::UnexpectedError', @@ -43,9 +43,9 @@ export class PgDbGodError implements Error { ) } - public static dbAlreadyExist(): PgDbGodError { + public static dbAlreadyExist(): PgGodError { const code = '42P04' - return new PgDbGodError( + return new PgGodError( errorProtocol[code].name, errorProtocol[code].message, code, @@ -53,9 +53,9 @@ export class PgDbGodError implements Error { ) } - public static dbDoesNotExist(): PgDbGodError { + public static dbDoesNotExist(): PgGodError { const code = '3D000' - return new PgDbGodError( + return new PgGodError( errorProtocol[code].name, errorProtocol[code].message, code, diff --git a/src/god-stuff.ts b/src/god-stuff.ts index 975c398..a7a33ab 100644 --- a/src/god-stuff.ts +++ b/src/god-stuff.ts @@ -1,5 +1,5 @@ import { Client } from 'pg' -import { PgDbGodError } from './error' +import { PgGodError } from './error' export type DbCredential = { user: string @@ -43,12 +43,12 @@ export async function createDatabase(newDbConfig: NewDbConfig, dbCredential: Par WHERE lower(datname) = lower('${newDbConfig.databaseName}'); `) - if (existingDb.rowCount > 0 && newDbConfig.errorIfExist) throw PgDbGodError.dbAlreadyExist() + if (existingDb.rowCount > 0 && newDbConfig.errorIfExist) throw PgGodError.dbAlreadyExist() if (existingDb.rowCount > 0 && !newDbConfig.errorIfExist) return await client.query(`CREATE DATABASE "${newDbConfig.databaseName}";`) } catch (error) { - throw PgDbGodError.fromPgError(error) + throw PgGodError.fromPgError(error) } finally { await client.end() } @@ -78,12 +78,12 @@ export async function dropDatabase(dropDbConfig: DropDbConfig, dbCredential: DbC WHERE lower(datname) = lower('${dropDbConfig.databaseName}'); `) - if (existingDb.rowCount === 0 && dropDbConfig.errorIfNonExist) throw PgDbGodError.dbDoesNotExist() + if (existingDb.rowCount === 0 && dropDbConfig.errorIfNonExist) throw PgGodError.dbDoesNotExist() if (existingDb.rowCount === 0 && !dropDbConfig.errorIfNonExist) return await client.query(`DROP DATABASE "${dropDbConfig.databaseName}";`) } catch (error) { - throw PgDbGodError.fromPgError(error) + throw PgGodError.fromPgError(error) } finally { await client.end() }