Storage
Postgres through Drizzle, Prisma or any client, MongoDB and Mongoose, or bring your own store.
Drizzle (Postgres)#
Add the tables to your schema so drizzle-kit creates them:
// schema.ts
export { accessLog, breachNotices, breaches, consentEvents, erasureTasks, privacyNotices, rightsRequests } from "dpdp-kit/drizzle";If you don't use drizzle-kit, run DPDP_DDL (exported from dpdp-kit/drizzle). This works with any Drizzle Postgres driver: node-postgres, postgres.js, Neon and PGlite.
| Table | Holds |
|---|---|
dpdp_consent_events |
the consent records (hash chain) |
dpdp_requests |
rights requests |
dpdp_erasure_tasks |
scheduled erasure tasks |
dpdp_access_log |
who accessed whose data, and why (Rule 6) |
dpdp_breaches |
personal data breaches and their deadlines (Rule 7) |
dpdp_breach_notices |
who each breach affected, and whether they were told |
dpdp_privacy_notices |
each privacy notice as shown, kept as proof (Section 6(10)) |
Upgrading from 0.4.x? Run
MIGRATE_0_5_DDLonce, or addprivacyNoticesto your schema and generate a Drizzle migration. The consent table then accepts objections, and notices have somewhere to be saved. With Prisma, add the newDpdpPrivacyNoticemodel fromPRISMA_MODELSand runprisma migrate dev. MongoDB needs nothing.
Upgrading from 0.1.x? Add
rightsRequestsanderasureTasksto your schema and generate a migration, or runRECORDS_DDL.withdraw()now schedules erasure tasks, so it needs thedpdp_erasure_taskstable.Upgrading from 0.3.x? Add
breachesandbreachNoticesto your schema and generate a migration, or runBREACH_DDL.Upgrading from 0.2.x? Add
accessLogto your schema and generate a migration, or runACCESS_LOG_DDL, before usingdpdp.access.
drizzleData(db, { schema }) reads and erases personal data in your own tables. Pass your schema module when the data map uses Drizzle property names (userId) rather than column names (user_id).
Prisma (Postgres)#
Add dpdp-kit's models to your Prisma schema. Print them once and paste them into schema.prisma, or save them as prisma/schema/dpdp.prisma if you use a multi-file schema:
node -e 'import("dpdp-kit/prisma").then((m) => console.log(m.PRISMA_MODELS))' >> prisma/schema.prisma
npx prisma migrate dev --name dpdp-kitThey create the same tables as DPDP_DDL, so a database set up either way works with both.
import { readFileSync } from "node:fs";
import { createDpdp } from "dpdp-kit";
import { prismaData, prismaSchema, prismaStore } from "dpdp-kit/prisma";
import { prisma } from "./db";
const schema = prismaSchema(readFileSync("prisma/schema.prisma", "utf8"));
export const dpdp = createDpdp({
config,
store: prismaStore(prisma), // consent, requests, tasks, access log, breaches
data: prismaData(prisma, { schema }), // reads and erases personal data in your tables
});- It runs parameterised SQL through
prisma.$queryRawUnsafe. Postgres only. Tested on Prisma 7 with@prisma/adapter-pg. Every parameter has an explicit cast, so earlier versions should work too, but they aren't tested. - The schema lets the data map use model and field names (
User,phoneNumber) where the database has others (users,phone_number), and casts values to each column's type, soIntids,@db.Uuidcolumns and enums work. - Inside a transaction, pass the transaction client:
createDpdp({ config, store: prismaStore(tx) })withinprisma.$transaction(async (tx) => …). The consent record then commits or rolls back with your own writes.
Any other Postgres client#
dpdp-kit/postgres needs only a function that runs one query with $1, $2… parameters and returns its rows. That covers node-postgres, postgres.js, Kysely, Knex and Slonik.
import pg from "pg";
import { postgresData, postgresStore } from "dpdp-kit/postgres";
const pool = new pg.Pool();
const query = async (text: string, params: readonly unknown[]) => (await pool.query(text, [...params])).rows;
export const dpdp = createDpdp({ config, store: postgresStore(query), data: postgresData(query) });Create the tables by running DPDP_DDL, a plain SQL string exported by dpdp-kit/postgres (and dpdp-kit/drizzle). Pass postgresData(query, { schema }) a schema from prismaSchema() or drizzleSchema() to map names and cast values to column types.
Timestamps are read back as UTC text, so they come back exactly as written whatever the server's time zone (many Indian servers run in
Asia/Kolkata) and however the driver parses dates.
MongoDB and Mongoose#
import { createDpdp } from "dpdp-kit";
import { mongoData, mongooseSchema, mongoStore } from "dpdp-kit/mongodb";
// the official driver
const dpdp = createDpdp({ config, store: mongoStore(client.db()), data: mongoData(client.db(), { ObjectId }) });
// Mongoose: a function, so dpdp can be created before mongoose.connect()
const db = () => mongoose.connection.db;
const dpdp = createDpdp({
config,
store: mongoStore(db),
data: mongoData(db, { schema: mongooseSchema(mongoose), ObjectId: mongoose.Types.ObjectId }),
});- Records go into
dpdp_*collections. The store creates their indexes before its first write, including the unique(subjectId, seq)index that keeps each consent chain from forking. CallcreateMongoIndexes(db)from a deploy script if the app's user can't create indexes. - In the data map,
tableis a collection, or a Mongoose model name withschema. Fields are document paths, so"address.city"works. With a Mongoose schema,idmeans_id. - Pass
ObjectIdto find people by an ObjectId_id. Ids that look like numbers also match number fields. - Erasure removes a field (
$unset), or sets the field'serasereplacement. dpdp-kit audit --schema src/models.tsreads the Mongoose models a file exports.
Anything else#
Implement DpdpStore:
- consent events:
lastEvent,insert,events - requests and tasks: see
RequestStoreandTaskStore - the access log:
insertAccess,listAccess,pruneAccess(seeAccessLogStore) - breaches: see
BreachStore.updateBreachmust save only over the version that was read (the stored history is one entry shorter) and returnfalseotherwise, so changes made at once aren't lost. - optionally
accessSubjects, the distinct people in matching access-log entries, soaddAffectedFromAccessLogdoesn't load every entry
insert must throw SeqConflictError on a duplicate (subjectId, seq), and claimTask must be atomic. A store with only the three consent methods still works for consent; rights and retention then throw store-unsupported.
Implement DataAccess (read, eraseFields, deleteRows) for another database. eraseFields receives the replacement for each column (from each field's erase setting); a column without one becomes null. Add updateFields(table, subject, subjectId, values, where?) to support rights.correct().
For tests#
memoryStore() and memoryData() keep everything in memory:
import { createDpdp, memoryData, memoryStore } from "dpdp-kit";
const dpdp = createDpdp({ config, store: memoryStore(), data: memoryData() });