Skip to content

Commit 568cb5f

Browse files
committed
Added sample typed client
1 parent b3eaf10 commit 568cb5f

File tree

4 files changed

+91
-38
lines changed

4 files changed

+91
-38
lines changed

samples/simple-ts/package-lock.json

Lines changed: 39 additions & 37 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

samples/simple-ts/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"build:ts:watch": "tsc --watch",
1111
"start": "tsx ./src/index.ts",
1212
"start:shim": "tsx ./src/shim.ts",
13+
"start:typed": "tsx ./src/typedClient.ts",
1314
"lint": "npm run lint:eslint && npm run lint:prettier",
1415
"lint:prettier": "prettier --check \"**/**/!(*.d).{ts,json,md}\"",
1516
"lint:eslint": "eslint '**/*.ts'",
@@ -60,6 +61,6 @@
6061
"dist"
6162
],
6263
"dependencies": {
63-
"@event-driven-io/pongo": "0.12.4"
64+
"@event-driven-io/pongo": "0.14.4"
6465
}
6566
}

samples/simple-ts/src/pongo.config.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { pongoSchema } from '@event-driven-io/pongo';
2+
3+
export type User = { _id?: string; name: string; age: number };
4+
5+
export default {
6+
schema: pongoSchema.client({
7+
database: pongoSchema.db({
8+
users: pongoSchema.collection<User>('users'),
9+
}),
10+
}),
11+
};

samples/simple-ts/src/typedClient.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { pongoClient } from '@event-driven-io/pongo';
2+
import { v4 as uuid } from 'uuid';
3+
import config from './pongo.config';
4+
5+
const connectionString =
6+
'postgresql://postgres:postgres@localhost:5432/postgres';
7+
8+
const pongo = pongoClient(connectionString, {
9+
schema: { definition: config.schema },
10+
});
11+
const pongoDb = pongo.database;
12+
13+
const users = pongoDb.users;
14+
const roger = { name: 'Roger', age: 30 };
15+
const anita = { name: 'Anita', age: 25 };
16+
const cruella = { _id: uuid(), name: 'Cruella', age: 40 };
17+
18+
// Inserting
19+
await users.insertOne(roger);
20+
await users.insertOne(cruella);
21+
22+
const { insertedId } = await users.insertOne(anita);
23+
const anitaId = insertedId!;
24+
25+
// Updating
26+
await users.updateOne({ _id: anitaId }, { $set: { age: 31 } });
27+
28+
// Deleting
29+
await users.deleteOne({ _id: cruella._id });
30+
31+
// Finding by Id
32+
const anitaFromDb = await users.findOne({ _id: anitaId });
33+
console.log(JSON.stringify(anitaFromDb));
34+
35+
// Finding more
36+
const usersFromDB = await users.find({ age: { $lt: 40 } });
37+
console.log(JSON.stringify(usersFromDB));
38+
39+
await pongo.close();

0 commit comments

Comments
 (0)