Pongo - Mongo but on Postgres and with strong consistency benefits.
Install Pongo as an npm module and save it to your package.json:
npm install @event-driven-io/pongoRead also introduction article on my blog for more context.
You can use Pongo syntax with explicit typing about supported syntax:
import { pongoClient } from "@event-driven-io/pongo";
import { v4 as uuid } from "uuid";
type User = { name: string; age: number };
const connectionString =
"postgresql://dbuser:secretpassword@database.server.com:3211/mydb";
const pongoClient = pongoClient(postgresConnectionString);
const pongoDb = pongoClient.db();
const users = pongoDb.collection<User>("users");
const roger = { name: "Roger", age: 30 };
const anita = { name: "Anita", age: 25 };
const cruella = { _id: uuid(), name: "Cruella", age: 40 };
// Inserting
await pongoCollection.insertOne(roger);
await pongoCollection.insertOne(cruella);
const { insertedId } = await pongoCollection.insertOne(alice);
const anitaId = insertedId;
// Updating
await users.updateOne({ _id: anitaId }, { $set: { age: 31 } });
// Deleting
await pongoCollection.deleteOne({ _id: cruella._id });
// Finding by Id
const anitaFromDb = await pongoCollection.findOne({ _id: anitaId });
// Finding more
const users = await pongoCollection.find({ age: { $lt: 40 } });Or use MongoDB compliant shim:
import { MongoClient } from "@event-driven-io/pongo";
import { v4 as uuid } from "uuid";
type User = { name: string; age: number };
const connectionString =
"postgresql://dbuser:secretpassword@database.server.com:3211/mydb";
const pongoClient = new MongoClient(postgresConnectionString);
const pongoDb = pongoClient.db();
const users = pongoDb.collection<User>("users");
const roger = { name: "Roger", age: 30 };
const anita = { name: "Anita", age: 25 };
const cruella = { _id: uuid(), name: "Cruella", age: 40 };
// Inserting
await pongoCollection.insertOne(roger);
await pongoCollection.insertOne(cruella);
const { insertedId } = await pongoCollection.insertOne(alice);
const anitaId = insertedId;
// Updating
await users.updateOne({ _id: anitaId }, { $set: { age: 31 } });
// Deleting
await pongoCollection.deleteOne({ _id: cruella._id });
// Finding by Id
const anitaFromDb = await pongoCollection.findOne({ _id: anitaId });
// Finding more
const users = await pongoCollection.find({ age: { $lt: 40 } }).toArray();What's there it's safe to use, but it's far from being 100% compliant with MongoDB.
