@prisma/client#Post TypeScript Examples

The following examples show how to use @prisma/client#Post. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: getPost.ts    From fullstack-starterkit with MIT License 6 votes vote down vote up
async function getPost(_: Parent, args: QueryGetPostArgs, context: Context): Promise<GetPostResult> {
  const { prisma } = context;
  const { input } = args;
  const { id }: GetPostInput = input;

  const post: Post | null = await prisma.post.findUnique({ where: { id } });

  return { post };
}
Example #2
Source File: db.test.ts    From fullstack-starterkit with MIT License 6 votes vote down vote up
describe('DB Test Suite', () => {
  test('User should be created', async () => {
    const input: User = createUserInput();
    const user = await prisma.user.create({ data: input });
    expect(user.id).toBe(input.id);
  });

  test('Post should be created', async () => {
    const input: Post = createPostInput();
    const post = await prisma.post.create({ data: input });
    expect(post.id).toBe(input.id);
  });
});
Example #3
Source File: [...nextcrud].ts    From next-crud with MIT License 6 votes vote down vote up
handler = NextCrud({
  adapter: new PrismaAdapter<User | Post, ModelName>({
    prismaClient: prisma,
  }),
  swagger: {
    title: 'My API CRUD',
    apiUrl: process.env.API_URL as string,
    config: {
      User: {
        tag: {
          name: 'Users',
        },
      },
      Post: {
        tag: {
          name: 'Posts',
        },
      },
    },
  },
})
Example #4
Source File: User.ts    From fullstack-starterkit with MIT License 5 votes vote down vote up
User = {
  posts: async (parent: Parent, _: Args, context: Context): Promise<Post[] | null> => {
    const { id } = parent;
    const { prisma } = context;

    return prisma.user.findUnique({ where: { id } }).posts();
  }
}
Example #5
Source File: index.ts    From next-crud with MIT License 4 votes vote down vote up
describe('Prisma interraction', () => {
  beforeAll(async () => {
    await createSeedData()
  })
  let adapter: PrismaAdapter<User | Post, Prisma.ModelName>
  let handler: NextApiHandler<User | Post>

  beforeEach(() => {
    adapter = new PrismaAdapter<User | Post, Prisma.ModelName>({
      prismaClient: prisma,
      manyRelations: {
        [Prisma.ModelName.User]: [
          'post.author',
          'comment.post',
          'comment.author',
        ],
      },
    })
    handler = NextCrud({
      adapter,
      models: {
        [Prisma.ModelName.User]: {
          name: 'users',
        },
      },
    })
  })

  it('should get the list of users', async () => {
    const { res } = getMockRes()
    const req = getMockReq({
      url: '/api/users',
      method: 'GET',
    })

    const expectedResult = await prisma.user.findMany()

    await handler(req, res)

    expect(res.send).toHaveBeenCalledWith(expectedResult)
  })

  it('should get a page based paginated users list', async () => {
    const { res } = getMockRes()
    const req = getMockReq({
      url: '/api/users?page=2&limit=2',
      method: 'GET',
    })

    const expectedResult = await prisma.user.findMany({
      skip: 2,
      take: 2,
    })

    await handler(req, res)

    expect(res.send).toHaveBeenCalledWith({
      data: expectedResult,
      pagination: {
        total: 4,
        pageCount: 2,
        page: 2,
      },
    })
  })

  it('should get the user with first id', async () => {
    const user = await prisma.user.findFirst()

    const { res } = getMockRes()
    const req = getMockReq({
      url: `/api/users/${user.id}`,
      method: 'GET',
    })

    await handler(req, res)

    expect(res.send).toHaveBeenCalledWith(user)
  })

  it('should get the list of users with only their email', async () => {
    const { res } = getMockRes()
    const req = getMockReq({
      url: '/api/users?select=email',
      method: 'GET',
    })

    const expectedResult = await prisma.user.findMany({
      select: {
        email: true,
      },
    })

    await handler(req, res)

    expect(res.send).toHaveBeenCalledWith(expectedResult)
  })

  it('should get the list of users with only their email and posts', async () => {
    const { res } = getMockRes()
    const req = getMockReq({
      url: '/api/users?select=email,posts',
      method: 'GET',
    })

    const expectedResult = await prisma.user.findMany({
      select: {
        email: true,
        posts: true,
      },
    })

    await handler(req, res)

    expect(res.send).toHaveBeenCalledWith(expectedResult)
  })

  it('should get the list of users with only their email and posts ids', async () => {
    const { res } = getMockRes()
    const req = getMockReq({
      url: '/api/users?select=email,posts,posts.id',
      method: 'GET',
    })

    const expectedResult = await prisma.user.findMany({
      select: {
        email: true,
        posts: {
          select: {
            id: true,
          },
        },
      },
    })

    await handler(req, res)

    expect(res.send).toHaveBeenCalledWith(expectedResult)
  })

  it('should get the list of users with only their email, posts ids, comments and comments users', async () => {
    const { res } = getMockRes()
    const req = getMockReq({
      url: '/api/users?select=email,posts,posts.id,posts.comment,posts.comment.author',
      method: 'GET',
    })

    const expectedResult = await prisma.user.findMany({
      select: {
        email: true,
        posts: {
          select: {
            id: true,
            comment: {
              select: {
                author: true,
              },
            },
          },
        },
      },
    })

    await handler(req, res)

    expect(res.send).toHaveBeenCalledWith(expectedResult)
  })

  it('should return the first 2 users', async () => {
    const { res } = getMockRes()
    const req = getMockReq({
      url: '/api/users?limit=2',
      method: 'GET',
    })

    const expectedResult = await prisma.user.findMany({
      take: 2,
    })

    await handler(req, res)

    expect(res.send).toHaveBeenCalledWith(expectedResult)
  })

  it('should return 2 users after the first 2 ones', async () => {
    const { res } = getMockRes()
    const req = getMockReq({
      url: '/api/users?skip=2&limit=2',
      method: 'GET',
    })

    const expectedResult = await prisma.user.findMany({
      take: 2,
      skip: 2,
    })

    await handler(req, res)

    expect(res.send).toHaveBeenCalledWith(expectedResult)
  })

  it('should return 2 users based on a cursor', async () => {
    const firstUser = await prisma.user.findFirst()

    const { res } = getMockRes()
    const req = getMockReq({
      url: `/api/users?limit=2&cursor={"id":${firstUser.id}}`,
      method: 'GET',
    })

    const expectedResult = await prisma.user.findMany({
      take: 2,
      cursor: {
        id: firstUser.id,
      },
    })

    await handler(req, res)

    expect(res.send).toHaveBeenCalledWith(expectedResult)
  })

  it('should filter user by its email', async () => {
    const { res } = getMockRes()
    const req = getMockReq({
      url: `/api/users?where={"email":{"$eq":"[email protected]"}}`,
      method: 'GET',
    })

    const expectedResult = await prisma.user.findMany({
      where: {
        email: {
          equals: '[email protected]',
        },
      },
    })

    await handler(req, res)

    expect(res.send).toHaveBeenCalledWith(expectedResult)
  })

  it('should filter users where email does not match', async () => {
    const { res } = getMockRes()
    const req = getMockReq({
      url: `/api/users?where={"email":{"$neq":"[email protected]"}}`,
      method: 'GET',
    })

    const expectedResult = await prisma.user.findMany({
      where: {
        email: {
          not: '[email protected]',
        },
      },
    })

    await handler(req, res)

    expect(res.send).toHaveBeenCalledWith(expectedResult)
  })

  it('should filter users where email starts with john and ends with .com', async () => {
    const { res } = getMockRes()
    const req = getMockReq({
      url: `/api/users?where={"email":{"$and":{"$starts":"john", "$ends":".com"}}}`,
      method: 'GET',
    })

    const expectedResult = await prisma.user.findMany({
      where: {
        AND: [
          { email: { startsWith: 'john' } },
          { email: { endsWith: '.com' } },
        ],
      },
    })

    await handler(req, res)

    expect(res.send).toHaveBeenCalledWith(expectedResult)
  })

  it('should create a user', async () => {
    const { res } = getMockRes()
    const req = getMockReq({
      url: `/api/users`,
      method: 'POST',
      body: {
        email: '[email protected]',
      },
    })

    await handler(req, res)

    const expectedResult = await prisma.user.findFirst({
      where: {
        email: '[email protected]',
      },
    })

    expect(res.send).toHaveBeenCalledWith(expectedResult)
  })

  it('should update a user', async () => {
    const user = await prisma.user.findFirst({
      where: {
        email: '[email protected]',
      },
    })
    const { res } = getMockRes()
    const req = getMockReq({
      url: `/api/users/${user.id}`,
      method: 'PATCH',
      body: {
        email: '[email protected]',
      },
    })

    await handler(req, res)

    const expectedResult = await prisma.user.findFirst({
      where: {
        email: '[email protected]',
      },
    })

    expect(res.send).toHaveBeenCalledWith(expectedResult)
  })

  it('should update a user and respond with only its email', async () => {
    const user = await prisma.user.findFirst({
      where: {
        email: '[email protected]',
      },
    })
    const { res } = getMockRes()
    const req = getMockReq({
      url: `/api/users/${user.id}?select=email`,
      method: 'PATCH',
      body: {
        email: '[email protected]',
      },
    })

    await handler(req, res)

    const expectedResult = await prisma.user.findFirst({
      where: {
        email: '[email protected]',
      },
      select: {
        email: true,
      },
    })

    expect(res.send).toHaveBeenCalledWith(expectedResult)
  })

  it('should delete the previously created user', async () => {
    const user = await prisma.user.findFirst({
      where: {
        email: '[email protected]',
      },
    })
    const { res } = getMockRes()
    const req = getMockReq({
      url: `/api/users/${user.id}`,
      method: 'DELETE',
    })

    await handler(req, res)

    expect(res.send).toHaveBeenCalledWith(user)
  })

  afterAll(async () => {
    await prisma.comment.deleteMany()
    await prisma.post.deleteMany()
    await prisma.user.deleteMany()
    await prisma.$disconnect()
  })
})