refactor: rename package

This commit is contained in:
2019-06-21 16:47:18 +08:00
parent 2f990bc41d
commit 7288d33ce2
39 changed files with 39 additions and 38 deletions

4
packages/db-mongo/.gitignore vendored Normal file
View File

@@ -0,0 +1,4 @@
/node_modules
/dist
/package-lock.json
/yarn.lock

View File

@@ -0,0 +1,22 @@
src
test
typings
bundled
build
coverage
docs
wiki
gulpfile.js
bower.json
karma.conf.js
tsconfig.json
typings.json
CONTRIBUTING.md
ISSUE_TEMPLATE.md
PULL_REQUEST_TEMPLATE.md
tslint.json
wallaby.js
.travis.yml
.gitignore
.vscode
type_definitions

View File

@@ -0,0 +1,11 @@
# `cc-server-db-mongo`
> TODO: description
## Usage
```
const ccServerDbMongo = require('cc-server-db-mongo');
// TODO: DEMONSTRATE API
```

View File

@@ -0,0 +1,34 @@
{
"name": "@cc-server/db-mongo",
"version": "0.3.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": {
"build": "rimraf dist && npx tsc",
"test": "echo \"Error: run tests from root\" && exit 1"
},
"repository": {
"type": "git",
"url": "git+https://github.com/502647092/cc-server-parent.git"
},
"dependencies": {
"@cc-server/db": "^0.3.0",
"@cc-server/ioc": "^0.3.1",
"inversify": "^5.0.1",
"mongodb": "^3.2.7",
"reflect-metadata": "^0.1.13"
},
"devDependencies": {
"@types/mongodb": "^3.1.28",
"mocha": "^6.1.4",
"rimraf": "^2.6.3",
"typescript": "^3.5.1"
},
"gitHead": "7d84393a3cb6be6be9ed51d71f12677d2d7d0728"
}

View File

@@ -0,0 +1,27 @@
import { Db, MongoClient } from 'mongodb';
const connStr = process.env.FAAS_MONGO_URL || 'mongodb://192.168.0.2:27017';
const dbName = process.env.FAAS_MONGO_DB || "faas";
export class MongoDBConnection {
private static isConnected: boolean = false;
private static db: Db;
public static getConnection(result: (connection: Db) => void) {
if (this.isConnected) {
return result(this.db);
} else {
this.connect((error: Error, db: Db) => {
return result(this.db);
});
}
}
private static connect(result: (error: Error, db: Db) => void) {
MongoClient.connect(connStr, { useNewUrlParser: true }, (err, client) => {
this.db = client.db(dbName);
this.isConnected = true;
return result(err, this.db);
});
}
}

View File

@@ -0,0 +1,67 @@
import { Db, ObjectID, Collection } from 'mongodb';
import { MongoDBConnection } from './connection';
import { provide } from '@cc-server/ioc'
import { DBClient } from '@cc-server/db'
@provide(DBClient)
export class MongoDBClient<T = any> implements DBClient {
private table: string;
private db: Db;
private collection: Collection<T>;
constructor() {
MongoDBConnection.getConnection((connection) => {
this.db = connection;
if (this.table) {
this.collection = this.db.collection(this.table);
}
});
}
public getProvide<P>(): P {
return this.db as {} as P;
}
public setTable(table: string): void {
this.table = table;
if (this.db) {
this.collection = this.db.collection(table);
}
}
public async find(filter: object): Promise<T[]> {
return await this.collection.find(filter).toArray();
}
public async findOne(filter: Object): Promise<T> {
let result = await this.collection.find(filter).limit(1).toArray();
return result[0];
}
public async findOneById(objectId: string): Promise<T> {
return await this.findOne({ _id: new ObjectID(objectId) })
}
public async insertOne(model: T): Promise<T> {
var insert = await this.collection.insertOne(model);
return insert.ops[0];
}
public async updateOne(where: any, model: any): Promise<boolean> {
let result = await this.collection.updateOne(where, { $set: model });
return result.result.ok == 1 && result.result.n > 0;
}
public async updateById(objectId: string, model: any): Promise<boolean> {
return await this.updateOne({ _id: new ObjectID(objectId) }, model)
}
public async deleteOne(where: any): Promise<boolean> {
let result = await this.collection.deleteOne(where);
return result.result.ok === 1 && result.result.n > 0
}
public async deleteById(objectId: string): Promise<boolean> {
return this.deleteOne({ _id: new ObjectID(objectId) });
}
}

View File

@@ -0,0 +1,2 @@
export * from './client'
export * from 'mongodb';

View File

@@ -0,0 +1,7 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"baseUrl": "src",
"outDir": "dist"
}
}