Refactor config management to use Drizzle ORM

- Replaced Sequelize with Drizzle ORM in config-related routes and models.
- Updated database queries to use Drizzle's syntax for selecting, inserting, updating, and deleting configurations.
- Removed the ConfigModel class and replaced it with direct database interactions.
- Introduced nanoid for generating unique IDs for new configurations.
- Added new routes for managing marks, including CRUD operations and versioning.
- Implemented transaction handling for critical operations in the MarkModel.
- Enhanced error handling and validation in routes.
This commit is contained in:
2026-02-05 16:31:09 +08:00
parent 5200cf4c38
commit 266b7b33de
13 changed files with 924 additions and 191 deletions

View File

@@ -1,10 +1,10 @@
import { ConfigModel, Config } from '../models/model.ts';
import { Config } from '../models/model.ts';
import { CustomError } from '@kevisual/router';
import { redis } from '@/app.ts';
import { User } from '@/models/user.ts';
import { redis, db, schema } from '@/app.ts';
import { eq, and } from 'drizzle-orm';
import { UserPermission, UserPermissionOptions } from '@kevisual/permission';
export class ShareConfigService extends ConfigModel {
export class ShareConfigService {
/**
* 获取分享的配置
* @param key 配置的key
@@ -22,26 +22,30 @@ export class ShareConfigService extends ConfigModel {
}
const owner = username;
if (shareCacheConfig) {
const permission = new UserPermission({ permission: shareCacheConfig?.data?.permission, owner });
const permission = new UserPermission({ permission: (shareCacheConfig?.data as any)?.permission, owner });
const result = permission.checkPermissionSuccess(options);
if (!result.success) {
throw new CustomError(403, 'no permission');
}
return shareCacheConfig;
}
const user = await User.findOne({
where: { username },
});
const users = await db.select()
.from(schema.cfUser)
.where(eq(schema.cfUser.username, username))
.limit(1);
const user = users[0];
if (!user) {
throw new CustomError(404, 'user not found');
}
const config = await ConfigModel.findOne({
where: { key, uid: user.id },
});
const configs = await db.select()
.from(schema.kvConfig)
.where(and(eq(schema.kvConfig.key, key), eq(schema.kvConfig.uid, user.id)))
.limit(1);
const config = configs[0];
if (!config) {
throw new CustomError(404, 'config not found');
}
const permission = new UserPermission({ permission: config?.data?.permission, owner });
const permission = new UserPermission({ permission: (config?.data as any)?.permission, owner });
const result = permission.checkPermissionSuccess(options);
if (!result.success) {
throw new CustomError(403, 'no permission');