Skip to content

Commit

Permalink
feat(mongoose): added mongoose module wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
Stradivario committed Mar 7, 2024
1 parent d516e91 commit c0f0928
Show file tree
Hide file tree
Showing 13 changed files with 320 additions and 8 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
"module": "./dist/index.js",
"typings": "./dist/index.d.ts",
"dependencies": {
"fastify": "^4.26.2"
"fastify": "^4.26.2",
"mongoose": "^8.2.1"
}
}
1 change: 1 addition & 0 deletions packages/mongoose/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

28 changes: 28 additions & 0 deletions packages/mongoose/LICENSE
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.
21 changes: 21 additions & 0 deletions packages/mongoose/README.md
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 {}
```
16 changes: 16 additions & 0 deletions packages/mongoose/jest.config.js
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
};
43 changes: 43 additions & 0 deletions packages/mongoose/package.json
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"
}
1 change: 1 addition & 0 deletions packages/mongoose/src/helpers/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './mongoose.transformers';
21 changes: 21 additions & 0 deletions packages/mongoose/src/helpers/mongoose.transformers.ts
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,
};
3 changes: 3 additions & 0 deletions packages/mongoose/src/index.ts
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';
5 changes: 5 additions & 0 deletions packages/mongoose/src/mongodb.injection.tokens.ts
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;
23 changes: 23 additions & 0 deletions packages/mongoose/src/mongodb.module.ts
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),
},
],
};
}
}
31 changes: 31 additions & 0 deletions packages/mongoose/tsconfig.json
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"
]
}
Loading

0 comments on commit c0f0928

Please sign in to comment.