Skip to content

Commit 9bd7b8b

Browse files
committed
perf: Delete default plugin column from database
1 parent a76ab26 commit 9bd7b8b

File tree

23 files changed

+95
-279
lines changed

23 files changed

+95
-279
lines changed

apps/backend/src/plugins/welcome/config.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,5 @@
77
"code": "welcome",
88
"author": "VitNode",
99
"author_url": "https://vitnode.com/",
10-
"support_url": "https://github.com/VitNode/vitnode/issues",
11-
"allow_default": true
12-
}
10+
"support_url": "https://github.com/VitNode/vitnode/issues"
11+
}
Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,9 @@
1-
// ! DO NOT TOUCH THIS FILE!!!
2-
// ! If you remove this then default page plugin will not work
1+
import DefaultPage from '@/plugins/welcome/templates/default-page';
32
import React from 'react';
4-
import { getMiddlewareData } from 'vitnode-frontend/api/get-middleware-data';
53
import { generateMetadataDefaultPage } from 'vitnode-frontend/views/theme/views/default-page';
64

75
export const generateMetadata = generateMetadataDefaultPage;
86

9-
export default async function Page() {
10-
const { plugin_code_default } = await getMiddlewareData();
11-
12-
const PageFromTheme = React.lazy(async () =>
13-
import(`@/plugins/${plugin_code_default}/templates/default-page`).then(
14-
module => ({
15-
default: module.default,
16-
}),
17-
),
18-
);
19-
20-
return <PageFromTheme />;
7+
export default function Page() {
8+
return <DefaultPage />;
219
}

docs/dev/permissions-admin.mdx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ Edit the `permissions_admin` in the `config.json` file from your plugin.
5858
"author": "VitNode",
5959
"author_url": "https://vitnode.com/",
6060
"support_url": "https://github.com/VitNode/vitnode/issues",
61-
"allow_default": true,
6261
// [!code ++]
6362
"permissions_admin": [
6463
// [!code ++]

packages/backend/scripts/update-plugins.ts

Lines changed: 43 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,6 @@ export const updatePlugins = async ({
1414
db: NodePgDatabase<typeof coreSchemaDatabase>;
1515
pluginsPath: string;
1616
}) => {
17-
let isDefaultIndex: null | number = null;
18-
const defaultPlugin = await db.query.core_plugins.findFirst({
19-
where: (table, { eq }) => eq(table.default, true),
20-
});
2117
const plugins = (await readdir(pluginsPath)).filter(
2218
plugin => !['core', 'plugins.module.ts'].includes(plugin),
2319
);
@@ -30,58 +26,53 @@ export const updatePlugins = async ({
3026
},
3127
});
3228

33-
for (const [index, code] of plugins.entries()) {
34-
const pluginPath = join(pluginsPath, code);
35-
const configPath = join(pluginPath, 'config.json');
36-
if (!existsSync(configPath)) {
37-
continue;
38-
}
39-
40-
const config = JSON.parse(
41-
await readFile(join(pluginPath, 'config.json'), 'utf8'),
42-
);
29+
await Promise.all(
30+
plugins.map(async code => {
31+
const pluginPath = join(pluginsPath, code);
32+
const configPath = join(pluginPath, 'config.json');
33+
if (!existsSync(configPath)) {
34+
return;
35+
}
4336

44-
if (config.allow_default && isDefaultIndex === null) {
45-
isDefaultIndex = index;
46-
}
37+
const config = JSON.parse(
38+
await readFile(join(pluginPath, 'config.json'), 'utf8'),
39+
);
4740

48-
const plugin = pluginsFromDatabase.find(
49-
plugin => plugin.code === config.code,
50-
);
41+
const plugin = pluginsFromDatabase.find(
42+
plugin => plugin.code === config.code,
43+
);
5144

52-
if (plugin) {
53-
await tx
54-
.update(core_plugins)
55-
.set({
56-
name: config.name,
57-
description: config.description,
58-
support_url: config.support_url,
59-
author: config.author,
60-
author_url: config.author_url,
61-
allow_default: config.allow_default,
62-
version: config.version,
63-
version_code: config.version_code,
64-
})
65-
.where(eq(core_plugins.id, plugin.id));
66-
} else {
67-
await tx.insert(core_plugins).values([
68-
{
69-
name: config.name,
70-
description: config.description,
71-
code: config.code,
72-
support_url: config.support_url,
73-
author: config.author,
74-
author_url: config.author_url,
75-
allow_default: config.allow_default,
76-
version: config.version,
77-
version_code: config.version_code,
78-
default: isDefaultIndex === index && !defaultPlugin,
79-
},
80-
]);
81-
}
45+
if (plugin) {
46+
await tx
47+
.update(core_plugins)
48+
.set({
49+
name: config.name,
50+
description: config.description,
51+
support_url: config.support_url,
52+
author: config.author,
53+
author_url: config.author_url,
54+
version: config.version,
55+
version_code: config.version_code,
56+
})
57+
.where(eq(core_plugins.id, plugin.id));
58+
} else {
59+
await tx.insert(core_plugins).values([
60+
{
61+
name: config.name,
62+
description: config.description,
63+
code: config.code,
64+
support_url: config.support_url,
65+
author: config.author,
66+
author_url: config.author_url,
67+
version: config.version,
68+
version_code: config.version_code,
69+
},
70+
]);
71+
}
8272

83-
await tx.execute(sql`commit`);
84-
}
73+
await tx.execute(sql`commit`);
74+
}),
75+
);
8576

8677
// Remove plugins that are not in the plugins folder
8778
const pluginsToDelete = pluginsFromDatabase.filter(

packages/backend/src/app.module.ts

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,6 @@ export const ABSOLUTE_PATHS = {
4646
plugin: join(internalPaths.frontend, 'plugins', code),
4747
templates: join(internalPaths.frontend, 'plugins', code, 'templates'),
4848
languages: join(internalPaths.frontend, 'plugins', code, 'langs'),
49-
default_page: join(
50-
internalPaths.frontend,
51-
'plugins',
52-
code,
53-
'templates',
54-
'default-page.tsx',
55-
),
5649
admin_pages_auth: join(
5750
internalPaths.frontend,
5851
'app',

packages/backend/src/core/admin/plugins/services/create.service.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ export class CreatePluginsAdminService {
3838
$schema: 'https://api.vitnode.com/public/vitnode/plugin.schema.json',
3939
code,
4040
...rest,
41-
allow_default: true,
4241
nav: [],
4342
version: '0.0.1',
4443
version_code: 1,

packages/backend/src/core/admin/plugins/services/delete.service.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {
99
Injectable,
1010
NotFoundException,
1111
} from '@nestjs/common';
12-
import { eq } from 'drizzle-orm';
12+
import { count, eq } from 'drizzle-orm';
1313
import { existsSync } from 'fs';
1414
import { rm } from 'fs/promises';
1515
import { join } from 'path';
@@ -35,16 +35,21 @@ export class DeletePluginsAdminService {
3535
where: (table, { eq }) => eq(table.id, id),
3636
columns: {
3737
code: true,
38-
default: true,
3938
},
4039
});
4140

4241
if (!plugin) {
4342
throw new NotFoundException();
4443
}
4544

46-
if (plugin.default) {
47-
throw new BadRequestException('DEFAULT_PLUGIN_CANNOT_BE_DELETED');
45+
const [pluginCount] = await this.databaseService.db
46+
.select({
47+
count: count(),
48+
})
49+
.from(core_plugins);
50+
51+
if (pluginCount.count === 1) {
52+
throw new BadRequestException('Cannot delete the last plugin');
4853
}
4954

5055
await this.changeFilesHelper.changeFiles({

packages/backend/src/core/admin/plugins/services/edit.service.ts

Lines changed: 8 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {
66
Injectable,
77
NotFoundException,
88
} from '@nestjs/common';
9-
import { eq, ne } from 'drizzle-orm';
9+
import { eq } from 'drizzle-orm';
1010
import { readFile, writeFile } from 'fs/promises';
1111
import {
1212
ConfigPlugin,
@@ -20,7 +20,7 @@ export class EditPluginsAdminService {
2020

2121
async edit({
2222
code,
23-
body: { default: isDefault = false, ...rest },
23+
body,
2424
}: {
2525
body: EditPluginsAdminBody;
2626
code: string;
@@ -37,26 +37,9 @@ export class EditPluginsAdminService {
3737
throw new BadRequestException('PLUGIN_CODE_MISMATCH');
3838
}
3939

40-
if (isDefault) {
41-
if (!plugin.enabled) {
42-
throw new BadRequestException('PLUGIN_NOT_ENABLED');
43-
}
44-
45-
// Set all other plugins to default: false
46-
await this.databaseService.db
47-
.update(core_plugins)
48-
.set({
49-
default: false,
50-
})
51-
.where(ne(core_plugins.code, code));
52-
}
53-
5440
const [updatePlugin] = await this.databaseService.db
5541
.update(core_plugins)
56-
.set({
57-
...rest,
58-
default: isDefault,
59-
})
42+
.set(body)
6043
.where(eq(core_plugins.code, code))
6144
.returning();
6245

@@ -66,11 +49,11 @@ export class EditPluginsAdminService {
6649
await readFile(path, 'utf8'),
6750
);
6851

69-
config.name = rest.name;
70-
config.description = rest.description;
71-
config.author = rest.author;
72-
config.author_url = rest.author_url;
73-
config.support_url = rest.support_url;
52+
config.name = body.name;
53+
config.description = body.description;
54+
config.author = body.author;
55+
config.author_url = body.author_url;
56+
config.support_url = body.support_url;
7457

7558
await writeFile(path, JSON.stringify(config, null, 2));
7659

packages/backend/src/core/admin/plugins/services/export.service.ts

Lines changed: 8 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,6 @@ export class ExportPluginsAdminService {
4242
const configJSON: ConfigPlugin = JSON.parse(
4343
await readFile(pathInfoJSON, 'utf8'),
4444
);
45-
const allow_default = existsSync(
46-
ABSOLUTE_PATHS.plugin({ code }).frontend.default_page,
47-
);
4845

4946
if (
5047
(!version && !configJSON.version) ||
@@ -66,32 +63,19 @@ export class ExportPluginsAdminService {
6663
});
6764
}
6865

69-
configJSON.allow_default = allow_default;
7066
configJSON.version = version ?? configJSON.version;
7167
configJSON.version_code = version_code ?? configJSON.version_code;
7268

7369
await writeFile(pathInfoJSON, JSON.stringify(configJSON, null, 2), 'utf8');
7470

75-
// Update only allow_default
76-
if (!version || !version_code) {
77-
await this.databaseService.db
78-
.update(core_plugins)
79-
.set({
80-
allow_default,
81-
updated_at: new Date(),
82-
})
83-
.where(eq(core_plugins.code, code));
84-
} else {
85-
await this.databaseService.db
86-
.update(core_plugins)
87-
.set({
88-
version,
89-
version_code,
90-
allow_default,
91-
updated_at: new Date(),
92-
})
93-
.where(eq(core_plugins.code, code));
94-
}
71+
await this.databaseService.db
72+
.update(core_plugins)
73+
.set({
74+
version,
75+
version_code,
76+
updated_at: new Date(),
77+
})
78+
.where(eq(core_plugins.code, code));
9579

9680
// Prepare the export
9781
const tempFolderName = removeSpecialCharacters(

packages/backend/src/core/middleware/services/show.service.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@ export class ShowMiddlewareService {
6060
this.databaseService.db.query.core_plugins.findMany({
6161
columns: {
6262
code: true,
63-
default: true,
6463
},
6564
}),
6665
this.databaseService.db.query.core_languages.findMany({
@@ -76,10 +75,6 @@ export class ShowMiddlewareService {
7675
}),
7776
]);
7877

79-
const plugin_code_default = plugins.find(plugin => plugin.default)?.code;
80-
if (!plugin_code_default) {
81-
throw new InternalServerErrorException('Plugin not found');
82-
}
8378
const manifest = await this.getManifests({
8479
langCodes: langs.map(lang => lang.code),
8580
});
@@ -124,7 +119,6 @@ export class ShowMiddlewareService {
124119
editor: {
125120
sticky: configFromDb.editor_sticky ?? false,
126121
},
127-
plugin_code_default,
128122
site_description: manifest.map(item => ({
129123
language_code: item.lang,
130124
value: item.description,

0 commit comments

Comments
 (0)