MOBILE-4304 core: Implement SubPartial helper

main
Noel De Martin 2024-02-12 15:13:22 +01:00
parent c80674776a
commit d94402637e
9 changed files with 24 additions and 11 deletions

View File

@ -29,6 +29,7 @@ import {
import { CoreDebugDatabaseTable } from './debug-database-table';
import { CoreEagerDatabaseTable } from './eager-database-table';
import { CoreLazyDatabaseTable } from './lazy-database-table';
import { SubPartial } from '@/core/utils/types';
/**
* Database table proxy used to route database interactions through different implementations.
@ -155,14 +156,14 @@ export class CoreDatabaseTableProxy<
/**
* @inheritdoc
*/
async insert(record: Omit<DBRecord, RowIdColumn> & Partial<Pick<DBRecord, RowIdColumn>>): Promise<number> {
async insert(record: SubPartial<DBRecord, RowIdColumn>): Promise<number> {
return this.target.insert(record);
}
/**
* @inheritdoc
*/
syncInsert(record: Omit<DBRecord, RowIdColumn> & Partial<Pick<DBRecord, RowIdColumn>>): void {
syncInsert(record: SubPartial<DBRecord, RowIdColumn>): void {
this.target.syncInsert(record);
}

View File

@ -12,6 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.
import { SubPartial } from '@/core/utils/types';
import { CoreError } from '@classes/errors/error';
import { SQLiteDB, SQLiteDBRecordValue, SQLiteDBRecordValues } from '@classes/sqlitedb';
@ -259,7 +260,7 @@ export class CoreDatabaseTable<
* @param record Database record.
* @returns New record row id.
*/
async insert(record: Omit<DBRecord, RowIdColumn> & Partial<Pick<DBRecord, RowIdColumn>>): Promise<number> {
async insert(record: SubPartial<DBRecord, RowIdColumn>): Promise<number> {
const rowId = await this.database.insertRecord(this.tableName, record);
return rowId;
@ -270,7 +271,7 @@ export class CoreDatabaseTable<
*
* @param record Database record.
*/
syncInsert(record: Omit<DBRecord, RowIdColumn> & Partial<Pick<DBRecord, RowIdColumn>>): void {
syncInsert(record: SubPartial<DBRecord, RowIdColumn>): void {
// The current database architecture does not support synchronous operations,
// so calling this method will mean that errors will be silenced. Because of that,
// this should only be called if using the asynchronous alternatives is not possible.

View File

@ -21,6 +21,7 @@ import {
CoreDatabaseReducer,
CoreDatabaseQueryOptions,
} from './database-table';
import { SubPartial } from '@/core/utils/types';
/**
* Database table proxy used to debug runtime operations.
@ -153,7 +154,7 @@ export class CoreDebugDatabaseTable<
/**
* @inheritdoc
*/
insert(record: Omit<DBRecord, RowIdColumn> & Partial<Pick<DBRecord, RowIdColumn>>): Promise<number> {
insert(record: SubPartial<DBRecord, RowIdColumn>): Promise<number> {
this.logger.log('insert', record);
return this.target.insert(record);

View File

@ -21,6 +21,7 @@ import {
CoreDatabaseReducer,
CoreDatabaseQueryOptions,
} from './database-table';
import { SubPartial } from '@/core/utils/types';
/**
* Wrapper used to improve performance by caching all the records for faster read operations.
@ -154,7 +155,7 @@ export class CoreEagerDatabaseTable<
/**
* @inheritdoc
*/
async insert(record: Omit<DBRecord, RowIdColumn> & Partial<Pick<DBRecord, RowIdColumn>>): Promise<number> {
async insert(record: SubPartial<DBRecord, RowIdColumn>): Promise<number> {
const rowId = await this.insertAndRemember(record, this.records);
return rowId;

View File

@ -16,6 +16,7 @@ import { CoreConstants } from '@/core/constants';
import { SQLiteDB, SQLiteDBRecordValues } from '@classes/sqlitedb';
import { CoreLogger } from '@singletons/logger';
import { CoreDatabaseTable, GetDBRecordPrimaryKey } from './database-table';
import { SubPartial } from '@/core/utils/types';
/**
* Database wrapper that caches database records in memory to speed up read operations.
@ -79,7 +80,7 @@ export abstract class CoreInMemoryDatabaseTable<
* @returns New record row id.
*/
protected async insertAndRemember(
record: Omit<DBRecord, RowIdColumn> & Partial<Pick<DBRecord, RowIdColumn>>,
record: SubPartial<DBRecord, RowIdColumn>,
records: Record<string, DBRecord | null>,
): Promise<number> {
const rowId = await super.insert(record);

View File

@ -21,6 +21,7 @@ import {
GetDBRecordPrimaryKey,
CoreDatabaseQueryOptions,
} from './database-table';
import { SubPartial } from '@/core/utils/types';
/**
* Wrapper used to improve performance by caching records that are used often for faster read operations.
@ -138,7 +139,7 @@ export class CoreLazyDatabaseTable<
/**
* @inheritdoc
*/
async insert(record: Omit<DBRecord, RowIdColumn> & Partial<Pick<DBRecord, RowIdColumn>>): Promise<number> {
async insert(record: SubPartial<DBRecord, RowIdColumn>): Promise<number> {
const rowId = await this.insertAndRemember(record, this.records);
return rowId;

View File

@ -47,6 +47,7 @@ import { AsyncInstance, asyncInstance } from '@/core/utils/async-instance';
import { LazyMap, lazyMap } from '@/core/utils/lazy-map';
import { CoreDatabaseTable } from '@classes/database/database-table';
import { CoreDatabaseCachingStrategy } from '@classes/database/database-table-proxy';
import { SubPartial } from '@/core/utils/types';
/**
* Equivalent to Moodle's implementation of H5PFrameworkInterface.
@ -780,7 +781,7 @@ export class CoreH5PFramework {
embedTypes = libraryData.embedTypes.join(', ');
}
const data: Omit<CoreH5PLibraryDBRecord, 'id'> & Partial<Pick<CoreH5PLibraryDBRecord, 'id'>> = {
const data: SubPartial<CoreH5PLibraryDBRecord, 'id'> = {
title: libraryData.title,
machinename: libraryData.machineName,
majorversion: libraryData.majorVersion,
@ -930,7 +931,7 @@ export class CoreH5PFramework {
throw new CoreError('Attempted to create content of library without id');
}
const data: Omit<CoreH5PContentDBRecord, 'id'> & Partial<Pick<CoreH5PContentDBRecord, 'id'>> = {
const data: SubPartial<CoreH5PContentDBRecord, 'id'> = {
jsoncontent: content.params ?? '{}',
mainlibraryid: content.library?.libraryId,
timemodified: Date.now(),

View File

@ -19,6 +19,7 @@ import { ModalController, Translate } from '@singletons';
import { FAQ_QRCODE_IMAGE_HTML, FAQ_URL_IMAGE_HTML, GET_STARTED_URL } from '@features/login/constants';
import { CoreDomUtils } from '@services/utils/dom';
import { CoreCancellablePromise } from '@classes/cancellable-promise';
import { SubPartial } from '@/core/utils/types';
/**
* Component that displays help to connect to a site.
@ -217,5 +218,5 @@ enum AnswerFormat {
* Question definition.
*/
type QuestionDefinition = Omit<Question, 'id' | 'answer'> & {
answer: Omit<Answer, 'class'> & Partial<Pick<Answer, 'class'>>;
answer: SubPartial<Answer, 'class'>;
};

View File

@ -34,6 +34,11 @@ export type GetClosureArgs<T> = T extends (...args: infer TArgs) => any ? TArgs
*/
export type Pretty<T> = T extends infer U ? {[K in keyof U]: U[K]} : never;
/**
* Helper to convert some keys of an object to optional.
*/
export type SubPartial<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T, K>>;
/**
* Helper type to omit union.
* You can use it if need to omit an element from types union.