Compare commits
1 Commits
Author | SHA1 | Date |
---|---|---|
MiaoWoo | f13e98568b |
|
@ -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 './activation'
|
||||
export * from 'inversify-express-utils'
|
||||
export { get, post }
|
||||
export { get, post, put, patch, del }
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
import 'reflect-metadata';
|
||||
import * as fs from 'fs';
|
||||
import * as http from 'http';
|
||||
import * as path from 'path';
|
||||
import * as globby from "globby";
|
||||
import * as express from "express";
|
||||
import * as prettyjson from "prettyjson";
|
||||
|
@ -104,11 +105,18 @@ export class CcServerBoot {
|
|||
return this;
|
||||
}
|
||||
|
||||
public scan(path: fs.PathLike) {
|
||||
let files = fs.readdirSync(path);
|
||||
for (const file of files) {
|
||||
|
||||
public scan(scanDir: string) {
|
||||
let files = fs.readdirSync(scanDir);
|
||||
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() {
|
||||
|
|
|
@ -1,17 +1,14 @@
|
|||
import { CcServerBoot, express } from './index'
|
||||
|
||||
import './function/http';
|
||||
import './function/websocket';
|
||||
import { CcServerBoot } from './index'
|
||||
import { DBClient } from '@cc-server/db';
|
||||
import { MongoClient } from 'mongodb';
|
||||
import { MongoCollection } from '@cc-server/db-mongo';
|
||||
|
||||
let boot = new CcServerBoot().static('public');
|
||||
let boot = new CcServerBoot().scan(__dirname + '/function');
|
||||
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();
|
||||
boot.start();
|
||||
}
|
||||
})
|
||||
|
|
|
@ -16,7 +16,7 @@ export class MongoCollection<T = any> implements DBClient {
|
|||
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();
|
||||
return result[0];
|
||||
}
|
||||
|
@ -26,20 +26,21 @@ export class MongoCollection<T = any> implements DBClient {
|
|||
}
|
||||
|
||||
public async insertOne(model: T): Promise<T> {
|
||||
//@ts-ignore
|
||||
var insert = await this.collection.insertOne(model);
|
||||
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 });
|
||||
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)
|
||||
}
|
||||
|
||||
public async deleteOne(where: any): Promise<boolean> {
|
||||
public async deleteOne(where: object): Promise<boolean> {
|
||||
let result = await this.collection.deleteOne(where);
|
||||
return result.result.ok === 1 && result.result.n > 0
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue