// (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'; import { CoreApp, CoreAppSchema } from '@services/app'; import { SQLiteDB } from '@classes/sqlitedb'; import { makeSingleton } from '@singletons/core.singletons'; const TABLE_NAME = 'core_config'; /** * Factory to provide access to dynamic and permanent config and settings. * It should not be abused into a temporary storage. */ @Injectable() export class CoreConfigProvider { protected appDB: SQLiteDB; protected tableSchema: CoreAppSchema = { name: 'CoreConfigProvider', version: 1, tables: [ { name: TABLE_NAME, columns: [ { name: 'name', type: 'TEXT', unique: true, notNull: true, }, { name: 'value', }, ], }, ], }; protected dbReady: Promise; // Promise resolved when the app DB is initialized. constructor() { this.appDB = CoreApp.instance.getDB(); this.dbReady = CoreApp.instance.createTablesFromSchema(this.tableSchema).catch(() => { // Ignore errors. }); } /** * Deletes an app setting. * * @param name The config name. * @return Promise resolved when done. */ async delete(name: string): Promise { await this.dbReady; await this.appDB.deleteRecords(TABLE_NAME, { name }); } /** * 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. */ async get(name: string, defaultValue?: T): Promise { await this.dbReady; try { const entry = await this.appDB.getRecord(TABLE_NAME, { name }); return entry.value; } catch (error) { if (typeof defaultValue != 'undefined') { return defaultValue; } throw error; } } /** * 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. */ async set(name: string, value: number | string): Promise { await this.dbReady; await this.appDB.insertRecord(TABLE_NAME, { name, value }); } } export class CoreConfig extends makeSingleton(CoreConfigProvider) {} type ConfigDBEntry = { name: string; // eslint-disable-next-line @typescript-eslint/no-explicit-any value: any; };