Skip to content

Commit

Permalink
feat: generate json-schema based on Prisma schema
Browse files Browse the repository at this point in the history
beeman committed Jan 2, 2021
1 parent b1e84a3 commit e67157f
Showing 14 changed files with 765 additions and 94 deletions.
143 changes: 143 additions & 0 deletions libs/api/core/data-access/src/prisma/json-schema/json-schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"definitions": {
"User": {
"type": "object",
"properties": {
"id": {
"type": "string"
},
"createdAt": {
"type": "string",
"format": "date-time"
},
"updatedAt": {
"type": "string",
"format": "date-time"
},
"role": {
"type": "string",
"enum": ["Admin", "User"]
},
"developer": {
"type": "boolean"
},
"email": {
"type": "string"
},
"username": {
"type": "string"
},
"password": {
"type": ["string", "null"]
},
"firstName": {
"type": ["string", "null"]
},
"lastName": {
"type": ["string", "null"]
},
"avatarUrl": {
"type": ["string", "null"]
},
"location": {
"type": ["string", "null"]
},
"phone": {
"type": ["string", "null"]
},
"bio": {
"type": ["string", "null"]
},
"products": {
"type": "array",
"items": {
"$ref": "#/definitions/Product"
}
}
}
},
"Product": {
"type": "object",
"properties": {
"id": {
"type": "string"
},
"createdAt": {
"type": "string",
"format": "date-time"
},
"updatedAt": {
"type": "string",
"format": "date-time"
},
"name": {
"type": "string"
},
"description": {
"type": "string"
},
"price": {
"type": "integer"
},
"owner": {
"anyOf": [
{
"$ref": "#/definitions/User"
},
{
"type": "null"
}
]
},
"category": {
"anyOf": [
{
"$ref": "#/definitions/Category"
},
{
"type": "null"
}
]
}
}
},
"Category": {
"type": "object",
"properties": {
"id": {
"type": "string"
},
"createdAt": {
"type": "string",
"format": "date-time"
},
"updatedAt": {
"type": "string",
"format": "date-time"
},
"name": {
"type": "string"
},
"products": {
"type": "array",
"items": {
"$ref": "#/definitions/Product"
}
}
}
}
},
"type": "object",
"properties": {
"user": {
"$ref": "#/definitions/User"
},
"product": {
"$ref": "#/definitions/Product"
},
"category": {
"$ref": "#/definitions/Category"
}
}
}
4 changes: 4 additions & 0 deletions libs/api/core/data-access/src/prisma/schema.prisma
Original file line number Diff line number Diff line change
@@ -8,6 +8,10 @@ generator client {
binaryTargets = ["native"]
}

generator jsonSchema {
provider = "prisma-json-schema-generator"
}

model User {
id String @id @default(cuid())
createdAt DateTime @default(now())
5 changes: 4 additions & 1 deletion libs/api/meta/feature/src/lib/api-meta-feature.resolver.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Query, Resolver } from '@nestjs/graphql'
import { Mutation, Query, Resolver } from '@nestjs/graphql'
import { ApiMetaDataAccessService, MetaSchema } from '@nx-prisma-admin/api/meta/data-access'

@Resolver()
@@ -9,4 +9,7 @@ export class ApiMetaFeatureResolver {
metaSchema() {
return this.service.metaSchema()
}

// @Mutation()
// createModel() {}
}
7 changes: 0 additions & 7 deletions libs/api/meta/feature/src/lib/api-meta-feature.spec.ts

This file was deleted.

3 changes: 0 additions & 3 deletions libs/api/meta/feature/src/lib/api-meta-feature.ts

This file was deleted.

21 changes: 21 additions & 0 deletions libs/web/core/feature/src/ngx-router-fns/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { LoadChildren, Route } from '@angular/router'

export function route(r: Route): Route {
return { path: '', ...r }
}

export function routeRedirect(path: string): Route {
return route({ pathMatch: 'full', redirectTo: path })
}

export function routeLazy(path: string, loadChildren: LoadChildren): Route {
return route({ path, loadChildren })
}

export function routeChildren(component: any, children: Route[]): Route {
return route({ component, children })
}

