Compare commits
14 Commits
Author | SHA1 | Date |
---|---|---|
MiaoWoo | f13e98568b | |
MiaoWoo | e078e7d503 | |
MiaoWoo | d4eabcac23 | |
MiaoWoo | f02d43d750 | |
MiaoWoo | 2e560077a6 | |
MiaoWoo | f2d0d28596 | |
MiaoWoo | dcd5031db2 | |
MiaoWoo | 140f881062 | |
MiaoWoo | 5fa5603735 | |
MiaoWoo | ec24afbd13 | |
MiaoWoo | 831a47fbf3 | |
MiaoWoo | ac73c52395 | |
MiaoWoo | e6783610d4 | |
MiaoWoo | 4d416fb628 |
|
@ -1,5 +1,5 @@
|
||||||
/node_modules
|
node_modules
|
||||||
/dist
|
dist
|
||||||
/package-lock.json
|
package-lock.json
|
||||||
/yarn.lock
|
yarn.lock
|
||||||
/*.log
|
*.log
|
||||||
|
|
|
@ -1,4 +1,3 @@
|
||||||
src
|
|
||||||
test
|
test
|
||||||
typings
|
typings
|
||||||
bundled
|
bundled
|
||||||
|
@ -19,4 +18,5 @@ wallaby.js
|
||||||
.travis.yml
|
.travis.yml
|
||||||
.gitignore
|
.gitignore
|
||||||
.vscode
|
.vscode
|
||||||
type_definitions
|
type_definitions
|
||||||
|
node_modules
|
136
README.md
136
README.md
|
@ -1,11 +1,137 @@
|
||||||
# `cc-server-parent`
|
# `cc-server`
|
||||||
|
|
||||||
> TODO: description
|
> 一个简易的 IOC 注入 Web 开发框架 仿照 SpringBoot 使用 TypeScript 开发
|
||||||
|
|
||||||
## Usage
|
## Usage
|
||||||
|
|
||||||
```
|
> 当前包尚未发布到 NPM 中央仓库
|
||||||
const ccServerDbMongo = require('cc-server-parent');
|
> 如需使用 请自行配制 repo `https://repo.yumc.pw/repository/npm/`
|
||||||
|
|
||||||
// TODO: DEMONSTRATE API
|
### Simple Example
|
||||||
|
|
||||||
|
```TypeScript
|
||||||
|
import { CcServerBoot, express } from './index'
|
||||||
|
|
||||||
|
import './function/http'; // => WebServer
|
||||||
|
import './function/websocket'; // => Socket.IO
|
||||||
|
import { DBClient } from '@cc-server/db';
|
||||||
|
import { MongoClient } from 'mongodb';
|
||||||
|
import { MongoCollection } from '@cc-server/db-mongo';
|
||||||
|
|
||||||
|
let boot = new CcServerBoot().static('public');
|
||||||
|
MongoClient.connect("mongodb://192.168.2.5:27017", { useNewUrlParser: true }, (error, client) => {
|
||||||
|
if (error) {
|
||||||
|
console.log(error)
|
||||||
|
} else {
|
||||||
|
boot.container.bind(DBClient).toConstantValue(new MongoCollection(client.db("faas").collection("users"))).whenTargetNamed("users")
|
||||||
|
boot.build().listen();
|
||||||
|
}
|
||||||
|
})
|
||||||
|
```
|
||||||
|
|
||||||
|
### WebServer
|
||||||
|
|
||||||
|
```TypeScript
|
||||||
|
import { DBClient } from '@cc-server/db'
|
||||||
|
import { inject, named } from '@cc-server/ioc';
|
||||||
|
import { Vaild, NotBlank, NotNull, controller, requestBody, get, post, requestParam } from '@cc-server/binding'
|
||||||
|
import '@cc-server/db-mongo'
|
||||||
|
|
||||||
|
class ExampleModel {
|
||||||
|
_id: string;
|
||||||
|
@NotBlank("username must not be blank!")
|
||||||
|
username: string;
|
||||||
|
password: string;
|
||||||
|
@NotNull()
|
||||||
|
age: number;
|
||||||
|
email: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
@controller('/example')
|
||||||
|
export class Controller {
|
||||||
|
@inject(DBClient)
|
||||||
|
@named("users")
|
||||||
|
private client: DBClient
|
||||||
|
|
||||||
|
@get('/')
|
||||||
|
public async list(): Promise<ExampleModel[]> {
|
||||||
|
return this.client.find({});
|
||||||
|
}
|
||||||
|
|
||||||
|
@get('/:id')
|
||||||
|
public async get(
|
||||||
|
@requestParam('id') id: string
|
||||||
|
): Promise<ExampleModel> {
|
||||||
|
return this.client.findOneById(id);
|
||||||
|
}
|
||||||
|
|
||||||
|
@post('/')
|
||||||
|
public async create(
|
||||||
|
@Vaild() @requestBody() model: ExampleModel
|
||||||
|
): Promise<ExampleModel> {
|
||||||
|
return model;
|
||||||
|
}
|
||||||
|
|
||||||
|
@post('/:id')
|
||||||
|
public async update(
|
||||||
|
@requestParam('id') id: string,
|
||||||
|
@Vaild() @requestBody() model: ExampleModel
|
||||||
|
): Promise<boolean> {
|
||||||
|
return this.client.updateById(id, model);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### Socket.IO
|
||||||
|
|
||||||
|
```TypeScript
|
||||||
|
import { lazyInjectNamed } from '@cc-server/ioc'
|
||||||
|
import { controller, httpPost, requestBody } from '@cc-server/binding';
|
||||||
|
import { namespace, listener, interfaces, BroadcastMessage, io, TYPE } from '@cc-server/ws'
|
||||||
|
|
||||||
|
@namespace('/', (socket: io.Socket, next: (err?: any) => void) => {
|
||||||
|
console.log(socket.nsp.name, socket.id, 'before connection');
|
||||||
|
next();
|
||||||
|
})
|
||||||
|
export class Namespace extends interfaces.Namespace {
|
||||||
|
private cache: { [key: string]: string } = {};
|
||||||
|
|
||||||
|
public async connection(socket: io.Socket) {
|
||||||
|
console.log(this.nsp.name, socket.id, 'connection');
|
||||||
|
this.defer(socket, socket => console.log(this.nsp.name, socket.id, 'defer', this))
|
||||||
|
return `Welcome to Websocket Chat Room Now: ${Date.now()} Your ID: ${socket.id}! \r\n`;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async disconnect(socket: io.Socket) {
|
||||||
|
console.log(this.nsp.name, socket.id, 'disconnect');
|
||||||
|
}
|
||||||
|
|
||||||
|
@listener('message', (socket: io.Socket, packet: io.Packet, next: (err?: any) => void) => {
|
||||||
|
console.log(socket.nsp.name, socket.id, 'listener middleware', [...packet]);
|
||||||
|
next();
|
||||||
|
})
|
||||||
|
public async message(socket: io.Socket, data: any) {
|
||||||
|
console.log(this.nsp.name, socket.id, 'message', data)
|
||||||
|
this.cache[socket.id] = (this.cache[socket.id] || '') + data;
|
||||||
|
if (data == '\r' && this.cache[socket.id] !== "") {
|
||||||
|
let result = new BroadcastMessage(this.cache[socket.id] + '\n')
|
||||||
|
return delete this.cache[socket.id] && result;
|
||||||
|
}
|
||||||
|
return data;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@controller('/websocket')
|
||||||
|
export class WebSocketController {
|
||||||
|
@lazyInjectNamed(TYPE.Namespace, Namespace.name)
|
||||||
|
private root: Namespace;
|
||||||
|
|
||||||
|
@httpPost('/')
|
||||||
|
public async create(
|
||||||
|
@requestBody() model: Object
|
||||||
|
): Promise<Object> {
|
||||||
|
this.root.nsp.send(JSON.stringify(model));
|
||||||
|
return model;
|
||||||
|
}
|
||||||
|
}
|
||||||
```
|
```
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
{
|
{
|
||||||
"npmClient": "yarn",
|
"npmClient": "yarn",
|
||||||
"useWorkspaces": true,
|
"useWorkspaces": true,
|
||||||
"version": "0.6.2",
|
"version": "0.7.0",
|
||||||
"packages": [
|
"packages": [
|
||||||
"packages/*"
|
"packages/*"
|
||||||
],
|
],
|
||||||
|
|
|
@ -1,4 +0,0 @@
|
||||||
/node_modules
|
|
||||||
/dist
|
|
||||||
/package-lock.json
|
|
||||||
/yarn.lock
|
|
|
@ -1,11 +1,12 @@
|
||||||
{
|
{
|
||||||
"name": "@cc-server/binding",
|
"name": "@cc-server/binding",
|
||||||
"version": "0.6.2",
|
"version": "0.7.0",
|
||||||
"description": "> TODO: description",
|
"description": "> TODO: description",
|
||||||
"author": "MiaoWoo <admin@yumc.pw>",
|
"author": "MiaoWoo <admin@yumc.pw>",
|
||||||
"homepage": "https://github.com/502647092/cc-server-parent#readme",
|
"homepage": "https://github.com/502647092/cc-server-parent#readme",
|
||||||
"license": "ISC",
|
"license": "ISC",
|
||||||
"main": "dist/index.js",
|
"main": "dist/index.js",
|
||||||
|
"typings": "dist/index.d.ts",
|
||||||
"publishConfig": {
|
"publishConfig": {
|
||||||
"registry": "https://repo.yumc.pw/repository/npm-hosted/"
|
"registry": "https://repo.yumc.pw/repository/npm-hosted/"
|
||||||
},
|
},
|
||||||
|
@ -23,9 +24,9 @@
|
||||||
"reflect-metadata": "^0.1.13"
|
"reflect-metadata": "^0.1.13"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"mocha": "^6.1.4",
|
"mocha": "^7.0.0",
|
||||||
"rimraf": "^2.6.3",
|
"rimraf": "^3.0.0",
|
||||||
"typescript": "^3.5.1"
|
"typescript": "^3.7.4"
|
||||||
},
|
},
|
||||||
"bugs": {
|
"bugs": {
|
||||||
"url": "https://github.com/502647092/cc-server-parent/issues"
|
"url": "https://github.com/502647092/cc-server-parent/issues"
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
import { httpGet as get, httpPost as post } from 'inversify-express-utils'
|
import { httpGet as get, httpPost as post, httpPut as put, httpPatch as patch, httpDelete as del } from 'inversify-express-utils'
|
||||||
export * from './decorators'
|
export * from './decorators'
|
||||||
export * from './activation'
|
export * from './activation'
|
||||||
export * from 'inversify-express-utils'
|
export * from 'inversify-express-utils'
|
||||||
export { get, post }
|
export { get, post, put, patch, del }
|
||||||
|
|
|
@ -1,4 +0,0 @@
|
||||||
/node_modules
|
|
||||||
/dist
|
|
||||||
/package-lock.json
|
|
||||||
/yarn.lock
|
|
|
@ -1,22 +0,0 @@
|
||||||
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
|
|
|
@ -1,11 +1,12 @@
|
||||||
{
|
{
|
||||||
"name": "@cc-server/core",
|
"name": "@cc-server/core",
|
||||||
"version": "0.6.2",
|
"version": "0.7.0",
|
||||||
"description": "> TODO: description",
|
"description": "> TODO: description",
|
||||||
"author": "MiaoWoo <admin@yumc.pw>",
|
"author": "MiaoWoo <admin@yumc.pw>",
|
||||||
"homepage": "https://github.com/502647092/cc-server-parent#readme",
|
"homepage": "https://github.com/502647092/cc-server-parent#readme",
|
||||||
"license": "ISC",
|
"license": "ISC",
|
||||||
"main": "dist/index.js",
|
"main": "dist/index.js",
|
||||||
|
"typings": "dist/index.d.ts",
|
||||||
"publishConfig": {
|
"publishConfig": {
|
||||||
"registry": "https://repo.yumc.pw/repository/npm-hosted/"
|
"registry": "https://repo.yumc.pw/repository/npm-hosted/"
|
||||||
},
|
},
|
||||||
|
@ -20,27 +21,27 @@
|
||||||
"url": "git+https://github.com/502647092/cc-server-parent.git"
|
"url": "git+https://github.com/502647092/cc-server-parent.git"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@cc-server/binding": "^0.6.2",
|
"@cc-server/binding": "^0.7.0",
|
||||||
"@cc-server/db-mongo": "^0.6.2",
|
"@cc-server/db-mongo": "^0.7.0",
|
||||||
"@cc-server/ioc": "^0.6.2",
|
"@cc-server/ioc": "^0.7.0",
|
||||||
"@cc-server/ws": "^0.6.2",
|
"@cc-server/ws": "^0.7.0",
|
||||||
"body-parser": "^1.19.0",
|
"body-parser": "^1.19.0",
|
||||||
"globby": "^9.2.0",
|
"globby": "^10.0.2",
|
||||||
"inversify": "^5.0.1",
|
"inversify": "^5.0.1",
|
||||||
"inversify-express-utils": "^6.3.2",
|
"inversify-express-utils": "^6.3.2",
|
||||||
"prettyjson": "^1.2.1",
|
"prettyjson": "^1.2.1",
|
||||||
"socket.io": "^2.2.0"
|
"socket.io": "^2.3.0"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@types/body-parser": "^1.17.0",
|
"@types/body-parser": "^1.17.1",
|
||||||
"@types/express": "^4.17.0",
|
"@types/express": "4.17.0",
|
||||||
"@types/prettyjson": "^0.0.29",
|
"@types/prettyjson": "^0.0.29",
|
||||||
"@types/socket.io": "^2.1.2",
|
"@types/socket.io": "^2.1.4",
|
||||||
"mocha": "^6.1.4",
|
"mocha": "^7.0.0",
|
||||||
"rimraf": "^2.6.3",
|
"rimraf": "^3.0.0",
|
||||||
"ts-node": "^8.2.0",
|
"ts-node": "^8.5.4",
|
||||||
"ts-node-dev": "^1.0.0-pre.40",
|
"ts-node-dev": "^1.0.0-pre.44",
|
||||||
"typescript": "^3.5.1"
|
"typescript": "^3.7.4"
|
||||||
},
|
},
|
||||||
"gitHead": "7d84393a3cb6be6be9ed51d71f12677d2d7d0728"
|
"gitHead": "7d84393a3cb6be6be9ed51d71f12677d2d7d0728"
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,10 +1,8 @@
|
||||||
import { DBClient } from '@cc-server/db'
|
import { DBClient } from '@cc-server/db'
|
||||||
import { inject, postConstruct } from '@cc-server/ioc';
|
import { inject, named } from '@cc-server/ioc';
|
||||||
import { Vaild, NotBlank, NotNull, controller, requestBody, get, post, requestParam } from '@cc-server/binding'
|
import { Vaild, NotBlank, NotNull, controller, requestBody, get, post, requestParam } from '@cc-server/binding'
|
||||||
import '@cc-server/db-mongo'
|
import '@cc-server/db-mongo'
|
||||||
|
|
||||||
const TABLE = 'users'
|
|
||||||
|
|
||||||
class ExampleModel {
|
class ExampleModel {
|
||||||
_id: string;
|
_id: string;
|
||||||
@NotBlank("username must not be blank!")
|
@NotBlank("username must not be blank!")
|
||||||
|
@ -18,13 +16,9 @@ class ExampleModel {
|
||||||
@controller('/example')
|
@controller('/example')
|
||||||
export class Controller {
|
export class Controller {
|
||||||
@inject(DBClient)
|
@inject(DBClient)
|
||||||
|
@named("users")
|
||||||
private client: DBClient
|
private client: DBClient
|
||||||
|
|
||||||
@postConstruct()
|
|
||||||
private init(): void {
|
|
||||||
this.client.setTable(TABLE);
|
|
||||||
}
|
|
||||||
|
|
||||||
@get('/')
|
@get('/')
|
||||||
public async list(): Promise<ExampleModel[]> {
|
public async list(): Promise<ExampleModel[]> {
|
||||||
return this.client.find({});
|
return this.client.find({});
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
import 'reflect-metadata';
|
import 'reflect-metadata';
|
||||||
import * as fs from 'fs';
|
import * as fs from 'fs';
|
||||||
import * as http from 'http';
|
import * as http from 'http';
|
||||||
|
import * as path from 'path';
|
||||||
import * as globby from "globby";
|
import * as globby from "globby";
|
||||||
import * as express from "express";
|
import * as express from "express";
|
||||||
import * as prettyjson from "prettyjson";
|
import * as prettyjson from "prettyjson";
|
||||||
|
@ -79,6 +80,10 @@ export class CcServerBoot {
|
||||||
.use(bodyParser.raw());
|
.use(bodyParser.raw());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
get container() {
|
||||||
|
return this._container;
|
||||||
|
}
|
||||||
|
|
||||||
get server() {
|
get server() {
|
||||||
return this._server;
|
return this._server;
|
||||||
}
|
}
|
||||||
|
@ -100,11 +105,18 @@ export class CcServerBoot {
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public scan(path: fs.PathLike) {
|
public scan(scanDir: string) {
|
||||||
let files = fs.readdirSync(path);
|
let files = fs.readdirSync(scanDir);
|
||||||
for (const file of files) {
|
for (let file of files) {
|
||||||
|
let moduleDir = path.join(scanDir, file)
|
||||||
|
let stat = fs.statSync(moduleDir);
|
||||||
|
if (stat.isDirectory()) {
|
||||||
|
this.scan(moduleDir)
|
||||||
|
} else if (stat.isFile() && (file.endsWith('.js') || file.endsWith('.ts'))) {
|
||||||
|
require(moduleDir);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public build() {
|
public build() {
|
||||||
|
@ -122,10 +134,18 @@ export class CcServerBoot {
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public start(port: number = 80) {
|
public print() {
|
||||||
const routeInfo = getRouteInfo(this._container);
|
console.log(prettyjson.render({ routes: { http: getRouteInfo(this._container), websocket: getNamespaceInfo() } }));
|
||||||
console.log(prettyjson.render({ routes: routeInfo }));
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public listen(port: number = 80) {
|
||||||
this._server.listen(port);
|
this._server.listen(port);
|
||||||
console.log(`Server started on port ${port} :)`);
|
console.log(`Server listen on port ${port} :)`);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public start(port: number = 80) {
|
||||||
|
return this.static('public').build().print().listen(port);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,14 @@
|
||||||
import { CcServerBoot, express } from './index'
|
import { CcServerBoot } from './index'
|
||||||
|
import { DBClient } from '@cc-server/db';
|
||||||
|
import { MongoClient } from 'mongodb';
|
||||||
|
import { MongoCollection } from '@cc-server/db-mongo';
|
||||||
|
|
||||||
import './function/http';
|
let boot = new CcServerBoot().scan(__dirname + '/function');
|
||||||
import './function/websocket';
|
MongoClient.connect("mongodb://192.168.2.5:27017", { useNewUrlParser: true }, (error, client) => {
|
||||||
|
if (error) {
|
||||||
new CcServerBoot().static('public').build().start();
|
console.log(error)
|
||||||
|
} else {
|
||||||
|
boot.container.bind(DBClient).toConstantValue(new MongoCollection(client.db("faas").collection("users"))).whenTargetNamed("users")
|
||||||
|
boot.start();
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
|
@ -1,4 +0,0 @@
|
||||||
/node_modules
|
|
||||||
/dist
|
|
||||||
/package-lock.json
|
|
||||||
/yarn.lock
|
|
|
@ -1,11 +1,12 @@
|
||||||
{
|
{
|
||||||
"name": "@cc-server/db-mongo",
|
"name": "@cc-server/db-mongo",
|
||||||
"version": "0.6.2",
|
"version": "0.7.0",
|
||||||
"description": "> TODO: description",
|
"description": "> TODO: description",
|
||||||
"author": "MiaoWoo <admin@yumc.pw>",
|
"author": "MiaoWoo <admin@yumc.pw>",
|
||||||
"homepage": "https://faas.yumc.pw",
|
"homepage": "https://faas.yumc.pw",
|
||||||
"license": "ISC",
|
"license": "ISC",
|
||||||
"main": "dist/index.js",
|
"main": "dist/index.js",
|
||||||
|
"typings": "dist/index.d.ts",
|
||||||
"publishConfig": {
|
"publishConfig": {
|
||||||
"registry": "https://repo.yumc.pw/repository/npm-hosted/"
|
"registry": "https://repo.yumc.pw/repository/npm-hosted/"
|
||||||
},
|
},
|
||||||
|
@ -19,17 +20,17 @@
|
||||||
"url": "git+https://github.com/502647092/cc-server-parent.git"
|
"url": "git+https://github.com/502647092/cc-server-parent.git"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@cc-server/db": "^0.6.2",
|
"@cc-server/db": "^0.7.0",
|
||||||
"@cc-server/ioc": "^0.6.2",
|
"@cc-server/ioc": "^0.7.0",
|
||||||
"inversify": "^5.0.1",
|
"inversify": "^5.0.1",
|
||||||
"mongodb": "^3.2.7",
|
"mongodb": "^3.4.1",
|
||||||
"reflect-metadata": "^0.1.13"
|
"reflect-metadata": "^0.1.13"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@types/mongodb": "^3.1.28",
|
"@types/mongodb": "^3.3.14",
|
||||||
"mocha": "^6.1.4",
|
"mocha": "^7.0.0",
|
||||||
"rimraf": "^2.6.3",
|
"rimraf": "^3.0.0",
|
||||||
"typescript": "^3.5.1"
|
"typescript": "^3.7.4"
|
||||||
},
|
},
|
||||||
"gitHead": "7d84393a3cb6be6be9ed51d71f12677d2d7d0728"
|
"gitHead": "7d84393a3cb6be6be9ed51d71f12677d2d7d0728"
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,38 +1,22 @@
|
||||||
import { DBClient } from '@cc-server/db'
|
import { DBClient } from '@cc-server/db'
|
||||||
import { MongoDBConnection } from './connection'
|
import { ObjectID, Collection } from 'mongodb'
|
||||||
import { Db, ObjectID, Collection } from 'mongodb'
|
|
||||||
import { provide, postConstruct } from '@cc-server/ioc'
|
|
||||||
|
|
||||||
@provide(DBClient)
|
export class MongoCollection<T = any> implements DBClient {
|
||||||
export class MongoDBClient<T = any> implements DBClient {
|
|
||||||
private table: string;
|
|
||||||
private db: Db;
|
|
||||||
private collection: Collection<T>;
|
private collection: Collection<T>;
|
||||||
|
|
||||||
@postConstruct()
|
constructor(collection: Collection<T>) {
|
||||||
private async init() {
|
this.collection = collection;
|
||||||
this.db = await MongoDBConnection.getConnection();
|
|
||||||
if (this.table) {
|
|
||||||
this.collection = this.db.collection(this.table);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public getProvide<P>(): P {
|
public getProvide<P>(): P {
|
||||||
return this.db as {} as P;
|
return this.collection 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[]> {
|
public async find(filter: object): Promise<T[]> {
|
||||||
return await this.collection.find(filter).toArray();
|
return await this.collection.find(filter).toArray();
|
||||||
}
|
}
|
||||||
|
|
||||||
public async findOne(filter: Object): Promise<T> {
|
public async findOne(filter: object): Promise<T> {
|
||||||
let result = await this.collection.find(filter).limit(1).toArray();
|
let result = await this.collection.find(filter).limit(1).toArray();
|
||||||
return result[0];
|
return result[0];
|
||||||
}
|
}
|
||||||
|
@ -42,20 +26,21 @@ export class MongoDBClient<T = any> implements DBClient {
|
||||||
}
|
}
|
||||||
|
|
||||||
public async insertOne(model: T): Promise<T> {
|
public async insertOne(model: T): Promise<T> {
|
||||||
|
//@ts-ignore
|
||||||
var insert = await this.collection.insertOne(model);
|
var insert = await this.collection.insertOne(model);
|
||||||
return insert.ops[0];
|
return insert.ops[0];
|
||||||
}
|
}
|
||||||
|
|
||||||
public async updateOne(where: any, model: any): Promise<boolean> {
|
public async updateOne(where: any, model: T): Promise<boolean> {
|
||||||
let result = await this.collection.updateOne(where, { $set: model });
|
let result = await this.collection.updateOne(where, { $set: model });
|
||||||
return result.result.ok == 1 && result.result.n > 0;
|
return result.result.ok == 1 && result.result.n > 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async updateById(objectId: string, model: any): Promise<boolean> {
|
public async updateById(objectId: string, model: T): Promise<boolean> {
|
||||||
return await this.updateOne({ _id: new ObjectID(objectId) }, model)
|
return await this.updateOne({ _id: new ObjectID(objectId) }, model)
|
||||||
}
|
}
|
||||||
|
|
||||||
public async deleteOne(where: any): Promise<boolean> {
|
public async deleteOne(where: object): Promise<boolean> {
|
||||||
let result = await this.collection.deleteOne(where);
|
let result = await this.collection.deleteOne(where);
|
||||||
return result.result.ok === 1 && result.result.n > 0
|
return result.result.ok === 1 && result.result.n > 0
|
||||||
}
|
}
|
|
@ -1,18 +0,0 @@
|
||||||
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 db: Db;
|
|
||||||
|
|
||||||
public static async getConnection(): Promise<Db> {
|
|
||||||
if (!this.db) { this.db = await this.connect() }
|
|
||||||
return this.db;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static async connect(): Promise<Db> {
|
|
||||||
let client = await MongoClient.connect(connStr, { useNewUrlParser: true });
|
|
||||||
return client.db(dbName);
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -0,0 +1,7 @@
|
||||||
|
export const TYPE = {
|
||||||
|
URL: Symbol.for('URL'),
|
||||||
|
DB: Symbol.for('DB'),
|
||||||
|
Client: Symbol.for('Client'),
|
||||||
|
Database: Symbol.for('Database'),
|
||||||
|
Collection: Symbol.for('Collection')
|
||||||
|
}
|
|
@ -1,2 +1,3 @@
|
||||||
export * from 'mongodb'
|
export * from 'mongodb'
|
||||||
export * from './client'
|
export * from './client'
|
||||||
|
export * from './constants'
|
||||||
|
|
|
@ -1,4 +0,0 @@
|
||||||
/node_modules
|
|
||||||
/dist
|
|
||||||
/package-lock.json
|
|
||||||
/yarn.lock
|
|
|
@ -1,22 +0,0 @@
|
||||||
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
|
|
|
@ -1,11 +1,12 @@
|
||||||
{
|
{
|
||||||
"name": "@cc-server/db",
|
"name": "@cc-server/db",
|
||||||
"version": "0.6.2",
|
"version": "0.7.0",
|
||||||
"description": "> TODO: description",
|
"description": "> TODO: description",
|
||||||
"author": "MiaoWoo <admin@yumc.pw>",
|
"author": "MiaoWoo <admin@yumc.pw>",
|
||||||
"homepage": "https://github.com/502647092/cc-server-parent#readme",
|
"homepage": "https://github.com/502647092/cc-server-parent#readme",
|
||||||
"license": "ISC",
|
"license": "ISC",
|
||||||
"main": "dist/index.js",
|
"main": "dist/index.js",
|
||||||
|
"typings": "dist/index.d.ts",
|
||||||
"publishConfig": {
|
"publishConfig": {
|
||||||
"registry": "https://repo.yumc.pw/repository/npm-hosted/"
|
"registry": "https://repo.yumc.pw/repository/npm-hosted/"
|
||||||
},
|
},
|
||||||
|
@ -22,8 +23,8 @@
|
||||||
"url": "https://github.com/502647092/cc-server-parent/issues"
|
"url": "https://github.com/502647092/cc-server-parent/issues"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"mocha": "^6.1.4",
|
"mocha": "^7.0.0",
|
||||||
"rimraf": "^2.6.3",
|
"rimraf": "^3.0.0",
|
||||||
"typescript": "^3.5.1"
|
"typescript": "^3.7.4"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
export const DBClient = Symbol.for('DBClient')
|
export const DBClient = Symbol.for('DBClient')
|
||||||
export interface DBClient<T = any> {
|
export interface DBClient<T = any> {
|
||||||
getProvide<P>(): P;
|
getProvide<P>(): P;
|
||||||
setTable(table: string): void;
|
|
||||||
find(filter: object): Promise<T[]>;
|
find(filter: object): Promise<T[]>;
|
||||||
findOne(filter: Object): Promise<T>;
|
findOne(filter: Object): Promise<T>;
|
||||||
findOneById(objectId: string): Promise<T>;
|
findOneById(objectId: string): Promise<T>;
|
||||||
|
|
|
@ -1,4 +0,0 @@
|
||||||
/node_modules
|
|
||||||
/dist
|
|
||||||
/package-lock.json
|
|
||||||
/yarn.lock
|
|
|
@ -1,22 +0,0 @@
|
||||||
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
|
|
|
@ -1,11 +1,12 @@
|
||||||
{
|
{
|
||||||
"name": "@cc-server/ioc",
|
"name": "@cc-server/ioc",
|
||||||
"version": "0.6.2",
|
"version": "0.7.0",
|
||||||
"description": "> TODO: description",
|
"description": "> TODO: description",
|
||||||
"author": "MiaoWoo <admin@yumc.pw>",
|
"author": "MiaoWoo <admin@yumc.pw>",
|
||||||
"homepage": "https://faas.yumc.pw",
|
"homepage": "https://faas.yumc.pw",
|
||||||
"license": "ISC",
|
"license": "ISC",
|
||||||
"main": "dist/index.js",
|
"main": "dist/index.js",
|
||||||
|
"typings": "dist/index.d.ts",
|
||||||
"publishConfig": {
|
"publishConfig": {
|
||||||
"registry": "https://repo.yumc.pw/repository/npm-hosted/"
|
"registry": "https://repo.yumc.pw/repository/npm-hosted/"
|
||||||
},
|
},
|
||||||
|
@ -24,9 +25,9 @@
|
||||||
"reflect-metadata": "^0.1.13"
|
"reflect-metadata": "^0.1.13"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"mocha": "^6.1.4",
|
"mocha": "^7.0.0",
|
||||||
"rimraf": "^2.6.3",
|
"rimraf": "^3.0.0",
|
||||||
"typescript": "^3.5.1"
|
"typescript": "^3.7.4"
|
||||||
},
|
},
|
||||||
"gitHead": "7d84393a3cb6be6be9ed51d71f12677d2d7d0728"
|
"gitHead": "7d84393a3cb6be6be9ed51d71f12677d2d7d0728"
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,4 +0,0 @@
|
||||||
/node_modules
|
|
||||||
/dist
|
|
||||||
/package-lock.json
|
|
||||||
/yarn.lock
|
|
|
@ -1,22 +0,0 @@
|
||||||
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
|
|
|
@ -1,11 +1,12 @@
|
||||||
{
|
{
|
||||||
"name": "@cc-server/ws",
|
"name": "@cc-server/ws",
|
||||||
"version": "0.6.2",
|
"version": "0.7.0",
|
||||||
"description": "> TODO: description",
|
"description": "> TODO: description",
|
||||||
"author": "MiaoWoo <admin@yumc.pw>",
|
"author": "MiaoWoo <admin@yumc.pw>",
|
||||||
"homepage": "https://faas.yumc.pw",
|
"homepage": "https://faas.yumc.pw",
|
||||||
"license": "ISC",
|
"license": "ISC",
|
||||||
"main": "dist/index.js",
|
"main": "dist/index.js",
|
||||||
|
"typings": "dist/index.d.ts",
|
||||||
"publishConfig": {
|
"publishConfig": {
|
||||||
"registry": "https://repo.yumc.pw/repository/npm-hosted/"
|
"registry": "https://repo.yumc.pw/repository/npm-hosted/"
|
||||||
},
|
},
|
||||||
|
@ -19,16 +20,16 @@
|
||||||
"url": "git+https://github.com/502647092/cc-server-parent.git"
|
"url": "git+https://github.com/502647092/cc-server-parent.git"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@cc-server/ioc": "^0.6.2",
|
"@cc-server/ioc": "^0.7.0",
|
||||||
"inversify": "^5.0.1",
|
"inversify": "^5.0.1",
|
||||||
"reflect-metadata": "^0.1.13",
|
"reflect-metadata": "^0.1.13",
|
||||||
"socket.io": "^2.2.0"
|
"socket.io": "^2.3.0"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@types/socket.io": "^2.1.2",
|
"@types/socket.io": "^2.1.4",
|
||||||
"mocha": "^6.1.4",
|
"mocha": "^7.0.0",
|
||||||
"rimraf": "^2.6.3",
|
"rimraf": "^3.0.0",
|
||||||
"typescript": "^3.5.1"
|
"typescript": "^3.7.4"
|
||||||
},
|
},
|
||||||
"gitHead": "7d84393a3cb6be6be9ed51d71f12677d2d7d0728"
|
"gitHead": "7d84393a3cb6be6be9ed51d71f12677d2d7d0728"
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,6 +6,7 @@
|
||||||
"module": "commonjs",
|
"module": "commonjs",
|
||||||
"sourceMap": true,
|
"sourceMap": true,
|
||||||
"declaration": true,
|
"declaration": true,
|
||||||
|
"declarationMap": true,
|
||||||
"noImplicitAny": true,
|
"noImplicitAny": true,
|
||||||
"allowUnreachableCode": true,
|
"allowUnreachableCode": true,
|
||||||
"experimentalDecorators": true,
|
"experimentalDecorators": true,
|
||||||
|
|
Loading…
Reference in New Issue