Created
July 28, 2022 07:28
-
-
Save MarZab/3cb9eea8481fc84bbfc690c4953a21a0 to your computer and use it in GitHub Desktop.
TypeORM 0.3 Wrapper
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
#!/usr/bin/env node | |
import { spawn } from 'child_process'; | |
import { join } from 'path'; | |
import yargs from 'yargs'; | |
import { hideBin } from 'yargs/helpers'; | |
async function runCommand(data: { stage: string }, ...args) { | |
// eslint-disable-next-line no-console | |
console.log( | |
`STAGE=${data.stage} yarn node -r tsconfig-paths/register $(yarn bin typeorm-ts-node-commonjs) ${args.join(' ')} `, | |
); | |
process.env.STAGE = data.stage; | |
return new Promise<void>((resolve) => { | |
const cmd = spawn('yarn', ['node', '-r tsconfig-paths/register', '$(yarn bin typeorm-ts-node-commonjs)', ...args], { | |
shell: true, | |
stdio: 'inherit', | |
}); | |
cmd.on('exit', function () { | |
resolve(); | |
}); | |
}); | |
} | |
yargs(hideBin(process.argv)) | |
.scriptName('yarn typeorm:cli') | |
.command('schema:sync', ' Synchronizes your entities with database schema.', {}, async (argv) => { | |
const stage = argv.stage as string; | |
const dataSource: string = (argv.dataSource || argv.d) as string; | |
await runCommand({ stage }, 'schema:sync', `-d ${dataSource}`); | |
}) | |
.command('schema:log', 'Shows sql to be executed by schema:sync command.', {}, async (argv) => { | |
const stage = argv.stage as string; | |
const dataSource: string = (argv.dataSource || argv.d) as string; | |
await runCommand({ stage }, 'schema:log', `-d ${dataSource}`); | |
}) | |
.command('schema:drop', 'Drops all tables in the database on your default dataSource.', {}, async (argv) => { | |
const stage = argv.stage as string; | |
const dataSource: string = (argv.dataSource || argv.d) as string; | |
await runCommand({ stage }, 'schema:drop', `-d ${dataSource}`); | |
}) | |
.command('query <query>', ' Executes given SQL query on a default dataSource.', {}, async (argv) => { | |
const stage = argv.stage as string; | |
const dataSource: string = (argv.dataSource || argv.d) as string; | |
await runCommand({ stage }, 'query', `-d ${dataSource}`, `"${argv.query as string}"`); | |
}) | |
.command('migration:create [name]', 'Creates a new migration file.', {}, async (argv) => { | |
const module: string = (argv.m || argv.module) as string; | |
const name = (argv.name as string) || 'unnamed'; | |
const stage = argv.stage as string; | |
const filePath = module | |
? join('src', 'modules', module, 'database', 'migrations', name) | |
: join('src', 'migrations', name); | |
await runCommand({ stage }, 'migration:create', filePath); | |
}) | |
.command( | |
'migration:generate [name]', | |
'Generates a new migration file with sql. Needs to be executed to update schema.', | |
{}, | |
async (argv) => { | |
const module: string = (argv.m || argv.module) as string; | |
const name = (argv.name as string) || 'unnamed'; | |
const stage = argv.stage as string; | |
const dataSource: string = (argv.dataSource || argv.d) as string; | |
const filePath = module | |
? join('src', 'modules', module, 'database', 'migrations', name) | |
: join('src', 'migrations', name); | |
await runCommand({ stage }, 'migration:generate', filePath, `-d ${dataSource}`); | |
}, | |
) | |
.command('migration:run', 'Runs all pending migrations.', {}, async (argv) => { | |
const stage = argv.stage as string; | |
const dataSource: string = (argv.dataSource || argv.d) as string; | |
await runCommand({ stage }, 'migration:run', `-d ${dataSource}`); | |
}) | |
.command('migration:show', 'Show all migrations and whether they have been run or not', {}, async (argv) => { | |
const stage = argv.stage as string; | |
const dataSource: string = (argv.dataSource || argv.d) as string; | |
await runCommand({ stage }, 'migration:run', `-d ${dataSource}`); | |
}) | |
.command('migration:revert', 'Reverts last executed migration.', {}, async (argv) => { | |
const stage = argv.stage as string; | |
const dataSource: string = (argv.dataSource || argv.d) as string; | |
await runCommand({ stage }, 'migration:run', `-d ${dataSource}`); | |
}) | |
.command('cache:clear', 'Clears all data stored in query runner cache.', {}, async (argv) => { | |
const stage = argv.stage as string; | |
const dataSource: string = (argv.dataSource || argv.d) as string; | |
await runCommand({ stage }, 'cache:clear', `-d ${dataSource}`); | |
}) | |
.command('version', 'Prints TypeORM version this project uses.', {}, async (argv) => { | |
const stage = argv.stage as string; | |
await runCommand({ stage }, 'version'); | |
}) | |
.options({ | |
d: { | |
default: './src/modules/app/app.data-source.ts', | |
type: 'string', | |
global: true, | |
alias: 'dataSource', | |
}, | |
m: { | |
describe: 'module name inside src/modules/', | |
alias: 'module', | |
global: true, | |
type: 'string', | |
}, | |
stage: { | |
global: true, | |
type: 'string', | |
default: process.env[`${process.env.CONFIG_PREFIX || 'app'}__stage`] || process.env.STAGE || 'local', | |
}, | |
}) | |
.strict(true) | |
.parse(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment