Introduction to Pongo
Pongo is an innovative tool that brings the familiar functionality of MongoDB to the robust and highly consistent environment of PostgreSQL. By leveraging PostgreSQL's JSONB support, Pongo enables users to enjoy the best of both worlds: the flexibility and ease of use of MongoDB with the performance and consistency of PostgreSQL.
Getting Started with Pongo
To get started with Pongo, developers can easily install it as an npm module and integrate it into their existing Node.js projects. Below is a simple command to install Pongo:
npm install @event-driven-io/pongo
For a deeper understanding and contextual framework, users are encouraged to explore the introduction article available on the event-driven blog.
Using Pongo
Pongo allows developers to interact with PostgreSQL as if it were a document database like MongoDB. The syntax is intuitive and requires explicit typing about the structure of data being manipulated. Here is a brief example to illustrate its use:
import { pongoClient, ObjectId } from "@event-driven-io/pongo";
type User = { name: string; age: number };
const connectionString = "postgresql://YOUR_CONNECTION_DETAILS";
const pongo = pongoClient(connectionString);
const pongoDb = pongo.db();
const users = pongoDb.collection<User>("users");
const newUser = { name: "Alice", age: 27 };
await users.insertOne(newUser);
const foundUser = await users.findOne({ name: "Alice" });
Pongo also provides a MongoDB compliant shim, allowing users to leverage existing MongoDB code with minimal changes.
How Pongo Works
Pongo functions by utilizing PostgreSQL’s JSONB data type, storing JSON data in a binary format. This approach enhances performance compared to traditional JSON storage methods and enables efficient querying capabilities similar to that of MongoDB.
Pongo translates typical MongoDB commands into equivalent PostgreSQL queries. For instance, when updating data in MongoDB syntax:
await users.updateOne({ _id: someId }, { $push: { tags: "character" } });
Pongo converts it to a PostgreSQL SQL command:
UPDATE "users"
SET data = jsonb_set(data, '{tags}', (COALESCE(data->'tags', '[]'::jsonb) || to_jsonb('character')))
WHERE _id = 'someId';
Why Choose Pongo?
Pongo offers several appealing advantages over traditional MongoDB setups, such as:
- Strong Consistency: By utilizing PostgreSQL, Pongo ensures ACID compliance, offering robust transactional integrity.
- Integration: Benefit from seamless compatibility with existing systems that use PostgreSQL.
- Ease of Use: Maintain the familiarity of MongoDB's API syntax while leveraging PostgreSQL's powerful capabilities.
- Performance: Superior performance through efficient JSONB indexing and querying.
- Advanced Features: Access PostgreSQL’s advanced features like partitioning and logical replication.
Pongo vs. Other Solutions
While tools like FerretDB also aim to bridge MongoDB and PostgreSQL, Pongo uniquely translates MongoDB API calls directly into SQL queries without the need for additional proxies. This approach supports serverless integrations and shared resource utilization for PostgreSQL-based tools.
Is Pongo Ready for Your Project?
Pongo is in active development. Although it is not yet fully compliant with all MongoDB features, it is safe to use in projects that don't rely on specific MongoDB capabilities not yet implemented in Pongo.
Community and Contribution
Pongo is a community-driven project. Contributions in the form of code, feature requests, and feedback are highly encouraged. The community strives to adopt inclusive behavior as defined by the Contributor Covenant Code of Conduct. Furthermore, sponsorships are welcomed to expedite development and bring more features to life.
By choosing Pongo, developers can simplify their data handling processes by combining the structural power of PostgreSQL with the flexibility of MongoDB syntax. Whether ensuring consistency, enhancing performance, or broadening integration capabilities, Pongo stands out as a powerful tool for modern database management.