Refactor app management to use Drizzle ORM

- Replaced Sequelize models with Drizzle ORM for app and app list management.
- Updated routes in app-manager to utilize new database queries.
- Removed obsolete Sequelize model files for app, app list, and app domain.
- Introduced new helper functions for app and app domain management.
- Enhanced user app management with improved file handling and user migration.
- Adjusted public API routes to align with new database structure.
- Implemented caching mechanisms for domain management.
This commit is contained in:
2026-02-07 01:26:16 +08:00
parent d62a75842f
commit 7dfa96d165
40 changed files with 1066 additions and 1171 deletions

View File

@@ -1,7 +1,9 @@
import { app } from '@/app.ts';
import { AppDomainModel } from '../module/app-domain.ts';
import { AppModel } from '../module/app.ts';
import { app, db, schema } from '@/app.ts';
import { AppDomain, AppDomainHelper } from '../module/app-domain-drizzle.ts';
import { App } from '../module/app-drizzle.ts';
import { CustomError } from '@kevisual/router';
import { eq, or } from 'drizzle-orm';
import { randomUUID } from 'crypto';
app
.route({
@@ -11,10 +13,12 @@ app
})
.define(async (ctx) => {
const { page = 1, pageSize = 999 } = ctx.query.data || {};
const { count, rows } = await AppDomainModel.findAndCountAll({
offset: (page - 1) * pageSize,
limit: pageSize,
});
const offset = (page - 1) * pageSize;
const rows = await db.select().from(schema.kvAppDomain)
.limit(pageSize)
.offset(offset);
const countResult = await db.select().from(schema.kvAppDomain);
const count = countResult.length;
ctx.body = { count, list: rows, pagination: { page, pageSize } };
return ctx;
})
@@ -31,11 +35,10 @@ app
if (!domain) {
ctx.throw(400, 'domain is required');
}
let domainInfo: AppDomainModel;
let domainInfo: AppDomain | undefined;
if (id) {
domainInfo = await AppDomainModel.findByPk(id);
} else {
domainInfo = await AppDomainModel.create({ domain });
const domains = await db.select().from(schema.kvAppDomain).where(eq(schema.kvAppDomain.id, id)).limit(1);
domainInfo = domains[0];
}
const checkAppId = async () => {
const isUUID = (id: string) => {
@@ -45,7 +48,8 @@ app
if (!isUUID(rest.appId)) {
ctx.throw(400, 'appId is not valid');
}
const appInfo = await AppModel.findByPk(rest.appId);
const apps = await db.select().from(schema.kvApp).where(eq(schema.kvApp.id, rest.appId)).limit(1);
const appInfo = apps[0];
if (!appInfo) {
ctx.throw(400, 'appId is not exist');
}
@@ -53,24 +57,31 @@ app
};
try {
if (!domainInfo) {
domainInfo = await AppDomainModel.create({ domain, data: {}, ...rest });
await checkAppId();
const newDomains = await db.insert(schema.kvAppDomain).values({ id: randomUUID(), domain, data: {}, ...rest }).returning();
domainInfo = newDomains[0];
} else {
if (rest.status && domainInfo.status !== rest.status) {
await domainInfo.clearCache();
await AppDomainHelper.clearCache(domainInfo.domain!);
}
await checkAppId();
await domainInfo.update({
domain,
data: {
...domainInfo.data,
...data,
},
...rest,
});
const domainData = domainInfo.data as any;
const updateResult = await db.update(schema.kvAppDomain)
.set({
domain,
data: {
...domainData,
...data,
},
...rest,
updatedAt: new Date().toISOString()
})
.where(eq(schema.kvAppDomain.id, domainInfo.id))
.returning();
domainInfo = updateResult[0];
}
ctx.body = domainInfo;
} catch (error) {
} catch (error: any) {
if (error.code) {
ctx.throw(error.code, error.message);
}
@@ -94,9 +105,9 @@ app
ctx.throw(400, 'id or domain is required');
}
if (id) {
await AppDomainModel.destroy({ where: { id }, force: true });
await db.delete(schema.kvAppDomain).where(eq(schema.kvAppDomain.id, id));
} else {
await AppDomainModel.destroy({ where: { domain }, force: true });
await db.delete(schema.kvAppDomain).where(eq(schema.kvAppDomain.domain, domain));
}
ctx.body = { message: 'delete domain success' };
@@ -115,7 +126,8 @@ app
if (!id && !domain) {
ctx.throw(400, 'id or domain is required');
}
const domainInfo = await AppDomainModel.findOne({ where: { id } });
const domains = await db.select().from(schema.kvAppDomain).where(eq(schema.kvAppDomain.id, id)).limit(1);
const domainInfo = domains[0];
if (!domainInfo) {
ctx.throw(404, 'domain not found');
}