Skip to content

Cleaned up connection setup by wrapping it with factory method #54

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

Merged
merged 8 commits into from
Aug 7, 2024

Conversation

oskardudycz
Copy link
Contributor

@oskardudycz oskardudycz commented Aug 6, 2024

Fixes #53

Now, it should be easier to implement new ones.

Added also useful helpers that should make the Dumbo usable even as a standalone too. You can do the following:

import { dumbo, rawSql } from '@event-driven-io/dumbo';

const pool = dumbo({ connectionString });

const result = await pool.execute.query<User>(rawSql('SELECT * from users'));

or with transactions:

import { dumbo, sql } from '@event-driven-io/dumbo';

const userId = 32;
const userName = "test@test.com";
const pool = dumbo({ connectionString });

await pool.withTransaction(async ({ execute }) => {
  await execute.command(sql('INSERT INTO users VALUES(%s, %L)', userId, userName));
  await execute.command(sql('INSERT INTO user_roles VALUES(%s, %L)', userId, 'admin') );
})

You can now also share the connection and transaction between your tool and Pongo, e.g. by doing:

import { dumbo, sql } from '@event-driven-io/dumbo';

const userId = 32;
const userName = "test@test.com";
const pool = dumbo({ connectionString });

await pool.withTransaction(async ({ execute }) => {
  // sharing transaction connection with pongo
  const pongo = pongoClient(connectionString, { connection });
  
  const users = pongo.db().collection<User>('users');
  await users.insertOne({ name: randomUUID() });
  await users.insertOne({ name: randomUUID() });

  // doing also insert outside of pongo
  await execute.command(sql('INSERT INTO user_roles VALUES(%s, %L)', userId, 'admin') );
})

That will be useful for sharing connection and its transaction e.g. between Emmett and Pongo
@oskardudycz oskardudycz force-pushed the connection_refactoring branch from 2cb65c9 to c26978d Compare August 7, 2024 07:22
@oskardudycz oskardudycz marked this pull request as ready for review August 7, 2024 08:27
@oskardudycz oskardudycz merged commit b6bd618 into main Aug 7, 2024
1 check passed
@oskardudycz oskardudycz deleted the connection_refactoring branch August 7, 2024 09:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Refactor pool, connection and transaction’s executor setup to use composable method instead of spread operator
1 participant