Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions lib/mongo/models/plugins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@ export const PluginZodSchema = z.object({
export type MongoPluginSchemaType = z.infer<typeof PluginZodSchema>;

const pluginMongooseSchema = new Schema({
toolId: { type: String },
type: { type: String, required: true, enum: Object.values(pluginTypeEnum.enum) }
toolId: { type: String, required: true },
type: { type: String, required: true, enum: Object.values(pluginTypeEnum.enum) },

// @deprecated
objectName: { type: String }
});

pluginMongooseSchema.index({ toolId: 1 }, { unique: true, sparse: true });
pluginMongooseSchema.index({ type: 1 });
pluginMongooseSchema.index({ type: 1, toolId: 1 }, { unique: true });

export const MongoPlugin = getMongoModel('system_plugins', pluginMongooseSchema);
export const MongoSystemPlugin = getMongoModel('system_plugins', pluginMongooseSchema);
6 changes: 3 additions & 3 deletions modules/tool/api/upload/confirmUpload.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { s } from '@/router/init';
import { contract } from '@/contract';
import { UploadToolsS3Path } from '@tool/constants';
import { MongoPlugin, pluginTypeEnum } from '@/mongo/models/plugins';
import { MongoSystemPlugin, pluginTypeEnum } from '@/mongo/models/plugins';
import { refreshVersionKey } from '@/cache';
import { SystemCacheKeyEnum } from '@/cache/type';
import { mongoSessionRun } from '@/mongo/utils';
Expand All @@ -28,10 +28,10 @@ export default s.route(contract.tool.upload.confirmUpload, async ({ body }) => {

await mongoSessionRun(async (session) => {
const allToolsInstalled = (
await MongoPlugin.find({ type: pluginTypeEnum.enum.tool }).lean()
await MongoSystemPlugin.find({ type: pluginTypeEnum.enum.tool }).lean()
).map((tool) => tool.toolId);
// create all that not exists
await MongoPlugin.create(
await MongoSystemPlugin.create(
toolIds
.filter((toolId) => !allToolsInstalled.includes(toolId))
.map((toolId) => ({
Expand Down
4 changes: 2 additions & 2 deletions modules/tool/api/upload/delete.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { contract } from '@/contract';
import { MongoPlugin } from '@/mongo/models/plugins';
import { MongoSystemPlugin } from '@/mongo/models/plugins';
import { mongoSessionRun } from '@/mongo/utils';
import { s } from '@/router/init';
import { privateS3Server, publicS3Server } from '@/s3';
Expand All @@ -11,7 +11,7 @@ import { addLog } from '@/utils/log';
export default s.route(contract.tool.upload.delete, async ({ query: { toolId } }) => {
addLog.debug(`Deleting tool: ${toolId}`);
const res = await mongoSessionRun(async (session) => {
const result = await MongoPlugin.findOneAndDelete({ toolId }).session(session);
const result = await MongoSystemPlugin.findOneAndDelete({ toolId }).session(session);
if (!result || !result.toolId) {
return {
status: 404,
Expand Down
10 changes: 5 additions & 5 deletions modules/tool/api/upload/install.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { join } from 'path';
import { batch } from '@/utils/parallel';
import { parsePkg } from '@tool/utils';
import { writeFile } from 'fs/promises';
import { MongoPlugin, pluginTypeEnum } from '@/mongo/models/plugins';
import { MongoSystemPlugin, pluginTypeEnum } from '@/mongo/models/plugins';
import { refreshVersionKey } from '@/cache';
import { SystemCacheKeyEnum } from '@/cache/type';
import { addLog } from '@/utils/log';
Expand All @@ -30,11 +30,11 @@ export default s.route(contract.tool.upload.install, async ({ body }) => {
<T>(item: T): item is NonNullable<T> => !!item
);

const allToolsInstalled = (await MongoPlugin.find({ type: pluginTypeEnum.enum.tool }).lean()).map(
(tool) => tool.toolId
);
const allToolsInstalled = (
await MongoSystemPlugin.find({ type: pluginTypeEnum.enum.tool }).lean()
).map((tool) => tool.toolId);
// create all that not exists
await MongoPlugin.create(
await MongoSystemPlugin.create(
toolIds
.filter((toolId) => !allToolsInstalled.includes(toolId))
.map((toolId) => ({
Expand Down
4 changes: 2 additions & 2 deletions modules/tool/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { join } from 'path';
import { readdir } from 'fs/promises';
import type { ToolMapType } from './type';
import { isProd } from '@/constants';
import { MongoPlugin } from '@/mongo/models/plugins';
import { MongoSystemPlugin } from '@/mongo/models/plugins';
import { refreshDir } from '@/utils/fs';
import { addLog } from '@/utils/log';
import { basePath, toolsDir, UploadToolsS3Path } from './constants';
Expand Down Expand Up @@ -33,7 +33,7 @@ export async function initTools() {
await refreshDir(toolsDir);
// 1. download pkgs into pkg dir
// 1.1 get tools from mongo
const toolsInMongo = await MongoPlugin.find({
const toolsInMongo = await MongoSystemPlugin.find({
type: 'tool'
}).lean();

Expand Down