-
-
Notifications
You must be signed in to change notification settings - Fork 285
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
20 changed files
with
1,527 additions
and
1,284 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import {AfterRoutesInit, Constant, ExpressApplication, OnInit, Service} from "@tsed/common"; | ||
import * as Mongoose from "mongoose"; | ||
import {MDBConnection} from "./interfaces"; | ||
import {ValidationErrorMiddleware} from "./middlewares/ValidationErrorMiddleware"; | ||
import {MongooseService} from "./services/MongooseService"; | ||
|
||
@Service() | ||
export class MongooseModule implements OnInit, AfterRoutesInit { | ||
@Constant("mongoose.url") | ||
private url: string; | ||
|
||
@Constant("mongoose.connectionOptions") | ||
private connectionOptions: Mongoose.ConnectionOptions; | ||
|
||
@Constant("mongoose.urls") | ||
private urls: {[key: string]: MDBConnection}; | ||
|
||
/** | ||
* | ||
* @type {Map<any, any>} | ||
* @private | ||
*/ | ||
private _instances: Map<string, Mongoose.Mongoose> = new Map(); | ||
|
||
constructor(@ExpressApplication private expressApp: ExpressApplication, private mongooseService: MongooseService) {} | ||
|
||
$onInit(): Promise<any> | void { | ||
const promises: Promise<Mongoose.Mongoose>[] = []; | ||
|
||
if (this.url) { | ||
promises.push(this.mongooseService.connect("default", this.url, this.connectionOptions || {})); | ||
} | ||
|
||
if (this.urls) { | ||
Object.keys(this.urls).forEach((key: string) => { | ||
promises.push(this.mongooseService.connect(key, this.urls[key].url, this.urls[key].connectionOptions || {})); | ||
}); | ||
} | ||
|
||
return Promise.all(promises); | ||
} | ||
|
||
$afterRoutesInit(): void { | ||
this.expressApp.use(ValidationErrorMiddleware as any); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import {ServerSettingsService} from "@tsed/common"; | ||
import {inject, TestContext} from "@tsed/testing"; | ||
import * as Sinon from "sinon"; | ||
import {MongooseModule} from "../src"; | ||
|
||
describe("MongooseModule", () => { | ||
describe("$onInit()", () => { | ||
describe("when url is given", () => { | ||
before( | ||
inject([MongooseModule, ServerSettingsService], (mongooseModule: MongooseModule, serverSetttings: ServerSettingsService) => { | ||
this.mongooseModule = mongooseModule; | ||
|
||
serverSetttings.set("mongoose", { | ||
url: "mongodb://test", | ||
connectionOptions: {options: "options"} | ||
}); | ||
|
||
this.connectStub = Sinon.stub(this.mongooseModule.mongooseService, "connect").resolves("test" as any); | ||
|
||
return (this.result = mongooseModule.$onInit()); | ||
}) | ||
); | ||
|
||
after( | ||
inject([ServerSettingsService], (serverSetttings: ServerSettingsService) => { | ||
serverSetttings.set("mongoose", { | ||
url: undefined, | ||
connectionOptions: undefined, | ||
urls: undefined | ||
}); | ||
this.connectStub.restore(); | ||
}) | ||
); | ||
after(TestContext.reset); | ||
|
||
it("should call the connect method", () => { | ||
this.connectStub.should.have.been.calledWithExactly("default", "mongodb://test", {options: "options"}); | ||
}); | ||
|
||
it("should return a promise", () => { | ||
this.result.should.eventually.deep.eq(["test"]); | ||
}); | ||
}); | ||
|
||
describe("when a list of urls is given", () => { | ||
before( | ||
inject([MongooseModule, ServerSettingsService], (mongooseModule: MongooseModule, serverSetttings: ServerSettingsService) => { | ||
this.mongooseModule = mongooseModule; | ||
|
||
serverSetttings.set("mongoose", { | ||
url: undefined, | ||
connectionOptions: undefined, | ||
urls: { | ||
db1: { | ||
url: "mongodb://test", | ||
connectionOptions: {options: "options"} | ||
} | ||
} | ||
}); | ||
|
||
this.connectStub = Sinon.stub(this.mongooseModule.mongooseService, "connect").resolves("test" as any); | ||
|
||
return (this.result = mongooseModule.$onInit()); | ||
}) | ||
); | ||
|
||
after( | ||
inject([ServerSettingsService], (serverSetttings: ServerSettingsService) => { | ||
serverSetttings.set("mongoose", { | ||
url: undefined, | ||
connectionOptions: undefined, | ||
urls: undefined | ||
}); | ||
this.connectStub.restore(); | ||
}) | ||
); | ||
|
||
it("should call the connect method", () => { | ||
this.connectStub.should.have.been.calledWithExactly("db1", "mongodb://test", {options: "options"}); | ||
}); | ||
|
||
it("should return a promise", () => { | ||
return this.result.should.eventually.deep.eq(["test"]); | ||
}); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.