Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "storebox-api",
"version": "1.2.0",
"version": "1.3.0",
"description": "",
"main": "index.js",
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion src/express.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { config } from "@utils/config";
import helmet = require("helmet");

import { error } from "./modules/common/middlewares/logger.middleware";
import { isTest } from "./modules/common/helper/env";
import { isTest } from "@utils/env";

const RedisStore = connectRedis(session);

Expand Down
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import { config } from "@utils/config";
import { initExpress } from "./express";
import { ApplicationModule } from "./modules/app.module";
import { ValidationPipe } from "@pipes/validation";
import { systemLogger } from "./modules/common/helper/log";
import { isDevelopment } from "./modules/common/helper/env";
import { systemLogger } from "@utils/log";
import { isDevelopment } from "@utils/env";

const bootstrap = async () => {
const server = initExpress();
Expand Down
11 changes: 3 additions & 8 deletions src/models/Categroy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,12 @@ import { DEF_PER_COUNT } from "@dtos/page";
import { isArray } from "util";
import { reduce, includes, difference } from "lodash";
import { MongoError } from "mongodb";
import { config } from "@utils/config";
import keyv = require("keyv");
import newCache = require("@utils/newCache");

import { isTest } from "../modules/common/helper/env";
export const FLAG = "categories";

export const cache = new keyv({
uri: isTest ? undefined : config.redis.url,
namespace: "Categories"
});
export const cache = newCache(FLAG);

export const FLAG = "categories";
export type CategoryDoc = IDoc<ICategory>;

const Definition: SchemaDefinition = {
Expand Down
12 changes: 3 additions & 9 deletions src/models/Collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,12 @@ import {
} from "@models/common";
import { IGoods, FLAG as GoodFlag, Model as GoodsModels } from "@models/Good";
import { IUser, FLAG as UserFlag } from "@models/User";
import { config } from "@utils/config";
import keyv = require("keyv");

import { isTest } from "../modules/common/helper/env";

export const cache = new keyv({
uri: isTest ? undefined : config.redis.url,
namespace: "Collections"
});
import newCache = require("@utils/newCache");

export const FLAG = "collections";

export const cache = newCache(FLAG);

const Definition: SchemaDefinition = {
name: {
type: String,
Expand Down
11 changes: 3 additions & 8 deletions src/models/Good.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,12 @@ import {
import { IValues, Flag as ValueFlag } from "@models/Value";
import { IUser, FLAG as UserFlag } from "@models/User";
import { ICategory, FLAG as CategoryFlag } from "@models/Categroy";
import { config } from "@utils/config";
import keyv = require("keyv");
import newCache = require("@utils/newCache");

import { isTest } from "../modules/common/helper/env";
export const FLAG = "goods";

export const cache = new keyv({
uri: isTest ? undefined : config.redis.url,
namespace: "Goods"
});
export const cache = newCache(FLAG);

export const FLAG = "goods";
export type GoodDoc = IDoc<IGoods>;

const Definition: SchemaDefinition = {
Expand Down
13 changes: 4 additions & 9 deletions src/models/Regexp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,13 @@ import {
import { ICategory, FLAG as CF, Model as CM } from "@models/Categroy";
import { DEF_PER_COUNT } from "@dtos/page";
import isRegExp = require("@utils/isRegExp");
import { config } from "@utils/config";
import keyv = require("keyv");
import newCache = require("@utils/newCache");

import { INewRegexp } from "../modules/regexps/regexps.dto";
import { isTest } from "../modules/common/helper/env";

export const cache = new keyv({
uri: isTest ? undefined : config.redis.url,
namespace: "Regexps"
});
export const FLAG = "regexps";

export const cache = newCache(FLAG);

const Definition: SchemaDefinition = {
name: { type: String, required: true, unique: true },
Expand Down Expand Up @@ -69,8 +66,6 @@ RegexpSchema.static("removeRegexp", (id: ObjectId) => {

// endregion static methods

export const FLAG = "regexps";

interface IRegexpModel<T extends RegexpDoc> extends M<T> {
/**
* 创建新规则
Expand Down
14 changes: 4 additions & 10 deletions src/models/System.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
import { model, SchemaDefinition, Model as M } from "mongoose";
import { Base, IDoc, IDocRaw, MODIFY_MOTHODS } from "./common";
import { config } from "@utils/config";
import keyv = require("keyv");
import newCache = require("@utils/newCache");

import { isTest } from "../modules/common/helper/env";
export const FLAG = "system";

export const cache = new keyv({
uri: isTest ? undefined : config.redis.url,
namespace: "System"
});
export const cache = newCache(FLAG);

const Definition: SchemaDefinition = {
key: { type: String, required: true },
Expand All @@ -22,8 +18,6 @@ export interface ISystem extends IDocRaw {

const SystemSchema = new Base(Definition).createSchema();

export const Flag = "system";

export type SystemDoc = IDoc<ISystem>;

for (const method of MODIFY_MOTHODS) {
Expand All @@ -32,4 +26,4 @@ for (const method of MODIFY_MOTHODS) {
});
}

export const Model: M<SystemDoc> = model(Flag, SystemSchema);
export const Model: M<SystemDoc> = model(FLAG, SystemSchema);
14 changes: 4 additions & 10 deletions src/models/Token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,11 @@ import {
Base, IDoc, IDocRaw, ObjectId, MODIFY_MOTHODS, existsValidator
} from "@models/common";
import { IUser, FLAG as UserFlag } from "@models/User";
import { config } from "@utils/config";
import keyv = require("keyv");
import newCache = require("@utils/newCache");

import { isTest } from "../modules/common/helper/env";
export const FLAG = "tokens";

export const cache = new keyv({
uri: isTest ? undefined : config.redis.url,
namespace: "Tokens"
});

export const Flag = "tokens";
export const cache = newCache(FLAG);

const Definition: SchemaDefinition = {
token: { type: String, unique: true, index: true },
Expand Down Expand Up @@ -64,7 +58,7 @@ for (const method of MODIFY_MOTHODS) {
});
}

export const Model = model(Flag, TokensSchema) as M<TokenDoc>;
export const Model = model(FLAG, TokensSchema) as M<TokenDoc>;

const getCount = (userId: ObjectId): Promise<number> => {
return Model.count({ user: userId }).exec();
Expand Down
11 changes: 3 additions & 8 deletions src/models/User.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,12 @@ import { config } from "@utils/config";
import { ObjectId, existsValidator } from "@models/common";
import { DEF_PER_COUNT } from "@dtos/page";
import { Base, IDoc, IDocRaw, MODIFY_MOTHODS } from "./common";
import keyv = require("keyv");

import { isTest } from "../modules/common/helper/env";

export const cache = new keyv({
uri: isTest ? undefined : config.redis.url,
namespace: "Users"
});
import newCache = require("@utils/newCache");

export const FLAG = "users";

export const cache = newCache(FLAG);

const Definition: SchemaDefinition = {
username: { type: String, required: true, trim: true },
password: { type: String, required: true },
Expand Down
3 changes: 3 additions & 0 deletions src/models/Usergroup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@ import { model, SchemaDefinition, Model as M, SchemaTypes } from "mongoose";
import { FLAG as UF, IUser } from "@models/User";
import { ObjectId, existsValidator } from "@models/common";
import { Base, IDoc, IDocRaw } from "./common";
import newCache = require("@utils/newCache");

const Definition: SchemaDefinition = {
name: { type: String, required: true }
};

export const FLAG = "usergroups";

export const cache = newCache(FLAG);

export interface IUsergroups extends IDocRaw {
name: string;
}
Expand Down
2 changes: 1 addition & 1 deletion src/modules/common/middlewares/logger.middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
} from "@nestjs/common";
import {
getMeta, downloadLogger, apiLogger, accessLogger, systemLogger
} from "../helper/log";
} from "@utils/log";

const access: RequestHandler = (req, res, next) => {
// const logger = accessLogger;
Expand Down
2 changes: 1 addition & 1 deletion src/modules/database/database.providers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Model as UsersModel } from "@models/User";
import { Model as UsergroupsModel } from "@models/Usergroup";
import { Model as UserUsergroupsModel } from "@models/User-Usergroup";
import { SystemService } from "@services/system";
import { systemLogger } from "../common/helper/log";
import { systemLogger } from "@utils/log";

const getDatabaseUrl = () => {
let host: string[] = [ ];
Expand Down
13 changes: 5 additions & 8 deletions src/modules/files/files.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,10 @@ export class FilesController {
public async downloadFile(
@Req() req, @Res() res: Response, @Param() params: DownlaodDto
) {
let obj: GoodDoc;
try {
obj = (await this.goodsSvr.get({
_id: params.id, category: params.cid
}))[0];
} catch (error) {
throw new BadRequestException(error.toString());
}
const obj = (await this.goodsSvr.get({
_id: params.id, category: params.cid
}))[0];

if (!obj) {
throw new NotFoundException();
}
Expand All @@ -57,6 +53,7 @@ export class FilesController {
throw new BadRequestException("Disallow download the File");
}
res.download(filepath, good.originname, (err) => {
/* istanbul ignore if */
if (err) {
// Recode Error
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ abstract class ModelService<D extends IDocRaw> {
}

private checkModel() {
/* istanbul ignore if */
if (!this.model) {
throw new BadGatewayException("Lost Model");
}
Expand All @@ -52,33 +53,36 @@ abstract class ModelService<D extends IDocRaw> {

public async create(obj: object) {
this.checkModel();
await this.beforeAll();
await this.beforeEach();
await this.runBeforeAll();
await this.runBeforeEach();
try {
return await this.model.create(obj);
} catch (error) {
/* istanbul ignore next */
throw new BadRequestException(error.toString());
}
}

public async delete(cond: object) {
this.checkModel();
await this.beforeAll();
await this.beforeEach();
await this.runBeforeAll();
await this.runBeforeEach();
try {
return await this.model.findOneAndRemove(cond).exec();
} catch (error) {
/* istanbul ignore next */
throw new BadRequestException(error.toString());
}
}

public async deleteById(id: ObjectId) {
this.checkModel();
await this.beforeAll();
await this.beforeEach();
await this.runBeforeAll();
await this.runBeforeEach();
try {
return await this.model.findByIdAndRemove(id).exec();
} catch (error) {
/* istanbul ignore next */
throw new BadRequestException(error.toString());
}
}
Expand All @@ -87,20 +91,21 @@ abstract class ModelService<D extends IDocRaw> {
id: ObjectId, ctx: object, opts = this.DEF_UPDATE_OPTIONS
) {
this.checkModel();
await this.beforeAll();
await this.beforeEach();
await this.runBeforeAll();
await this.runBeforeEach();
const options = Object.assign({ }, this.DEF_UPDATE_OPTIONS, opts);
try {
return await this.model.update({ _id: id }, ctx, options).exec();
} catch (error) {
/* istanbul ignore next */
throw new BadRequestException(error.toString());
}
}

protected async find(cond: object, opts?: IGetOptions) {
this.checkModel();
await this.beforeAll();
await this.beforeEach();
await this.runBeforeAll();
await this.runBeforeEach();
const p = this.model.find(cond);
return this.documentQueryProcess(p, opts).exec();
}
Expand All @@ -113,8 +118,8 @@ abstract class ModelService<D extends IDocRaw> {

protected async findOne(cond: object, opts?: IGetOptions) {
this.checkModel();
await this.beforeAll();
await this.beforeEach();
await this.runBeforeAll();
await this.runBeforeEach();
const p = this.model.findOne(cond);
return this.documentQueryProcess(p, opts).exec();
}
Expand All @@ -127,8 +132,8 @@ abstract class ModelService<D extends IDocRaw> {

protected async findById(id: ObjectId, opts?: IGetOptions) {
this.checkModel();
await this.beforeAll();
await this.beforeEach();
await this.runBeforeAll();
await this.runBeforeEach();
const p = this.model.findById(id);
return this.documentQueryProcess(p, opts).exec();
}
Expand All @@ -141,8 +146,8 @@ abstract class ModelService<D extends IDocRaw> {

protected async total(cond: object = { }) {
this.checkModel();
await this.beforeAll();
await this.beforeEach();
await this.runBeforeAll();
await this.runBeforeEach();
return this.model.count(cond).exec();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { Component, BadRequestException } from "@nestjs/common";
import { ObjectId } from "@models/common";
import { Model as CategoriesModel, cache, ICategoryRaw, ICategory } from "@models/Categroy";
import {
Model as CategoriesModel, cache, ICategoryRaw, ICategory
} from "@models/Categroy";
import { isFunction, isArray } from "util";
import { BaseService, IGetOptions } from "@services/base";
import { difference } from "lodash";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
} from "@models/Collection";
import { ObjectId } from "@models/common";
import { DEF_PER_COUNT } from "@dtos/page";
import { IEditCollection } from "../../../modules/collections/collections.dto";
import { IEditCollection } from "../modules/collections/collections.dto";
import { BaseService, IGetOptions } from "@services/base";

@Component()
Expand Down
Loading