postgres
@webda/postgres
PostgreSQL-backed Store for Webda — persists model instances as JSONB rows in Postgres with automatic table creation, view generation, and full WebdaQL query support.
When to use it
- You want a relational database backing store for Webda models with ACID guarantees.
- You are already running PostgreSQL and want Webda models to live alongside existing tables.
- You need to query across models using familiar SQL tooling while keeping the Webda model API.
Install
pnpm add @webda/postgres
Configuration
{
"services": {
"postStore": {
"type": "PostgresStore",
"model": "MyApp/Post",
"postgresqlServer": {
"host": "localhost",
"port": 5432,
"database": "myapp",
"user": "webda",
"password": "${POSTGRES_PASSWORD}"
},
"autoCreateTable": true
}
}
}
Or rely on libpq environment variables (PGHOST, PGPORT, PGDATABASE, PGUSER, PGPASSWORD):
{
"services": {
"postStore": {
"type": "PostgresStore",
"model": "MyApp/Post",
"autoCreateTable": true
}
}
}
| Parameter | Type | Default | Required | Description |
|---|---|---|---|---|
postgresqlServer | ClientConfig | PoolConfig | libpq env vars | No | PostgreSQL connection config (host, port, database, user, password) |
usePool | boolean | false | No | Use a connection pool (pg.Pool) instead of a single client |
autoCreateTable | boolean | true | No | Automatically create the JSONB table if it does not exist |
viewPrefix | string | "" | No | Prefix for auto-generated SQL views |
views | string[] | ["regex:.*"] | No | Regexp list of model names to generate views for |
Usage
import { CoreModel, Model, Expose } from "@webda/core";
@Model()
export class Post extends CoreModel {
@Expose() title: string;
@Expose() content: string;
@Expose() publishedAt: Date;
}
// With PostgresStore configured, the standard Webda model API is backed by Postgres:
const post = new Post();
post.title = "Hello Postgres";
await post.save(); // INSERT INTO posts ...
const found = await Post.ref(post.uuid).get(); // SELECT ... WHERE uuid = ?
const results = await Post.query("title CONTAINS 'Hello'"); // filtered query
// The table schema uses JSONB:
// CREATE TABLE IF NOT EXISTS posts (
// uuid TEXT PRIMARY KEY,
// data JSONB NOT NULL
// );
Reference
- API reference: see the auto-generated typedoc at
docs/pages/Modules/postgres/. - Source:
packages/postgres - Related:
@webda/mongodbfor a document-store alternative;@webda/awsforDynamoStore;@webda/corefor theStorebase class.
Classes
- PostgresParameters
- PostgresPubSubParameters
- PostgresPubSubService
- PostgresQueueParameters
- PostgresQueueService
- PostgresRepository
- PostgresStore
- SQLComparisonExpression
- SQLStore
- SQLStoreParameters