更新
This commit is contained in:
+49
-49
@@ -2,22 +2,22 @@
|
||||
const Sequelize = require("sequelize");
|
||||
const xe = require("xe-utils")
|
||||
module.exports = class Api {
|
||||
constructor(sequelize,models) {
|
||||
constructor(sequelize, models) {
|
||||
this.sequelize = sequelize
|
||||
this.models = models
|
||||
this.init()
|
||||
}
|
||||
|
||||
init() {
|
||||
this.tableName = ""
|
||||
this.tableWhere = ""
|
||||
this.tableData = ""
|
||||
this.tablePage = ""
|
||||
this.tableLimit = ""
|
||||
this.tableOrder = ""
|
||||
this.tableGroup = ""
|
||||
this.tableAttributes = ""
|
||||
this.t = ""
|
||||
this.tableName = null
|
||||
this.tableWhere = null
|
||||
this.tableData = null
|
||||
this.tablePage = null
|
||||
this.tableLimit = null
|
||||
this.tableOrder = null
|
||||
this.tableGroup = null
|
||||
this.tableAttributes = null
|
||||
this.t = null
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -166,7 +166,7 @@ module.exports = class Api {
|
||||
}
|
||||
const res = await this.models[this.tableName].findOne(
|
||||
{
|
||||
where: this.tableWhere,
|
||||
where : this.tableWhere,
|
||||
transaction: this.t
|
||||
}
|
||||
)
|
||||
@@ -184,12 +184,12 @@ module.exports = class Api {
|
||||
}
|
||||
const res = await this.models[this.tableName].findAll(
|
||||
{
|
||||
where: this.tableWhere,
|
||||
offset: (this.tablePage && this.tableLimit) ? this.tablePage - 1 * this.tableLimit : null,
|
||||
limit: this.tableLimit,
|
||||
order: this.tableOrder || [['updatedAt', 'DESC']], // 时间排序
|
||||
group: this.tableGroup,
|
||||
attributes: this.tableAttributes,
|
||||
where : this.tableWhere,
|
||||
offset : (this.tablePage && this.tableLimit) ? this.tablePage - 1 * this.tableLimit : null,
|
||||
limit : this.tableLimit,
|
||||
order : this.tableOrder || [['updatedAt', 'DESC']], // 时间排序
|
||||
group : this.tableGroup,
|
||||
attributes : this.tableAttributes,
|
||||
transaction: this.t
|
||||
}
|
||||
)
|
||||
@@ -210,8 +210,8 @@ module.exports = class Api {
|
||||
}
|
||||
const res = await this.models[this.tableName].findOrCreate(
|
||||
{
|
||||
defaults: this.tableData,
|
||||
where: this.tableWhere,
|
||||
defaults : this.tableData,
|
||||
where : this.tableWhere,
|
||||
transaction: this.t
|
||||
}
|
||||
)
|
||||
@@ -232,12 +232,12 @@ module.exports = class Api {
|
||||
}
|
||||
const {count, rows} = await this.models[this.tableName].findAndCountAll(
|
||||
{
|
||||
where: this.tableWhere,
|
||||
offset: (this.tablePage && this.tableLimit) ? this.tablePage - 1 * this.tableLimit : null,
|
||||
limit: this.tableLimit,
|
||||
order: this.tableOrder || [['updatedAt', 'DESC']], // 时间排序
|
||||
group: this.tableGroup,
|
||||
attributes: this.tableAttributes,
|
||||
where : this.tableWhere,
|
||||
offset : (this.tablePage && this.tableLimit) ? this.tablePage - 1 * this.tableLimit : null,
|
||||
limit : this.tableLimit,
|
||||
order : this.tableOrder || [['updatedAt', 'DESC']], // 时间排序
|
||||
group : this.tableGroup,
|
||||
attributes : this.tableAttributes,
|
||||
transaction: this.t
|
||||
}
|
||||
)
|
||||
@@ -264,7 +264,7 @@ module.exports = class Api {
|
||||
|
||||
let res = await this.models[this.tableName].findOne(
|
||||
{
|
||||
where: this.tableWhere,
|
||||
where : this.tableWhere,
|
||||
transaction: this.t
|
||||
}
|
||||
)
|
||||
@@ -279,7 +279,7 @@ module.exports = class Api {
|
||||
await this.models[this.tableName].update(
|
||||
data,
|
||||
{
|
||||
where: this.tableWhere,
|
||||
where : this.tableWhere,
|
||||
transaction: this.t
|
||||
}
|
||||
)
|
||||
@@ -296,14 +296,14 @@ module.exports = class Api {
|
||||
await this.models[this.tableName].decrement(
|
||||
this.tableData,
|
||||
{
|
||||
where: this.tableWhere,
|
||||
where : this.tableWhere,
|
||||
transaction: this.t
|
||||
}
|
||||
)
|
||||
|
||||
res = await this.models[this.tableName].findOne(
|
||||
{
|
||||
where: this.tableWhere,
|
||||
where : this.tableWhere,
|
||||
transaction: this.t
|
||||
}
|
||||
)
|
||||
@@ -329,7 +329,7 @@ module.exports = class Api {
|
||||
}
|
||||
let res = await this.models[this.tableName].findOne(
|
||||
{
|
||||
where: this.tableWhere,
|
||||
where : this.tableWhere,
|
||||
transaction: this.t
|
||||
}
|
||||
)
|
||||
@@ -344,7 +344,7 @@ module.exports = class Api {
|
||||
await this.models[this.tableName].update(
|
||||
data,
|
||||
{
|
||||
where: this.tableWhere,
|
||||
where : this.tableWhere,
|
||||
transaction: this.t
|
||||
}
|
||||
)
|
||||
@@ -361,14 +361,14 @@ module.exports = class Api {
|
||||
await this.models[this.tableName].increment(
|
||||
this.tableData,
|
||||
{
|
||||
where: this.tableWhere,
|
||||
where : this.tableWhere,
|
||||
transaction: this.t
|
||||
}
|
||||
)
|
||||
|
||||
res = await this.models[this.tableName].findOne(
|
||||
{
|
||||
where: this.tableWhere,
|
||||
where : this.tableWhere,
|
||||
transaction: this.t
|
||||
}
|
||||
)
|
||||
@@ -387,10 +387,10 @@ module.exports = class Api {
|
||||
const res = await this.models[this.tableName].count(
|
||||
this.tableData,
|
||||
{
|
||||
where: this.tableWhere,
|
||||
order: this.tableOrder || [['updatedAt', 'DESC']], // 时间排序
|
||||
group: this.tableGroup,
|
||||
attributes: this.tableAttributes,
|
||||
where : this.tableWhere,
|
||||
order : this.tableOrder || [['updatedAt', 'DESC']], // 时间排序
|
||||
group : this.tableGroup,
|
||||
attributes : this.tableAttributes,
|
||||
transaction: this.t
|
||||
}
|
||||
)
|
||||
@@ -408,10 +408,10 @@ module.exports = class Api {
|
||||
const res = await this.models[this.tableName].sum(
|
||||
this.tableData,
|
||||
{
|
||||
where: this.tableWhere,
|
||||
order: this.tableOrder || [['updatedAt', 'DESC']], // 时间排序
|
||||
group: this.tableGroup,
|
||||
attributes: this.tableAttributes,
|
||||
where : this.tableWhere,
|
||||
order : this.tableOrder || [['updatedAt', 'DESC']], // 时间排序
|
||||
group : this.tableGroup,
|
||||
attributes : this.tableAttributes,
|
||||
transaction: this.t
|
||||
}
|
||||
)
|
||||
@@ -429,10 +429,10 @@ module.exports = class Api {
|
||||
const res = await this.models[this.tableName].max(
|
||||
this.tableData,
|
||||
{
|
||||
where: this.tableWhere,
|
||||
order: this.tableOrder || [['updatedAt', 'DESC']], // 时间排序
|
||||
group: this.tableGroup,
|
||||
attributes: this.tableAttributes,
|
||||
where : this.tableWhere,
|
||||
order : this.tableOrder || [['updatedAt', 'DESC']], // 时间排序
|
||||
group : this.tableGroup,
|
||||
attributes : this.tableAttributes,
|
||||
transaction: this.t
|
||||
}
|
||||
)
|
||||
@@ -450,10 +450,10 @@ module.exports = class Api {
|
||||
const res = await this.models[this.tableName].min(
|
||||
this.tableData,
|
||||
{
|
||||
where: this.tableWhere,
|
||||
order: this.tableOrder || [['updatedAt', 'DESC']], // 时间排序
|
||||
group: this.tableGroup,
|
||||
attributes: this.tableAttributes,
|
||||
where : this.tableWhere,
|
||||
order : this.tableOrder || [['updatedAt', 'DESC']], // 时间排序
|
||||
group : this.tableGroup,
|
||||
attributes : this.tableAttributes,
|
||||
transaction: this.t
|
||||
}
|
||||
)
|
||||
|
||||
+16
-4
@@ -1,11 +1,11 @@
|
||||
module.exports = {
|
||||
database: "bamboo",
|
||||
username: "bamboo",
|
||||
password: "bamboo",
|
||||
username: "root",
|
||||
password: "123456",
|
||||
options: {
|
||||
dialect: 'mysql',
|
||||
host: "192.168.1.26",
|
||||
port: 3306,
|
||||
host: "127.0.0.1",
|
||||
port: 3357,
|
||||
// 禁用日志记录或提供自定义日志记录功能;默认值:console.log
|
||||
// logging: false,
|
||||
// model的全局配置
|
||||
@@ -44,4 +44,16 @@ module.exports = {
|
||||
},
|
||||
},
|
||||
path:"app/*/model/*.js",
|
||||
sqlite:{
|
||||
host: 'localhost',
|
||||
dialect: 'sqlite',
|
||||
pool: {
|
||||
max: 5,
|
||||
min: 0,
|
||||
acquire: 30000,
|
||||
idle: 10000
|
||||
},
|
||||
storage: './database.sqlite',
|
||||
operatorsAliases: false
|
||||
}
|
||||
}
|
||||
|
||||
+12
-5
@@ -2,6 +2,7 @@
|
||||
* mysql/sequelize
|
||||
*
|
||||
* 参考文档:
|
||||
* https://demopark.github.io/sequelize-docs-Zh-CN/
|
||||
* https://sequelize.org/
|
||||
* https://sequelize.org/api/v6/identifiers
|
||||
* https://www.sequelize.com.cn/
|
||||
@@ -10,6 +11,7 @@ const config = require("./config")
|
||||
const Sequelize = require("sequelize");
|
||||
const operatorsAliases = require("./operatorsAliases");
|
||||
const Api = require("./api");
|
||||
const Tool = require("./tool.js");
|
||||
module.exports = async (app) => {
|
||||
const {database, username, password, options,} = config
|
||||
const sequelize = new Sequelize(database, username, password, {
|
||||
@@ -27,11 +29,11 @@ module.exports = async (app) => {
|
||||
model[key] = res.model[key]
|
||||
}
|
||||
model['id'] = {
|
||||
type: Sequelize.INTEGER,
|
||||
comment: '表自增id',
|
||||
allowNull: false,
|
||||
unique: 'id',
|
||||
primaryKey: true,
|
||||
type : Sequelize.INTEGER,
|
||||
comment : '表自增id',
|
||||
allowNull : false,
|
||||
unique : 'id',
|
||||
primaryKey : true,
|
||||
autoIncrement: true,
|
||||
}
|
||||
sequelize.define(parse.name, model)
|
||||
@@ -41,6 +43,10 @@ module.exports = async (app) => {
|
||||
app.models = sequelize.models
|
||||
const api = new Api(sequelize, sequelize.models)
|
||||
app.db = api
|
||||
app.db.tool = new Tool(app, sequelize)
|
||||
//全局变量
|
||||
app.alias["$db"] = app.db
|
||||
|
||||
// console.log(api);
|
||||
// console.log(await app.db.table("User").find());
|
||||
|
||||
@@ -60,6 +66,7 @@ module.exports = async (app) => {
|
||||
}
|
||||
|
||||
})
|
||||
// await app.db.tool.reduction()
|
||||
|
||||
})
|
||||
|
||||
|
||||
@@ -0,0 +1,50 @@
|
||||
const config = require("./config")
|
||||
import mysqldump from 'mysqldump';
|
||||
|
||||
const Importer = require('mysql-import');
|
||||
|
||||
module.exports = class {
|
||||
constructor(app, sequelize) {
|
||||
this.app = app
|
||||
this.sequelize = sequelize
|
||||
this.file = this.app.root + '/' + config.database + '.sql'
|
||||
}
|
||||
|
||||
//备份数据库所有表
|
||||
async backup(file) {
|
||||
this.file = file || this.app.root + '/' + config.database + '.sql'
|
||||
console.log('备份数据库中...');
|
||||
await mysqldump({
|
||||
connection: {
|
||||
host : config.options.host,
|
||||
port : config.options.port,
|
||||
user : config.username,
|
||||
password: config.password,
|
||||
database: config.database,
|
||||
|
||||
},
|
||||
dumpToFile: this.file,
|
||||
});
|
||||
console.log('备份数据库完成:', this.file);
|
||||
}
|
||||
//还原备份sql文件
|
||||
async reduction() {
|
||||
console.time("reduction")
|
||||
//先备份旧数据
|
||||
await this.backup(this.app.root + '/' + 'extend/mysql/backup/' + Date.now() + '.sql')
|
||||
//清空数据库
|
||||
await this.sequelize.drop()
|
||||
//导入sql文件
|
||||
const importer = new Importer({
|
||||
host : config.options.host,
|
||||
port : config.options.port,
|
||||
user : config.username,
|
||||
password: config.password,
|
||||
database: config.database,
|
||||
});
|
||||
this.file = this.app.root + '/' + config.database + '.sql'
|
||||
await importer.import(this.file)
|
||||
console.timeEnd("reduction")
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user