From 37f4ae6bdd8a9efa6b532bdbc24cdb8a1829a3e7 Mon Sep 17 00:00:00 2001 From: Oskar Dudycz Date: Fri, 12 Jul 2024 17:30:38 +0200 Subject: [PATCH] Changed _id type to TEXT Changed _id type to Text. In PostgreSQL this should be almost equally the same indexable. Of course it'll take a bit more storage, but let's live with that for now. We can in the future allow as Marten does to use different definition of collection table. Changing from uuid to text will allow more sophisticated key strategies. Most importantly it'll reuse the stream id as document id for Emmett projections. Besides that added columns for future concepts: - _version - for optimistic concurrency handling, - _partition - for built-in PostgreSQL partitioning, - _created, _modified - for timestamp metadata, - metadata - for technical additional data, - _archived - for soft deletion and also partitioning --- README.md | 10 ++++++++-- src/docs/getting-started.md | 10 ++++++++-- src/packages/pongo/src/postgres/postgresCollection.ts | 11 ++++++++++- 3 files changed, 26 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index d1f61c6..17c8096 100644 --- a/README.md +++ b/README.md @@ -104,8 +104,14 @@ Pongo uses the following table structure for storing collections: ```sql CREATE TABLE IF NOT EXISTS "YourCollectionName" ( - _id UUID PRIMARY KEY, - data JSONB + _id TEXT PRIMARY KEY, + data JSONB NOT NULL, + metadata JSONB NOT NULL DEFAULT '{}', + _version BIGINT NOT NULL DEFAULT 1, + _partition TEXT NOT NULL DEFAULT 'png_global', + _archived BOOLEAN NOT NULL DEFAULT FALSE, + _created TIMESTAMPTZ NOT NULL DEFAULT now(), + _updated TIMESTAMPTZ NOT NULL DEFAULT now() ) ``` diff --git a/src/docs/getting-started.md b/src/docs/getting-started.md index 24eb8a1..a728f08 100644 --- a/src/docs/getting-started.md +++ b/src/docs/getting-started.md @@ -102,8 +102,14 @@ Pongo uses the following table structure for storing collections: ```sql CREATE TABLE IF NOT EXISTS "YourCollectionName" ( - _id UUID PRIMARY KEY, - data JSONB + _id TEXT PRIMARY KEY, + data JSONB NOT NULL, + metadata JSONB NOT NULL DEFAULT '{}', + _version BIGINT NOT NULL DEFAULT 1, + _partition TEXT NOT NULL DEFAULT 'png_global', + _archived BOOLEAN NOT NULL DEFAULT FALSE, + _created TIMESTAMPTZ NOT NULL DEFAULT now(), + _updated TIMESTAMPTZ NOT NULL DEFAULT now() ) ``` diff --git a/src/packages/pongo/src/postgres/postgresCollection.ts b/src/packages/pongo/src/postgres/postgresCollection.ts index 86878b1..1e60d40 100644 --- a/src/packages/pongo/src/postgres/postgresCollection.ts +++ b/src/packages/pongo/src/postgres/postgresCollection.ts @@ -179,7 +179,16 @@ export const postgresCollection = ( export const collectionSQLBuilder = (collectionName: string) => ({ createCollection: (): SQL => sql( - 'CREATE TABLE IF NOT EXISTS %I (_id UUID PRIMARY KEY, data JSONB)', + `CREATE TABLE IF NOT EXISTS %I ( + _id TEXT PRIMARY KEY, + data JSONB NOT NULL, + metadata JSONB NOT NULL DEFAULT '{}', + _version BIGINT NOT NULL DEFAULT 1, + _partition TEXT NOT NULL DEFAULT 'png_global', + _archived BOOLEAN NOT NULL DEFAULT FALSE, + _created TIMESTAMPTZ NOT NULL DEFAULT now(), + _updated TIMESTAMPTZ NOT NULL DEFAULT now() + )`, collectionName, ), insertOne: (document: WithId): SQL =>