feat: add cc-server-db
This commit is contained in:
parent
7d84393a3c
commit
73daf1e44e
@ -1,8 +1,11 @@
|
||||
{
|
||||
"npmClient": "yarn",
|
||||
"useWorkspaces": true,
|
||||
"scripts": {
|
||||
"build": "npx tsc"
|
||||
},
|
||||
"packages": [
|
||||
"packages/*"
|
||||
],
|
||||
"version": "0.0.2"
|
||||
}
|
||||
}
|
@ -11,6 +11,7 @@
|
||||
},
|
||||
"scripts": {
|
||||
"dev": "npx ts-node src/index.ts",
|
||||
"build": "npx tsc",
|
||||
"test": "echo \"Error: run tests from root\" && exit 1"
|
||||
},
|
||||
"dependencies": {
|
||||
@ -24,6 +25,8 @@
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/body-parser": "^1.17.0",
|
||||
"@types/express": "^4.17.0",
|
||||
"ts-node": "^8.2.0"
|
||||
}
|
||||
},
|
||||
"gitHead": "7d84393a3cb6be6be9ed51d71f12677d2d7d0728"
|
||||
}
|
||||
|
@ -1,18 +1,19 @@
|
||||
import {
|
||||
controller, httpGet, httpPost, httpPut, httpDelete
|
||||
controller, httpGet, httpPost
|
||||
} from 'inversify-express-utils';
|
||||
import { inject } from 'inversify';
|
||||
import { MongoDBClient } from 'cc-server-db-mongo'
|
||||
import { DBClient } from 'cc-server-db'
|
||||
import 'cc-server-db-mongo'
|
||||
|
||||
@controller('/')
|
||||
export class UserController {
|
||||
constructor(
|
||||
@inject(MongoDBClient) private MongoDBClient: MongoDBClient
|
||||
@inject(DBClient) private client: DBClient
|
||||
) { }
|
||||
|
||||
@httpGet('/')
|
||||
public async getUsers(): Promise<any[]> {
|
||||
return []
|
||||
return this.client.find('users', {});
|
||||
}
|
||||
|
||||
@httpPost('/')
|
||||
|
@ -1,22 +0,0 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"outDir": "dist",
|
||||
"sourceRoot": "src",
|
||||
"module": "commonjs",
|
||||
"target": "es5",
|
||||
"lib": [
|
||||
"es6",
|
||||
"dom"
|
||||
],
|
||||
"noImplicitAny": false,
|
||||
"sourceMap": false,
|
||||
"emitDecoratorMetadata": true,
|
||||
"experimentalDecorators": true,
|
||||
"moduleResolution": "node",
|
||||
"noUnusedLocals": true
|
||||
},
|
||||
"compileOnSave": false,
|
||||
"exclude": [
|
||||
"node_modules"
|
||||
]
|
||||
}
|
@ -1,19 +1,11 @@
|
||||
import 'reflect-metadata';
|
||||
import { InversifyExpressServer } from 'inversify-express-utils';
|
||||
import { Container } from 'inversify';
|
||||
import * as bodyParser from 'body-parser';
|
||||
import { container, buildProviderModule } from 'cc-server-ioc';
|
||||
|
||||
import './function/handle';
|
||||
import { METADATA_KEY } from 'cc-server-ioc';
|
||||
|
||||
// load everything needed to the Container
|
||||
let container = new Container();
|
||||
Reflect.defineMetadata(METADATA_KEY.container, container, Reflect)
|
||||
|
||||
// auto load service
|
||||
let services: Function[] = Reflect.getMetadata(METADATA_KEY.service, Reflect);
|
||||
for (const service of services) {
|
||||
service()
|
||||
}
|
||||
container.load(buildProviderModule());
|
||||
|
||||
// start the server
|
||||
let server = new InversifyExpressServer(container);
|
||||
|
@ -10,15 +10,19 @@
|
||||
"registry": "https://repo.yumc.pw/repository/npm-hosted/"
|
||||
},
|
||||
"scripts": {
|
||||
"build": "npx tsc",
|
||||
"test": "echo \"Error: run tests from root\" && exit 1"
|
||||
},
|
||||
"dependencies": {
|
||||
"cc-server-db": "^0.0.2",
|
||||
"cc-server-ioc": "^0.0.2",
|
||||
"inversify": "^5.0.1",
|
||||
"mongodb": "^3.2.7",
|
||||
"reflect-metadata": "^0.1.13"
|
||||
"reflect-metadata": "^0.1.13",
|
||||
"typescript": "^3.5.1"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/mongodb": "^3.1.28"
|
||||
}
|
||||
},
|
||||
"gitHead": "7d84393a3cb6be6be9ed51d71f12677d2d7d0728"
|
||||
}
|
||||
|
@ -1,17 +1,10 @@
|
||||
import { Db, ObjectID, UpdateWriteOpResult } from 'mongodb';
|
||||
import { injectable } from 'inversify';
|
||||
import { Db, ObjectID } from 'mongodb';
|
||||
import { MongoDBConnection } from './connection';
|
||||
import { service } from 'cc-server-ioc'
|
||||
import { provide } from 'cc-server-ioc'
|
||||
import { DBClient } from 'cc-server-db'
|
||||
|
||||
interface DBClient<T = any> {
|
||||
find(table: string, where: object): T
|
||||
}
|
||||
|
||||
export const NAME: string = 'MongoDBClient'
|
||||
|
||||
@service()
|
||||
@injectable()
|
||||
export class MongoDBClient<T = any> implements DBClient {
|
||||
@provide(DBClient)
|
||||
export class MongoDBClient<T = any> {
|
||||
public db: Db;
|
||||
|
||||
constructor() {
|
||||
@ -20,8 +13,8 @@ export class MongoDBClient<T = any> implements DBClient {
|
||||
});
|
||||
}
|
||||
|
||||
public find(collection: string, filter: object): Promise<T[]> {
|
||||
return this.db.collection(collection).find(filter).toArray();
|
||||
public async find(collection: string, filter: object): Promise<T[]> {
|
||||
return await this.db.collection(collection).find(filter).toArray();
|
||||
}
|
||||
|
||||
public async findOne(collection: string, filter: Object): Promise<T> {
|
||||
@ -30,7 +23,7 @@ export class MongoDBClient<T = any> implements DBClient {
|
||||
}
|
||||
|
||||
public async findOneById(collection: string, objectId: string): Promise<T> {
|
||||
return this.findOne(collection, { _id: new ObjectID(objectId) })
|
||||
return await this.findOne(collection, { _id: new ObjectID(objectId) })
|
||||
}
|
||||
|
||||
public async insertOne(collection: string, model: T): Promise<T> {
|
||||
@ -38,12 +31,13 @@ export class MongoDBClient<T = any> implements DBClient {
|
||||
return insert.ops[0];
|
||||
}
|
||||
|
||||
public updateOne(collection: string, where: any, model: any): Promise<UpdateWriteOpResult> {
|
||||
return this.db.collection(collection).updateOne(where, { $set: model });
|
||||
public async updateOne(collection: string, where: any, model: any): Promise<boolean> {
|
||||
let result = await this.db.collection(collection).updateOne(where, { $set: model });
|
||||
return result.result.ok == 1 && result.result.n > 0;
|
||||
}
|
||||
|
||||
public updateById(collection: string, objectId: string, model: any): Promise<UpdateWriteOpResult> {
|
||||
return this.updateOne(collection, { _id: new ObjectID(objectId) }, { $set: model })
|
||||
public async updateById(collection: string, objectId: string, model: any): Promise<boolean> {
|
||||
return await this.updateOne(collection, { _id: new ObjectID(objectId) }, { $set: model })
|
||||
}
|
||||
|
||||
public async deleteOne(collection: string, where: any): Promise<boolean> {
|
||||
|
@ -1,7 +1,7 @@
|
||||
import { Db, MongoClient } from 'mongodb';
|
||||
|
||||
const connStr = process.env.MONGO_URL || 'mongodb://192.168.0.2:27017';
|
||||
const dbName = process.env.MONGO_DB || "frppool";
|
||||
const connStr = process.env.FAAS_MONGO_URL || 'mongodb://192.168.0.2:27017';
|
||||
const dbName = process.env.FAAS_MONGO_DB || "frppool";
|
||||
|
||||
export class MongoDBConnection {
|
||||
private static isConnected: boolean = false;
|
||||
|
4
packages/cc-server-db/.gitignore
vendored
Normal file
4
packages/cc-server-db/.gitignore
vendored
Normal file
@ -0,0 +1,4 @@
|
||||
/node_modules
|
||||
/dist
|
||||
/package-lock.json
|
||||
/yarn.lock
|
11
packages/cc-server-db/README.md
Normal file
11
packages/cc-server-db/README.md
Normal file
@ -0,0 +1,11 @@
|
||||
# `cc-server-db`
|
||||
|
||||
> TODO: description
|
||||
|
||||
## Usage
|
||||
|
||||
```
|
||||
const ccServerDb = require('cc-server-db');
|
||||
|
||||
// TODO: DEMONSTRATE API
|
||||
```
|
27
packages/cc-server-db/package.json
Normal file
27
packages/cc-server-db/package.json
Normal file
@ -0,0 +1,27 @@
|
||||
{
|
||||
"name": "cc-server-db",
|
||||
"version": "0.0.2",
|
||||
"description": "Now I’m the model of a modern major general / The venerated Virginian veteran whose men are all / Lining up, to put me up on a pedestal / Writin’ letters to relatives / Embellishin’ my elegance and eloquence / But the elephant is in the room / The truth is in ya face when ya hear the British cannons go / BOOM",
|
||||
"keywords": [],
|
||||
"author": "MiaoWoo <admin@yumc.pw>",
|
||||
"license": "ISC",
|
||||
"main": "dist/index.js",
|
||||
"publishConfig": {
|
||||
"registry": "https://repo.yumc.pw/repository/npm-hosted/"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git+https://github.com/502647092/cc-server-parent.git"
|
||||
},
|
||||
"scripts": {
|
||||
"build": "npx tsc",
|
||||
"test": "echo \"Error: run tests from root\" && exit 1"
|
||||
},
|
||||
"bugs": {
|
||||
"url": "https://github.com/502647092/cc-server-parent/issues"
|
||||
},
|
||||
"homepage": "https://github.com/502647092/cc-server-parent#readme",
|
||||
"dependencies": {
|
||||
"typescript": "^3.5.1"
|
||||
}
|
||||
}
|
11
packages/cc-server-db/src/index.ts
Normal file
11
packages/cc-server-db/src/index.ts
Normal file
@ -0,0 +1,11 @@
|
||||
export const DBClient = Symbol.for('DBClient')
|
||||
export interface DBClient<T = any> {
|
||||
find(collection: string, filter: object): Promise<T[]>;
|
||||
findOne(collection: string, filter: Object): Promise<T>;
|
||||
findOneById(collection: string, objectId: string): Promise<T>;
|
||||
insertOne(collection: string, model: T): Promise<T>;
|
||||
updateOne(collection: string, where: any, model: any): Promise<boolean>;
|
||||
updateById(collection: string, objectId: string, model: any): Promise<boolean>;
|
||||
deleteOne(collection: string, where: any): Promise<boolean>;
|
||||
deleteById(collection: string, objectId: string): Promise<boolean>;
|
||||
}
|
7
packages/cc-server-db/tsconfig.json
Normal file
7
packages/cc-server-db/tsconfig.json
Normal file
@ -0,0 +1,7 @@
|
||||
{
|
||||
"extends": "../../tsconfig.json",
|
||||
"compilerOptions": {
|
||||
"baseUrl": "src",
|
||||
"outDir": "dist"
|
||||
}
|
||||
}
|
@ -10,10 +10,14 @@
|
||||
"registry": "https://repo.yumc.pw/repository/npm-hosted/"
|
||||
},
|
||||
"scripts": {
|
||||
"build": "npx tsc",
|
||||
"test": "echo \"Error: run tests from root\" && exit 1"
|
||||
},
|
||||
"dependencies": {
|
||||
"inversify": "^5.0.1",
|
||||
"reflect-metadata": "^0.1.13"
|
||||
}
|
||||
}
|
||||
"inversify-binding-decorators": "^4.0.0",
|
||||
"reflect-metadata": "^0.1.13",
|
||||
"typescript": "^3.5.1"
|
||||
},
|
||||
"gitHead": "7d84393a3cb6be6be9ed51d71f12677d2d7d0728"
|
||||
}
|
@ -1,4 +0,0 @@
|
||||
export const METADATA_KEY = {
|
||||
container: "cc-server-core:container",
|
||||
service: "cc-server-core:service"
|
||||
};
|
@ -1,16 +0,0 @@
|
||||
import 'reflect-metadata'
|
||||
import { Container } from 'inversify';
|
||||
import { METADATA_KEY } from './constant/types';
|
||||
|
||||
function service(name: string | symbol) {
|
||||
return function(target: any) {
|
||||
let services = Reflect.getMetadata(METADATA_KEY.service, Reflect) || []
|
||||
services = [() => {
|
||||
let container: Container = Reflect.getMetadata(METADATA_KEY.container, Reflect)
|
||||
container.bind(name || target).to(target);
|
||||
}, ...services]
|
||||
Reflect.defineMetadata(METADATA_KEY.service, services, Reflect)
|
||||
}
|
||||
}
|
||||
|
||||
export { service }
|
@ -1,2 +1,22 @@
|
||||
export * from './constant/types'
|
||||
export * from './decorators'
|
||||
import "reflect-metadata";
|
||||
import { Container, inject } from 'inversify';
|
||||
import { autoProvide, provide, fluentProvide, buildProviderModule } from 'inversify-binding-decorators';
|
||||
|
||||
let container = new Container();
|
||||
// Reflects all decorators provided by this package and packages them into
|
||||
// a module to be loaded by the container
|
||||
// container.load(buildProviderModule());
|
||||
|
||||
const provideNamed = (identifier, name) => {
|
||||
return fluentProvide(identifier)
|
||||
.whenTargetNamed(name)
|
||||
.done();
|
||||
};
|
||||
|
||||
const provideSingleton = (identifier: any) => {
|
||||
return fluentProvide(identifier)
|
||||
.inSingletonScope()
|
||||
.done();
|
||||
};
|
||||
|
||||
export { container, autoProvide, provide, provideNamed, provideSingleton, inject, buildProviderModule };
|
||||
|
@ -1,8 +1,11 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"baseUrl": "src",
|
||||
"outDir": "dist",
|
||||
"target": "es6",
|
||||
"module": "commonjs",
|
||||
"sourceMap": true,
|
||||
"declaration": true,
|
||||
"allowUnreachableCode": true,
|
||||
"experimentalDecorators": true,
|
||||
"emitDecoratorMetadata": true
|
||||
|
Loading…
Reference in New Issue
Block a user