Postgres avec Drizzle : client partagé, schémas par module, transactions et identifiants ULID
Code8 fichiersContexte~547 tokensanalyse réussie
Installer
$
genpm add @core/dbCe que vous obtenez
- Source dans src/lib/db/, 8 fichiers. (7,6 ko)
- Règles IA dans src/lib/db/AGENTS.md, plus les fichiers de règles de l’IDE.
- Variables d’environnement ajoutées à .env.example : DATABASE_URL.
README
Ce paquet n’a pas de README.
~547 tokens→ src/lib/db/AGENTS.md→ .cursor/rules/genpm-core-db.mdc
Voici exactement ce que lit votre IA quand elle travaille dans src/lib/db. Rien d’autre n’est ajouté à son contexte.
@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.
L’arborescence exacte qui sera injectée, après .genpmignore. Épinglée à
// Columnas estándar para que todas las tablas de todos los módulos se parezcan.
import { text, timestamp } from 'drizzle-orm/pg-core';
import { newId } from './ulid.js';
/** Clave primaria `text` con ULID prefijado generado en la app: `id: primaryId('usr')`. */
export const primaryId = (prefix: string) =>
text('id')
.primaryKey()
.$defaultFn(() => newId(prefix));
/** `created_at` / `updated_at` con zona horaria; `updated_at` se actualiza solo en cada `update()`. */
export const timestamps = {
createdAt: timestamp('created_at', { withTimezone: true, mode: 'date' }).notNull().defaultNow(),
updatedAt: timestamp('updated_at', { withTimezone: true, mode: 'date' })
.notNull()
.defaultNow()
.$onUpdateFn(() => new Date()),
};
Ce paquet ne déclare aucun serveur MCP.
| Version | Commit | Publié | Analyse |
|---|---|---|---|
| 1.0.0 | 28d4b9f | il y a 4 heures | ✔ analyse réussie |
- genpm
- aucun
- proposé
- GenPM propose la commande npm et ne l’exécute que si vous acceptez.
- analyse
- analyse réussie · 0 problème
- commit
- v1.0.0 → 28d4b9feecb83c7a3efc1ec8fd975ee003b0e3f3 · vérifié après téléchargement
- scripts
- Aucun. GenPM n’exécute jamais le code des paquets.
- licence
- MIT
- signalement
- Vous avez repéré un problème ?