Skip to main content

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
}
}
}
ParameterTypeDefaultRequiredDescription
postgresqlServerClientConfig | PoolConfiglibpq env varsNoPostgreSQL connection config (host, port, database, user, password)
usePoolbooleanfalseNoUse a connection pool (pg.Pool) instead of a single client
autoCreateTablebooleantrueNoAutomatically create the JSONB table if it does not exist
viewPrefixstring""NoPrefix for auto-generated SQL views
viewsstring[]["regex:.*"]NoRegexp 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​

Classes​

Interfaces​