Add persistence
You will add persistent data to a Putnami app.
Putnami splits persistence by data shape:
- use
@putnami/databasefor relational PostgreSQL data, joins, and migrations - use
@putnami/documentfor document-shaped NoSQL data with portable repositories and cursor pagination - use
@putnami/storagefor files and blobs
This guide walks through the PostgreSQL path. If your feature is document-shaped instead, see the Document storage reference.
Steps
The PostgreSQL path is steps 2 to 5 of
Build a Postgres API in TypeScript: add @putnami/database, declare a
table, its repository and its migration, enable sql({ autoApply: true }),
and read and write from routes. The rest of that page tests the API against a
real database and packages it as an image.
Result
You now have an app that persists data.
Companion sample:
typescript/samples/06-database— a runnable project with repository pattern, migrations, and transactions. Runputnami serve @example/databasefrom the workspace root.
Document storage quick start
If you need document storage instead of PostgreSQL, the equivalent setup uses @putnami/document:
import { Collection, DocumentId, Field, Repository } from '@putnami/document';
export const UsersCollection = Collection(
'users',
{
id: DocumentId(String),
email: Field(String),
name: Field(String),
},
{
indexes: [{ fields: ['email'] }],
},
);
export class UserRepository extends Repository<typeof UsersCollection> {
constructor() {
super(UsersCollection);
}
}import { application } from '@putnami/application';
import { document } from '@putnami/document';
export const app = () => application().use(document());document:
backend: memoryYou now have the matching document-storage path when relational tables are not the right fit.