Init Project...

This commit is contained in:
MiaoWoo 2019-06-10 18:56:29 +08:00
commit 1746ffa773
25 changed files with 358 additions and 0 deletions

4
.gitignore vendored Normal file
View File

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

8
lerna.json Normal file
View File

@ -0,0 +1,8 @@
{
"npmClient": "yarn",
"useWorkspaces": true,
"packages": [
"packages/*"
],
"version": "0.0.1"
}

14
package.json Normal file
View File

@ -0,0 +1,14 @@
{
"private": true,
"name": "cc-server-parent",
"version": "1.0.0",
"main": "index.js",
"author": "MiaoWoo <admin@yumc.pw>",
"license": "MIT",
"workspaces": [
"packages/*"
],
"devDependencies": {
"lerna": "^3.14.1"
}
}

4
packages/cc-server-core/.gitignore vendored Normal file
View File

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

View File

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

View File

@ -0,0 +1,29 @@
{
"name": "cc-server-core",
"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": {
"dev": "npx ts-node src/index.ts",
"test": "echo \"Error: run tests from root\" && exit 1"
},
"dependencies": {
"body-parser": "^1.19.0",
"cc-server-db-mongo": "^0.0.1",
"cc-server-ioc": "^0.0.1",
"inversify": "^5.0.1",
"inversify-express-utils": "^6.3.2",
"reflect-metadata": "^0.1.13",
"typescript": "^3.5.1"
},
"devDependencies": {
"@types/body-parser": "^1.17.0",
"ts-node": "^8.2.0"
}
}

View File

@ -0,0 +1,22 @@
import {
controller, httpGet, httpPost, httpPut, httpDelete
} from 'inversify-express-utils';
import { inject } from 'inversify';
import { MongoDBClient } from 'cc-server-db-mongo'
@controller('/')
export class UserController {
constructor(
@inject(MongoDBClient) private MongoDBClient: MongoDBClient
) { }
@httpGet('/')
public async getUsers(): Promise<any[]> {
return []
}
@httpPost('/')
public async newUser(): Promise<any> {
return {}
}
}

View File

@ -0,0 +1,22 @@
{
"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"
]
}

View File

@ -0,0 +1,32 @@
import 'reflect-metadata';
import { InversifyExpressServer } from 'inversify-express-utils';
import { Container } from 'inversify';
import * as bodyParser from 'body-parser';
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()
}
// start the server
let server = new InversifyExpressServer(container);
server.setConfig((app) => {
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(bodyParser.json());
app.use(bodyParser.raw())
});
let serverInstance = server.build();
serverInstance.listen(80);
console.log('Server started on port 80 :)');

View File

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

View File

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

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,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"
}
}

View 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) });
}
}

View 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);
});
}
}

View File

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

View File

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

4
packages/cc-server-ioc/.gitignore vendored Normal file
View File

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

View File

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

View File

@ -0,0 +1,20 @@
{
"name": "cc-server-ioc",
"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": {
"inversify": "^5.0.1",
"reflect-metadata": "^0.1.13"
},
"devDependencies": {}
}

View File

@ -0,0 +1,4 @@
export const METADATA_KEY = {
container: "cc-server-core:container",
service: "cc-server-core:service"
};

View File

@ -0,0 +1,16 @@
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 }

View File

@ -0,0 +1,2 @@
export * from './constant/types'
export * from './decorators'

View File

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

10
tsconfig.json Normal file
View File

@ -0,0 +1,10 @@
{
"compilerOptions": {
"target": "es6",
"module": "commonjs",
"sourceMap": true,
"allowUnreachableCode": true,
"experimentalDecorators": true,
"emitDecoratorMetadata": true
}
}