2020-10-07 08:53:19 +00: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';
|
|
|
|
|
2020-10-28 13:25:18 +00:00
|
|
|
import { CoreApp } from '@services/app';
|
2020-10-07 08:53:19 +00:00
|
|
|
import { SQLiteDB } from '@classes/sqlitedb';
|
2020-11-24 08:31:11 +00:00
|
|
|
import { makeSingleton } from '@singletons';
|
2020-12-01 17:37:24 +00:00
|
|
|
import { CONFIG_TABLE_NAME, APP_SCHEMA, ConfigDBEntry } from '@services/database/config';
|
2020-10-14 06:29:32 +00:00
|
|
|
|
2020-10-07 08:53:19 +00:00
|
|
|
/**
|
|
|
|
* Factory to provide access to dynamic and permanent config and settings.
|
|
|
|
* It should not be abused into a temporary storage.
|
|
|
|
*/
|
2020-11-19 15:35:17 +00:00
|
|
|
@Injectable({ providedIn: 'root' })
|
2020-10-07 08:53:19 +00:00
|
|
|
export class CoreConfigProvider {
|
2020-10-14 06:29:32 +00:00
|
|
|
|
2020-12-01 17:37:24 +00:00
|
|
|
protected appDB: Promise<SQLiteDB>;
|
|
|
|
protected resolveAppDB!: (appDB: SQLiteDB) => void;
|
2020-10-07 08:53:19 +00:00
|
|
|
|
|
|
|
constructor() {
|
2020-12-01 17:37:24 +00:00
|
|
|
this.appDB = new Promise(resolve => this.resolveAppDB = resolve);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-12-09 12:39:18 +00:00
|
|
|
* Initialize database.
|
2020-12-01 17:37:24 +00:00
|
|
|
*/
|
2020-12-09 12:39:18 +00:00
|
|
|
async initializeDatabase(): Promise<void> {
|
2020-12-01 17:37:24 +00:00
|
|
|
try {
|
2021-03-02 10:41:04 +00:00
|
|
|
await CoreApp.createTablesFromSchema(APP_SCHEMA);
|
2020-12-01 17:37:24 +00:00
|
|
|
} catch (e) {
|
2020-10-07 08:53:19 +00:00
|
|
|
// Ignore errors.
|
2020-12-01 17:37:24 +00:00
|
|
|
}
|
|
|
|
|
2021-03-02 10:41:04 +00:00
|
|
|
this.resolveAppDB(CoreApp.getDB());
|
2020-10-07 08:53:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Deletes an app setting.
|
|
|
|
*
|
|
|
|
* @param name The config name.
|
|
|
|
* @return Promise resolved when done.
|
|
|
|
*/
|
2020-10-14 06:29:32 +00:00
|
|
|
async delete(name: string): Promise<void> {
|
2020-12-01 17:37:24 +00:00
|
|
|
const db = await this.appDB;
|
2020-10-07 08:53:19 +00:00
|
|
|
|
2020-12-01 17:37:24 +00:00
|
|
|
await db.deleteRecords(CONFIG_TABLE_NAME, { name });
|
2020-10-07 08:53:19 +00: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 06:29:32 +00:00
|
|
|
async get<T>(name: string, defaultValue?: T): Promise<T> {
|
2020-12-01 17:37:24 +00:00
|
|
|
const db = await this.appDB;
|
2020-10-07 08:53:19 +00:00
|
|
|
|
|
|
|
try {
|
2020-12-01 17:37:24 +00:00
|
|
|
const entry = await db.getRecord<ConfigDBEntry>(CONFIG_TABLE_NAME, { name });
|
2020-10-07 08:53:19 +00:00
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
2020-10-14 06:29:32 +00:00
|
|
|
async set(name: string, value: number | string): Promise<void> {
|
2020-12-01 17:37:24 +00:00
|
|
|
const db = await this.appDB;
|
2020-10-07 08:53:19 +00:00
|
|
|
|
2020-12-01 17:37:24 +00:00
|
|
|
await db.insertRecord(CONFIG_TABLE_NAME, { name, value });
|
2020-10-07 08:53:19 +00:00
|
|
|
}
|
2020-10-14 06:29:32 +00:00
|
|
|
|
2020-10-07 08:53:19 +00:00
|
|
|
}
|
|
|
|
|
2021-03-02 10:41:04 +00:00
|
|
|
export const CoreConfig = makeSingleton(CoreConfigProvider);
|