Skip to content

Commit

Permalink
Added sample typed client
Browse files Browse the repository at this point in the history
  • Loading branch information
oskardudycz committed Sep 12, 2024
1 parent b3eaf10 commit 568cb5f
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 38 deletions.
76 changes: 39 additions & 37 deletions samples/simple-ts/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion samples/simple-ts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"build:ts:watch": "tsc --watch",
"start": "tsx ./src/index.ts",
"start:shim": "tsx ./src/shim.ts",
"start:typed": "tsx ./src/typedClient.ts",
"lint": "npm run lint:eslint && npm run lint:prettier",
"lint:prettier": "prettier --check \"**/**/!(*.d).{ts,json,md}\"",
"lint:eslint": "eslint '**/*.ts'",
Expand Down Expand Up @@ -60,6 +61,6 @@
"dist"
],
"dependencies": {
"@event-driven-io/pongo": "0.12.4"
"@event-driven-io/pongo": "0.14.4"
}
}
11 changes: 11 additions & 0 deletions samples/simple-ts/src/pongo.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { pongoSchema } from '@event-driven-io/pongo';

export type User = { _id?: string; name: string; age: number };

export default {
schema: pongoSchema.client({
database: pongoSchema.db({
users: pongoSchema.collection<User>('users'),
}),
}),
};
39 changes: 39 additions & 0 deletions samples/simple-ts/src/typedClient.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { pongoClient } from '@event-driven-io/pongo';
import { v4 as uuid } from 'uuid';
import config from './pongo.config';

const connectionString =
'postgresql://postgres:postgres@localhost:5432/postgres';

const pongo = pongoClient(connectionString, {
schema: { definition: config.schema },
});
const pongoDb = pongo.database;

const users = pongoDb.users;
const roger = { name: 'Roger', age: 30 };
const anita = { name: 'Anita', age: 25 };
const cruella = { _id: uuid(), name: 'Cruella', age: 40 };

// Inserting
await users.insertOne(roger);
await users.insertOne(cruella);

const { insertedId } = await users.insertOne(anita);
const anitaId = insertedId!;

// Updating
await users.updateOne({ _id: anitaId }, { $set: { age: 31 } });

// Deleting
await users.deleteOne({ _id: cruella._id });

// Finding by Id
const anitaFromDb = await users.findOne({ _id: anitaId });
console.log(JSON.stringify(anitaFromDb));

// Finding more
const usersFromDB = await users.find({ age: { $lt: 40 } });
console.log(JSON.stringify(usersFromDB));

await pongo.close();

0 comments on commit 568cb5f

Please sign in to comment.