Skip to content

Commit c3ed330

Browse files
committed
Added sample typed client
1 parent b3eaf10 commit c3ed330

File tree

5 files changed

+99
-40
lines changed

5 files changed

+99
-40
lines changed

samples/simple-ts/package-lock.json

+39-37
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

samples/simple-ts/package.json

+4-2
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'",
@@ -23,7 +24,8 @@
2324
"test:watch": "run-p test:unit:watch test:int:watch test:e2e:watch",
2425
"test:unit:watch": "glob -d -c \"node --import tsx --test --watch\" **/*.unit.spec.ts",
2526
"test:int:watch": "glob -d -c \"node --import tsx --test --watch\" **/*.int.spec.ts",
26-
"test:e2e:watch": "glob -d -c \"node --import tsx --test --watch\" **/*.e2e.spec.ts"
27+
"test:e2e:watch": "glob -d -c \"node --import tsx --test --watch\" **/*.e2e.spec.ts",
28+
"migrate": "pongo migrate run --config ./dist/pongo.config.js --connectionString postgresql://postgres:postgres@localhost:5432/postgres"
2729
},
2830
"repository": {
2931
"type": "git",
@@ -60,6 +62,6 @@
6062
"dist"
6163
],
6264
"dependencies": {
63-
"@event-driven-io/pongo": "0.12.4"
65+
"@event-driven-io/pongo": "0.14.4"
6466
}
6567
}

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

+11
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

+39
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, autoMigration: 'CreateOrUpdate' },
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();

samples/simple-ts/tsup.config.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,12 @@ export default defineConfig({
1010
skipNodeModulesBundle: true,
1111
target: 'esnext',
1212
outDir: 'dist', //env === 'production' ? 'dist' : 'lib',
13-
entry: ['src/index.ts'],
13+
entry: [
14+
'src/index.ts',
15+
'src/pongo.config.ts',
16+
'src/typedClient.ts',
17+
'src/shim.ts',
18+
],
1419
sourcemap: true,
1520
tsconfig: 'tsconfig.json', // workaround for https://github.com/egoist/tsup/issues/571#issuecomment-1760052931
1621
});

0 commit comments

Comments
 (0)