Re-init DB and scaffold all models

This commit is contained in:
Kurt Hutten
2020-11-02 07:02:11 +11:00
parent 55f9dfd6de
commit 86df1d501d
97 changed files with 3202 additions and 2122 deletions

View File

@@ -1,4 +1,4 @@
import { createUserInsecure } from 'src/services/users/users.js'
// import { createUserInsecure } from 'src/services/users/users.js'
export const handler = async (req, _context) => {
const body = JSON.parse(req.body)
@@ -66,7 +66,7 @@ export const handler = async (req, _context) => {
bio: 'default bio'
// full_name: user.user_metadata.full_name
}
await createUserInsecure({input})
// await createUserInsecure({input})
return {
statusCode: 200,

View File

@@ -0,0 +1,35 @@
export const schema = gql`
type Comment {
id: String!
text: String!
user: User!
userId: String!
part: Part!
partId: String!
createdAt: DateTime!
updatedAt: DateTime!
}
type Query {
comments: [Comment!]!
comment(id: String!): Comment
}
input CreateCommentInput {
text: String!
userId: String!
partId: String!
}
input UpdateCommentInput {
text: String
userId: String
partId: String
}
type Mutation {
createComment(input: CreateCommentInput!): Comment!
updateComment(id: String!, input: UpdateCommentInput!): Comment!
deleteComment(id: String!): Comment!
}
`

View File

@@ -0,0 +1,38 @@
export const schema = gql`
type PartReaction {
id: String!
emote: String!
user: User!
userId: String!
part: Part!
partId: String!
createdAt: DateTime!
updatedAt: DateTime!
}
type Query {
partReactions: [PartReaction!]!
partReaction(id: String!): PartReaction
}
input CreatePartReactionInput {
emote: String!
userId: String!
partId: String!
}
input UpdatePartReactionInput {
emote: String
userId: String
partId: String
}
type Mutation {
createPartReaction(input: CreatePartReactionInput!): PartReaction!
updatePartReaction(
id: String!
input: UpdatePartReactionInput!
): PartReaction!
deletePartReaction(id: String!): PartReaction!
}
`

View File

@@ -0,0 +1,42 @@
export const schema = gql`
type Part {
id: String!
title: String!
description: String
code: String
mainImage: String
createdAt: DateTime!
updatedAt: DateTime!
user: User!
userId: String!
Comment: [Comment]!
Reaction: [PartReaction]!
}
type Query {
parts: [Part!]!
part(id: String!): Part
}
input CreatePartInput {
title: String!
description: String
code: String
mainImage: String
userId: String!
}
input UpdatePartInput {
title: String
description: String
code: String
mainImage: String
userId: String
}
type Mutation {
createPart(input: CreatePartInput!): Part!
updatePart(id: String!, input: UpdatePartInput!): Part!
deletePart(id: String!): Part!
}
`

View File

@@ -0,0 +1,39 @@
export const schema = gql`
type User {
id: String!
userName: String!
email: String!
createdAt: DateTime!
updatedAt: DateTime!
image: String
bio: String
Part: [Part]!
Reaction: [PartReaction]!
Comment: [Comment]!
}
type Query {
users: [User!]!
user(id: String!): User
}
input CreateUserInput {
userName: String!
email: String!
image: String
bio: String
}
input UpdateUserInput {
userName: String
email: String
image: String
bio: String
}
type Mutation {
createUser(input: CreateUserInput!): User!
updateUser(id: String!, input: UpdateUserInput!): User!
deleteUser(id: String!): User!
}
`

View File

@@ -0,0 +1,38 @@
import { db } from 'src/lib/db'
import { foreignKeyReplacement } from 'src/services/helpers'
export const comments = () => {
return db.comment.findMany()
}
export const comment = ({ id }) => {
return db.comment.findOne({
where: { id },
})
}
export const createComment = ({ input }) => {
return db.comment.create({
data: foreignKeyReplacement(input),
})
}
export const updateComment = ({ id, input }) => {
return db.comment.update({
data: foreignKeyReplacement(input),
where: { id },
})
}
export const deleteComment = ({ id }) => {
return db.comment.delete({
where: { id },
})
}
export const Comment = {
user: (_obj, { root }) =>
db.comment.findOne({ where: { id: root.id } }).user(),
part: (_obj, { root }) =>
db.comment.findOne({ where: { id: root.id } }).part(),
}

View File

@@ -0,0 +1,9 @@
/*
import { comments } from './comments'
*/
describe('comments', () => {
it('returns true', () => {
expect(true).toBe(true)
})
})

View File

@@ -0,0 +1,13 @@
export const foreignKeyReplacement = (input) => {
let output = input
const foreignKeys = Object.keys(input).filter((k) => k.match(/Id$/))
foreignKeys.forEach((key) => {
const modelName = key.replace(/Id$/, '')
const value = input[key]
delete output[key]
output = Object.assign(output, {
[modelName]: { connect: { id: value } },
})
})
return output
}

View File

@@ -0,0 +1,38 @@
import { db } from 'src/lib/db'
import { foreignKeyReplacement } from 'src/services/helpers'
export const partReactions = () => {
return db.partReaction.findMany()
}
export const partReaction = ({ id }) => {
return db.partReaction.findOne({
where: { id },
})
}
export const createPartReaction = ({ input }) => {
return db.partReaction.create({
data: foreignKeyReplacement(input),
})
}
export const updatePartReaction = ({ id, input }) => {
return db.partReaction.update({
data: foreignKeyReplacement(input),
where: { id },
})
}
export const deletePartReaction = ({ id }) => {
return db.partReaction.delete({
where: { id },
})
}
export const PartReaction = {
user: (_obj, { root }) =>
db.partReaction.findOne({ where: { id: root.id } }).user(),
part: (_obj, { root }) =>
db.partReaction.findOne({ where: { id: root.id } }).part(),
}

View File

@@ -0,0 +1,9 @@
/*
import { partReactions } from './partReactions'
*/
describe('partReactions', () => {
it('returns true', () => {
expect(true).toBe(true)
})
})

View File

@@ -0,0 +1,40 @@
import { db } from 'src/lib/db'
import { foreignKeyReplacement } from 'src/services/helpers'
import { user } from 'src/services/users/users'
export const parts = () => {
return db.part.findMany()
}
export const part = ({ id }) => {
return db.part.findOne({
where: { id },
})
}
export const createPart = async ({ input }) => {
return db.part.create({
data: foreignKeyReplacement(input),
})
}
export const updatePart = ({ id, input }) => {
return db.part.update({
data: foreignKeyReplacement(input),
where: { id },
})
}
export const deletePart = ({ id }) => {
return db.part.delete({
where: { id },
})
}
export const Part = {
user: (_obj, { root }) => db.part.findOne({ where: { id: root.id } }).user(),
Comment: (_obj, { root }) =>
db.part.findOne({ where: { id: root.id } }).Comment(),
Reaction: (_obj, { root }) =>
db.part.findOne({ where: { id: root.id } }).Reaction(),
}

View File

@@ -0,0 +1,9 @@
/*
import { parts } from './parts'
*/
describe('parts', () => {
it('returns true', () => {
expect(true).toBe(true)
})
})

View File

@@ -0,0 +1,38 @@
import { db } from 'src/lib/db'
export const users = () => {
return db.user.findMany()
}
export const user = ({ id }) => {
return db.user.findOne({
where: { id },
})
}
export const createUser = ({ input }) => {
return db.user.create({
data: input,
})
}
export const updateUser = ({ id, input }) => {
return db.user.update({
data: input,
where: { id },
})
}
export const deleteUser = ({ id }) => {
return db.user.delete({
where: { id },
})
}
export const User = {
Part: (_obj, { root }) => db.user.findOne({ where: { id: root.id } }).Part(),
Reaction: (_obj, { root }) =>
db.user.findOne({ where: { id: root.id } }).Reaction(),
Comment: (_obj, { root }) =>
db.user.findOne({ where: { id: root.id } }).Comment(),
}

View File

@@ -0,0 +1,9 @@
/*
import { users } from './users'
*/
describe('users', () => {
it('returns true', () => {
expect(true).toBe(true)
})
})