-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(mongoose): added mongoose module wrapper
- Loading branch information
1 parent
d516e91
commit c0f0928
Showing
13 changed files
with
320 additions
and
8 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
|
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,28 @@ | ||
BSD 3-Clause License | ||
|
||
Copyright (c) 2019, Reactive Solutions LTD 205455032. All rights reserved. | ||
|
||
Redistribution and use in source and binary forms, with or without | ||
modification, are permitted provided that the following conditions are met: | ||
|
||
* Redistributions of source code must retain the above copyright notice, this | ||
list of conditions and the following disclaimer. | ||
|
||
* Redistributions in binary form must reproduce the above copyright notice, | ||
this list of conditions and the following disclaimer in the documentation | ||
and/or other materials provided with the distribution. | ||
|
||
* Neither the name of the copyright holder nor the names of its | ||
contributors may be used to endorse or promote products derived from | ||
this software without specific prior written permission. | ||
|
||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE | ||
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | ||
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER | ||
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | ||
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
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,21 @@ | ||
# @rhtml/mongoose | ||
|
||
#### Installation | ||
|
||
```bash | ||
npm i @rhtml/mongoose | ||
``` | ||
|
||
#### Usage | ||
|
||
```ts | ||
import { Module } from '@rhtml/di'; | ||
import mongoose from 'mongoose'; | ||
|
||
import { MongoDbModule } from './mongodb'; | ||
|
||
@Module({ | ||
imports: [MongoDbModule.forRoot(mongoose, 'MONGODB_CONNECTION_STRING')], | ||
}) | ||
export class CoreModule {} | ||
``` |
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,16 @@ | ||
module.exports = { | ||
testEnvironment: 'node', | ||
testPathIgnorePatterns: ['/node_modules/'], | ||
coverageReporters: ['lcov', 'html'], | ||
rootDir: './', | ||
moduleFileExtensions: ['ts', 'tsx', 'js', 'json', 'node'], | ||
globals: { | ||
__DEV__: true | ||
}, | ||
transform: { | ||
'\\.(ts|tsx)$': 'ts-jest' | ||
}, | ||
testRegex: '/src/.*\\.spec.(ts|tsx|js)$', | ||
verbose: true, | ||
collectCoverage: true | ||
}; |
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,43 @@ | ||
{ | ||
"name": "@rhtml/mongoose", | ||
"version": "0.0.3", | ||
"description": "Mongoose module wrapper", | ||
"scripts": { | ||
"start": "npx gapi start --local --path=./example/main.ts", | ||
"patch": "npm run build && npm version patch && npm publish --update-readme --access public && npm run delete-dist", | ||
"delete-dist": "rm -rf dist", | ||
"clean": "git clean -dxf", | ||
"test": "echo 'no tests specified'", | ||
"lint": "npx eslint . --ext .ts", | ||
"lint-fix": "npx eslint . --fix --ext .ts", | ||
"build": "rm -rf dist && tsc", | ||
"build-prod": "npx gapi build --local --path=./src/index.ts" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "[email protected]:r-html/rhtml.git" | ||
}, | ||
"peerDependencies": { | ||
"mongoose": "^8.2.1" | ||
}, | ||
"dependencies": {}, | ||
"devDependencies": { | ||
"@types/jest": "^24.0.18", | ||
"jest": "^24.9.0", | ||
"ts-jest": "25.2.1", | ||
"@rhtml/di": "^0.0.128", | ||
"typescript": "^5.3.3" | ||
}, | ||
"author": "Kristiyan Tachev", | ||
"license": "MIT", | ||
"browserslist": [ | ||
"last 1 chrome versions" | ||
], | ||
"files": [ | ||
"dist" | ||
], | ||
"main": "./dist/index.js", | ||
"types": "./dist/index.d.ts", | ||
"module": "./dist/index.js", | ||
"typings": "./dist/index.d.ts" | ||
} |
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 @@ | ||
export * from './mongoose.transformers'; |
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,21 @@ | ||
import { HydratedDocument, SchemaOptions, ToObjectOptions } from 'mongoose'; | ||
|
||
export const MongooseIdTransformer: ToObjectOptions<HydratedDocument<unknown>> = | ||
{ | ||
transform: (_doc, ret) => { | ||
const model = { | ||
id: ret._id, | ||
...ret, | ||
}; | ||
delete model['_id']; | ||
delete model['__v']; | ||
return model; | ||
}, | ||
}; | ||
|
||
export const GlobalModelOptions: SchemaOptions = { | ||
toObject: MongooseIdTransformer, | ||
toJSON: MongooseIdTransformer, | ||
versionKey: false, | ||
timestamps: true, | ||
}; |
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,3 @@ | ||
export * from './mongodb.module'; | ||
export * from './mongodb.injection.tokens'; | ||
export * from './helpers'; |
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,5 @@ | ||
import { InjectionToken } from '@rhtml/di'; | ||
import { Mongoose } from 'mongoose'; | ||
|
||
export const MONGOOSE_CONNECTION_TOKEN = new InjectionToken<Mongoose>(); | ||
export type MONGOOSE_CONNECTION_TOKEN = Mongoose; |
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,23 @@ | ||
import { Module, ModuleWithProviders } from '@rhtml/di'; | ||
import mongoose, { ConnectOptions } from 'mongoose'; | ||
|
||
import { MONGOOSE_CONNECTION_TOKEN } from './mongodb.injection.tokens'; | ||
|
||
@Module() | ||
export class MongoDbModule { | ||
public static forRoot( | ||
mongooseInstance: typeof mongoose, | ||
url: string, | ||
options?: ConnectOptions | ||
): ModuleWithProviders { | ||
return { | ||
module: MongoDbModule, | ||
providers: [ | ||
{ | ||
provide: MONGOOSE_CONNECTION_TOKEN, | ||
useFactory: () => mongooseInstance.connect(url, options), | ||
}, | ||
], | ||
}; | ||
} | ||
} |
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,31 @@ | ||
{ | ||
"compilerOptions": { | ||
"baseUrl": ".", | ||
"sourceMap": true, | ||
"module": "commonjs", | ||
"declaration": true, | ||
"moduleResolution": "node", | ||
"skipLibCheck": true, | ||
"emitDecoratorMetadata": true, | ||
"experimentalDecorators": true, | ||
"esModuleInterop": true, | ||
"target": "es6", | ||
"outDir": "dist", | ||
"types": [], | ||
"lib": [ | ||
"es2015", | ||
"es2016", | ||
"es6", | ||
"esnext.asynciterable", | ||
"es2017", | ||
"dom" | ||
] | ||
}, | ||
"include": [ | ||
"src/**/*" | ||
], | ||
"exclude": [ | ||
"./node_modules", | ||
"../../node_modules" | ||
] | ||
} |
Oops, something went wrong.