fix: db错误

This commit is contained in:
ningmengchongshui 2024-06-15 12:28:49 +08:00
parent b790a79f98
commit fc1020d310
5 changed files with 38 additions and 103 deletions

@ -0,0 +1 @@
Subproject commit 0a44a0f65aa24c5ca97c9a7af3ecf3f3676e990e

View File

@ -1,29 +1,17 @@
import { Sequelize, DataTypes, Model } from 'sequelize' import { Sequelize, DataTypes, Model } from 'sequelize'
import { Data } from '../miao.js' import { Data } from '#miao'
import { join } from 'path'
/**
*
*/
Data.createDir('/data/db', 'root') Data.createDir('/data/db', 'root')
/** // TODO DB自定义
* DB自定义 const sequelize = new Sequelize({
*/
export const sequelize = new Sequelize({
dialect: 'sqlite', dialect: 'sqlite',
storage: join(process.cwd(), '/data/db/data.db'), storage: process.cwd() + '/data/db/data.db',
logging: false logging: false
}) })
/**
*
*/
await sequelize.authenticate() await sequelize.authenticate()
/**
*
*/
export default class BaseModel extends Model { export default class BaseModel extends Model {
static Types = DataTypes static Types = DataTypes
@ -34,3 +22,4 @@ export default class BaseModel extends Model {
model.COLUMNS = columns model.COLUMNS = columns
} }
} }
export { sequelize }

View File

@ -1,28 +1,26 @@
import BaseModel from './BaseModel.js' import BaseModel from './BaseModel.js'
import { DataTypes } from 'sequelize'
/** const { Types } = BaseModel
*
*/
const COLUMNS = { const COLUMNS = {
// 用户IDqq为数字 // 用户IDqq为数字
ltuid: { ltuid: {
type: DataTypes.INTEGER, type: Types.INTEGER,
primaryKey: true primaryKey: true
}, },
// MysUser类型mys / hoyolab // MysUser类型mys / hoyolab
type: { type: {
type: DataTypes.STRING, type: Types.STRING,
defaultValue: 'mys', defaultValue: 'mys',
notNull: true notNull: true
}, },
// CK // CK
ck: DataTypes.STRING, ck: Types.STRING,
device: DataTypes.STRING, device: Types.STRING,
uids: { uids: {
type: DataTypes.STRING, type: Types.STRING,
get() { get() {
let data = this.getDataValue('uids') let data = this.getDataValue('uids')
let ret = {} let ret = {}
@ -39,15 +37,7 @@ const COLUMNS = {
} }
} }
/**
*
*/
class MysUserDB extends BaseModel { class MysUserDB extends BaseModel {
ck = null
type = null
device = null
uids = null
static async find(ltuid = '', create = false) { static async find(ltuid = '', create = false) {
// DB查询 // DB查询
let mys = await MysUserDB.findByPk(ltuid) let mys = await MysUserDB.findByPk(ltuid)
@ -59,6 +49,11 @@ class MysUserDB extends BaseModel {
return mys || false return mys || false
} }
ck = null
type = null
device = null
uids = null
async saveDB(mys) { async saveDB(mys) {
if (!mys.ck || !mys.device || !mys.db) { if (!mys.ck || !mys.device || !mys.db) {
return false return false
@ -72,17 +67,7 @@ class MysUserDB extends BaseModel {
} }
} }
/**
*
*/
BaseModel.initDB(MysUserDB, COLUMNS) BaseModel.initDB(MysUserDB, COLUMNS)
/**
*
*/
await MysUserDB.sync() await MysUserDB.sync()
/**
*
*/
export default MysUserDB export default MysUserDB

View File

@ -1,34 +1,34 @@
import BaseModel from './BaseModel.js' import BaseModel from './BaseModel.js'
import lodash from 'lodash' import lodash from 'lodash'
import { UserGameDB } from './index.js'
import MysUtil from '../mys/MysUtil.js' import MysUtil from '../mys/MysUtil.js'
import { DataTypes } from 'sequelize' import MysUserDB from './MysUserDB.js'
const { Types } = BaseModel
/**
*
*/
const COLUMNS = { const COLUMNS = {
// 用户IDqq为数字 // 用户IDqq为数字
id: { id: {
type: DataTypes.STRING, type: Types.STRING,
autoIncrement: false, autoIncrement: false,
primaryKey: true primaryKey: true
}, },
type: { type: {
type: DataTypes.STRING, type: Types.STRING,
defaultValue: 'qq', defaultValue: 'qq',
notNull: true notNull: true
}, },
// 昵称 // 昵称
name: DataTypes.STRING, name: Types.STRING,
// 头像 // 头像
face: DataTypes.STRING, face: Types.STRING,
ltuids: DataTypes.STRING, ltuids: Types.STRING,
games: { games: {
type: DataTypes.STRING, type: Types.STRING,
get() { get() {
let data = this.getDataValue('games') let data = this.getDataValue('games')
let ret = {} let ret = {}
@ -50,19 +50,10 @@ const COLUMNS = {
this.setDataValue('games', JSON.stringify(data)) this.setDataValue('games', JSON.stringify(data))
} }
}, },
data: DataTypes.STRING data: Types.STRING
} }
/**
*
*/
class UserDB extends BaseModel { class UserDB extends BaseModel {
/**
*
* @param id
* @param type
* @returns
*/
static async find(id, type = 'qq') { static async find(id, type = 'qq') {
// user_id // user_id
id = type === 'qq' ? '' + id : type + id id = type === 'qq' ? '' + id : type + id
@ -77,16 +68,9 @@ class UserDB extends BaseModel {
return user return user
} }
games = null
ltuids = null
/**
*
* @param user
*/
async saveDB(user) { async saveDB(user) {
const db = this let db = this
const ltuids = [] let ltuids = []
lodash.forEach(user.mysUsers, mys => { lodash.forEach(user.mysUsers, mys => {
if (mys.ck && mys.ltuid) { if (mys.ck && mys.ltuid) {
ltuids.push(mys.ltuid) ltuids.push(mys.ltuid)
@ -111,17 +95,7 @@ class UserDB extends BaseModel {
} }
} }
/**
*
*/
BaseModel.initDB(UserDB, COLUMNS) BaseModel.initDB(UserDB, COLUMNS)
/**
*
*/
await UserDB.sync() await UserDB.sync()
/**
*
*/
export default UserDB export default UserDB

View File

@ -1,18 +1,17 @@
import { DataTypes } from 'sequelize'
import BaseModel from './BaseModel.js' import BaseModel from './BaseModel.js'
import lodash from 'lodash' import lodash from 'lodash'
/**
* const { Types } = BaseModel
*/
const COLUMNS = { const COLUMNS = {
// 用户IDqq为数字 // 用户IDqq为数字
userId: { userId: {
type: DataTypes.STRING type: Types.STRING
}, },
game: DataTypes.STRING, game: Types.STRING,
uid: DataTypes.STRING, uid: Types.STRING,
data: { data: {
type: DataTypes.STRING, type: Types.STRING,
get() { get() {
let data = this.getDataValue('data') let data = this.getDataValue('data')
let ret = {} let ret = {}
@ -34,22 +33,9 @@ const COLUMNS = {
} }
} }
/**
*
*/
class UserGameDB extends BaseModel {} class UserGameDB extends BaseModel {}
/**
*
*/
BaseModel.initDB(UserGameDB, COLUMNS) BaseModel.initDB(UserGameDB, COLUMNS)
/**
*
*/
await UserGameDB.sync() await UserGameDB.sync()
/**
*
*/
export default UserGameDB export default UserGameDB