diff --git a/packages/datastore-storage-adapter/src/ExpoSQLiteAdapter/ExpoSQLiteDatabase.ts b/packages/datastore-storage-adapter/src/ExpoSQLiteAdapter/ExpoSQLiteDatabase.ts index 3cab19acc04..4271f3b4387 100644 --- a/packages/datastore-storage-adapter/src/ExpoSQLiteAdapter/ExpoSQLiteDatabase.ts +++ b/packages/datastore-storage-adapter/src/ExpoSQLiteAdapter/ExpoSQLiteDatabase.ts @@ -107,21 +107,23 @@ class ExpoSQLiteDatabase implements CommonSQLiteDatabase { public batchQuery( queryParameterizedStatements = new Set(), ): Promise { - return new Promise((resolveTransaction, rejectTransaction) => { + return new Promise((resolve, reject) => { + const resolveTransaction = resolve; + const rejectTransaction = reject; this.db.transaction(async transaction => { try { const results: any[] = await Promise.all( [...queryParameterizedStatements].map( ([statement, params]) => - new Promise((resolve, reject) => { + new Promise((_resolve, _reject) => { transaction.executeSql( statement, params, (_, result) => { - resolve(result.rows._array[0]); + _resolve(result.rows._array[0]); }, (_, error) => { - reject(error); + _reject(error); logger.warn(error); return true; @@ -143,22 +145,24 @@ class ExpoSQLiteDatabase implements CommonSQLiteDatabase { saveParameterizedStatements = new Set(), deleteParameterizedStatements?: Set, ): Promise { - return new Promise((resolveTransaction, rejectTransaction) => { + return new Promise((resolve, reject) => { + const resolveTransaction = resolve; + const rejectTransaction = reject; this.db.transaction(async transaction => { try { // await for all sql statements promises to resolve await Promise.all( [...saveParameterizedStatements].map( ([statement, params]) => - new Promise((resolve, reject) => { + new Promise((_resolve, _reject) => { transaction.executeSql( statement, params, () => { - resolve(null); + _resolve(null); }, (_, error) => { - reject(error); + _reject(error); logger.warn(error); return true; @@ -171,15 +175,15 @@ class ExpoSQLiteDatabase implements CommonSQLiteDatabase { await Promise.all( [...deleteParameterizedStatements].map( ([statement, params]) => - new Promise((resolve, reject) => { + new Promise((_resolve, _reject) => { transaction.executeSql( statement, params, () => { - resolve(null); + _resolve(null); }, (_, error) => { - reject(error); + _reject(error); logger.warn(error); return true; @@ -205,33 +209,35 @@ class ExpoSQLiteDatabase implements CommonSQLiteDatabase { const [queryStatement, queryParams] = queryParameterizedStatement; const [deleteStatement, deleteParams] = deleteParameterizedStatement; - return new Promise((resolveTransaction, rejectTransaction) => { + return new Promise((resolve, reject) => { + const resolveTransaction = resolve; + const rejectTransaction = reject; this.db.transaction(async transaction => { try { - const result: T[] = await new Promise((resolve, reject) => { + const result: T[] = await new Promise((_resolve, _reject) => { transaction.executeSql( queryStatement, queryParams, - (_, result) => { - resolve(result.rows._array || []); + (_, sqlResult) => { + _resolve(sqlResult.rows._array || []); }, (_, error) => { - reject(error); + _reject(error); logger.warn(error); return true; }, ); }); - await new Promise((resolve, reject) => { + await new Promise((_resolve, _reject) => { transaction.executeSql( deleteStatement, deleteParams, () => { - resolve(null); + _resolve(null); }, (_, error) => { - reject(error); + _reject(error); logger.warn(error); return true; @@ -248,21 +254,23 @@ class ExpoSQLiteDatabase implements CommonSQLiteDatabase { } private executeStatements(statements: string[]): Promise { - return new Promise((resolveTransaction, rejectTransaction) => { + return new Promise((resolve, reject) => { + const resolveTransaction = resolve; + const rejectTransaction = reject; this.db.transaction(async transaction => { try { await Promise.all( statements.map( statement => - new Promise((resolve, reject) => { + new Promise((_resolve, _reject) => { transaction.executeSql( statement, [], () => { - resolve(null); + _resolve(null); }, (_, error) => { - reject(error); + _reject(error); return true; }, diff --git a/packages/datastore-storage-adapter/src/SQLiteAdapter/SQLiteDatabase.ts b/packages/datastore-storage-adapter/src/SQLiteAdapter/SQLiteDatabase.ts index fc7bb66e426..c43483df97b 100644 --- a/packages/datastore-storage-adapter/src/SQLiteAdapter/SQLiteDatabase.ts +++ b/packages/datastore-storage-adapter/src/SQLiteAdapter/SQLiteDatabase.ts @@ -140,7 +140,14 @@ class SQLiteDatabase implements CommonSQLiteDatabase { }, logger.warn, ); - tx.executeSql(deleteStatement, deleteParams, () => {}, logger.warn); + tx.executeSql( + deleteStatement, + deleteParams, + () => { + // no-op + }, + logger.warn, + ); }); return results; diff --git a/packages/datastore-storage-adapter/src/common/CommonSQLiteAdapter.ts b/packages/datastore-storage-adapter/src/common/CommonSQLiteAdapter.ts index 5ddf2e2ac53..e0f49b0c591 100644 --- a/packages/datastore-storage-adapter/src/common/CommonSQLiteAdapter.ts +++ b/packages/datastore-storage-adapter/src/common/CommonSQLiteAdapter.ts @@ -70,9 +70,9 @@ export class CommonSQLiteAdapter implements StorageAdapter { ) => PersistentModelConstructor, ) { if (!this.initPromise) { - this.initPromise = new Promise((res, rej) => { - this.resolve = res; - this.reject = rej; + this.initPromise = new Promise((_resolve, _reject) => { + this.resolve = _resolve; + this.reject = _reject; }); } else { await this.initPromise; @@ -89,6 +89,7 @@ export class CommonSQLiteAdapter implements StorageAdapter { this.schema.namespaces.user.models, ).some(model => Object.values(model.fields).some(field => + // eslint-disable-next-line no-prototype-builtins field.association?.hasOwnProperty('targetNames'), ), ); @@ -158,13 +159,19 @@ export class CommonSQLiteAdapter implements StorageAdapter { const { modelName, item, instance } = resItem; const { id } = item; - const [queryStatement, params] = queryByIdStatement(id, modelName); - const fromDB = await this.db.get(queryStatement, params); + const [queryStatementForRestItem, paramsForRestItem] = queryByIdStatement( + id, + modelName, + ); + const fromDBForRestItem = await this.db.get( + queryStatementForRestItem, + paramsForRestItem, + ); const opType: OpType = - fromDB === undefined ? OpType.INSERT : OpType.UPDATE; + fromDBForRestItem === undefined ? OpType.INSERT : OpType.UPDATE; - const saveStatement = fromDB + const saveStatement = fromDBForRestItem ? modelUpdateStatement(instance, modelName) : modelInsertStatement(instance, modelName); @@ -247,10 +254,10 @@ export class CommonSQLiteAdapter implements StorageAdapter { page, ); - return await this.db.getAll(queryStatement, params); + return this.db.getAll(queryStatement, params); })()) as T[]; - return await this.load(namespaceName, modelConstructor.name, records); + return this.load(namespaceName, modelConstructor.name, records); } private async getById( @@ -401,7 +408,8 @@ export class CommonSQLiteAdapter implements StorageAdapter { const { id, _deleted } = item; const { instance } = connectedModels.find( - ({ instance }) => instance.id === id, + ({ instance: connectedModelInstance }) => + connectedModelInstance.id === id, ); if (_deleted) { diff --git a/packages/datastore-storage-adapter/src/common/SQLiteUtils.ts b/packages/datastore-storage-adapter/src/common/SQLiteUtils.ts index 4b9dca12ae0..c89cfc49bb6 100644 --- a/packages/datastore-storage-adapter/src/common/SQLiteUtils.ts +++ b/packages/datastore-storage-adapter/src/common/SQLiteUtils.ts @@ -98,9 +98,10 @@ export function getSQLiteType( return 'TEXT'; case 'Float': return 'REAL'; - default: + default: { const _: never = scalar as never; throw new Error(`unknown type ${scalar as string}`); + } } } @@ -342,10 +343,11 @@ export const whereConditionFromPredicateObject = ({ case 'notContains': statement = [`instr("${field}", ?) ${logicalOperator}`, [operand]]; break; - default: + default: { const _: never = logicalOperatorKey; // Incorrect WHERE clause can result in data loss throw new Error('Cannot map predicate to a valid WHERE clause'); + } } return statement; @@ -364,12 +366,13 @@ export function whereClauseFromPredicate( return [whereClause, params]; function recurse( - predicate: PredicatesGroup | PredicateObject, - result = [], - params = [], + recursedPredicate: PredicatesGroup | PredicateObject, + recursedResult = [], + recursedParams = [], ): void { - if (isPredicateGroup(predicate)) { - const { type: groupType, predicates: groupPredicates } = predicate; + if (isPredicateGroup(recursedPredicate)) { + const { type: groupType, predicates: groupPredicates } = + recursedPredicate; let filterType = ''; let isNegation = false; switch (groupType) { @@ -382,25 +385,26 @@ export function whereClauseFromPredicate( case 'or': filterType = 'OR'; break; - default: + default: { const _: never = groupType as never; throw new Error(`Invalid ${groupType}`); + } } const groupResult = []; for (const p of groupPredicates) { - recurse(p, groupResult, params); + recurse(p, groupResult, recursedParams); } - result.push( + recursedResult.push( `${isNegation ? 'NOT' : ''}(${groupResult.join(` ${filterType} `)})`, ); - } else if (isPredicateObj(predicate)) { + } else if (isPredicateObj(recursedPredicate)) { const [condition, conditionParams] = - whereConditionFromPredicateObject(predicate); + whereConditionFromPredicateObject(recursedPredicate); - result.push(condition); + recursedResult.push(condition); - params.push(...conditionParams); + recursedParams.push(...conditionParams); } } }