2020-10-07 10:53:19 +02:00
|
|
|
// (C) Copyright 2015 Moodle Pty Ltd.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
import { Injectable } from '@angular/core';
|
2022-02-03 12:35:16 +01:00
|
|
|
import { CoreDatabaseCachingStrategy, CoreDatabaseTableProxy } from '@classes/database/database-table-proxy';
|
2020-10-28 14:25:18 +01:00
|
|
|
import { CoreApp } from '@services/app';
|
2022-02-03 12:35:16 +01:00
|
|
|
import { APP_SCHEMA, ConfigDBEntry, CONFIG_TABLE_NAME } from '@services/database/config';
|
2020-11-24 09:31:11 +01:00
|
|
|
import { makeSingleton } from '@singletons';
|
2022-02-03 12:35:16 +01:00
|
|
|
import { CoreDatabaseTable } from '@classes/database/database-table';
|
2022-01-25 09:33:40 +01:00
|
|
|
import { CorePromisedValue } from '@classes/promised-value';
|
2020-10-14 08:29:32 +02:00
|
|
|
|
2020-10-07 10:53:19 +02:00
|
|
|
/**
|
|
|
|
* Factory to provide access to dynamic and permanent config and settings.
|
|
|
|
* It should not be abused into a temporary storage.
|
|
|
|
*/
|
2020-11-19 16:35:17 +01:00
|
|
|
@Injectable({ providedIn: 'root' })
|
2020-10-07 10:53:19 +02:00
|
|
|
export class CoreConfigProvider {
|
2020-10-14 08:29:32 +02:00
|
|
|
|
2022-02-03 12:35:16 +01:00
|
|
|
protected table: CorePromisedValue<CoreDatabaseTable<ConfigDBEntry, 'name'>> = new CorePromisedValue();
|
2020-12-01 18:37:24 +01:00
|
|
|
|
|
|
|
/**
|
2020-12-09 13:39:18 +01:00
|
|
|
* Initialize database.
|
2020-12-01 18:37:24 +01:00
|
|
|
*/
|
2020-12-09 13:39:18 +01:00
|
|
|
async initializeDatabase(): Promise<void> {
|
2020-12-01 18:37:24 +01:00
|
|
|
try {
|
2021-03-02 11:41:04 +01:00
|
|
|
await CoreApp.createTablesFromSchema(APP_SCHEMA);
|
2020-12-01 18:37:24 +01:00
|
|
|
} catch (e) {
|
2020-10-07 10:53:19 +02:00
|
|
|
// Ignore errors.
|
2020-12-01 18:37:24 +01:00
|
|
|
}
|
|
|
|
|
2022-02-03 12:35:16 +01:00
|
|
|
const table = new CoreDatabaseTableProxy<ConfigDBEntry, 'name'>(
|
|
|
|
{ cachingStrategy: CoreDatabaseCachingStrategy.Eager },
|
|
|
|
CoreApp.getDB(),
|
|
|
|
CONFIG_TABLE_NAME,
|
|
|
|
['name'],
|
|
|
|
);
|
|
|
|
|
|
|
|
await table.initialize();
|
2022-01-25 09:33:40 +01:00
|
|
|
|
2022-02-03 12:35:16 +01:00
|
|
|
this.table.resolve(table);
|
2020-10-07 10:53:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Deletes an app setting.
|
|
|
|
*
|
|
|
|
* @param name The config name.
|
|
|
|
* @return Promise resolved when done.
|
|
|
|
*/
|
2020-10-14 08:29:32 +02:00
|
|
|
async delete(name: string): Promise<void> {
|
2022-02-03 12:35:16 +01:00
|
|
|
const table = await this.table;
|
2020-10-07 10:53:19 +02:00
|
|
|
|
2022-01-25 09:33:40 +01:00
|
|
|
await table.deleteByPrimaryKey({ name });
|
2020-10-07 10:53:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get an app setting.
|
|
|
|
*
|
|
|
|
* @param name The config name.
|
|
|
|
* @param defaultValue Default value to use if the entry is not found.
|
|
|
|
* @return Resolves upon success along with the config data. Reject on failure.
|
|
|
|
*/
|
2020-10-14 08:29:32 +02:00
|
|
|
async get<T>(name: string, defaultValue?: T): Promise<T> {
|
2022-02-03 12:35:16 +01:00
|
|
|
try {
|
|
|
|
const table = await this.table;
|
|
|
|
const record = await table.findByPrimaryKey({ name });
|
2020-10-07 10:53:19 +02:00
|
|
|
|
2022-01-25 09:33:40 +01:00
|
|
|
return record.value;
|
2022-02-03 12:35:16 +01:00
|
|
|
} catch (error) {
|
|
|
|
if (defaultValue !== undefined) {
|
|
|
|
return defaultValue;
|
|
|
|
}
|
2020-10-07 10:53:19 +02:00
|
|
|
|
2022-02-03 12:35:16 +01:00
|
|
|
throw error;
|
2020-10-07 10:53:19 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set an app setting.
|
|
|
|
*
|
|
|
|
* @param name The config name.
|
|
|
|
* @param value The config value. Can only store number or strings.
|
|
|
|
* @return Promise resolved when done.
|
|
|
|
*/
|
2020-10-14 08:29:32 +02:00
|
|
|
async set(name: string, value: number | string): Promise<void> {
|
2022-02-03 12:35:16 +01:00
|
|
|
const table = await this.table;
|
2020-10-07 10:53:19 +02:00
|
|
|
|
2022-01-25 09:33:40 +01:00
|
|
|
await table.insert({ name, value });
|
2020-10-07 10:53:19 +02:00
|
|
|
}
|
2020-10-14 08:29:32 +02:00
|
|
|
|
2020-10-07 10:53:19 +02:00
|
|
|
}
|
|
|
|
|
2021-03-02 11:41:04 +01:00
|
|
|
export const CoreConfig = makeSingleton(CoreConfigProvider);
|