mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-10-19 05:26:47 +00:00
724c88c201
A mechanical prettier --write over server/src, server/tests, server/scripts and shared/src with the workspaces' .prettierrc; nothing but layout and import order changes, and every file keeps the same set of imports. shared is formatted throughout and its format baseline is empty. The server baseline keeps the 35 files that Prettier would grow past their lint:size limit (they need a split first, which is not a formatting change), and the generated entity files stay as their generator writes them.
52 lines
1.9 KiB
TypeScript
52 lines
1.9 KiB
TypeScript
import { PluginSettingsFields } from '../../../src/db/entities/PluginSettingsFields.entity';
|
|
import { PluginUserConfig } from '../../../src/db/entities/PluginUserConfig.entity';
|
|
import { Plugins } from '../../../src/db/entities/Plugins.entity';
|
|
import { dbNow } from '../../../src/db/types/db-timestamp.type';
|
|
import type { FactoryOrm } from './context';
|
|
import { createRow, findRow, insertRow, upsertRow } from './rows';
|
|
import type { EntityData, EntityDTO } from '@mikro-orm/core';
|
|
|
|
export type PluginRow = EntityDTO<Plugins>;
|
|
|
|
/**
|
|
* An installed plugin row, inactive and disabled unless told otherwise. Only
|
|
* the row: no child process is spawned, which is what a test of the admin or
|
|
* settings surface wants.
|
|
*/
|
|
export async function makePlugin(orm: FactoryOrm, id: string, overrides: EntityData<Plugins> = {}): Promise<PluginRow> {
|
|
await insertRow(orm, Plugins, { id, name: id, version: '1.0.0', ...overrides });
|
|
const plugin = await findRow(orm, Plugins, { id });
|
|
if (!plugin) throw new Error(`makePlugin: no plugin ${id} after its insert`);
|
|
return plugin;
|
|
}
|
|
|
|
/** One settings field the plugin declares: instance-scoped text unless told otherwise. */
|
|
export function makePluginSettingsField(
|
|
orm: FactoryOrm,
|
|
pluginId: string,
|
|
fieldKey: string,
|
|
overrides: EntityData<PluginSettingsFields> = {},
|
|
): Promise<EntityDTO<PluginSettingsFields>> {
|
|
return createRow(orm, PluginSettingsFields, {
|
|
plugin_id: pluginId,
|
|
field_key: fieldKey,
|
|
label: fieldKey,
|
|
...overrides,
|
|
});
|
|
}
|
|
|
|
/** Stores the user's config for the plugin as JSON, replacing any earlier one. */
|
|
export async function setPluginUserConfig(
|
|
orm: FactoryOrm,
|
|
pluginId: string,
|
|
userId: number,
|
|
config: Record<string, unknown>,
|
|
): Promise<void> {
|
|
await upsertRow(orm, PluginUserConfig, {
|
|
plugin_id: pluginId,
|
|
user_id: userId,
|
|
config: JSON.stringify(config),
|
|
updated_at: dbNow(),
|
|
});
|
|
}
|