Skip to content

Commit

Permalink
feat: refactored cli options + enhanced commands
Browse files Browse the repository at this point in the history
  • Loading branch information
tada5hi committed Jul 20, 2023
1 parent 7cc56bc commit 637250b
Show file tree
Hide file tree
Showing 14 changed files with 222 additions and 110 deletions.
20 changes: 11 additions & 9 deletions README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,17 @@ If you use esm, the executable must be changed from `typeorm-extension` to `type
The following commands are available in the terminal:
- `typeorm-extension db:create` to create the database
- `typeorm-extension db:drop` to drop the database
- `typeorm-extension seed` seed the database
- `typeorm-extension seed:run` seed the database
- `typeorm-extension seed:create` to create a new seeder

If the application has not yet been built or is to be tested with ts-node, the commands can be adapted as follows:

```
"scripts": {
"db:create": "ts-node ./node_modules/typeorm-extension/bin/cli.cjs db:create",
"db:drop": "ts-node ./node_modules/typeorm-extension/bin/cli.cjs db:drop",
"seed": "ts-node ./node_modules/typeorm-extension/bin/cli.cjs seed"
"seed:run": "ts-node ./node_modules/typeorm-extension/bin/cli.cjs seed:run",
"seed:create": "ts-node ./node_modules/typeorm-extension/bin/cli.cjs seed:create"
}
```
To test the application in the context of an esm project, the following adjustments must be made:
Expand All @@ -80,13 +82,13 @@ for the seeder- & factory-location.

#### Options

| Option | Commands | Default | Deprecated | Description |
|-------------------------|---------------------------------|-----------------|------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `--root` or `-r` | `db:create`, `db:drop` & `seed` | `process.cwd()` | `no` | Path to the data-source / config file. |
| `--dataSource` or `-d` | `db:create`, `db:drop` & `seed` | `data-source` | `no` | Name of the data-source file. |
| `--synchronize` or `-s` | `db:create` & `db:drop` | `yes` | `no` | Synchronize the database schema after database creation. Options: `yes` or `no`. |
| `--initialDatabase` | `db:create` | `undefined` | `no` | Specify the initial database to connect to. This option is only relevant for the `postgres` driver, which must always to connect to a database. If no database is provided, the database name will be equal to the connection user name. |
| `--seed` | `seed` | `undefined` | `no` | Specify a specific seed class to run. |
| Option | Commands | Default | Description |
|-------------------------|----------------------------------------------------|-----------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `--root` or `-r` | `db:create`, `db:drop`, `seed:create` & `seed:run` | `process.cwd()` | Root directory of the project. |
| `--dataSource` or `-d` | `db:create`, `db:drop` & `seed:run` | `data-source` | Name (incl. relative path) of the data-source file. |
| `--synchronize` or `-s` | `db:create` & `db:drop` | `yes` | Synchronize the database schema after database creation. Options: `yes` or `no`. |
| `--initialDatabase` | `db:create` | `undefined` | Specify the initial database to connect to. This option is only relevant for the `postgres` driver, which must always to connect to a database. If no database is provided, the database name will be equal to the connection user name. |
| `--name` | `seed:create` & `seed:run` | `undefined` | Specify a seeder name to create or run. |
### Database
An alternative to the CLI variant, is to `create` the database in the code base during the runtime of the application.
Therefore, provide the `DataSourceOptions` for the DataSource manually, or let it be created automatically:
Expand Down
50 changes: 25 additions & 25 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
"dependencies": {
"@faker-js/faker": "^8.0.2",
"consola": "^3.2.3",
"locter": "^1.1.2",
"locter": "^1.2.0",
"pascal-case": "^3.1.2",
"rapiq": "^0.9.0",
"reflect-metadata": "^0.1.13",
Expand Down
42 changes: 32 additions & 10 deletions src/cli/commands/database/create.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
import { consola } from 'consola';
import type { Arguments, Argv, CommandModule } from 'yargs';
import { buildDataSourceOptions } from '../../../data-source';
import type { DatabaseCreateContext } from '../../../database';
import { createDatabase } from '../../../database';
import { CodeTransformation, setCodeTransformation } from '../../../utils';
import {
CodeTransformation,
parseFilePath,
resolveFilePath,
setCodeTransformation,
} from '../../../utils';

export interface DatabaseCreateArguments extends Arguments {
codeTransformation: string,
root: string;
dataSource: 'data-source' | string;
tsconfig: string,
dataSource: string;
synchronize: string;
initialDatabase?: unknown;
}
Expand All @@ -27,12 +34,17 @@ export class DatabaseCreateCommand implements CommandModule {
.option('root', {
alias: 'r',
default: process.cwd(),
describe: 'Path to the data-source / config file.',
describe: 'Root directory of the project.',
})
.option('tsconfig', {
alias: 'tc',
default: 'tsconfig.json',
describe: 'Name (incl. relative path) of the tsconfig file.',
})
.option('dataSource', {
alias: 'd',
default: 'data-source',
describe: 'Name of the file with the data-source.',
describe: 'Name (incl. relative path) of the data-source file.',
})
.option('synchronize', {
alias: 's',
Expand All @@ -45,16 +57,22 @@ export class DatabaseCreateCommand implements CommandModule {
});
}

async handler(raw: Arguments, exitProcess = true) {
async handler(raw: Arguments) {
const args : DatabaseCreateArguments = raw as DatabaseCreateArguments;

if (args.codeTransformation) {
setCodeTransformation(args.codeTransformation);
}

const source = parseFilePath(args.dataSource, args.root);
consola.info(`DataSource Directory: ${source.directory}`);
consola.info(`DataSource Name: ${source.name}`);

const tsconfig = resolveFilePath(args.root, args.tsconfig);
const dataSourceOptions = await buildDataSourceOptions({
directory: args.root,
dataSourceName: args.dataSource,
directory: source.directory,
dataSourceName: source.name,
tsconfig,
});

const context : DatabaseCreateContext = {
Expand All @@ -71,10 +89,14 @@ export class DatabaseCreateCommand implements CommandModule {

context.synchronize = args.synchronize === 'yes';

await createDatabase(context);

if (exitProcess) {
try {
await createDatabase(context);
consola.success('Created database.');
process.exit(0);
} catch (e) {
consola.warn('Failed to create database.');
consola.error(e);
process.exit(1);
}
}
}
39 changes: 29 additions & 10 deletions src/cli/commands/database/drop.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
import { consola } from 'consola';
import type { Arguments, Argv, CommandModule } from 'yargs';
import { buildDataSourceOptions } from '../../../data-source';
import type { DatabaseDropContext } from '../../../database';
import { dropDatabase } from '../../../database';
import { CodeTransformation, setCodeTransformation } from '../../../utils';
import {
CodeTransformation, parseFilePath, resolveFilePath, setCodeTransformation,
} from '../../../utils';

export interface DatabaseDropArguments extends Arguments {
codeTransformation: string,
root: string;
dataSource: 'data-source' | string;
tsconfig: string,
dataSource: string;
}

export class DatabaseDropCommand implements CommandModule {
Expand All @@ -25,28 +29,39 @@ export class DatabaseDropCommand implements CommandModule {
.option('root', {
alias: 'r',
default: process.cwd(),
describe: 'Path to the data-source / config file.',
describe: 'Root directory of the project.',
})
.option('tsconfig', {
alias: 'tc',
default: 'tsconfig.json',
describe: 'Name (incl. relative path) of the tsconfig file.',
})
.option('dataSource', {
alias: 'd',
default: 'data-source',
describe: 'Name of the file with the data-source.',
describe: 'Name (incl. relative path) of the data-source file.',
})
.option('initialDatabase', {
describe: 'Specify the initial database to connect to.',
});
}

async handler(raw: Arguments, exitProcess = true) {
async handler(raw: Arguments) {
const args : DatabaseDropArguments = raw as DatabaseDropArguments;

if (args.codeTransformation) {
setCodeTransformation(args.codeTransformation);
}

const source = parseFilePath(args.dataSource, args.root);
consola.info(`DataSource Directory: ${source.directory}`);
consola.info(`DataSource Name: ${source.name}`);

const tsconfig = resolveFilePath(args.root, args.tsconfig);
const dataSourceOptions = await buildDataSourceOptions({
directory: args.root,
dataSourceName: args.dataSource,
directory: source.directory,
dataSourceName: source.name,
tsconfig,
});

const context : DatabaseDropContext = {
Expand All @@ -61,10 +76,14 @@ export class DatabaseDropCommand implements CommandModule {
context.initialDatabase = args.initialDatabase;
}

await dropDatabase(context);

if (exitProcess) {
try {
await dropDatabase(context);
consola.success('Dropped database.');
process.exit(0);
} catch (e) {
consola.warn('Failed to drop database.');
consola.error(e);
process.exit(1);
}
}
}
Loading

0 comments on commit 637250b

Please sign in to comment.