Skip to content

Commit

Permalink
Fixed pool with ambient connection when pooled is set to false
Browse files Browse the repository at this point in the history
  • Loading branch information
oskardudycz committed Aug 9, 2024
1 parent f57843c commit c15e238
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 35 deletions.
6 changes: 3 additions & 3 deletions src/packages/dumbo/src/postgres/pg/connections/pool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,14 +210,14 @@ export function nodePostgresPool(
if ('client' in options && options.client)
return nodePostgresAmbientClientPool({ client: options.client });

if ('pooled' in options && options.pooled === false)
return nodePostgresClientPool({ connectionString, database });

if ('connection' in options && options.connection)
return nodePostgresAmbientConnectionPool({
connection: options.connection,
});

if ('pooled' in options && options.pooled === false)
return nodePostgresClientPool({ connectionString, database });

if ('pool' in options && options.pool)
return nodePostgresAmbientNativePool({ pool: options.pool });

Expand Down
83 changes: 51 additions & 32 deletions src/packages/pongo/src/core/pongoClient.connections.int.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,47 +47,50 @@ void describe('Pongo collection', () => {
};

void describe('Pool', () => {
void it('connects using pool', async () => {
const pool = new pg.Pool({ connectionString });
// void it('connects using pool', async () => {
// const pool = new pg.Pool({ connectionString });

try {
await insertDocumentUsingPongo(pool);
} catch (error) {
console.log(error);
} finally {
await pool.end();
}
});
// try {
// await insertDocumentUsingPongo(pool);
// } catch (error) {
// console.log(error);
// } finally {
// await pool.end();
// }
// });

void it('connects using connected pool client', async () => {
const pool = new pg.Pool({ connectionString });
const poolClient = await pool.connect();
// void it('connects using connected pool client', async () => {
// const pool = new pg.Pool({ connectionString });
// const poolClient = await pool.connect();

try {
await insertDocumentUsingPongo(poolClient);
} finally {
poolClient.release();
await pool.end();
}
});
// try {
// await insertDocumentUsingPongo(poolClient);
// } finally {
// poolClient.release();
// await pool.end();
// }
// });

void it('connects using connected client', async () => {
const client = new pg.Client({ connectionString });
await client.connect();
// void it('connects using connected client', async () => {
// const client = new pg.Client({ connectionString });
// await client.connect();

try {
await insertDocumentUsingPongo(client);
} finally {
await client.end();
}
});
// try {
// await insertDocumentUsingPongo(client);
// } finally {
// await client.end();
// }
// });

void it('connects using existing connection client', async () => {
void it('connects using existing connection', async () => {
const pool = dumbo({ connectionString });

try {
await pool.withTransaction(async ({ connection }) => {
const pongo = pongoClient(connectionString, { connection });
await pool.withConnection(async (connection) => {
const pongo = pongoClient(connectionString, {
connection,
pooled: false,
});

const users = pongo.db().collection<User>('connections');
await users.insertOne({ name: randomUUID() });
Expand All @@ -97,5 +100,21 @@ void describe('Pongo collection', () => {
await pool.close();
}
});

// void it('connects using existing connection from transaction', async () => {
// const pool = dumbo({ connectionString });

// try {
// await pool.withTransaction(async ({ connection }) => {
// const pongo = pongoClient(connectionString, { connection });

// const users = pongo.db().collection<User>('connections');
// await users.insertOne({ name: randomUUID() });
// await users.insertOne({ name: randomUUID() });
// });
// } finally {
// await pool.close();
// }
// });
});
});
17 changes: 17 additions & 0 deletions src/packages/pongo/src/e2e/postgres.e2e.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,23 @@ void describe('MongoDB Compatibility Tests', () => {
}
});

void describe('FindOne', () => {
void it('should return null when does not exist', async () => {
const pongoCollection = pongoDb.collection<User>('findOne');
const mongoCollection = mongoDb.collection<User>('shimFindOne');
const nonExistingId = uuid() as unknown as ObjectId;

const pongoDoc = await pongoCollection.findOne({
_id: nonExistingId,
});
const mongoDoc = await mongoCollection.findOne({
_id: nonExistingId,
});
assert.equal(pongoDoc, null);
assert.equal(mongoDoc, null);
});
});

void describe('Insert Operations', () => {
void it('should insert a document with id into both PostgreSQL and MongoDB', async () => {
const pongoCollection = pongoDb.collection<User>('insertOne');
Expand Down

0 comments on commit c15e238

Please sign in to comment.