Skip to content

Commit

Permalink
feat(datasource): pass datasource to prisma init
Browse files Browse the repository at this point in the history
  • Loading branch information
marcjulian committed Jul 1, 2021
1 parent a92c7b2 commit 9cdd23e
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 17 deletions.
35 changes: 21 additions & 14 deletions schematics/nestjs-prisma/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { npmScripts } from './npm-scripts';
import { Schema } from './schema';
import {
Rule,
Expand Down Expand Up @@ -72,23 +73,22 @@ function addNpmScripts(_options: Schema): Rule {

const pkg = JSON.parse(buffer.toString());

pkg.scripts['migrate:dev'] = 'prisma migrate dev';
pkg.scripts['migrate:dev:create'] = 'prisma migrate dev --create-only';
pkg.scripts['migrate:deploy'] = 'npx prisma migrate deploy';
pkg.scripts['migrate:reset'] = 'npx prisma migrate reset';
pkg.scripts['migrate:resolve'] = 'npx prisma migrate resolve';
pkg.scripts['prisma:generate'] = 'npx prisma generate';
pkg.scripts['prisma:generate:watch'] = 'npx prisma generate --watch';
pkg.scripts['prisma:studio'] = 'npx prisma studio';
context.logger.info(`✅️ Added Prisma scripts [${npmScripts.length}]`);

npmScripts.map(
(npmScript) => (pkg.scripts[npmScript.name] = npmScript.command),
);

tree.overwrite(pkgPath, JSON.stringify(pkg, null, 2));
return tree;
};
}

function addPrismaService(_options: Schema): Rule {
return (_tree: Tree) => {
return (_tree: Tree, context) => {
if (_options.addPrismaService) {
context.logger.info(`✅️ Added custom PrismaModule and PrismaService`);

const sourceTemplates = url('./templates/services');

const sourceParametrizedTemplates = apply(sourceTemplates, [
Expand All @@ -101,8 +101,10 @@ function addPrismaService(_options: Schema): Rule {
}

function addDocker(_options: Schema): Rule {
return (_tree: Tree) => {
return (_tree: Tree, context) => {
if (_options.addDocker) {
context.logger.info(`✅️ Added Docker files`);

const sourceTemplates = url('./templates/docker');

const sourceParametrizedTemplates = apply(sourceTemplates, [
Expand All @@ -116,7 +118,7 @@ function addDocker(_options: Schema): Rule {
}

function excludePrismaFromBuild(): Rule {
return (tree: Tree) => {
return (tree: Tree, context) => {
const tsconfigBuildPath = 'tsconfig.build.json';

const buffer = tree.read(tsconfigBuildPath);
Expand All @@ -125,6 +127,8 @@ function excludePrismaFromBuild(): Rule {
throw new SchematicsException(`Could not find ${tsconfigBuildPath}.`);
}

context.logger.info(`✅️ Add "prisma" directory to "excludes" in ${tsconfigBuildPath}`);

const tsconfig = JSON.parse(buffer.toString());

tsconfig.exclude = [...tsconfig.exclude, 'prisma'];
Expand All @@ -137,9 +141,12 @@ function prismaInit(_options: Schema): Rule {
return (_tree: Tree, context: SchematicContext) => {
if (!_options.skipPrismaInit) {
const packageInstall = context.addTask(new NodePackageInstallTask());
context.addTask(new RunSchematicTask('prisma-init', {}), [
packageInstall,
]);
context.addTask(
new RunSchematicTask('prisma-init', {
datasource: _options.datasourceProvider,
}),
[packageInstall],
);
}
return _tree;
};
Expand Down
31 changes: 31 additions & 0 deletions schematics/nestjs-prisma/npm-scripts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
export interface NpmScript {
name: string;
command: string;
}

export const npmScripts: NpmScript[] = [
{
name: 'migrate:dev',
command: 'npx prisma migrate dev',
},
{
name: 'migrate:dev:create',
command: 'npx prisma migrate dev --create-only',
},
{
name: 'migrate:deploy',
command: 'npx prisma migrate deploy',
},
{
name: 'prisma:generate',
command: 'npx prisma generate',
},
{
name: 'prisma:studio',
command: 'npx prisma studio',
},
{
name: 'prisma:seed',
command: 'npx prisma db seed --preview-feature',
},
];
6 changes: 6 additions & 0 deletions schematics/nestjs-prisma/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@
"description": "Create a Dockerfile and docker-compose.yaml.",
"x-prompt": "Do you like to Dockerize your application?"
},
"datasourceProvider": {
"type": "string",
"default": "postgresql",
"enum": ["postgresql", "mysql", "sqlite", "sqlserver"],
"x-prompt": "Which datasource provider do you want to use for `prisma init`?"
},
"dockerNodeImageVersion": {
"type": "string",
"description": "Node version for the builder and runner image.",
Expand Down
11 changes: 11 additions & 0 deletions schematics/nestjs-prisma/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ export interface Schema {
*/
addDocker?: boolean;

/**
* Select a datasource provider to pass to `prisma init`.
*/
datasourceProvider: DatasourceProvider;

/**
* Node version for the builder and runner image.
*/
Expand All @@ -34,3 +39,9 @@ export interface Schema {
*/
skipPrismaInit?: boolean;
}

export type DatasourceProvider =
| 'postgresql'
| 'mysql'
| 'sqlite'
| ' sqlserver';
15 changes: 12 additions & 3 deletions schematics/prisma-init/index.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,23 @@
import { DatasourceProvider } from './../nestjs-prisma/schema';
import { Rule, SchematicContext, Tree } from '@angular-devkit/schematics';
import { exec } from 'child_process';
import { Observable } from 'rxjs';

interface InitOptions {
datasource: DatasourceProvider;
}

// You don't have to export the function as default. You can also have more than one rule factory
// per file.
export function prismaInit(_options: any): Rule {
export function prismaInit(_options: InitOptions): Rule {
return (host: Tree, _context: SchematicContext) => {
_context.logger.info('Initialized Prisma');
_context.logger.info(
`✅️ Initialized Prisma - Datasource ${_options.datasource}`,
);
return new Observable<Tree>((subscriber) => {
const child = exec('npx prisma init');
const child = exec(
`npx prisma init --datasource-provider ${_options.datasource}`,
);
child.on('error', (error) => {
subscriber.error(error);
});
Expand Down

0 comments on commit 9cdd23e

Please sign in to comment.