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:
@@ -1,6 +1,8 @@
|
||||
import { app } from '@/app.ts';
|
||||
import { AppModel } from '../module/app.ts';
|
||||
import { AppDomainModel } from '../module/app-domain.ts';
|
||||
import { app, db, schema } from '@/app.ts';
|
||||
import { App, AppData } from '../module/app-drizzle.ts';
|
||||
import { AppDomain, AppDomainHelper } from '../module/app-domain-drizzle.ts';
|
||||
import { eq, and } from 'drizzle-orm';
|
||||
import { randomUUID } from 'crypto';
|
||||
|
||||
app
|
||||
.route({
|
||||
@@ -9,17 +11,17 @@ app
|
||||
})
|
||||
.define(async (ctx) => {
|
||||
const { domain } = ctx.query.data;
|
||||
// const query = {
|
||||
// }
|
||||
const domainInfo = await AppDomainModel.findOne({ where: { domain } });
|
||||
const domainInfos = await db.select().from(schema.kvAppDomain).where(eq(schema.kvAppDomain.domain, domain)).limit(1);
|
||||
const domainInfo = domainInfos[0];
|
||||
if (!domainInfo || !domainInfo.appId) {
|
||||
ctx.throw(404, 'app not found');
|
||||
}
|
||||
const app = await AppModel.findByPk(domainInfo.appId);
|
||||
if (!app) {
|
||||
const apps = await db.select().from(schema.kvApp).where(eq(schema.kvApp.id, domainInfo.appId)).limit(1);
|
||||
const appFound = apps[0];
|
||||
if (!appFound) {
|
||||
ctx.throw(404, 'app not found');
|
||||
}
|
||||
ctx.body = app;
|
||||
ctx.body = appFound;
|
||||
return ctx;
|
||||
})
|
||||
.addTo(app);
|
||||
@@ -37,7 +39,8 @@ app
|
||||
if (!domain || !appId) {
|
||||
ctx.throw(400, 'domain and appId are required');
|
||||
}
|
||||
const domainInfo = await AppDomainModel.create({ domain, appId, uid });
|
||||
const newDomains = await db.insert(schema.kvAppDomain).values({ id: randomUUID(), domain, appId, uid }).returning();
|
||||
const domainInfo = newDomains[0];
|
||||
ctx.body = domainInfo;
|
||||
return ctx;
|
||||
})
|
||||
@@ -59,12 +62,17 @@ app
|
||||
if (!status) {
|
||||
ctx.throw(400, 'status is required');
|
||||
}
|
||||
let domainInfo: AppDomainModel | null = null;
|
||||
let domainInfo: AppDomain | undefined;
|
||||
if (id) {
|
||||
domainInfo = await AppDomainModel.findByPk(id);
|
||||
const domains = await db.select().from(schema.kvAppDomain).where(eq(schema.kvAppDomain.id, id)).limit(1);
|
||||
domainInfo = domains[0];
|
||||
}
|
||||
if (!domainInfo && domain) {
|
||||
domainInfo = await AppDomainModel.findOne({ where: { domain, appId } });
|
||||
const domains = await db.select().from(schema.kvAppDomain).where(and(
|
||||
eq(schema.kvAppDomain.domain, domain),
|
||||
eq(schema.kvAppDomain.appId, appId)
|
||||
)).limit(1);
|
||||
domainInfo = domains[0];
|
||||
}
|
||||
if (!domainInfo) {
|
||||
ctx.throw(404, 'domain not found');
|
||||
@@ -72,19 +80,23 @@ app
|
||||
if (domainInfo.uid !== uid) {
|
||||
ctx.throw(403, 'domain must be owned by the user');
|
||||
}
|
||||
if (!domainInfo.checkCanUpdateStatus(status)) {
|
||||
if (!AppDomainHelper.checkCanUpdateStatus(domainInfo.status!, status)) {
|
||||
ctx.throw(400, 'domain status can not be updated');
|
||||
}
|
||||
const updateData: any = {};
|
||||
if (status) {
|
||||
domainInfo.status = status;
|
||||
updateData.status = status;
|
||||
}
|
||||
|
||||
if (appId) {
|
||||
domainInfo.appId = appId;
|
||||
updateData.appId = appId;
|
||||
}
|
||||
await domainInfo.save({ fields: ['status', 'appId'] });
|
||||
|
||||
ctx.body = domainInfo;
|
||||
updateData.updatedAt = new Date().toISOString();
|
||||
const updateResult = await db.update(schema.kvAppDomain)
|
||||
.set(updateData)
|
||||
.where(eq(schema.kvAppDomain.id, domainInfo.id))
|
||||
.returning();
|
||||
const updatedDomain = updateResult[0];
|
||||
ctx.body = updatedDomain;
|
||||
return ctx;
|
||||
})
|
||||
.addTo(app);
|
||||
|
||||
Reference in New Issue
Block a user