2022-02-03 12:35:16 +01: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.
|
|
|
|
|
2022-02-03 13:35:01 +01:00
|
|
|
import { CoreConstants } from '@/core/constants';
|
2022-02-03 13:56:36 +01:00
|
|
|
import { asyncInstance } from '@/core/utils/async-instance';
|
2022-02-03 12:35:16 +01:00
|
|
|
import { SQLiteDB, SQLiteDBRecordValues } from '@classes/sqlitedb';
|
2022-02-10 10:43:06 +01:00
|
|
|
import { CoreConfig, CoreConfigProvider } from '@services/config';
|
2022-02-03 13:35:01 +01:00
|
|
|
import { CoreEventObserver, CoreEvents } from '@singletons/events';
|
2022-02-08 14:38:35 +01:00
|
|
|
import {
|
|
|
|
CoreDatabaseReducer,
|
|
|
|
CoreDatabaseTable,
|
|
|
|
CoreDatabaseConditions,
|
|
|
|
GetDBRecordPrimaryKey,
|
|
|
|
CoreDatabaseQueryOptions,
|
|
|
|
} from './database-table';
|
2022-02-03 13:45:57 +01:00
|
|
|
import { CoreDebugDatabaseTable } from './debug-database-table';
|
2022-02-03 12:35:16 +01:00
|
|
|
import { CoreEagerDatabaseTable } from './eager-database-table';
|
2022-02-03 13:23:08 +01:00
|
|
|
import { CoreLazyDatabaseTable } from './lazy-database-table';
|
2022-02-03 12:35:16 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Database table proxy used to route database interactions through different implementations.
|
|
|
|
*
|
|
|
|
* This class allows using a database wrapper with different optimization strategies that can be changed at runtime.
|
|
|
|
*/
|
|
|
|
export class CoreDatabaseTableProxy<
|
|
|
|
DBRecord extends SQLiteDBRecordValues = SQLiteDBRecordValues,
|
|
|
|
PrimaryKeyColumn extends keyof DBRecord = 'id',
|
|
|
|
PrimaryKey extends GetDBRecordPrimaryKey<DBRecord, PrimaryKeyColumn> = GetDBRecordPrimaryKey<DBRecord, PrimaryKeyColumn>
|
|
|
|
> extends CoreDatabaseTable<DBRecord, PrimaryKeyColumn, PrimaryKey> {
|
|
|
|
|
|
|
|
protected config: CoreDatabaseConfiguration;
|
2022-02-03 13:56:36 +01:00
|
|
|
protected target = asyncInstance<CoreDatabaseTable<DBRecord, PrimaryKeyColumn>>();
|
2022-02-03 13:35:01 +01:00
|
|
|
protected environmentObserver?: CoreEventObserver;
|
2022-02-03 12:35:16 +01:00
|
|
|
|
|
|
|
constructor(
|
|
|
|
config: Partial<CoreDatabaseConfiguration>,
|
|
|
|
database: SQLiteDB,
|
|
|
|
tableName: string,
|
|
|
|
primaryKeyColumns?: PrimaryKeyColumn[],
|
|
|
|
) {
|
|
|
|
super(database, tableName, primaryKeyColumns);
|
|
|
|
|
|
|
|
this.config = { ...this.getConfigDefaults(), ...config };
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
|
|
|
async initialize(): Promise<void> {
|
2022-02-03 13:35:01 +01:00
|
|
|
this.environmentObserver = CoreEvents.on(CoreConfigProvider.ENVIRONMENT_UPDATED, () => this.updateTarget());
|
2022-02-03 12:35:16 +01:00
|
|
|
|
2022-02-03 13:35:01 +01:00
|
|
|
await this.updateTarget();
|
|
|
|
}
|
2022-02-03 12:35:16 +01:00
|
|
|
|
2022-02-03 13:35:01 +01:00
|
|
|
/**
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
|
|
|
async destroy(): Promise<void> {
|
|
|
|
this.environmentObserver?.off();
|
2022-02-03 12:35:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
2022-02-08 14:38:35 +01:00
|
|
|
async getMany(conditions?: Partial<DBRecord>, options?: Partial<CoreDatabaseQueryOptions<DBRecord>>): Promise<DBRecord[]> {
|
|
|
|
return this.target.getMany(conditions, options);
|
2022-02-03 12:35:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
2022-02-08 14:38:35 +01:00
|
|
|
getManyWhere(conditions: CoreDatabaseConditions<DBRecord>): Promise<DBRecord[]> {
|
|
|
|
return this.target.getManyWhere(conditions);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
|
|
|
async getOne(
|
|
|
|
conditions?: Partial<DBRecord>,
|
|
|
|
options?: Partial<Omit<CoreDatabaseQueryOptions<DBRecord>, 'offset' | 'limit'>>,
|
|
|
|
): Promise<DBRecord> {
|
|
|
|
return this.target.getOne(conditions, options);
|
2022-02-03 12:35:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
2022-02-07 14:02:29 +01:00
|
|
|
async getOneByPrimaryKey(primaryKey: PrimaryKey): Promise<DBRecord> {
|
|
|
|
return this.target.getOneByPrimaryKey(primaryKey);
|
2022-02-03 12:35:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
|
|
|
async reduce<T>(reducer: CoreDatabaseReducer<DBRecord, T>, conditions?: CoreDatabaseConditions<DBRecord>): Promise<T> {
|
2022-02-03 13:56:36 +01:00
|
|
|
return this.target.reduce<T>(reducer, conditions);
|
2022-02-03 12:35:16 +01:00
|
|
|
}
|
|
|
|
|
2022-02-08 14:38:35 +01:00
|
|
|
/**
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
|
|
|
hasAny(conditions?: Partial<DBRecord>): Promise<boolean> {
|
|
|
|
return this.target.hasAny(conditions);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
|
|
|
count(conditions?: Partial<DBRecord>): Promise<number> {
|
|
|
|
return this.target.count(conditions);
|
|
|
|
}
|
|
|
|
|
2022-02-03 12:35:16 +01:00
|
|
|
/**
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
|
|
|
async insert(record: DBRecord): Promise<void> {
|
2022-02-03 13:56:36 +01:00
|
|
|
return this.target.insert(record);
|
2022-02-03 12:35:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
|
|
|
async update(updates: Partial<DBRecord>, conditions?: Partial<DBRecord>): Promise<void> {
|
2022-02-03 13:56:36 +01:00
|
|
|
return this.target.update(updates, conditions);
|
2022-02-03 12:35:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
|
|
|
async updateWhere(updates: Partial<DBRecord>, conditions: CoreDatabaseConditions<DBRecord>): Promise<void> {
|
2022-02-03 13:56:36 +01:00
|
|
|
return this.target.updateWhere(updates, conditions);
|
2022-02-03 12:35:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
|
|
|
async delete(conditions?: Partial<DBRecord>): Promise<void> {
|
2022-02-03 13:56:36 +01:00
|
|
|
return this.target.delete(conditions);
|
2022-02-03 12:35:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
|
|
|
async deleteByPrimaryKey(primaryKey: PrimaryKey): Promise<void> {
|
2022-02-03 13:56:36 +01:00
|
|
|
return this.target.deleteByPrimaryKey(primaryKey);
|
2022-02-03 12:35:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get default configuration values.
|
|
|
|
*
|
|
|
|
* @returns Config defaults.
|
|
|
|
*/
|
|
|
|
protected getConfigDefaults(): CoreDatabaseConfiguration {
|
|
|
|
return {
|
|
|
|
cachingStrategy: CoreDatabaseCachingStrategy.None,
|
2022-02-03 13:45:57 +01:00
|
|
|
debug: false,
|
2022-02-03 12:35:16 +01:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-02-03 13:35:01 +01:00
|
|
|
/**
|
|
|
|
* Get database configuration to use at runtime.
|
|
|
|
*
|
|
|
|
* @returns Database configuration.
|
|
|
|
*/
|
2022-02-10 10:43:06 +01:00
|
|
|
protected async getRuntimeConfig(): Promise<CoreDatabaseConfiguration> {
|
|
|
|
await CoreConfig.ready();
|
|
|
|
|
2022-02-03 13:35:01 +01:00
|
|
|
return {
|
|
|
|
...this.config,
|
|
|
|
...CoreConstants.CONFIG.databaseOptimizations,
|
|
|
|
...CoreConstants.CONFIG.databaseTableOptimizations?.[this.tableName],
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update underlying target instance.
|
|
|
|
*/
|
|
|
|
protected async updateTarget(): Promise<void> {
|
2022-02-03 13:56:36 +01:00
|
|
|
const oldTarget = this.target.instance;
|
2022-02-10 10:43:06 +01:00
|
|
|
const newTarget = await this.createTarget();
|
2022-02-03 13:35:01 +01:00
|
|
|
|
|
|
|
if (oldTarget) {
|
|
|
|
await oldTarget.destroy();
|
|
|
|
|
2022-02-03 13:56:36 +01:00
|
|
|
this.target.resetInstance();
|
2022-02-03 13:35:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
await newTarget.initialize();
|
|
|
|
|
2022-02-03 13:56:36 +01:00
|
|
|
this.target.setInstance(newTarget);
|
2022-02-03 13:35:01 +01:00
|
|
|
}
|
|
|
|
|
2022-02-03 12:35:16 +01:00
|
|
|
/**
|
|
|
|
* Create proxy target.
|
|
|
|
*
|
|
|
|
* @returns Target instance.
|
|
|
|
*/
|
2022-02-10 10:43:06 +01:00
|
|
|
protected async createTarget(): Promise<CoreDatabaseTable<DBRecord, PrimaryKeyColumn>> {
|
|
|
|
const config = await this.getRuntimeConfig();
|
2022-02-03 13:45:57 +01:00
|
|
|
const table = this.createTable(config.cachingStrategy);
|
2022-02-03 13:35:01 +01:00
|
|
|
|
2022-02-03 13:45:57 +01:00
|
|
|
return config.debug ? new CoreDebugDatabaseTable(table) : table;
|
2022-02-03 12:35:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a database table using the given caching strategy.
|
|
|
|
*
|
|
|
|
* @param cachingStrategy Caching strategy.
|
|
|
|
* @returns Database table.
|
|
|
|
*/
|
|
|
|
protected createTable(cachingStrategy: CoreDatabaseCachingStrategy): CoreDatabaseTable<DBRecord, PrimaryKeyColumn> {
|
|
|
|
switch (cachingStrategy) {
|
|
|
|
case CoreDatabaseCachingStrategy.Eager:
|
|
|
|
return new CoreEagerDatabaseTable(this.database, this.tableName, this.primaryKeyColumns);
|
2022-02-03 13:23:08 +01:00
|
|
|
case CoreDatabaseCachingStrategy.Lazy:
|
|
|
|
return new CoreLazyDatabaseTable(this.database, this.tableName, this.primaryKeyColumns);
|
2022-02-03 12:35:16 +01:00
|
|
|
case CoreDatabaseCachingStrategy.None:
|
|
|
|
return new CoreDatabaseTable(this.database, this.tableName, this.primaryKeyColumns);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Database proxy configuration.
|
|
|
|
*/
|
|
|
|
export interface CoreDatabaseConfiguration {
|
|
|
|
cachingStrategy: CoreDatabaseCachingStrategy;
|
2022-02-03 13:45:57 +01:00
|
|
|
debug: boolean;
|
2022-02-03 12:35:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Database caching strategies.
|
|
|
|
*/
|
|
|
|
export enum CoreDatabaseCachingStrategy {
|
|
|
|
Eager = 'eager',
|
2022-02-03 13:23:08 +01:00
|
|
|
Lazy = 'lazy',
|
2022-02-03 12:35:16 +01:00
|
|
|
None = 'none',
|
|
|
|
}
|