Created
February 12, 2018 14:41
-
-
Save sigmasoldi3r/c2e533bffc1603abe0b0917448eea466 to your computer and use it in GitHub Desktop.
Regenerates services for accessing the repositories when using TypeORM
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
#!/home/lenovo/.node/bin/node | |
/** | |
* Regenrates the default repositories given a model. | |
*/ | |
const fs = require('fs'); | |
const program = require('commander'); | |
const path = require('path'); | |
const author = 'Pablo Blanco Celdran'; | |
program | |
.version('0.0.1') | |
.arguments('<model>') | |
.option('-f, --force', 'Overwrites repository files even if they exist.') | |
.option('-o, --out [output dir]', 'Specifies the output directory, defaults to cwd.') | |
.option('-r, --repo <repositories>', 'Specifies the repositories location where entities are linked.') | |
.parse(process.argv); | |
if (!program.args.length) { | |
console.error('Missing model argument: Specify the input folder!'); | |
program.help(); | |
process.exit(-1); | |
} | |
if (!program.repo) { | |
console.error('Missing repositories folder!'); | |
program.help(); | |
process.exit(-1); | |
} | |
const output = program.out || './'; | |
const input = program.args[0]; | |
const repo = program.repo; | |
const list = fs.readdirSync(path.normalize(input)); | |
function writef(entity, service, repoc, input, output) { | |
fs.writeFileSync(path.join(output, service) + '.ts', `import { ${entity} } from '${path.join(input, entity)}'; | |
import { OrmRepository } from 'typeorm-typedi-extensions'; | |
import { Service } from 'typedi'; | |
import { ${repoc} } from '${path.join(repo, repoc)}'; | |
/** | |
* Default service for the ${entity} entity, using ${repo}. | |
* Use this service to access the repositories. | |
* @created ${new Date().toDateString()} | |
* @author ${author} | |
*/ | |
@Service() | |
export class ${service} { | |
@OrmRepository(${entity}) | |
private repo: ${repoc}; | |
/** | |
* Gets all without filtering. | |
* @returns {Promise<${entity}[]>} | |
*/ | |
public async getAll(): Promise<${entity}[]> { | |
return this.repo.find(); | |
} | |
/** | |
* Gets one by id. | |
* @param {number} id | |
* @returns {Promise<${entity}>} | |
*/ | |
public async getOneById(id: number): Promise<${entity}> { | |
return this.repo.findOneById(id); | |
} | |
/** | |
* Persists the passed entity: Updating if exist or creating if not. | |
* @param {${entity}} ${entity.toLowerCase()} | |
* @returns {Promise<${entity}>} | |
*/ | |
public async persist(${entity.toLowerCase()}: ${entity}): Promise<${entity}> { | |
return this.repo.save(${entity.toLowerCase()}); | |
} | |
/** | |
* Removes the entity. | |
* @param {${entity}} ${entity.toLowerCase()} | |
* @returns {Promise<${entity}>} | |
*/ | |
public async remove(${entity.toLowerCase()}: ${entity}): Promise<${entity}> { | |
return this.repo.remove(${entity.toLowerCase()}); | |
} | |
}`); | |
} | |
function formatRepoName(repoName, root) { | |
return ` - File ${path.join(root, repoName)}.ts`; | |
} | |
function report(repoName, output, err) { | |
console.log(`${formatRepoName(repoName, output)} raised an error, skipping... (${err.message})`); | |
} | |
let written = 0, skipped = 0; | |
for (const entity of list) { | |
const entityName = entity.replace('.ts', '').trim(); | |
const repoName = `${entityName}Repository`; | |
const serviceName = `${entityName}Service`; | |
try { | |
const stat = fs.statSync(path.join(output, repoName) + '.ts'); | |
if (program.force) { | |
try { | |
writef(entityName, serviceName, repoName, input, output); | |
written++; | |
console.log(`${formatRepoName(serviceName, output)} already exists, overwriting...`); | |
} catch (err) { | |
skipped++; | |
report(serviceName, output, err); | |
} | |
} else { | |
skipped++; | |
console.log(`${formatRepoName(serviceName, output)} already exists, skipping...`); | |
} | |
} catch (err) { | |
try { | |
writef(entityName, serviceName, repoName, input, output); | |
written++; | |
console.log(`${formatRepoName(serviceName, output)} created.`); | |
} catch (err) { | |
skipped++; | |
report(serviceName, output, err); | |
} | |
} | |
} | |
console.log(`Done! Total: ${list.length}, skipped ${skipped} and written ${written} files.`); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment