|
2 | 2 |
|
3 | 3 | 
|
4 | 4 |
|
5 |
| -TODO |
| 5 | +Pongo is a MongoDB on Postgres with all strong consistency benefits. |
| 6 | + |
| 7 | +Install Pongo as an npm module and save it to your package.json |
| 8 | + |
| 9 | +```bash |
| 10 | +npm install @event-driven-io/pongo |
| 11 | +``` |
| 12 | + |
| 13 | +## Example |
| 14 | + |
| 15 | +You can use Pongo syntax with explicit typing about supported syntax: |
| 16 | + |
| 17 | +```ts |
| 18 | +import { pongoClient } from '@event-driven-io/pongo'; |
| 19 | +import { v4 as uuid } from 'uuid'; |
| 20 | + |
| 21 | +type User = { name: string; age: number }; |
| 22 | + |
| 23 | +const connectionString = |
| 24 | + 'postgresql://dbuser:[email protected]:3211/mydb'; |
| 25 | + |
| 26 | +const pongoClient = pongoClient(postgresConnectionString); |
| 27 | +const pongoDb = pongoClient.db(); |
| 28 | + |
| 29 | +const users = pongoDb.collection<User>('users'); |
| 30 | +const roger = { name: 'Roger', age: 30 }; |
| 31 | +const anita = { name: 'Anita', age: 25 }; |
| 32 | +const cruella = { _id: uuid(), name: 'Cruella', age: 40 }; |
| 33 | + |
| 34 | +// Inserting |
| 35 | +await pongoCollection.insertOne(roger); |
| 36 | +await pongoCollection.insertOne(cruella); |
| 37 | + |
| 38 | +let inserted = await pongoCollection.insertOne(alice); |
| 39 | +const anitaId = inserted.insertedId; |
| 40 | + |
| 41 | +// Updating |
| 42 | +await users.updateOne({ _id: anitaId }, { $set: { age: 31 } }); |
| 43 | + |
| 44 | +// Deleting |
| 45 | +await pongoCollection.deleteOne({ _id: cruella._id }); |
| 46 | + |
| 47 | +// Finding by Id |
| 48 | +const anitaFromDb = await pongoCollection.findOne({ _id: anitaId }); |
| 49 | + |
| 50 | +// Finding more |
| 51 | +const users = await pongoCollection.find({ age: { $lt: 40 } }); |
| 52 | +``` |
| 53 | + |
| 54 | +Or use MongoDB compliant shim: |
| 55 | + |
| 56 | +```ts |
| 57 | +import { MongoClient } from '@event-driven-io/pongo'; |
| 58 | +import { v4 as uuid } from 'uuid'; |
| 59 | + |
| 60 | +type User = { name: string; age: number }; |
| 61 | + |
| 62 | +const connectionString = |
| 63 | + 'postgresql://dbuser:[email protected]:3211/mydb'; |
| 64 | + |
| 65 | +const pongoClient = new MongoClient(postgresConnectionString); |
| 66 | +const pongoDb = pongoClient.db(); |
| 67 | + |
| 68 | +const users = pongoDb.collection<User>('users'); |
| 69 | +const roger = { name: 'Roger', age: 30 }; |
| 70 | +const anita = { name: 'Anita', age: 25 }; |
| 71 | +const cruella = { _id: uuid(), name: 'Cruella', age: 40 }; |
| 72 | + |
| 73 | +// Inserting |
| 74 | +await pongoCollection.insertOne(roger); |
| 75 | +await pongoCollection.insertOne(cruella); |
| 76 | + |
| 77 | +let inserted = await pongoCollection.insertOne(alice); |
| 78 | +const anitaId = inserted.insertedId; |
| 79 | + |
| 80 | +// Updating |
| 81 | +await users.updateOne({ _id: anitaId }, { $set: { age: 31 } }); |
| 82 | + |
| 83 | +// Deleting |
| 84 | +await pongoCollection.deleteOne({ _id: cruella._id }); |
| 85 | + |
| 86 | +// Finding by Id |
| 87 | +const anitaFromDb = await pongoCollection.findOne({ _id: anitaId }); |
| 88 | + |
| 89 | +// Finding more |
| 90 | +const users = await pongoCollection.find({ age: { $lt: 40 } }).toArray(); |
| 91 | +``` |
| 92 | + |
| 93 | +## Is it production ready? |
| 94 | + |
| 95 | +What's there it's safe to use, but it's far from being 100% compliant with MongoDB. |
0 commit comments