Skip to content

Commit

Permalink
Changed _id type to TEXT
Browse files Browse the repository at this point in the history
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
  • Loading branch information
oskardudycz committed Jul 12, 2024
1 parent 4bd5dea commit 37f4ae6
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 5 deletions.
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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()
)
```

Expand Down
10 changes: 8 additions & 2 deletions src/docs/getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -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()
)
```

Expand Down
11 changes: 10 additions & 1 deletion src/packages/pongo/src/postgres/postgresCollection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,16 @@ export const postgresCollection = <T extends PongoDocument>(
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: <T>(document: WithId<T>): SQL =>
Expand Down

0 comments on commit 37f4ae6

Please sign in to comment.