Init Project...
This commit is contained in:
		
							
								
								
									
										4
									
								
								packages/cc-server-db-mongo/.gitignore
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										4
									
								
								packages/cc-server-db-mongo/.gitignore
									
									
									
									
										vendored
									
									
										Normal file
									
								
							@@ -0,0 +1,4 @@
 | 
			
		||||
/node_modules
 | 
			
		||||
/dist
 | 
			
		||||
/package-lock.json
 | 
			
		||||
/yarn.lock
 | 
			
		||||
							
								
								
									
										11
									
								
								packages/cc-server-db-mongo/README.md
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										11
									
								
								packages/cc-server-db-mongo/README.md
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,11 @@
 | 
			
		||||
# `cc-server-db-mongo`
 | 
			
		||||
 | 
			
		||||
> TODO: description
 | 
			
		||||
 | 
			
		||||
## Usage
 | 
			
		||||
 | 
			
		||||
```
 | 
			
		||||
const ccServerDbMongo = require('cc-server-db-mongo');
 | 
			
		||||
 | 
			
		||||
// TODO: DEMONSTRATE API
 | 
			
		||||
```
 | 
			
		||||
							
								
								
									
										24
									
								
								packages/cc-server-db-mongo/package.json
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										24
									
								
								packages/cc-server-db-mongo/package.json
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,24 @@
 | 
			
		||||
{
 | 
			
		||||
    "name": "cc-server-db-mongo",
 | 
			
		||||
    "version": "0.0.1",
 | 
			
		||||
    "description": "> TODO: description",
 | 
			
		||||
    "author": "MiaoWoo <admin@yumc.pw>",
 | 
			
		||||
    "homepage": "https://faas.yumc.pw",
 | 
			
		||||
    "license": "ISC",
 | 
			
		||||
    "main": "dist/index.js",
 | 
			
		||||
    "publishConfig": {
 | 
			
		||||
        "registry": "https://repo.yumc.pw/repository/npm-hosted/"
 | 
			
		||||
    },
 | 
			
		||||
    "scripts": {
 | 
			
		||||
        "test": "echo \"Error: run tests from root\" && exit 1"
 | 
			
		||||
    },
 | 
			
		||||
    "dependencies": {
 | 
			
		||||
        "cc-server-ioc": "^0.0.1",
 | 
			
		||||
        "inversify": "^5.0.1",
 | 
			
		||||
        "mongodb": "^3.2.7",
 | 
			
		||||
        "reflect-metadata": "^0.1.13"
 | 
			
		||||
    },
 | 
			
		||||
    "devDependencies": {
 | 
			
		||||
        "@types/mongodb": "^3.1.28"
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										57
									
								
								packages/cc-server-db-mongo/src/client.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										57
									
								
								packages/cc-server-db-mongo/src/client.ts
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,57 @@
 | 
			
		||||
import { Db, ObjectID, UpdateWriteOpResult } from 'mongodb';
 | 
			
		||||
import { injectable } from 'inversify';
 | 
			
		||||
import { MongoDBConnection } from './connection';
 | 
			
		||||
import { service } from 'cc-server-ioc'
 | 
			
		||||
 | 
			
		||||
interface DBClient<T = any> {
 | 
			
		||||
    find(table: string, where: object): T
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
export const NAME: string = 'MongoDBClient'
 | 
			
		||||
 | 
			
		||||
@service()
 | 
			
		||||
@injectable()
 | 
			
		||||
export class MongoDBClient<T = any> implements DBClient {
 | 
			
		||||
    public db: Db;
 | 
			
		||||
 | 
			
		||||
    constructor() {
 | 
			
		||||
        MongoDBConnection.getConnection((connection) => {
 | 
			
		||||
            this.db = connection;
 | 
			
		||||
        });
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public find(collection: string, filter: object): Promise<T[]> {
 | 
			
		||||
        return this.db.collection(collection).find(filter).toArray();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public async findOne(collection: string, filter: Object): Promise<T> {
 | 
			
		||||
        let result = await this.db.collection(collection).find(filter).limit(1).toArray();
 | 
			
		||||
        return result[0];
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public async findOneById(collection: string, objectId: string): Promise<T> {
 | 
			
		||||
        return this.findOne(collection, { _id: new ObjectID(objectId) })
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public async insertOne(collection: string, model: T): Promise<T> {
 | 
			
		||||
        var insert = await this.db.collection(collection).insertOne(model);
 | 
			
		||||
        return insert.ops[0];
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public updateOne(collection: string, where: any, model: any): Promise<UpdateWriteOpResult> {
 | 
			
		||||
        return this.db.collection(collection).updateOne(where, { $set: model });
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public updateById(collection: string, objectId: string, model: any): Promise<UpdateWriteOpResult> {
 | 
			
		||||
        return this.updateOne(collection, { _id: new ObjectID(objectId) }, { $set: model })
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public async deleteOne(collection: string, where: any): Promise<boolean> {
 | 
			
		||||
        let result = await this.db.collection(collection).deleteOne(where);
 | 
			
		||||
        return result.result.ok === 1 && result.result.n > 0
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public async deleteById(collection: string, objectId: string): Promise<boolean> {
 | 
			
		||||
        return this.deleteOne(collection, { _id: new ObjectID(objectId) });
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										27
									
								
								packages/cc-server-db-mongo/src/connection.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										27
									
								
								packages/cc-server-db-mongo/src/connection.ts
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,27 @@
 | 
			
		||||
import { Db, MongoClient } from 'mongodb';
 | 
			
		||||
 | 
			
		||||
const connStr = process.env.MONGO_URL || 'mongodb://192.168.0.2:27017';
 | 
			
		||||
const dbName = process.env.MONGO_DB || "frppool";
 | 
			
		||||
 | 
			
		||||
export class MongoDBConnection {
 | 
			
		||||
    private static isConnected: boolean = false;
 | 
			
		||||
    private static db: Db;
 | 
			
		||||
 | 
			
		||||
    public static getConnection(result: (connection) => void) {
 | 
			
		||||
        if (this.isConnected) {
 | 
			
		||||
            return result(this.db);
 | 
			
		||||
        } else {
 | 
			
		||||
            this.connect((error, db: Db) => {
 | 
			
		||||
                return result(this.db);
 | 
			
		||||
            });
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private static connect(result: (error, db: Db) => void) {
 | 
			
		||||
        MongoClient.connect(connStr, { useNewUrlParser: true }, (err, client) => {
 | 
			
		||||
            this.db = client.db(dbName);
 | 
			
		||||
            this.isConnected = true;
 | 
			
		||||
            return result(err, this.db);
 | 
			
		||||
        });
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										1
									
								
								packages/cc-server-db-mongo/src/index.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										1
									
								
								packages/cc-server-db-mongo/src/index.ts
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1 @@
 | 
			
		||||
export * from './client'
 | 
			
		||||
							
								
								
									
										7
									
								
								packages/cc-server-db-mongo/tsconfig.json
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										7
									
								
								packages/cc-server-db-mongo/tsconfig.json
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,7 @@
 | 
			
		||||
{
 | 
			
		||||
    "extends": "../../tsconfig.json",
 | 
			
		||||
    "compilerOptions": {
 | 
			
		||||
        "baseUrl": "src",
 | 
			
		||||
        "outDir": "dist"
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user