Add database schema and Kysely packages

- Create initial database migration with full auth schema:
  - users, sessions, passkeys, devices tables
  - orgs, org_members, org_sites, org_invites tables
  - email_verifications, password_resets, login_requests tables
  - Indexes for common lookups and cleanup jobs
- Add @reviq/db-schema package with kysely-codegen
- Add @reviq/db package with Kysely client

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
RevIQ
2026-01-09 11:44:36 +08:00
parent 322155b4a1
commit 392d976812
12 changed files with 1676 additions and 0 deletions

View File

@@ -0,0 +1,12 @@
import { configs } from "@macalinao/eslint-config";
export default [
...configs.fast,
{
languageOptions: {
parserOptions: {
tsconfigRootDir: import.meta.dirname,
},
},
},
];

25
packages/db/package.json Normal file
View File

@@ -0,0 +1,25 @@
{
"name": "@reviq/db",
"version": "0.0.1",
"private": true,
"type": "module",
"exports": {
".": "./src/index.ts"
},
"scripts": {
"build": "tsc",
"clean": "tsc --build --clean && rm -rf dist/ node_modules/ .eslintcache",
"lint": "eslint . --cache"
},
"dependencies": {
"@reviq/db-schema": "workspace:*",
"kysely": "^0.28.9",
"pg": "^8.13.1"
},
"devDependencies": {
"@types/node": "^25.0.3",
"@types/pg": "^8.11.10",
"@macalinao/tsconfig": "catalog:",
"typescript": "catalog:"
}
}

37
packages/db/src/client.ts Normal file
View File

@@ -0,0 +1,37 @@
/**
* Kysely database client
*
* @module @reviq/db
*/
import { Kysely, PostgresDialect } from "kysely";
import type { Database } from "@reviq/db-schema";
import pg from "pg";
const { Pool } = pg;
/**
* Creates a new Kysely database client
*
* @param connectionString - PostgreSQL connection string (defaults to DATABASE_URL env var)
* @returns Kysely database instance
*/
export const createDb = (
connectionString: string = process.env.DATABASE_URL || ""
): Kysely<Database> => {
if (!connectionString) {
throw new Error(
"Database connection string is required. Set DATABASE_URL environment variable."
);
}
const dialect = new PostgresDialect({
pool: new Pool({
connectionString,
}),
});
return new Kysely<Database>({
dialect,
});
};

26
packages/db/src/index.ts Normal file
View File

@@ -0,0 +1,26 @@
/**
* Database client for RevIQ Publisher Dashboard
*
* @module @reviq/db
*/
import type { Kysely } from "kysely";
import type { Database } from "@reviq/db-schema";
import { createDb } from "./client.js";
/**
* Default database instance
*
* Uses DATABASE_URL environment variable for connection
*/
export const db: Kysely<Database> = createDb();
/**
* Export createDb for creating custom database instances
*/
export { createDb } from "./client.js";
/**
* Re-export database types from db-schema
*/
export type { Database } from "@reviq/db-schema";

14
packages/db/tsconfig.json Normal file
View File

@@ -0,0 +1,14 @@
{
"extends": "@macalinao/tsconfig/tsconfig.base.json",
"compilerOptions": {
"outDir": "./dist",
"rootDir": "./src",
"module": "NodeNext",
"moduleResolution": "NodeNext",
"declaration": true,
"declarationMap": true,
"types": ["node"]
},
"include": ["src/**/*"],
"exclude": ["node_modules", "dist"]
}