From d94402637e5d4dc187f8203f8295c6a808b2c383 Mon Sep 17 00:00:00 2001 From: Noel De Martin Date: Mon, 12 Feb 2024 15:13:22 +0100 Subject: [PATCH] MOBILE-4304 core: Implement SubPartial helper --- src/core/classes/database/database-table-proxy.ts | 5 +++-- src/core/classes/database/database-table.ts | 5 +++-- src/core/classes/database/debug-database-table.ts | 3 ++- src/core/classes/database/eager-database-table.ts | 3 ++- src/core/classes/database/inmemory-database-table.ts | 3 ++- src/core/classes/database/lazy-database-table.ts | 3 ++- src/core/features/h5p/classes/framework.ts | 5 +++-- src/core/features/login/components/site-help/site-help.ts | 3 ++- src/core/utils/types.ts | 5 +++++ 9 files changed, 24 insertions(+), 11 deletions(-) diff --git a/src/core/classes/database/database-table-proxy.ts b/src/core/classes/database/database-table-proxy.ts index 7fd63308c..2aca136a9 100644 --- a/src/core/classes/database/database-table-proxy.ts +++ b/src/core/classes/database/database-table-proxy.ts @@ -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 & Partial>): Promise { + async insert(record: SubPartial): Promise { return this.target.insert(record); } /** * @inheritdoc */ - syncInsert(record: Omit & Partial>): void { + syncInsert(record: SubPartial): void { this.target.syncInsert(record); } diff --git a/src/core/classes/database/database-table.ts b/src/core/classes/database/database-table.ts index c52cb7352..856eaa892 100644 --- a/src/core/classes/database/database-table.ts +++ b/src/core/classes/database/database-table.ts @@ -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 & Partial>): Promise { + async insert(record: SubPartial): Promise { 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 & Partial>): void { + syncInsert(record: SubPartial): 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. diff --git a/src/core/classes/database/debug-database-table.ts b/src/core/classes/database/debug-database-table.ts index cdef34947..a1cd08865 100644 --- a/src/core/classes/database/debug-database-table.ts +++ b/src/core/classes/database/debug-database-table.ts @@ -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 & Partial>): Promise { + insert(record: SubPartial): Promise { this.logger.log('insert', record); return this.target.insert(record); diff --git a/src/core/classes/database/eager-database-table.ts b/src/core/classes/database/eager-database-table.ts index c5c75f9c9..3bfbf6e8d 100644 --- a/src/core/classes/database/eager-database-table.ts +++ b/src/core/classes/database/eager-database-table.ts @@ -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 & Partial>): Promise { + async insert(record: SubPartial): Promise { const rowId = await this.insertAndRemember(record, this.records); return rowId; diff --git a/src/core/classes/database/inmemory-database-table.ts b/src/core/classes/database/inmemory-database-table.ts index 3af78a470..7f01ad912 100644 --- a/src/core/classes/database/inmemory-database-table.ts +++ b/src/core/classes/database/inmemory-database-table.ts @@ -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 & Partial>, + record: SubPartial, records: Record, ): Promise { const rowId = await super.insert(record); diff --git a/src/core/classes/database/lazy-database-table.ts b/src/core/classes/database/lazy-database-table.ts index c0e049e43..a745e29f4 100644 --- a/src/core/classes/database/lazy-database-table.ts +++ b/src/core/classes/database/lazy-database-table.ts @@ -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 & Partial>): Promise { + async insert(record: SubPartial): Promise { const rowId = await this.insertAndRemember(record, this.records); return rowId; diff --git a/src/core/features/h5p/classes/framework.ts b/src/core/features/h5p/classes/framework.ts index 6689fa20f..0e488f4a7 100644 --- a/src/core/features/h5p/classes/framework.ts +++ b/src/core/features/h5p/classes/framework.ts @@ -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 & Partial> = { + const data: SubPartial = { 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 & Partial> = { + const data: SubPartial = { jsoncontent: content.params ?? '{}', mainlibraryid: content.library?.libraryId, timemodified: Date.now(), diff --git a/src/core/features/login/components/site-help/site-help.ts b/src/core/features/login/components/site-help/site-help.ts index ba1b6101d..2bb6fa923 100644 --- a/src/core/features/login/components/site-help/site-help.ts +++ b/src/core/features/login/components/site-help/site-help.ts @@ -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 & { - answer: Omit & Partial>; + answer: SubPartial; }; diff --git a/src/core/utils/types.ts b/src/core/utils/types.ts index 30a311d82..bb4b85917 100644 --- a/src/core/utils/types.ts +++ b/src/core/utils/types.ts @@ -34,6 +34,11 @@ export type GetClosureArgs = T extends (...args: infer TArgs) => any ? TArgs */ export type Pretty = T extends infer U ? {[K in keyof U]: U[K]} : never; +/** + * Helper to convert some keys of an object to optional. + */ +export type SubPartial = Omit & Partial>; + /** * Helper type to omit union. * You can use it if need to omit an element from types union.