Files
2537e64970 feat(db): install into a configurable PostgreSQL schema (#5)
* feat(db): install into a configurable PostgreSQL schema

Add DB_SCHEMA (and dbOptions.schema / createDatabase({ schema }) /
runMigrations(db, { schema })) so notifkit can live in a schema other than
public. Defaults to public, where behaviour is unchanged.

For a custom schema, runMigrations creates the schema, retargets the
bundled migrations at it (rewrites the "public". qualifiers drizzle-kit
emits and sets search_path for the unqualified objects with SET LOCAL,
inside the migration transaction), refuses any migration that still
mentions public afterwards, and keeps the drizzle journal in the same
schema. Afterwards it checks via information_schema that every table of
the package's Drizzle schema exists in the target schema and throws with
the names of any that are missing. createDatabase sets search_path on
every connection.

Add a notifkit-migrate bin (next to notifkit-create-admin) that reads
DATABASE_URL and DB_SCHEMA, applies the migrations, closes the connection
and exits non-zero on failure. The README now uses it for production
migrations instead of drizzle-kit, which ignores DB_SCHEMA, and warns
that switching an existing public install to DB_SCHEMA without moving
its tables first makes it look empty.

Closes #4

* test(db): fix typecheck and lint, move schema tests under tests/

---------

Co-authored-by: Alexander Savchenko <savchenko_av@nlmk.com>
Co-authored-by: coo1estguy <70365929+coolestcodeguy@users.noreply.github.com>
2026-10-07 21:27:45 +05:30

72 lines
2.3 KiB
JavaScript

#!/usr/bin/env node
/**
* Create or update an admin user for the Notifkit Dashboard.
*
* Usage:
* ADMIN_EMAIL=admin@example.com ADMIN_PASSWORD=secret npx notifkit-create-admin
* npx notifkit-create-admin admin@example.com secret [role]
* node scripts/create-admin.mjs admin@example.com secret [role]
*/
import { config } from "dotenv";
import { resolve } from "node:path";
import postgres from "postgres";
import { scrypt, randomBytes } from "node:crypto";
import { promisify } from "node:util";
config({ path: resolve(process.cwd(), ".env") });
const scryptAsync = promisify(scrypt);
async function hashPassword(password) {
const salt = randomBytes(16).toString("hex");
const derivedKey = await scryptAsync(password, salt, 64);
return `${salt}:${derivedKey.toString("hex")}`;
}
const email = process.argv[2] || process.env.ADMIN_EMAIL;
const password = process.argv[3] || process.env.ADMIN_PASSWORD;
const role = process.argv[4] || "admin";
if (!email || !password) {
console.error("Usage: npx notifkit-create-admin <email> <password> [role]");
console.error("Or set ADMIN_EMAIL and ADMIN_PASSWORD in environment.");
process.exit(1);
}
const databaseUrl =
process.env.DATABASE_URL || "postgres://platform:platform@localhost:5432/notifkit";
const schema = (process.env.DB_SCHEMA || "public").trim();
if (!/^[a-z_][a-z0-9_]{0,62}$/.test(schema)) {
console.error(`Invalid DB_SCHEMA "${schema}": must be a lowercase PostgreSQL identifier.`);
process.exit(1);
}
const sql = postgres(databaseUrl, {
connection: schema === "public" ? {} : { search_path: `"${schema}"` },
});
try {
const passwordHash = await hashPassword(password);
const normalizedEmail = email.trim().toLowerCase();
const rows = await sql`
INSERT INTO admin_users (email, password_hash, role)
VALUES (${normalizedEmail}, ${passwordHash}, ${role})
ON CONFLICT (email) DO UPDATE
SET password_hash = EXCLUDED.password_hash,
role = EXCLUDED.role,
updated_at = NOW()
RETURNING id, email, role, created_at, updated_at
`;
console.log("\nAdmin user successfully saved:\n");
console.log(` ID: ${rows[0].id}`);
console.log(` Email: ${rows[0].email}`);
console.log(` Role: ${rows[0].role}\n`);
} catch (err) {
console.error(`Failed to create admin: ${err.message}`);
process.exit(1);
} finally {
await sql.end();
}