Skip to content

There can be more than Notion and Miro. AFFiNE(pronounced [ə‘fain]) is a next-gen knowledge base that brings planning, sorting and creating all together. Privacy first, open-source, customizable and ready to use.

chore(server): add import config script (#11242)

forehalo504a74d51293
diff --git a/packages/backend/server/src/data/app.ts b/packages/backend/server/src/data/app.ts
index 552599137c..9e9783e650 100644
--- a/packages/backend/server/src/data/app.ts
+++ b/packages/backend/server/src/data/app.ts
@@ -1,11 +1,18 @@
import { Module } from '@nestjs/common';
-import { AppModule as BusinessAppModule } from '../app.module';
+import { FunctionalityModules } from '../app.module';
import { CreateCommand, NameQuestion } from './commands/create';
+import { ImportConfigCommand } from './commands/import';
import { RevertCommand, RunCommand } from './commands/run';
@Module({
- imports: [BusinessAppModule],
- providers: [NameQuestion, CreateCommand, RunCommand, RevertCommand],
+ imports: FunctionalityModules,
+ providers: [
+ NameQuestion,
+ CreateCommand,
+ RunCommand,
+ RevertCommand,
+ ImportConfigCommand,
+ ],
})
export class CliAppModule {}
diff --git a/packages/backend/server/src/data/commands/import.ts b/packages/backend/server/src/data/commands/import.ts
new file mode 100644
index 0000000000..a3f1fc855c
--- /dev/null
+++ b/packages/backend/server/src/data/commands/import.ts
@@ -0,0 +1,58 @@
+import { readFileSync } from 'node:fs';
+import { resolve } from 'node:path';
+
+import { Logger } from '@nestjs/common';
+import { Command, CommandRunner } from 'nest-commander';
+
+import { ConfigFactory } from '../../base';
+import { Models } from '../../models';
+
+@Command({
+ name: 'import-config',
+ arguments: '[name]',
+ description: 'import config from a file',
+})
+export class ImportConfigCommand extends CommandRunner {
+ logger = new Logger(ImportConfigCommand.name);
+
+ constructor(
+ private readonly models: Models,
+ private readonly configFactory: ConfigFactory
+ ) {
+ super();
+ }
+
+ override async run(inputs: string[]): Promise<void> {
+ let path = inputs[0];
+ path = resolve(process.cwd(), path);
+
+ const overrides: Record<string, Record<string, any>> = JSON.parse(
+ readFileSync(path, 'utf-8')
+ );
+
+ const forValidation: { module: string; key: string; value: any }[] = [];
+ const forSaving: { key: string; value: any }[] = [];
+ Object.entries(overrides).forEach(([module, config]) => {
+ if (module === '$schema') {
+ return;
+ }
+
+ Object.entries(config).forEach(([key, value]) => {
+ forValidation.push({
+ module,
+ key,
+ value,
+ });
+ forSaving.push({
+ key: `${module}.${key}`,
+ value,
+ });
+ });
+ });
+
+ this.configFactory.validate(forValidation);
+
+ // @ts-expect-error null as user id
+ await this.models.appConfig.save(null, forSaving);
+ }
+}