@usereq/database
v0.1.8
Published
Drizzle ORM schema and a Postgres client factory for Node.js runtime using `postgres` (`drizzle-orm/postgres-js`). Optional [read replicas](https://orm.drizzle.team/docs/read-replicas) and optional [Upstash Redis query cache](https://orm.drizzle.team/docs
Readme
@usereq/database
Drizzle ORM schema and a Postgres client factory for Node.js runtime using postgres (drizzle-orm/postgres-js). Optional read replicas and optional Upstash Redis query cache are supported.
Requirements
- Node.js runtime —
createDatabaseusespostgresand is compatible with Node.js server runtimes (including Next.js). drizzle-orm— listed as apeerDependency; install a compatible version in the app that consumes this package (same major/minor range as this package’speerDependencies).
Install
npm install @usereq/database drizzle-orm postgres
# or
bun add @usereq/database drizzle-orm postgresIf you use Upstash cache, also install the Redis client Drizzle’s cache layer expects:
bun add @upstash/redisSchema
- users — id, email, name, role, ban fields
- accounts, sessions, verifications — auth-related tables
- organizations, members, invitations — org and membership tables
Schema is exported from the package root and from @usereq/database/schema if you only need types/tables for migrations.
Usage
createDatabase is async. You can pass a connection string or a config object.
Primary only
import { createDatabase, users } from "@usereq/database";
import { eq } from "drizzle-orm";
const db = await createDatabase(process.env.DATABASE_URL!);
const [user] = await db.select().from(users).where(eq(users.id, userId));Optional Upstash cache
Install @upstash/redis. Reads that use Drizzle’s cache APIs can be backed by Upstash; url and token are your REST credentials.
const db = await createDatabase({
connectionString: process.env.DATABASE_URL!,
cache: {
url: process.env.UPSTASH_REDIS_REST_URL!,
token: process.env.UPSTASH_REDIS_REST_TOKEN!,
global: true, // optional; defaults to true in this package
},
});Optional read replicas
Pass one or more replica connection strings. Drizzle routes read-style operations to replicas and writes to the primary.
const db = await createDatabase({
connectionString: process.env.DATABASE_URL!,
replicas: [
process.env.DATABASE_REPLICA_URL_1!,
process.env.DATABASE_REPLICA_URL_2!,
],
// cache is optional here too
});Migrations (drizzle-kit)
This package does not ship drizzle-kit or migration files. Your application repo should depend on drizzle-kit, set DATABASE_URL (or read it from env), and point schema at the installed package:
// drizzle.config.ts (in your app)
import { defineConfig } from "drizzle-kit";
export default defineConfig({
dialect: "postgresql",
schema: "./node_modules/@usereq/database/src/schema/index.ts",
out: "./drizzle",
dbCredentials: { url: process.env.DATABASE_URL! },
});Adjust schema if you use a different package manager layout (for example a monorepo path or a re-export file that imports from @usereq/database/schema).
Publishing to npm (org scope)
Org and scope — On npmjs.com, create an organization (for example
usereq). Scoped packages use the name@usereq/database; the org owner or members need publish rights for that scope.Login locally
npm loginEnsure the logged-in user is allowed to publish
@usereq/*.Version — Bump
"version"inpackage.json(or usenpm version patch|minor|major).Dry run — See what would be published:
npm pack --dry-runOnly files listed under
"files"inpackage.json(pluspackage.jsonandREADME.mdby npm rules) are included.Publish — Public scoped packages must use public access:
npm publish --access publicpublishConfig.accessis already set to"public"in this package, sonpm publishis enough once your account defaults allow it;--access publicis explicit and safe for first-time scoped publishes.CI (optional) — See GitHub Actions below.
After publish, consumers install with:
npm install @usereq/database drizzle-orm postgres
# or
bun add @usereq/database drizzle-orm postgresGitHub Actions
Workflows live under .github/workflows/ in this repository.
| Workflow | Trigger | What it does |
|----------|---------|----------------|
| ci.yml | Push / PR to main or master | bun install --frozen-lockfile, bun run check-types. |
| publish.yml | Push a version tag matching v* (e.g. v0.1.1) | Same checks, then npm publish --access public to the npm registry. |
Repository secret: add NPM_TOKEN (npm automation or granular token with publish rights for @usereq/database, and bypass 2FA if your account requires it). GitHub → Settings → Secrets and variables → Actions.
Release flow: bump version in package.json, commit, create and push a tag:
git tag v0.1.2
git push origin v0.1.2The publish workflow runs on the tagged commit; the tag should match the version you intend to ship.
