-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from evantahler/orm
ORM, types, and more!
- Loading branch information
Showing
35 changed files
with
839 additions
and
138 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { test, expect, beforeAll, afterAll } from "bun:test"; | ||
import { api, type ActionResponse } from "../../api"; | ||
import type { UserCreate } from "../../actions/user"; | ||
import { config } from "../../config"; | ||
|
||
const url = `http://${config.server.web.host}:${config.server.web.port}`; | ||
|
||
beforeAll(async () => { | ||
await api.start(); | ||
await api.db.clearDatabase(); | ||
}); | ||
|
||
afterAll(async () => { | ||
await api.stop(); | ||
}); | ||
|
||
test("user can be created", async () => { | ||
const res = await fetch(url + "/api/user", { | ||
method: "PUT", | ||
headers: { "Content-Type": "application/json" }, | ||
body: JSON.stringify({ | ||
name: "person 1", | ||
email: "[email protected]", | ||
password: "password", | ||
}), | ||
}); | ||
const response = (await res.json()) as ActionResponse<UserCreate>; | ||
expect(res.status).toBe(200); | ||
|
||
expect(response.id).toEqual(1); | ||
expect(response.email).toEqual("[email protected]"); | ||
}); | ||
|
||
test("email must be unique", async () => { | ||
const res = await fetch(url + "/api/user", { | ||
method: "PUT", | ||
headers: { "Content-Type": "application/json" }, | ||
body: JSON.stringify({ | ||
name: "person 1", | ||
email: "[email protected]", | ||
password: "password", | ||
}), | ||
}); | ||
const response = (await res.json()) as ActionResponse<UserCreate>; | ||
expect(res.status).toBe(500); | ||
expect(response.error?.message).toMatch(/violates unique constraint/); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { api, Action, type ActionParams } from "../api"; | ||
import { HTTP_METHOD } from "../classes/Action"; | ||
import { hashPassword, serializeUser } from "../ops/UserOps"; | ||
import { users } from "../schema/users"; | ||
import { ensureString } from "../util/formatters"; | ||
|
||
export class UserCreate implements Action { | ||
name = "userCreate"; | ||
web = { route: "/user", method: HTTP_METHOD.PUT }; | ||
inputs = { | ||
name: { | ||
required: true, | ||
validator: (p: string) => | ||
p.length < 3 ? "Name must be at least 3 characters" : undefined, | ||
formatter: ensureString, | ||
}, | ||
email: { | ||
required: true, | ||
validator: (p: string) => | ||
p.length < 3 || !p.includes("@") ? "Email invalid" : undefined, | ||
formatter: ensureString, | ||
}, | ||
password: { | ||
required: true, | ||
validator: (p: string) => | ||
p.length < 3 ? "Password must be at least 3 characters" : undefined, | ||
formatter: ensureString, | ||
}, | ||
}; | ||
|
||
async run(params: ActionParams<UserCreate>) { | ||
const user = ( | ||
await api.db.db | ||
.insert(users) | ||
.values({ | ||
name: params.name, | ||
email: params.email, | ||
password_hash: await hashPassword(params.password), | ||
}) | ||
.returning() | ||
)[0]; | ||
|
||
return serializeUser(user); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.