修改表唯一约束

This commit is contained in:
whyour 2022-01-27 00:49:44 +08:00
parent a3a62f65dd
commit 891619ad55
7 changed files with 23 additions and 15 deletions

View File

@ -30,10 +30,10 @@ export const initEnvPosition = 9999999999;
interface EnvInstance extends Model<Env, Env>, Env {}
export const EnvModel = sequelize.define<EnvInstance>('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,
});

View File

@ -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<T> = { code: number; data?: T; message?: string };

View File

@ -35,7 +35,7 @@ export enum CrontabStatus {
interface AppInstance extends Model<App, App>, App {}
export const AppModel = sequelize.define<AppInstance>('App', {
name: DataTypes.STRING,
name: { type: DataTypes.STRING, unique: 'name' },
scopes: DataTypes.JSON,
client_id: DataTypes.STRING,
client_secret: DataTypes.STRING,

View File

@ -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);

View File

@ -31,12 +31,6 @@ export default class CronService {
}
public async insert(payload: Crontab): Promise<Crontab> {
const cron = await CrontabModel.findOne({
where: { command: payload.command },
});
if (cron) {
return cron;
}
return await CrontabModel.create(payload, { returning: true });
}

View File

@ -28,8 +28,12 @@ export default class EnvService {
}
public async insert(payloads: Env[]): Promise<Env[]> {
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<Env> {

View File

@ -24,8 +24,8 @@ export default class OpenService {
return { ...doc, tokens: [] };
}
public async insert(payloads: App): Promise<App> {
const doc = await AppModel.create(payloads, { returning: true });
public async insert(payload: App): Promise<App> {
const doc = await AppModel.create(payload, { returning: true });
return doc.get({ plain: true }) as App;
}