DeanMo
07/31/2020, 2:31 AMRyan
07/31/2020, 7:10 AMDeanMo
07/31/2020, 7:31 AMDeanMo
07/31/2020, 7:33 AMDeanMo
07/31/2020, 7:33 AMDeanMo
07/31/2020, 7:33 AMDeanMo
07/31/2020, 7:34 AMDeanMo
07/31/2020, 7:35 AMDeanMo
07/31/2020, 7:36 AMDeanMo
07/31/2020, 7:36 AMDeanMo
07/31/2020, 7:37 AMDeanMo
07/31/2020, 7:38 AMDeanMo
07/31/2020, 7:38 AMDeanMo
07/31/2020, 7:39 AM"scripts": {
"nexus dev": "nexus dev",
"nexus build": "nexus build"
}
tsconfig.json
{
"compilerOptions": {
"target": "es2016",
"module": "commonjs",
"lib": [
"esnext"
],
"strict": true,
"rootDir": "api",
"noEmit": true,
"plugins": [
{
"name": "nexus/typescript-language-service"
}
],
"typeRoots": [
"node_modules/@types",
"types"
]
},
"include": [
"types.d.ts",
"api"
]
}
.vscode/launch.json
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Debug Nexus App",
"protocol": "inspector",
"runtimeExecutable": "${workspaceRoot}/node_modules/.bin/nexus",
"runtimeArgs": [
"dev"
],
"sourceMaps": true,
"console": "integratedTerminal"
}
]
}
npx prisma
npx prisma init
nexustoprisma/prisma/.env
DATABASE_URL="<postgresql://NAME:NAME@localhost:5432/DBNAME?schema=public>"
open pgadmin, create nexustoprisma
mkdir api
api/app.ts
import { use } from 'nexus'
import { prisma } from 'nexus-plugin-prisma'
use(
prisma({
features: {
crud: true,
},
})
)
nexustoprisma/prisma/schema.prisma
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}
generator client {
provider = "prisma-client-js"
}
model Employee {
id String @default(uuid()) @id
name String
email String @unique
photo_url String?
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
status Boolean @default(false)
employer Employer? @relation(fields: [employerID], references: [id])
employerID String?
}
model Employer {
id String @default(uuid()) @id
name String
email String @unique
photo_url String?
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
employees Employee[]
}
npx prisma migrate save --experimental
npx prisma migrate up --experimental
npx prisma generate
Generate data with seed.js
prisma/seed.js
const { PrismaClient } = require('@prisma/client')
const prisma = new PrismaClient()
const main = async () => {
let employees = [
'Julio', 'Dante', 'Emily',
'Willis', 'Flanders', 'Don',
'Denise', 'Rasheed', 'Anna',
]
let employers = ['Brock', 'Chad', 'Dave']
for (const employer of employers) {
employerList = await prisma.employer.create({
data: {
name: `${employer}`,
email: `${employer}@employerTest.com`,
},
})
console.log(employerList)
}
const empSeed = await prisma.employer.findMany()
for (const [index, emp] of employees.entries()) {
employeeList = await prisma.employee.create({
data: {
name: `${emp}`,
email: `${emp}@test.com`,
employer: {
connect: { id: empSeed[index % employers.length].id },
},
},
})
console.log(employeeList)
}
}
main()
.catch(e => console.error(e))
.finally(async () => {
await prisma.disconnect()
})
index.ts
import { PrismaClient } from '@prisma/client'
const prisma = new PrismaClient()
async function main() {
const allEmployees = await prisma.employee.findMany()
console.log(allEmployees)
}
main()
.catch(e => {
throw e
})
.finally(async () => {
await prisma.disconnect()
})
api/graphql/Employee.ts
import { schema } from 'nexus'
schema.objectType({
name: 'Employee',
description: 'Employee for the company',
definition(t) {
t.model.id()
t.model.name()
t.model.email()
t.model.photo_url()
t.model.createdAt()
t.model.updatedAt()
t.model.status()
t.model.employerID()
}
})
api/graphql.Employer.tsDeanMo
07/31/2020, 7:39 AMimport { schema } from 'nexus'
schema.objectType({
name: 'Employer',
description: 'Employer of the company',
definition(t) {
t.model.id()
t.model.name()
t.model.email()
t.model.photo_url()
t.model.createdAt()
t.model.updatedAt()
},
})
api/graphql/Mutation.ts
import { schema } from 'nexus'
schema.mutationType({
definition(t) {
t.crud.createOneEmployee()
t.crud.createOneEmployer()
t.crud.deleteOneEmployee()
t.crud.deleteManyEmployee()
t.crud.deleteOneEmployer()
t.crud.deleteManyEmployer()
t.crud.updateOneEmployee()
t.crud.updateManyEmployee()
t.crud.updateOneEmployer()
t.crud.updateManyEmployer()
},
})
DeanMo
07/31/2020, 7:40 AMDeanMo
07/31/2020, 7:40 AMDeanMo
07/31/2020, 7:41 AMDeanMo
07/31/2020, 7:42 AMnikolasburk
DeanMo
07/31/2020, 7:47 AMDeanMo
07/31/2020, 7:48 AMDeanMo
07/31/2020, 7:57 AMDeanMo
07/31/2020, 7:57 AM