Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added sample of typed client #83

Merged
merged 1 commit into from
Sep 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.

6 changes: 4 additions & 2 deletions 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 All @@ -23,7 +24,8 @@
"test:watch": "run-p test:unit:watch test:int:watch test:e2e:watch",
"test:unit:watch": "glob -d -c \"node --import tsx --test --watch\" **/*.unit.spec.ts",
"test:int:watch": "glob -d -c \"node --import tsx --test --watch\" **/*.int.spec.ts",
"test:e2e:watch": "glob -d -c \"node --import tsx --test --watch\" **/*.e2e.spec.ts"
"test:e2e:watch": "glob -d -c \"node --import tsx --test --watch\" **/*.e2e.spec.ts",
"migrate": "pongo migrate run --config ./dist/pongo.config.js --connectionString postgresql://postgres:postgres@localhost:5432/postgres"
},
"repository": {
"type": "git",
Expand Down Expand Up @@ -60,6 +62,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, autoMigration: 'CreateOrUpdate' },
});
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();
7 changes: 6 additions & 1 deletion samples/simple-ts/tsup.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,12 @@ export default defineConfig({
skipNodeModulesBundle: true,
target: 'esnext',
outDir: 'dist', //env === 'production' ? 'dist' : 'lib',
entry: ['src/index.ts'],
entry: [
'src/index.ts',
'src/pongo.config.ts',
'src/typedClient.ts',
'src/shim.ts',
],
sourcemap: true,
tsconfig: 'tsconfig.json', // workaround for https://github.com/egoist/tsup/issues/571#issuecomment-1760052931
});