Postgres con Drizzle: cliente compartido, esquemas por módulo, transacciones e IDs ULID
Código8 archivosContexto~547 tokensescaneo superado
Instalar
$
genpm add @core/dbQué obtienes
- Código en src/lib/db/, 8 archivos. (7,6 kB)
- Reglas de IA en src/lib/db/AGENTS.md, más archivos de reglas para tu IDE.
- Variables añadidas a .env.example: DATABASE_URL.
README
Este paquete no tiene README.
~547 tokens→ src/lib/db/AGENTS.md→ .cursor/rules/genpm-core-db.mdc
Esto es exactamente lo que lee tu IA cuando trabaja en src/lib/db. No se añade nada más a su contexto.
@core/db — rules for AI agents
Purpose
Shared Postgres access for every module: one Drizzle client, per-module schemas, transactions, ULID ids and standard timestamps. It owns no tables itself. Not an ORM wrapper: use Drizzle's query builder directly.
Map
index.ts— public API:getDb,createDb,setDb,withTransaction,primaryId,timestamps,newId,ulid; typesDb,Tx,Executor.client.ts— lazy client fromDATABASE_URL(postgres-js).columns.ts—primaryId(prefix)andtimestampscolumn helpers.drizzle.config.ts— drizzle-kit config that collectssrc/lib/*/schema.ts.
Integration
- Set
DATABASE_URL(any Postgres: local, Neon, Supabase, RDS). - Install the npm deps if GenPM did not:
drizzle-orm,postgres, anddrizzle-kitas a dev dependency. - Add scripts to
package.json:"db:generate": "drizzle-kit generate --config src/lib/db/drizzle.config.ts","db:migrate": "drizzle-kit migrate --config src/lib/db/drizzle.config.ts". - Run
db:generatethendb:migrateafter installing any module that has aschema.ts. - Query from app code:
import { getDb } from './lib/db/index.js'; const rows = await getDb().select().from(users); - Another driver (Neon serverless, PGlite in tests): build it with Drizzle and call
setDb(db)once at startup.
Conventions
- Each module defines its tables in its own
src/lib/<module>/schema.ts. Never put tables insrc/lib/db. - Table names are prefixed with the module when ambiguous (
auth_sessions, notsessions). - Primary keys:
id: primaryId('<3-4 letter prefix>'); spread...timestampsin every table. - Functions that write accept an optional
Executor(dbortx) so callers can compose them inwithTransaction. - Money as integer minor units; times as
timestamp with time zone.
Don't
- Don't edit or reference another module's tables except through that module's exported functions.
- Don't create a second client or connection pool; use
getDb(). - Don't log
DATABASE_URLor query parameters that may hold secrets. - Don't use
drizzle-kit pushagainst production; generate and review migrations.
# @core/db — rules for AI agents
## Purpose
Shared Postgres access for every module: one Drizzle client, per-module schemas, transactions, ULID ids and standard
timestamps. It owns no tables itself. Not an ORM wrapper: use Drizzle's query builder directly.
## Map
- `index.ts` — public API: `getDb`, `createDb`, `setDb`, `withTransaction`, `primaryId`, `timestamps`, `newId`, `ulid`; types `Db`, `Tx`, `Executor`.
- `client.ts` — lazy client from `DATABASE_URL` (postgres-js).
- `columns.ts` — `primaryId(prefix)` and `timestamps` column helpers.
- `drizzle.config.ts` — drizzle-kit config that collects `src/lib/*/schema.ts`.
## Integration
1. Set `DATABASE_URL` (any Postgres: local, Neon, Supabase, RDS).
2. Install the npm deps if GenPM did not: `drizzle-orm`, `postgres`, and `drizzle-kit` as a dev dependency.
3. Add scripts to `package.json`:
`"db:generate": "drizzle-kit generate --config src/lib/db/drizzle.config.ts"`,
`"db:migrate": "drizzle-kit migrate --config src/lib/db/drizzle.config.ts"`.
4. Run `db:generate` then `db:migrate` after installing any module that has a `schema.ts`.
5. Query from app code:
```ts
import { getDb } from './lib/db/index.js';
const rows = await getDb().select().from(users);
```
6. Another driver (Neon serverless, PGlite in tests): build it with Drizzle and call `setDb(db)` once at startup.
## Conventions
- Each module defines its tables in its own `src/lib/<module>/schema.ts`. Never put tables in `src/lib/db`.
- Table names are prefixed with the module when ambiguous (`auth_sessions`, not `sessions`).
- Primary keys: `id: primaryId('<3-4 letter prefix>')`; spread `...timestamps` in every table.
- Functions that write accept an optional `Executor` (`db` or `tx`) so callers can compose them in `withTransaction`.
- Money as integer minor units; times as `timestamp with time zone`.
## Don't
- Don't edit or reference another module's tables except through that module's exported functions.
- Don't create a second client or connection pool; use `getDb()`.
- Don't log `DATABASE_URL` or query parameters that may hold secrets.
- Don't use `drizzle-kit push` against production; generate and review migrations.
El árbol exacto que se inyectará, tras aplicar .genpmignore. Anclado a
# @core/db — rules for AI agents
## Purpose
Shared Postgres access for every module: one Drizzle client, per-module schemas, transactions, ULID ids and standard
timestamps. It owns no tables itself. Not an ORM wrapper: use Drizzle's query builder directly.
## Map
- `index.ts` — public API: `getDb`, `createDb`, `setDb`, `withTransaction`, `primaryId`, `timestamps`, `newId`, `ulid`; types `Db`, `Tx`, `Executor`.
- `client.ts` — lazy client from `DATABASE_URL` (postgres-js).
- `columns.ts` — `primaryId(prefix)` and `timestamps` column helpers.
- `drizzle.config.ts` — drizzle-kit config that collects `src/lib/*/schema.ts`.
## Integration
1. Set `DATABASE_URL` (any Postgres: local, Neon, Supabase, RDS).
2. Install the npm deps if GenPM did not: `drizzle-orm`, `postgres`, and `drizzle-kit` as a dev dependency.
3. Add scripts to `package.json`:
`"db:generate": "drizzle-kit generate --config src/lib/db/drizzle.config.ts"`,
`"db:migrate": "drizzle-kit migrate --config src/lib/db/drizzle.config.ts"`.
4. Run `db:generate` then `db:migrate` after installing any module that has a `schema.ts`.
5. Query from app code:
```ts
import { getDb } from './lib/db/index.js';
const rows = await getDb().select().from(users);
```
6. Another driver (Neon serverless, PGlite in tests): build it with Drizzle and call `setDb(db)` once at startup.
## Conventions
- Each module defines its tables in its own `src/lib/<module>/schema.ts`. Never put tables in `src/lib/db`.
- Table names are prefixed with the module when ambiguous (`auth_sessions`, not `sessions`).
- Primary keys: `id: primaryId('<3-4 letter prefix>')`; spread `...timestamps` in every table.
- Functions that write accept an optional `Executor` (`db` or `tx`) so callers can compose them in `withTransaction`.
- Money as integer minor units; times as `timestamp with time zone`.
## Don't
- Don't edit or reference another module's tables except through that module's exported functions.
- Don't create a second client or connection pool; use `getDb()`.
- Don't log `DATABASE_URL` or query parameters that may hold secrets.
- Don't use `drizzle-kit push` against production; generate and review migrations.
Este paquete no declara servidores MCP.
| Versión | Commit | Publicado | Escaneo |
|---|---|---|---|
| 1.0.0 | 28d4b9f | hace 5 horas | ✔ escaneo superado |
- genpm
- ninguno
- propuesta
- GenPM propone el comando npm y solo lo ejecuta si dices que sí.
- escaneo
- escaneo superado · 0 hallazgos
- commit
- v1.0.0 → 28d4b9feecb83c7a3efc1ec8fd975ee003b0e3f3 · verificado tras la descarga
- scripts
- Ninguno. GenPM nunca ejecuta código del paquete.
- licencia
- MIT
- reporte
- ¿Ves algo raro?