export function routeGroup(path: string, children: Route[]): Route {
return route({ path, children })
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { Component } from '@angular/core'
import { MetaModelCreateStore } from './meta-model-create.store'

@Component({
template: `
<ng-container *ngIf="vm$ | async as vm">
<div class="bg-gray-800 p-4 rounded shadow">
<meta-model [model]="vm?.model"></meta-model>
</div>
</ng-container>
`,
providers: [MetaModelCreateStore],
})
export class MetaModelCreateComponent {
readonly vm$ = this.store.vm$
constructor(private readonly store: MetaModelCreateStore) {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { CommonModule } from '@angular/common'
import { NgModule } from '@angular/core'
import { RouterModule } from '@angular/router'
import { UiMetaModelModule } from '@nx-prisma-admin/web/meta/ui'
import { MetaModelCreateComponent } from './meta-model-create.component'

@NgModule({
declarations: [MetaModelCreateComponent],
imports: [
CommonModule,
RouterModule.forChild([{ path: '', component: MetaModelCreateComponent }]),
UiMetaModelModule,
],
})
export class MetaModelCreateModule {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { Injectable } from '@angular/core'
import { ActivatedRoute } from '@angular/router'
import { ComponentStore } from '@ngrx/component-store'
import { MetaModel } from '@nx-prisma-admin/web/util/sdk'
import { map, pluck, switchMap } from 'rxjs/operators'
import { WebMetaFeatureStore } from '../web-meta-feature.store'

interface WebMetaModelState {
model?: MetaModel
}

@Injectable()
export class MetaModelCreateStore extends ComponentStore<WebMetaModelState> {
readonly model$ = this.select(this.state$, ({ model }) => model)
readonly vm$ = this.select(this.meta.schema$, this.model$, (schema, model) => ({ schema, model }))

constructor(route: ActivatedRoute, private readonly meta: WebMetaFeatureStore) {
super({})
this.loadModelEffect(route.params.pipe(pluck('modelId')))
}

readonly loadModelEffect = this.effect<string>((modelId$) =>
modelId$.pipe(
switchMap((modelId: string) =>
this.meta.vm$.pipe(map((res) => this.patchState({ model: res?.schema?.models.find((m) => m.id === modelId) }))),
),
),
)
}
Original file line number Diff line number Diff line change
@@ -4,27 +4,22 @@ import { WebMetaFeatureStore } from '../web-meta-feature.store'
@Component({
template: `
<ng-container *ngIf="vm$ | async as vm">
<div class="bg-gray-800 p-4 rounded shadow">
<div class="text-xl pb-3 px-1 font-semibold">Models</div>
<div class="bg-gray-800 p-4 rounded shadow flex flex-col space-y-4">
<div class="text-xl px-1 font-semibold">Models</div>
<div class="grid grid-cols-4 gap-6">
<ng-container *ngFor="let model of vm?.schema?.models">
<meta-model [model]="model"></meta-model>
<div class="bg-gray-700 rounded shadow p-4">
<div class="text-lg py-1 text-center font-semibold">
<a [routerLink]="['/meta/models', model?.id]">{{ model?.id }}</a>
</div>
</div>
</ng-container>
</div>
</div>
<div class="bg-gray-800 p-4 rounded shadow">
<div class="text-xl pb-3 px-1 font-semibold">Enums</div>
<div class="text-xl px-1 font-semibold">Enums</div>
<div class="grid grid-cols-4 gap-6">
<ng-container *ngFor="let enum of vm?.schema?.enums">
<div class="bg-gray-700 rounded shadow p-4">
<div class="text-lg pb-3 px-1 font-semibold">{{ enum.id }}</div>
<div class="font-mono text-sm grid grid-cols-1">
<ng-container *ngFor="let field of enum.values">
<div>
{{ field }}
</div>
</ng-container>
</div>
<div class="text-lg py-1 text-center font-semibold">{{ enum.id }}</div>
</div>
</ng-container>
</div>
39 changes: 16 additions & 23 deletions libs/web/meta/feature/src/lib/web-meta-feature.module.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,27 @@
import { CommonModule } from '@angular/common'
import { NgModule } from '@angular/core'
import { LoadChildren, Route, RouterModule } from '@angular/router'
import { RouterModule } from '@angular/router'
import {
routeChildren as children,
routeGroup as group,
routeLazy as lazy,
routeRedirect as redirect,
} from 'ngx-router-fns'
import { WebMetaFeatureComponent } from './web-meta-feature.component'

function route(r: Route): Route {
return { path: '', ...r }
}

function routeRedirect(path: string): Route {
return route({ pathMatch: 'full', redirectTo: path })
}

function routeLazy(path: string, loadChildren: LoadChildren): Route {
return route({ path, loadChildren })
}

function routeChildren(component: any, children: Route[]): Route {
return route({ component, children })
}

@NgModule({
declarations: [WebMetaFeatureComponent],
imports: [
RouterModule.forChild([
routeChildren(WebMetaFeatureComponent, [
routeRedirect('models'),
routeLazy('models', () => import('./model-list/meta-model-list.module').then((m) => m.MetaModelListModule)),
routeLazy('models/:modelId', () =>
import('./model-detail/meta-model-detail.module').then((m) => m.MetaModelDetailModule),
),
children(WebMetaFeatureComponent, [
redirect('models'),
group('models', [
lazy('', () => import('./model-list/meta-model-list.module').then((m) => m.MetaModelListModule)),
lazy('create', () => import('./model-create/meta-model-create.module').then((m) => m.MetaModelCreateModule)),
lazy(':modelId', () =>
import('./model-detail/meta-model-detail.module').then((m) => m.MetaModelDetailModule),
),
]),
]),
]),
CommonModule,
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -86,6 +86,7 @@
"joi": "^17.2.1",
"passport": "^0.4.1",
"passport-jwt": "^4.0.0",
"prisma-json-schema-generator": "^1.2.0",
"prismafile": "^1.0.10",
"reflect-metadata": "^0.1.13",
"rxjs": "~6.5.5",
1 change: 1 addition & 0 deletions tsconfig.base.json
Original file line number Diff line number Diff line change
@@ -15,6 +15,7 @@
"skipDefaultLibCheck": true,
"baseUrl": ".",
"paths": {
"ngx-router-fns": ["libs/web/core/feature/src/ngx-router-fns/index.ts"],
"@nx-prisma-admin/api-app-module": ["apps/api/src/app/app.module.ts"],
"@nx-prisma-admin/api/auth/data-access": ["libs/api/auth/data-access/src/index.ts"],
"@nx-prisma-admin/api/auth/feature": ["libs/api/auth/feature/src/index.ts"],
551 changes: 505 additions & 46 deletions yarn.lock

Large diffs are not rendered by default.

0 comments on commit e67157f

Please sign in to comment.