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

fix: adjust promise on open DB #36

Merged
merged 3 commits into from
Aug 23, 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
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,4 @@ jobs:
MONGO_INITDB_ROOT_PASSWORD: ${{secrets.MONGO_INITDB_ROOT_PASSWORD}}
MONGO_INITDB_DATABASE: ${{vars.MONGO_INITDB_DATABASE}}
- name:
run: docker-compose up -d
run: docker compose up -d
10 changes: 9 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ services:
hostname: mongodb7
networks:
- alpine_net
healthcheck:
test: echo 'db.runCommand("ping").ok' | mongosh 127.0.0.1:27017/test --quiet
start_period: 30s
start_interval: 30s
interval: 5s
timeout: 30s
retries: 50
deploy:
resources:
limits:
Expand All @@ -31,7 +38,8 @@ services:
ports:
- 3000:3000
depends_on:
- mongodb7
mongodb7:
condition: service_healthy
networks:
- alpine_net

Expand Down
11 changes: 6 additions & 5 deletions src/client-connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,14 @@ export class ClientConnection {
return (MConnClient: MongoClient) => MConnClient;
}

async connectOpen(dbName: string) {
await this.client.connect();
this.dbInstance = await this.client.db(dbName);
return this.dbInstance;
async openConnect(dbName: string) {
return this.client.connect().then(() => {
this.dbInstance = this.client.db(dbName);
return this.dbInstance;
});
}

async collection(collectionName: string) {
collection(collectionName: string) {
return this.dbInstance.collection(collectionName);
}
}
7 changes: 5 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,11 @@ export class SimpleConnection extends ClientConnection {
this.dbName = dbName;
}

async open() {
open() {
this.client = new MongoClient(this.connectionString);
await super.connectOpen(this.dbName);
return new Promise((resolve, reject) => {
super.openConnect(this.dbName)
.then((db) => resolve(db));
});
}
}
8 changes: 4 additions & 4 deletions tests/unit/client-connection.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,18 @@ describe("Unit Test for ClientConnection", () => {
});

it("expect connectOpen calls client connect", async () => {
await db.connectOpen("test");
await db.openConnect('test');
expect(dbMethodsStub.connect.called).to.be.true;
});

it("expect connectOpen calls client db", async () => {
await db.connectOpen("test");
await db.openConnect('test');
expect(dbMethodsStub.db.called).to.be.true;
});

it("expect collection calls dbInstance collection", async () => {
await db.connectOpen("test");
await db.collection("test");
const dbInstance = await db.openConnect('test');
dbInstance.collection('test');
expect(collectionStub.collection.called).to.be.true;
});
});
Loading