Skip to content

Commit 04812a7

Browse files
committed
Added more context to how Pongo works
1 parent 1514fa3 commit 04812a7

File tree

1 file changed

+50
-4
lines changed

1 file changed

+50
-4
lines changed

README.md

+50-4
Original file line numberDiff line numberDiff line change
@@ -109,16 +109,56 @@ CREATE TABLE IF NOT EXISTS "YourCollectionName" (
109109
)
110110
```
111111

112-
**The binary format of JSONB means that data is pre-parsed, allowing faster read and write operations than text-based JSON.** You don't have to re-parse the data every time you query it, which saves processing time and improves overall performance. Additionally, JSONB supports advanced indexing options like GIN and GiST indexes, making searches within JSONB documents much quicker and more efficient.
112+
**Essentially Pongo takes MongoDB api and translates it to the native PostgreSQL queries.** It is a similar concept to [Marten](https://martendb.io/) and AWS DocumentDB (see [here](https://www.enterprisedb.com/blog/documentdb-really-postgresql) or [there](https://news.ycombinator.com/item?id=18870397), they seem to be using Mongo syntactic sugar on top of AuroraDB with Postgres).
113+
114+
**E.g. the MongoDB update syntax:**
115+
116+
```typescript
117+
const pongoCollection = pongoDb.collection<User>("users");
118+
119+
await pongoCollection.updateOne(
120+
{ _id: someId },
121+
{ $push: { tags: "character" } }
122+
);
123+
```
124+
125+
will be translated to:
126+
127+
```sql
128+
UPDATE "users"
129+
SET data = jsonb_set(data, '{tags}', (COALESCE(data->'tags', '[]'::jsonb) || to_jsonb('character')))
130+
WHERE _id = '137ef052-e41c-428b-b606-1c8070a47eda';
131+
```
132+
133+
**Or for query:**
134+
135+
```typescript
136+
const result = await pongoCollection
137+
.find({ "address.history": { $elemMatch: { street: "Elm St" } } })
138+
.toArray();
139+
```
140+
141+
will result in:
142+
143+
```sql
144+
SELECT data
145+
FROM "users"
146+
WHERE jsonb_path_exists(
147+
data,
148+
'$.address.history[*] ? (@.street == "Elm St")'
149+
);
150+
```
151+
152+
### Storage
153+
154+
**The binary format of PostgreSQL JSONB means that data is pre-parsed, allowing faster read and write operations than text-based JSON.** You don't have to re-parse the data every time you query it, which saves processing time and improves overall performance. Additionally, JSONB supports advanced indexing options like GIN and GiST indexes, making searches within JSONB documents much quicker and more efficient.
113155

114156
Moreover, JSONB retains the flexibility of storing semi-structured data while allowing you to use PostgreSQL's robust querying capabilities. You can perform complex queries, joins, and transactions with JSONB data, just as you can with regular relational data.
115157

116158
**Contrary to common belief, JSON document data is structured.** JSON has structure, but it is not enforced for each document. We can easily extend the schema for our documents, even for specific ones, by adding new fields. We should also not fail if the field we expect to exist, but doesn't.
117159

118160
This flexibility, performance, and consistency combination makes PostgreSQL with JSONB a powerful tool. There are benchmarks showing that it can be even faster than MongoDB.
119161

120-
Pongo is a similar concept to [Marten](https://martendb.io/) and AWS DocumentDB (see [here](https://www.enterprisedb.com/blog/documentdb-really-postgresql) or [there](https://news.ycombinator.com/item?id=18870397), they seem to be using Mongo syntactic sugar on top of AuroraDB with Postgres).
121-
122162
Check more in:
123163

124164
- [JSON Types Documentation](https://www.postgresql.org/docs/current/datatype-json.html)
@@ -127,6 +167,12 @@ Check more in:
127167
- [MongoDB vs PostgreSQL JSONB Benchmark](https://info.enterprisedb.com/rs/069-ALB-339/images/PostgreSQL_MongoDB_Benchmark-WhitepaperFinal.pdf)
128168
- [How to JSON in PostgreSQL](https://ftisiot.net/postgresqljson/main/)
129169

170+
## Is Pongo an ORM?
171+
172+
It's not. It's focused on effective handling of the document data specifics. Node.js ORMs have capabilites to handle JSONB, e.g. DrizzleORM has a good support for that for basic operations. Yet, they're all but limited to querying, usually for advanced ones you need to fallback to JSONPath or JSONB functions (so raw SQL). As you saw above, this syntax is not super pleasant to deal with. That's why Pongo aims to do it for you.
173+
130174
## Is it production ready?
131175

132-
What's there it's safe to use, but it's far from being 100% compliant with MongoDB.
176+
What's there it's safe to use, but it's far from being 100% compliant with MongoDB. Pongo is a fresh project, so some stuff can be missing.
177+
178+
Pongo is a community project, so once you find something, we encourage you to send us a GH issue or Pull Request extending the support or test coverage!

0 commit comments

Comments
 (0)