初始化

This commit is contained in:
robin
2022-08-02 19:21:03 +08:00
parent 73c1343060
commit b7e5ee5fec
27 changed files with 564 additions and 156 deletions
+24 -4
View File
@@ -43,7 +43,11 @@ module.exports = class Api {
* @param {object} value 筛选条件对象.
*/
where(value) {
this.tableWhere = value
if (!this.tableWhere) {
this.tableWhere = value
} else {
Object.assign(this.tableWhere, value)
}
return this
}
@@ -84,12 +88,26 @@ module.exports = class Api {
return this
}
/**
* 模糊查询
* @param {string} value 关键字:你好.
* @param {array} fieldList 字段列表['createdAt'].
*/
search({value, fieldList}) {
if (!value|| !fieldList) return this
if (!this.tableWhere) this.tableWhere = {}
this.tableWhere['$or'] = fieldList.map(item => {
return {[item]: {'$like': `%${value || ''}%`}}
})
return this
}
/**
* 常用时间筛选
* @param {string|array|Number} value 时间内容:按时间段:['2000-1-1','2000-1-2'],按常用时间:day,按最近60分钟:60.
* @param {string} field 时间字段(默认createdAt字段)
*/
time(value, field) {
time({value, field}) {
if (!this.tableName) {
throw "请传表名"
}
@@ -251,7 +269,7 @@ module.exports = class Api {
* @param {number} min 字段减小后的值不能小于最小值
* @param {number} setValue 如果减小后的字段小于min,设置字段值为n
*/
async setDec(min, setValue) {
async setDec({min, setValue}) {
if (!this.tableName) {
throw "请传表名"
}
@@ -317,7 +335,7 @@ module.exports = class Api {
* @param {number} max 字段增加后的值不能大于最大值
* @param {number} setValue 如果增加后的字段大于max,设置字段值为n
*/
async setInc(max, setValue) {
async setInc({max, setValue}) {
if (!this.tableName) {
throw "请传表名"
}
@@ -494,6 +512,7 @@ module.exports = class Api {
...options
}
)
this.init()
return res
}
@@ -523,6 +542,7 @@ module.exports = class Api {
...options
}
)
this.init()
return res
}
+20 -10
View File
@@ -16,7 +16,7 @@ module.exports = async (app) => {
const {database, username, password, options,} = config
const sequelize = new Sequelize(database, username, password, {
...options,
operatorsAliases,
operatorsAliases:operatorsAliases,
});
// 加载model文件
@@ -24,22 +24,32 @@ module.exports = async (app) => {
list.forEach(item => {
const {res, parse} = item
const model = {}
let primaryKey = false
for (let key of Object.keys(res.model)) {
res.model[key].type = Sequelize[res.model[key].type]
model[key] = res.model[key]
//如果表设置了主键,那么就不设置默认主键为id了
if (res.model[key].primaryKey) {
primaryKey = true
res.model[key].allowNull = false
res.model[key].unique = key
res.model[key].autoIncrement = true
}
}
model['id'] = {
type : Sequelize.INTEGER,
comment : '表自增id',
allowNull : false,
unique : 'id',
primaryKey : true,
autoIncrement: true,
//如果没有默认主键
if (!primaryKey) {
model['id'] = {
type : Sequelize.INTEGER,
comment : '表自增id',
allowNull : false,
unique : 'id',
primaryKey : true,
autoIncrement: true,
}
}
sequelize.define(parse.name, model)
})
app.models = sequelize.models
const api = new Api(sequelize, sequelize.models)
app.db = api
+40 -43
View File
@@ -1,46 +1,43 @@
//定义别名
const Sequelize = require("sequelize");
module.exports = () => {
const Op = Sequelize.Op;
//操作符别名
const operatorsAliases = {
$eq: Op.eq,
$ne: Op.ne,
$gte: Op.gte,
$gt: Op.gt,
$lte: Op.lte,
$lt: Op.lt,
$not: Op.not,
$in: Op.in,
$notIn: Op.notIn,
$is: Op.is,
$like: Op.like,
$notLike: Op.notLike,
$iLike: Op.iLike,
$notILike: Op.notILike,
$regexp: Op.regexp,
$notRegexp: Op.notRegexp,
$iRegexp: Op.iRegexp,
$notIRegexp: Op.notIRegexp,
$between: Op.between,
$notBetween: Op.notBetween,
$overlap: Op.overlap,
$contains: Op.contains,
$contained: Op.contained,
$adjacent: Op.adjacent,
$strictLeft: Op.strictLeft,
$strictRight: Op.strictRight,
$noExtendRight: Op.noExtendRight,
$noExtendLeft: Op.noExtendLeft,
$substring: Op.substring,
$startsWith: Op.startsWith,
$endsWith: Op.endsWith,
$and: Op.and,
$or: Op.or,
$any: Op.any,
$all: Op.all,
$values: Op.values,
$col: Op.col
};
return operatorsAliases
const Op = Sequelize.Op;
module.exports = {
$eq : Op.eq,
$ne : Op.ne,
$gte : Op.gte,
$gt : Op.gt,
$lte : Op.lte,
$lt : Op.lt,
$not : Op.not,
$in : Op.in,
$notIn : Op.notIn,
$is : Op.is,
$like : Op.like,
$notLike : Op.notLike,
$iLike : Op.iLike,
$notILike : Op.notILike,
$regexp : Op.regexp,
$notRegexp : Op.notRegexp,
$iRegexp : Op.iRegexp,
$notIRegexp : Op.notIRegexp,
$between : Op.between,
$notBetween : Op.notBetween,
$overlap : Op.overlap,
$contains : Op.contains,
$contained : Op.contained,
$adjacent : Op.adjacent,
$strictLeft : Op.strictLeft,
$strictRight : Op.strictRight,
$noExtendRight: Op.noExtendRight,
$noExtendLeft : Op.noExtendLeft,
$substring : Op.substring,
$startsWith : Op.startsWith,
$endsWith : Op.endsWith,
$and : Op.and,
$or : Op.or,
$any : Op.any,
$all : Op.all,
$values : Op.values,
$col : Op.col
}
+2 -1
View File
@@ -43,7 +43,8 @@ module.exports = class {
database: config.database,
});
this.file = this.app.root + '/' + config.database + '.sql'
await importer.import(this.file)
// const res = await importer.import(this.file)
// console.log(res);
console.timeEnd("reduction")
}
+2 -2
View File
@@ -1,6 +1,6 @@
module.exports = {
port: 6379, // Redis port
host: "192.168.1.26", // Redis host
port: 6380, // Redis port
host: "127.0.0.1", // Redis host
// username: "", // needs Redis >= 6
password: "9715115286",
db: 0, // Defaults to 0