Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(server): system config #9517

Merged
merged 1 commit into from
May 15, 2024
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
2 changes: 1 addition & 1 deletion docs/docs/guides/database-queries.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ SELECT * FROM "users";
## System Config

```sql title="Custom settings"
SELECT "key", "value" FROM "system_config";
SELECT "key", "value" FROM "system_metadata" WHERE "key" = 'system-config';
```

(Only used when not using the [config file](/docs/install/config-file))
Expand Down
1 change: 0 additions & 1 deletion e2e/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,6 @@ export const utils = {
'sessions',
'users',
'system_metadata',
'system_config',
];

const sql: string[] = [];
Expand Down
10 changes: 5 additions & 5 deletions server/src/cores/storage.core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { ILoggerRepository } from 'src/interfaces/logger.interface';
import { IMoveRepository } from 'src/interfaces/move.interface';
import { IPersonRepository } from 'src/interfaces/person.interface';
import { IStorageRepository } from 'src/interfaces/storage.interface';
import { ISystemConfigRepository } from 'src/interfaces/system-config.interface';
import { ISystemMetadataRepository } from 'src/interfaces/system-metadata.interface';

export enum StorageFolder {
ENCODED_VIDEO = 'encoded-video',
Expand Down Expand Up @@ -49,10 +49,10 @@ export class StorageCore {
private moveRepository: IMoveRepository,
private personRepository: IPersonRepository,
private storageRepository: IStorageRepository,
systemConfigRepository: ISystemConfigRepository,
systemMetadataRepository: ISystemMetadataRepository,
private logger: ILoggerRepository,
) {
this.configCore = SystemConfigCore.create(systemConfigRepository, this.logger);
this.configCore = SystemConfigCore.create(systemMetadataRepository, this.logger);
}

static create(
Expand All @@ -61,7 +61,7 @@ export class StorageCore {
moveRepository: IMoveRepository,
personRepository: IPersonRepository,
storageRepository: IStorageRepository,
systemConfigRepository: ISystemConfigRepository,
systemMetadataRepository: ISystemMetadataRepository,
logger: ILoggerRepository,
) {
if (!instance) {
Expand All @@ -71,7 +71,7 @@ export class StorageCore {
moveRepository,
personRepository,
storageRepository,
systemConfigRepository,
systemMetadataRepository,
logger,
);
}
Expand Down
100 changes: 36 additions & 64 deletions server/src/cores/system-config.core.ts
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file

Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ import * as _ from 'lodash';
import { Subject } from 'rxjs';
import { SystemConfig, defaults } from 'src/config';
import { SystemConfigDto } from 'src/dtos/system-config.dto';
import { SystemConfigEntity, SystemConfigKey, SystemConfigValue } from 'src/entities/system-config.entity';
import { SystemMetadataKey } from 'src/entities/system-metadata.entity';
import { DatabaseLock } from 'src/interfaces/database.interface';
import { ILoggerRepository } from 'src/interfaces/logger.interface';
import { ISystemConfigRepository } from 'src/interfaces/system-config.interface';
import { ISystemMetadataRepository } from 'src/interfaces/system-metadata.interface';
import { getKeysDeep, unsetDeep } from 'src/utils/misc';
import { DeepPartial } from 'typeorm';

export type SystemConfigValidator = (config: SystemConfig, newConfig: SystemConfig) => void | Promise<void>;

Expand All @@ -25,11 +27,11 @@ export class SystemConfigCore {
config$ = new Subject<SystemConfig>();

private constructor(
private repository: ISystemConfigRepository,
private repository: ISystemMetadataRepository,
private logger: ILoggerRepository,
) {}

static create(repository: ISystemConfigRepository, logger: ILoggerRepository) {
static create(repository: ISystemMetadataRepository, logger: ILoggerRepository) {
if (!instance) {
instance = new SystemConfigCore(repository, logger);
}
Expand All @@ -55,41 +57,25 @@ export class SystemConfigCore {
}

async updateConfig(newConfig: SystemConfig): Promise<SystemConfig> {
const updates: SystemConfigEntity[] = [];
const deletes: SystemConfigEntity[] = [];

for (const key of Object.values(SystemConfigKey)) {
// get via dot notation
const item = { key, value: _.get(newConfig, key) as SystemConfigValue };
const defaultValue = _.get(defaults, key);
const isMissing = !_.has(newConfig, key);

if (
isMissing ||
item.value === null ||
item.value === '' ||
item.value === defaultValue ||
_.isEqual(item.value, defaultValue)
) {
deletes.push(item);
// get the difference between the new config and the default config
const partialConfig: DeepPartial<SystemConfig> = {};
for (const property of getKeysDeep(defaults)) {
const newValue = _.get(newConfig, property);
const isEmpty = newValue === undefined || newValue === null || newValue === '';
const defaultValue = _.get(defaults, property);
const isEqual = newValue === defaultValue || _.isEqual(newValue, defaultValue);

if (isEmpty || isEqual) {
continue;
}

updates.push(item);
_.set(partialConfig, property, newValue);
}

if (updates.length > 0) {
await this.repository.saveAll(updates);
}

if (deletes.length > 0) {
await this.repository.deleteKeys(deletes.map((item) => item.key));
}
await this.repository.set(SystemMetadataKey.SYSTEM_CONFIG, partialConfig);

const config = await this.getConfig(true);

this.config$.next(config);

return config;
}

Expand All @@ -103,16 +89,28 @@ export class SystemConfigCore {
}

private async buildConfig() {
const config = _.cloneDeep(defaults);
const overrides = this.isUsingConfigFile()
// load partial
const partial = this.isUsingConfigFile()
? await this.loadFromFile(process.env.IMMICH_CONFIG_FILE as string)
: await this.repository.load();
: await this.repository.get(SystemMetadataKey.SYSTEM_CONFIG);

// merge with defaults
const config = _.cloneDeep(defaults);
for (const property of getKeysDeep(partial)) {
_.set(config, property, _.get(partial, property));
}

// check for extra properties
const unknownKeys = _.cloneDeep(config);
for (const property of getKeysDeep(defaults)) {
unsetDeep(unknownKeys, property);
}

for (const { key, value } of overrides) {
// set via dot notation
_.set(config, key, value);
if (!_.isEmpty(unknownKeys)) {
this.logger.warn(`Unknown keys found: ${JSON.stringify(unknownKeys, null, 2)}`);
}

// validate full config
const errors = await validate(plainToInstance(SystemConfigDto, config));
if (errors.length > 0) {
if (this.isUsingConfigFile()) {
Expand All @@ -136,36 +134,10 @@ export class SystemConfigCore {
private async loadFromFile(filepath: string) {
try {
const file = await this.repository.readFile(filepath);
const config = loadYaml(file.toString()) as any;
const overrides: SystemConfigEntity<SystemConfigValue>[] = [];

for (const key of Object.values(SystemConfigKey)) {
const value = _.get(config, key);
this.unsetDeep(config, key);
if (value !== undefined) {
overrides.push({ key, value });
}
}

if (!_.isEmpty(config)) {
this.logger.warn(`Unknown keys found: ${JSON.stringify(config, null, 2)}`);
}

return overrides;
return loadYaml(file.toString()) as unknown;
} catch (error: Error | any) {
this.logger.error(`Unable to load configuration file: ${filepath}`);
throw error;
}
}

private unsetDeep(object: object, key: string) {
_.unset(object, key);
const path = key.split('.');
while (path.pop()) {
if (!_.isEmpty(_.get(object, path))) {
return;
}
_.unset(object, path);
}
}
}
2 changes: 0 additions & 2 deletions server/src/entities/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import { SessionEntity } from 'src/entities/session.entity';
import { SharedLinkEntity } from 'src/entities/shared-link.entity';
import { SmartInfoEntity } from 'src/entities/smart-info.entity';
import { SmartSearchEntity } from 'src/entities/smart-search.entity';
import { SystemConfigEntity } from 'src/entities/system-config.entity';
import { SystemMetadataEntity } from 'src/entities/system-metadata.entity';
import { TagEntity } from 'src/entities/tag.entity';
import { UserEntity } from 'src/entities/user.entity';
Expand All @@ -42,7 +41,6 @@ export const entities = [
SharedLinkEntity,
SmartInfoEntity,
SmartSearchEntity,
SystemConfigEntity,
SystemMetadataEntity,
TagEntity,
UserEntity,
Expand Down
145 changes: 0 additions & 145 deletions server/src/entities/system-config.entity.ts

This file was deleted.

Loading
Loading