Skip to content

Commit 3a86d06

Browse files
committed
Add prisma seed + hooks linter
1 parent c344f0b commit 3a86d06

File tree

8 files changed

+1847
-272
lines changed

8 files changed

+1847
-272
lines changed

backend/auth.ts

Lines changed: 54 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,71 +1,71 @@
1-
import { prisma } from './generated/prisma-client'
2-
import { ContextParameters } from 'graphql-yoga/dist/types'
1+
import { prisma, } from '../src/generated/prisma-client'
2+
import { ContextParameters, } from 'graphql-yoga/dist/types'
33
import * as jwt from 'jsonwebtoken'
4-
import { arg, stringArg, idArg, enumType, objectType } from 'nexus'
4+
import { arg, stringArg, idArg, enumType, objectType, } from 'nexus'
55
import * as bcrypt from 'bcrypt'
66

77
export const getUser = async (req: ContextParameters) => {
8-
const auth = req.request.get('Authorization')
8+
const auth = req.request.get('Authorization')
99

10-
if (auth) {
11-
const token = auth.replace('Bearer ', '')
12-
try {
13-
const { userId } = jwt.verify(token, 'APP_SECRET')
14-
// TO FIX: prisma.user ne retourne pas .role
15-
return prisma.user({ id: userId })
16-
} catch (error) {
17-
console.log('getUser error', error)
18-
return null
19-
}
20-
} else {
21-
return null
22-
}
10+
if (auth) {
11+
const token = auth.replace('Bearer ', '')
12+
try {
13+
const { userId, } = jwt.verify(token, 'APP_SECRET')
14+
// TO FIX: prisma.user ne retourne pas .role
15+
return prisma.user({ id: userId, })
16+
} catch (error) {
17+
console.log('getUser error', error)
18+
return null
19+
}
20+
} else {
21+
return null
22+
}
2323
}
2424

2525
export const signup = {
26-
type: 'AuthPayload',
27-
args: {
28-
name: stringArg({ nullable: false }),
29-
email: stringArg({ nullable: false }),
30-
password: stringArg({ nullable: false })
31-
},
32-
resolve: async (_, { name, email, password }, ctx) => {
33-
const password2 = await bcrypt.hash(password, 10)
26+
type: 'AuthPayload',
27+
args: {
28+
name: stringArg({ nullable: false, }),
29+
email: stringArg({ nullable: false, }),
30+
password: stringArg({ nullable: false, }),
31+
},
32+
resolve: async (_, { name, email, password, }, ctx) => {
33+
const password2 = await bcrypt.hash(password, 10)
3434

35-
const user = await ctx.prisma.createUser({
36-
name,
37-
email,
38-
password: password2,
39-
role: 'USER'
40-
})
35+
const user = await ctx.prisma.createUser({
36+
name,
37+
email,
38+
password: password2,
39+
role: 'USER',
40+
})
4141

42-
console.log({ user })
42+
console.log({ user, })
4343

44-
return {
45-
token: jwt.sign({ userId: user.id }, 'APP_SECRET'),
46-
user
47-
}
48-
}
44+
return {
45+
token: jwt.sign({ userId: user.id, }, 'APP_SECRET'),
46+
user,
47+
}
48+
},
4949
}
5050

5151
export const login = {
52-
type: 'AuthPayload',
53-
args: {
54-
email: stringArg({ nullable: false }),
55-
password: stringArg({ nullable: false })
56-
},
57-
resolve: async (_, { email, password }, ctx) => {
58-
const user = await ctx.prisma.user({ email })
59-
console.log({ user })
60-
if (!user) throw new Error('User not in db.')
52+
type: 'AuthPayload',
53+
args: {
54+
email: stringArg({ nullable: false, }),
55+
password: stringArg({ nullable: false, }),
56+
},
57+
resolve: async (_, { email, password, }, ctx) => {
58+
const user = await ctx.prisma.user({ email, })
59+
console.log({ user, })
60+
if (!user) throw new Error('User not in db.')
6161

62-
const valid = await bcrypt.compare(password, user.password)
63-
console.log({ valid })
64-
if (!valid) throw new Error('Wrong password.')
62+
const valid = await bcrypt.compare(password, user.password)
63+
console.log({ valid, })
64+
if (!valid) throw new Error('Wrong password.')
6565

66-
return {
67-
token: jwt.sign({ userId: user.id }, 'APP_SECRET'),
68-
user
69-
}
70-
}
66+
return {
67+
token: jwt.sign({ userId: user.id, }, 'APP_SECRET'),
68+
user,
69+
}
70+
},
7171
}

backend/package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@
88
"start": "dotenv ts-node index.ts",
99
"dev": "dotenv ts-node-dev --no-notify --respawn --transpileOnly ./",
1010
"build": "yarn run prisma:generate",
11-
"prisma:generate": "dotenv prisma generate",
12-
"prisma:deploy": "dotenv prisma deploy",
11+
"prisma:generate": "prisma generate --env-file .env",
12+
"prisma:deploy": "prisma deploy --env-file .env",
13+
"prisma:seed": "prisma seed --env-file .env",
1314
"docker:up": "docker-compose up"
1415
},
1516
"keywords": [],

backend/prisma.yml

Lines changed: 0 additions & 10 deletions
This file was deleted.

backend/prisma/prisma.yml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
endpoint: ${env:PRISMA_ENDPOINT}
2+
datamodel: datamodel.prisma
3+
databaseType: document
4+
5+
generate:
6+
- generator: typescript-client
7+
output: ../src/generated/prisma- client/
8+
9+
hooks:
10+
post-deploy:
11+
- yarn prisma:generate
12+
- npx nexus-prisma-generate --client ./src/generated/prisma-client --output ./src/generated/nexus-prisma
13+
14+
seed:
15+
run: yarn ts-node ./prisma/seed.ts

backend/prisma/seed.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { prisma, } from '../src/generated/prisma-client'
2+
3+
async function main() {
4+
await prisma.createUser({
5+
name: 'Admin',
6+
7+
password: 'admin',
8+
role: 'ADMIN',
9+
})
10+
}
11+
12+
main()

package.json

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,26 @@
1010
"frontend"
1111
]
1212
},
13-
"private": true
13+
"private": true,
14+
"devDependencies": {
15+
"eslint": "^5.16.0",
16+
"husky": "^2.3.0",
17+
"lint-staged": "^8.1.7",
18+
"prettier": "^1.17.1"
19+
},
20+
"husky": {
21+
"hooks": {
22+
"pre-commit": "lint-staged"
23+
}
24+
},
25+
"lint-staged": {
26+
"*.{js,css,json,md}": [
27+
"prettier --write",
28+
"git add"
29+
],
30+
"*.js": [
31+
"eslint --fix",
32+
"git add"
33+
]
34+
}
1435
}

0 commit comments

Comments
 (0)