feat: added prisma schema nx generator

feature/auth-configure
Artem-Darius Weber 1 year ago
parent 0eacf03ab7
commit 444a5b345a

@ -37,6 +37,11 @@
"jest": true
},
"rules": {}
},
{
"files": "*.json",
"parser": "jsonc-eslint-parser",
"rules": {}
}
]
}

@ -0,0 +1,3 @@
{
"eslint.validate": ["json"]
}

File diff suppressed because one or more lines are too long

@ -0,0 +1,5 @@
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
# yarn lockfile v1
yarn-path ".yarn/releases/yarn-1.22.19.cjs"

@ -0,0 +1,25 @@
{
"extends": ["../../.eslintrc.json"],
"ignorePatterns": ["!**/*"],
"overrides": [
{
"files": ["*.ts", "*.tsx", "*.js", "*.jsx"],
"rules": {}
},
{
"files": ["*.ts", "*.tsx"],
"rules": {}
},
{
"files": ["*.js", "*.jsx"],
"rules": {}
},
{
"files": ["./package.json", "./generators.json"],
"parser": "jsonc-eslint-parser",
"rules": {
"@nx/nx-plugin-checks": "error"
}
}
]
}

@ -0,0 +1,11 @@
# prisma-generator
This library was generated with [Nx](https://nx.dev).
## Building
Run `nx build prisma-generator` to build the library.
## Running unit tests
Run `nx test prisma-generator` to execute the unit tests via [Jest](https://jestjs.io).

@ -0,0 +1,9 @@
{
"generators": {
"prisma-generator": {
"factory": "./src/generators/prisma-generator/generator",
"schema": "./src/generators/prisma-generator/schema.json",
"description": "prisma-generator generator"
}
}
}

@ -0,0 +1,10 @@
/* eslint-disable */
export default {
displayName: 'prisma-generator',
preset: '../../jest.preset.js',
transform: {
'^.+\\.[tj]s$': ['ts-jest', { tsconfig: '<rootDir>/tsconfig.spec.json' }],
},
moduleFileExtensions: ['ts', 'js', 'html'],
coverageDirectory: '../../coverage/libs/prisma-generator',
};

@ -0,0 +1,6 @@
{
"name": "@platform/prisma-generator",
"version": "0.0.1",
"type": "commonjs",
"generators": "./generators.json"
}

@ -0,0 +1,66 @@
{
"name": "prisma-generator",
"$schema": "../../node_modules/nx/schemas/project-schema.json",
"sourceRoot": "libs/prisma-generator/src",
"projectType": "library",
"targets": {
"build": {
"executor": "@nx/js:tsc",
"outputs": ["{options.outputPath}"],
"options": {
"outputPath": "dist/libs/prisma-generator",
"main": "libs/prisma-generator/src/index.ts",
"tsConfig": "libs/prisma-generator/tsconfig.lib.json",
"assets": [
"libs/prisma-generator/*.md",
{
"input": "./libs/prisma-generator/src",
"glob": "**/!(*.ts)",
"output": "./src"
},
{
"input": "./libs/prisma-generator/src",
"glob": "**/*.d.ts",
"output": "./src"
},
{
"input": "./libs/prisma-generator",
"glob": "generators.json",
"output": "."
},
{
"input": "./libs/prisma-generator",
"glob": "executors.json",
"output": "."
}
]
}
},
"lint": {
"executor": "@nx/linter:eslint",
"outputs": ["{options.outputFile}"],
"options": {
"lintFilePatterns": [
"libs/prisma-generator/**/*.ts",
"libs/prisma-generator/package.json",
"libs/prisma-generator/generators.json"
]
}
},
"test": {
"executor": "@nx/jest:jest",
"outputs": ["{workspaceRoot}/coverage/{projectRoot}"],
"options": {
"jestConfig": "libs/prisma-generator/jest.config.ts",
"passWithNoTests": true
},
"configurations": {
"ci": {
"ci": true,
"codeCoverage": true
}
}
}
},
"tags": []
}

@ -0,0 +1,20 @@
import { createTreeWithEmptyWorkspace } from '@nx/devkit/testing';
import { Tree, readProjectConfiguration } from '@nx/devkit';
import { prismaGeneratorGenerator } from './generator';
import { PrismaGeneratorGeneratorSchema } from './schema';
describe('prisma-generator generator', () => {
let tree: Tree;
const options: PrismaGeneratorGeneratorSchema = { name: 'test' };
beforeEach(() => {
tree = createTreeWithEmptyWorkspace();
});
it('should run successfully', async () => {
await prismaGeneratorGenerator(tree, options);
const config = readProjectConfiguration(tree, 'test');
expect(config).toBeDefined();
});
});

@ -0,0 +1,58 @@
import {
addProjectConfiguration,
formatFiles,
generateFiles, joinPathFragments,
names,
Tree,
} from '@nx/devkit';
import * as path from 'path';
import { PrismaGeneratorGeneratorSchema } from './schema';
interface GeneratorOptions {
name: string;
provider: string;
connectionString: string;
}
export async function prismaGeneratorGenerator(
tree: Tree,
schema: GeneratorOptions
) {
const { name, className, constantName } = names(schema.name);
generateFiles(
tree,
joinPathFragments(__dirname, './template'),
'libs/prisma-clients',
{
dbType: schema.provider,
tmpl: '',
name,
className,
constantName,
outputLocation: `../../../../node_modules/.prisma/${name}-client`
}
);
if ( !tree.exists('.env') ) {
tree.write('.env', '')
}
let contents = tree.read('.env').toString();
contents += `${constantName}_SOURCE_URL=${schema.connectionString}\n`;
tree.write('.env', contents);
if ( !tree.exists('libs/prisma-clients/index.ts') ) {
tree.write('libs/prisma-clients/index.ts', '')
}
let exportsConents = tree.read('libs/prisma-clients/index.ts').toString()
exportsConents += `export { ${className}Client } from './${name}';\n`
tree.write('libs/prisma-clients/index.ts', exportsConents)
await formatFiles(tree);
}
export default prismaGeneratorGenerator;

@ -0,0 +1,28 @@
export interface PrismaGeneratorGeneratorSchema {
"name": {
"type": "string",
"description": "Prisma Project Name",
"x-prompt": "What do you want to call the project?"
},
"provider": {
"type": "string",
"description": "Database Type",
"x-prompt": {
"message": "Which type of database are you connecting to?",
"type": "list",
"items": [
{ "value": "sqlserver", "label": "MSSQL" },
{ "value": "postgresql", "label": "Postgres" },
{ "value": "mysql", "label": "MySQL" },
{ "value": "sqlite", "label": "SQLite" },
{ "value": "mongodb", "label": "MongoDB" }
]
},
"connectionString": {
"type": "string",
"description": "Connection String",
"x-prompt": "What is the connection string you want to use?"
}
},
"required": ["name", "provider", "connectionString"],
}

@ -0,0 +1,34 @@
{
"$schema": "http://json-schema.org/schema",
"$id": "PrismaGenerator",
"title": "",
"type": "object",
"properties": {
"name": {
"type": "string",
"description": "Prisma Project Name",
"x-prompt": "What do you want to call the project?"
},
"provider": {
"type": "string",
"description": "Database Type",
"x-prompt": {
"message": "Which type of database are you connecting to?",
"type": "list",
"items": [
{ "value": "sqlserver", "label": "MSSQL" },
{ "value": "postgresql", "label": "Postgres" },
{ "value": "mysql", "label": "MySQL" },
{ "value": "sqlite", "label": "SQLite" },
{ "value": "mongodb", "label": "MongoDB" }
]
}
},
"connectionString": {
"type": "string",
"description": "Connection String",
"x-prompt": "What is the connection string you want to use?"
}
},
"required": ["name", "provider", "connectionString"]
}

@ -0,0 +1,3 @@
node_modules
# Keep environment variables out of version control
.env

@ -0,0 +1,2 @@
export { PrismaClient as <%= className %>Client } from '.prisma/<%= name %>-client';
export * from '.prisma/<%= name %>-client'

@ -0,0 +1,26 @@
import {
CallHandler,
ConflictException,
ExecutionContext,
Injectable,
NestInterceptor,
NotFoundException
} from "@nestjs/common";
import { catchError, Observable } from "rxjs";
import { RecordNotFoundError } from "../error/record-not-found.error";
import { UniqueConstraintFailedError } from "../error/unique-constraint-failed.error";
@Injectable()
export class PrismaErrorInterceptor implements NestInterceptor {
intercept(context: ExecutionContext, next: CallHandler): Observable<any> {
return next
.handle()
.pipe(
catchError(err => {
if (err instanceof RecordNotFoundError) throw new NotFoundException(err.message);
if (err instanceof UniqueConstraintFailedError) throw new ConflictException(err.message);
throw err;
})
);
}
}

@ -0,0 +1,12 @@
// This is your Prisma schema file,
// learn more about it in the docs: https://pris.ly/d/prisma-schema
generator client {
provider = "prisma-client-js"
output = "<%= outputLocation %>"
}
datasource db {
provider = "<%= dbType %>"
url = env("<%= constantName %>_SOURCE_URL")
}

@ -0,0 +1,5 @@
import { Tree } from '@nrwl/devkit';
export default async function (tree: Tree, schema: any) {
console.log(schema)
}

@ -0,0 +1,16 @@
{
"extends": "../../tsconfig.base.json",
"compilerOptions": {
"module": "commonjs"
},
"files": [],
"include": [],
"references": [
{
"path": "./tsconfig.lib.json"
},
{
"path": "./tsconfig.spec.json"
}
]
}

@ -0,0 +1,10 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"outDir": "../../dist/out-tsc",
"declaration": true,
"types": ["node"]
},
"include": ["src/**/*.ts"],
"exclude": ["jest.config.ts", "src/**/*.spec.ts", "src/**/*.test.ts"]
}

@ -0,0 +1,14 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"outDir": "../../dist/out-tsc",
"module": "commonjs",
"types": ["jest", "node"]
},
"include": [
"jest.config.ts",
"src/**/*.test.ts",
"src/**/*.spec.ts",
"src/**/*.d.ts"
]
}

9094
package-lock.json generated

File diff suppressed because it is too large Load Diff

@ -14,6 +14,8 @@
"@nestjs/jwt": "^10.1.0",
"@nestjs/platform-express": "^10.0.2",
"@nestjs/typeorm": "^10.0.0",
"@nx/devkit": "16.5.5",
"@nx/plugin": "^16.5.5",
"@prisma/client": "^4.16.1",
"@swc/helpers": "~0.5.0",
"axios": "^1.0.0",
@ -36,8 +38,8 @@
"@nrwl/storybook": "^16.4.1",
"@nx/cypress": "16.4.0",
"@nx/eslint-plugin": "16.4.0",
"@nx/jest": "16.4.0",
"@nx/js": "16.4.1",
"@nx/jest": "16.5.5",
"@nx/js": "16.5.5",
"@nx/linter": "16.4.0",
"@nx/nest": "16.4.0",
"@nx/node": "16.4.0",
@ -51,6 +53,7 @@
"@storybook/core-server": "^7.0.24",
"@storybook/react-vite": "^7.0.24",
"@svgr/webpack": "^8.0.1",
"@swc-node/register": "~1.4.2",
"@swc/cli": "~0.1.62",
"@swc/core": "~1.3.51",
"@testing-library/react": "14.0.0",

@ -15,8 +15,9 @@
"skipDefaultLibCheck": true,
"baseUrl": ".",
"paths": {
"@platform/ui": ["libs/ui/src/index.ts"],
"@pconfig": ["config/index.ts"]
"@pconfig": ["config/index.ts"],
"@platform/prisma-generator": ["libs/prisma-generator/src/index.ts"],
"@platform/ui": ["libs/ui/src/index.ts"]
}
},
"exclude": ["node_modules", "tmp"]

File diff suppressed because it is too large Load Diff
Loading…
Cancel
Save