first commit
This commit is contained in:
@@ -0,0 +1,19 @@
|
||||
-- +up
|
||||
CREATE TABLE IF NOT EXISTS "posts" (
|
||||
"id" INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
"userId" INTEGER NOT NULL REFERENCES "users"("id"),
|
||||
"title" TEXT NOT NULL,
|
||||
"body" TEXT NOT NULL
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS "users" (
|
||||
"id" INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
"email" TEXT NOT NULL UNIQUE,
|
||||
"name" TEXT NOT NULL,
|
||||
"active" INTEGER NOT NULL DEFAULT 1,
|
||||
"createdAt" TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP
|
||||
);
|
||||
|
||||
-- +down
|
||||
DROP TABLE IF EXISTS "users";
|
||||
DROP TABLE IF EXISTS "posts";
|
||||
@@ -0,0 +1,7 @@
|
||||
-- +up
|
||||
INSERT INTO users (email, name, active) VALUES ('ada@wire.dev', 'Ada Lovelace', 1);
|
||||
INSERT INTO users (email, name, active) VALUES ('grace@wire.dev', 'Grace Hopper', 1);
|
||||
INSERT INTO users (email, name, active) VALUES ('linus@wire.dev', 'Linus Torvalds', 0);
|
||||
|
||||
-- +down
|
||||
DELETE FROM users WHERE email IN ('ada@wire.dev', 'grace@wire.dev', 'linus@wire.dev');
|
||||
@@ -0,0 +1,5 @@
|
||||
-- +up
|
||||
ALTER TABLE "users" ADD COLUMN "passwordHash" TEXT NOT NULL DEFAULT '';
|
||||
|
||||
-- +down
|
||||
ALTER TABLE "users" DROP COLUMN "passwordHash";
|
||||
@@ -0,0 +1,23 @@
|
||||
// AUTO-GENERATED by `wrnexus db generate` — do not edit.
|
||||
import type { Db, ExecResult } from "@wrnexus/db";
|
||||
import { users } from "./schema.ts";
|
||||
|
||||
export async function GetUserByEmail(db: Db, args: { email: string }): Promise<{ id: number; email: string; name: string; active: boolean; passwordHash: string; createdAt: Date } | null> {
|
||||
return (await db.one("SELECT * FROM users WHERE email = $1", [args.email], users)) as { id: number; email: string; name: string; active: boolean; passwordHash: string; createdAt: Date } | null;
|
||||
}
|
||||
|
||||
export async function ListUsers(db: Db): Promise<{ id: number; name: string; active: boolean }[]> {
|
||||
return (await db.all("SELECT id, name, active FROM users ORDER BY name", [], users)) as { id: number; name: string; active: boolean }[];
|
||||
}
|
||||
|
||||
export async function CountActive(db: Db, args: { active: boolean }): Promise<{ n: number } | null> {
|
||||
return (await db.one("SELECT COUNT(*) AS n FROM users WHERE active = $1", [args.active])) as { n: number } | null;
|
||||
}
|
||||
|
||||
export async function CreateUser(db: Db, args: { email: string; name: string; active: boolean }): Promise<ExecResult> {
|
||||
return db.exec("INSERT INTO users (email, name, active) VALUES ($1, $2, $3)", [args.email, args.name, args.active]);
|
||||
}
|
||||
|
||||
export async function DeactivateUser(db: Db, args: { id: number }): Promise<ExecResult> {
|
||||
return db.exec("UPDATE users SET active = 0 WHERE id = $1", [args.id]);
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
-- name: GetUserByEmail :one
|
||||
SELECT * FROM users WHERE email = :email;
|
||||
|
||||
-- name: ListUsers :many
|
||||
SELECT id, name, active FROM users ORDER BY name;
|
||||
|
||||
-- name: CountActive :one
|
||||
SELECT COUNT(*) AS n FROM users WHERE active = :active;
|
||||
|
||||
-- name: CreateUser :exec
|
||||
INSERT INTO users (email, name, active) VALUES (:email, :name, :active);
|
||||
|
||||
-- name: DeactivateUser :exec
|
||||
UPDATE users SET active = 0 WHERE id = :id;
|
||||
@@ -0,0 +1,35 @@
|
||||
// Database models — the source of truth. `wrnexus db new --from-models` generates
|
||||
// migrations from these, and query results are mapped back through them.
|
||||
import { v, table } from "@wrnexus/db";
|
||||
|
||||
export type User = {
|
||||
id: number;
|
||||
email: string;
|
||||
name: string;
|
||||
active: boolean;
|
||||
passwordHash: string;
|
||||
createdAt: Date;
|
||||
};
|
||||
|
||||
export const users = table<User>("users", {
|
||||
id: v.id(),
|
||||
email: v.string().unique(),
|
||||
name: v.string(),
|
||||
active: v.boolean().default(true),
|
||||
passwordHash: v.string().default(""),
|
||||
createdAt: v.timestamp().default("now"),
|
||||
});
|
||||
|
||||
export type Post = {
|
||||
id: number;
|
||||
userId: number;
|
||||
title: string;
|
||||
body: string;
|
||||
};
|
||||
|
||||
export const posts = table<Post>("posts", {
|
||||
id: v.id(),
|
||||
userId: v.int().references("users", "id"),
|
||||
title: v.string(),
|
||||
body: v.string(),
|
||||
});
|
||||
@@ -0,0 +1,23 @@
|
||||
// Re-runnable dev seed data. Run with: wrnexus db seed
|
||||
// Every seeded user has the password "password123" for local testing.
|
||||
import { hashPassword } from "@wrnexus/core";
|
||||
import type { Db } from "@wrnexus/db";
|
||||
|
||||
export default async function seed(db: Db): Promise<void> {
|
||||
const passwordHash = await hashPassword("password123");
|
||||
await db.exec("DELETE FROM users");
|
||||
const rows: [string, string, number][] = [
|
||||
["ada@wire.dev", "Ada Lovelace", 1],
|
||||
["grace@wire.dev", "Grace Hopper", 1],
|
||||
["linus@wire.dev", "Linus Torvalds", 0],
|
||||
["margaret@wire.dev", "Margaret Hamilton", 1],
|
||||
];
|
||||
for (const [email, name, active] of rows) {
|
||||
await db.exec("INSERT INTO users (email, name, active, passwordHash) VALUES (?, ?, ?, ?)", [
|
||||
email,
|
||||
name,
|
||||
active,
|
||||
passwordHash,
|
||||
]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user