From 891619ad552a3f0d94c73baba1bb5722e5a048ea Mon Sep 17 00:00:00 2001 From: whyour Date: Thu, 27 Jan 2022 00:49:44 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E8=A1=A8=E5=94=AF=E4=B8=80?= =?UTF-8?q?=E7=BA=A6=E6=9D=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- back/data/env.ts | 4 ++-- back/data/index.ts | 3 ++- back/data/open.ts | 2 +- back/loaders/db.ts | 11 ++++++++++- back/services/cron.ts | 6 ------ back/services/env.ts | 8 ++++++-- back/services/open.ts | 4 ++-- 7 files changed, 23 insertions(+), 15 deletions(-) diff --git a/back/data/env.ts b/back/data/env.ts index 1cc89275..946689c6 100644 --- a/back/data/env.ts +++ b/back/data/env.ts @@ -30,10 +30,10 @@ export const initEnvPosition = 9999999999; interface EnvInstance extends Model, Env {} export const EnvModel = sequelize.define('Env', { - value: DataTypes.STRING, + value: { type: DataTypes.STRING, unique: 'compositeIndex' }, timestamp: DataTypes.STRING, status: DataTypes.NUMBER, position: DataTypes.NUMBER, - name: DataTypes.STRING, + name: { type: DataTypes.STRING, unique: 'compositeIndex' }, remarks: DataTypes.STRING, }); diff --git a/back/data/index.ts b/back/data/index.ts index 337c0137..410cbf0b 100644 --- a/back/data/index.ts +++ b/back/data/index.ts @@ -4,10 +4,11 @@ import config from '../config/index'; export const sequelize = new Sequelize({ dialect: 'sqlite', storage: `${config.dbPath}database.sqlite`, - logging: false, pool: { max: 6, min: 0, idle: 30000, }, }); + +export type ResponseType = { code: number; data?: T; message?: string }; diff --git a/back/data/open.ts b/back/data/open.ts index feaacdcb..ae923ef9 100644 --- a/back/data/open.ts +++ b/back/data/open.ts @@ -35,7 +35,7 @@ export enum CrontabStatus { interface AppInstance extends Model, App {} export const AppModel = sequelize.define('App', { - name: DataTypes.STRING, + name: { type: DataTypes.STRING, unique: 'name' }, scopes: DataTypes.JSON, client_id: DataTypes.STRING, client_secret: DataTypes.STRING, diff --git a/back/loaders/db.ts b/back/loaders/db.ts index cc8494cd..be93c8ac 100644 --- a/back/loaders/db.ts +++ b/back/loaders/db.ts @@ -11,9 +11,18 @@ import { sequelize } from '../data'; export default async () => { try { - await sequelize.sync({ alter: true }); + await sequelize.sync(); await new Promise((resolve) => setTimeout(() => resolve(null), 5000)); + // try { + // const queryInterface = sequelize.getQueryInterface(); + // await queryInterface.addIndex('Crontabs', ['command'], { unique: true }); + // await queryInterface.addIndex('Envs', ['name', 'value'], { unique: true }); + // await queryInterface.addIndex('Apps', ['name'], { unique: true }); + // } catch (error) { + + // } + const crondbExist = await fileExist(config.cronDbFile); const dependenceDbExist = await fileExist(config.dependenceDbFile); const envDbExist = await fileExist(config.envDbFile); diff --git a/back/services/cron.ts b/back/services/cron.ts index 6f24484c..aaa91b73 100644 --- a/back/services/cron.ts +++ b/back/services/cron.ts @@ -31,12 +31,6 @@ export default class CronService { } public async insert(payload: Crontab): Promise { - const cron = await CrontabModel.findOne({ - where: { command: payload.command }, - }); - if (cron) { - return cron; - } return await CrontabModel.create(payload, { returning: true }); } diff --git a/back/services/env.ts b/back/services/env.ts index 68205134..ed0246f5 100644 --- a/back/services/env.ts +++ b/back/services/env.ts @@ -28,8 +28,12 @@ export default class EnvService { } public async insert(payloads: Env[]): Promise { - const docs = await EnvModel.bulkCreate(payloads); - return docs; + const result = []; + for (const env of payloads) { + const doc = await EnvModel.create(env, { returning: true }); + result.push(doc); + } + return result; } public async update(payload: Env): Promise { diff --git a/back/services/open.ts b/back/services/open.ts index 43d0b7db..f2705dec 100644 --- a/back/services/open.ts +++ b/back/services/open.ts @@ -24,8 +24,8 @@ export default class OpenService { return { ...doc, tokens: [] }; } - public async insert(payloads: App): Promise { - const doc = await AppModel.create(payloads, { returning: true }); + public async insert(payload: App): Promise { + const doc = await AppModel.create(payload, { returning: true }); return doc.get({ plain: true }) as App; }