Skip to content

Commit

Permalink
feat: make models more type-safe (remix-run#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelDeBoey authored Mar 18, 2022
1 parent 3da9364 commit a16f9a4
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 19 deletions.
30 changes: 19 additions & 11 deletions app/models/note.server.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,34 @@
import type { User, Note } from "@prisma/client";

import { prisma } from "~/db.server";

export function getNote({ userId, id }: { userId: string; id: string }) {
export type { Note } from "@prisma/client";

export function getNote({
id,
userId,
}: Pick<Note, "id"> & {
userId: User["id"];
}) {
return prisma.note.findFirst({
where: { id, userId },
});
}

export function getNoteListItems({ userId }: { userId: string }) {
export function getNoteListItems({ userId }: { userId: User["id"] }) {
return prisma.note.findMany({
where: { userId: userId },
where: { userId },
select: { id: true, title: true },
orderBy: { updatedAt: "desc" },
});
}

export function createNote({
title,
body,
title,
userId,
}: {
title: string;
body: string;
userId: string;
}: Pick<Note, "body" | "title"> & {
userId: User["id"];
}) {
return prisma.note.create({
data: {
Expand All @@ -36,10 +43,11 @@ export function createNote({
});
}

export function deleteNote({ id, userId }: { id: string; userId: string }) {
export function deleteNote({
id,
userId,
}: Pick<Note, "id"> & { userId: User["id"] }) {
return prisma.note.deleteMany({
where: { id, userId },
});
}

export type { Note } from "@prisma/client";
20 changes: 12 additions & 8 deletions app/models/user.server.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
import type { Password, User } from "@prisma/client";
import bcrypt from "@node-rs/bcrypt";

import { prisma } from "~/db.server";

export type { User } from "@prisma/client";

export async function getUserById(id: string) {
export async function getUserById(id: User["id"]) {
return prisma.user.findUnique({ where: { id } });
}

export async function getUserByEmail(email: string) {
export async function getUserByEmail(email: User["email"]) {
return prisma.user.findUnique({ where: { email } });
}

export async function createUser(email: string, password: string) {
export async function createUser(email: User["email"], password: string) {
const hashedPassword = await bcrypt.hash(password, 10);
const user = await prisma.user.create({

return prisma.user.create({
data: {
email,
password: {
Expand All @@ -23,15 +26,16 @@ export async function createUser(email: string, password: string) {
},
},
});

return user;
}

export async function deleteUserByEmail(email: string) {
export async function deleteUserByEmail(email: User["email"]) {
return prisma.user.delete({ where: { email } });
}

export async function verifyLogin(email: string, password: string) {
export async function verifyLogin(
email: User["email"],
password: Password["hash"]
) {
const userWithPassword = await prisma.user.findUnique({
where: { email },
include: {
Expand Down

0 comments on commit a16f9a4

Please sign in to comment.