switch to monorepo structure

This commit is contained in:
2025-05-06 11:00:35 +01:00
parent e1f927ad27
commit 07b7f1b51f
63 changed files with 2440 additions and 1011 deletions

View File

@@ -0,0 +1,62 @@
import { type User, DEMO_USER } from "@markone/core/user"
import { ulid } from "ulid"
import { db } from "~/database.ts"
interface UserWithPassword extends User {
password: string
}
const findUserByIdQuery = db.query("SELECT id, username FROM users WHERE id = $userId")
const findUserByUsernameQuery = db.query("SELECT id, username FROM users WHERE username = $username")
const findUserByUsernameWithPwQuery = db.query("SELECT id, username, password FROM users WHERE username = $username")
const createUserQuery = db.query("INSERT INTO users(id, username, password) VALUES ($id, $username, $password)")
function findUserByUsername(username: string, opts: { password: true }): UserWithPassword | null
function findUserByUsername(username: string, { password }: { password?: boolean }): User | UserWithPassword | null {
const row = (password ? findUserByUsernameWithPwQuery : findUserByUsernameQuery).get({ username })
if (!row) {
return null
}
return row as User
}
function findUserById(userId: string): User | null {
const row = findUserByIdQuery.get({ userId })
if (!row) {
return null
}
return row as User
}
function createUser(username: string, password: string): User {
const userId = ulid()
createUserQuery.run({
id: userId,
username,
password,
})
return {
id: userId,
username,
}
}
async function createDemoUser() {
const row = findUserByUsernameQuery.get({ username: DEMO_USER.username })
if (row) {
return
}
const hashedPassword = await Bun.password.hash(DEMO_USER.unhashedPassword, "argon2id")
createUserQuery.run({
id: DEMO_USER.id,
username: DEMO_USER.username,
password: hashedPassword,
})
}
export type { User }
export { DEMO_USER, createDemoUser, createUser, findUserByUsername, findUserById }