Skip to content

Commit

Permalink
fix: dataSourcePriority missing default value (#4061)
Browse files Browse the repository at this point in the history
(cherry picked from commit 70c569f)
  • Loading branch information
czy88840616 committed Sep 22, 2024
1 parent 57cfe16 commit f2fef8a
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 3 deletions.
2 changes: 1 addition & 1 deletion packages/core/src/common/dataSourceManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export abstract class DataSourceManager<
protected options: DataSourceManagerConfigOption<ConnectionOpts> = {};
protected modelMapping = new WeakMap();
private innerDefaultDataSourceName: string;
protected dataSourcePriority: Record<string, string>;
protected dataSourcePriority: Record<string, string> = {};

@Inject()
protected appDir: string;
Expand Down
2 changes: 1 addition & 1 deletion packages/mongoose/src/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export class MongooseConfiguration implements ILifeCycle {
let clientName: any;
for (const name of clientNames) {
if (
(await this.mongooseDataSourceManager.isConnected(name)) &&
!(await this.mongooseDataSourceManager.isConnected(name)) &&
!this.mongooseDataSourceManager.isLowPriority(name)
) {
clientName = name;
Expand Down
30 changes: 29 additions & 1 deletion packages/mongoose/test/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { close, createLightApp } from '@midwayjs/mock';
import { join } from 'path';
import { Schema } from 'mongoose';
import { MongooseConnectionServiceFactory, MongooseConnectionService } from '../src';
import { MongooseConnectionServiceFactory, MongooseConnectionService } from "../src";
import * as mongoose from '../src';
import { MidwayHealthService } from '@midwayjs/core';

interface User {
name: string;
Expand Down Expand Up @@ -59,4 +61,30 @@ describe('/test/index.test.ts', () => {
await service.init();
}).rejects.toThrowError(/instance not found/);
});

it("should test health check", async () => {
const app = await createLightApp({
imports: [
mongoose
],
globalConfig: {
mongoose: {
dataSource: {
default: {
uri: 'mongodb://a.b.c:27017/test',
options: {
serverSelectionTimeoutMS: 100,
},
}
}
}
},
});

const healthService = await app.getApplicationContext().getAsync(MidwayHealthService);
const re = await healthService.getStatus();
expect(re.status).toBe(false);
expect(re.reason).toMatch("is not ready");
await close(app);
});
});

0 comments on commit f2fef8a

Please sign in to comment.