Skip to content

Commit

Permalink
chore(data-storage-adapter): run yarn lint:fix
Browse files Browse the repository at this point in the history
  • Loading branch information
HuiSF committed May 2, 2024
1 parent 7a2a22b commit d65896a
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 48 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
import { CommonSQLiteAdapter } from '../common/CommonSQLiteAdapter';

import ExpoSQLiteDatabase from './ExpoSQLiteDatabase';

const ExpoSQLiteAdapter: CommonSQLiteAdapter = new CommonSQLiteAdapter(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,18 @@
import { ConsoleLogger } from '@aws-amplify/core';
import { PersistentModel } from '@aws-amplify/datastore';
import { deleteAsync, documentDirectory } from 'expo-file-system';
import { openDatabase, WebSQLDatabase } from 'expo-sqlite';
import { WebSQLDatabase, openDatabase } from 'expo-sqlite';

import { DB_NAME } from '../common/constants';
import { CommonSQLiteDatabase, ParameterizedStatement } from '../common/types';

const logger = new ConsoleLogger('ExpoSQLiteDatabase');

/*
Note:
ExpoSQLite transaction error callbacks require returning a boolean value to indicate whether the
error was handled or not. Returning a true value indicates the error was handled and does not
Note:
ExpoSQLite transaction error callbacks require returning a boolean value to indicate whether the
error was handled or not. Returning a true value indicates the error was handled and does not
rollback the whole transaction.
*/
Expand Down Expand Up @@ -56,6 +57,7 @@ class ExpoSQLiteDatabase implements CommonSQLiteDatabase {
params: (string | number)[],
): Promise<T> {
const results: T[] = await this.getAll(statement, params);

return results[0];
}

Expand All @@ -74,6 +76,7 @@ class ExpoSQLiteDatabase implements CommonSQLiteDatabase {
(_, error) => {
reject(error);
logger.warn(error);

return true;
},
);
Expand All @@ -93,6 +96,7 @@ class ExpoSQLiteDatabase implements CommonSQLiteDatabase {
(_, error) => {
reject(error);
logger.warn(error);

return true;
},
);
Expand All @@ -101,7 +105,7 @@ class ExpoSQLiteDatabase implements CommonSQLiteDatabase {
}

public batchQuery<T = any>(
queryParameterizedStatements: Set<ParameterizedStatement> = new Set(),
queryParameterizedStatements = new Set<ParameterizedStatement>(),
): Promise<T[]> {
return new Promise((resolveTransaction, rejectTransaction) => {
this.db.transaction(async transaction => {
Expand All @@ -119,6 +123,7 @@ class ExpoSQLiteDatabase implements CommonSQLiteDatabase {
(_, error) => {
reject(error);
logger.warn(error);

return true;
},
);
Expand All @@ -135,7 +140,7 @@ class ExpoSQLiteDatabase implements CommonSQLiteDatabase {
}

public batchSave(
saveParameterizedStatements: Set<ParameterizedStatement> = new Set(),
saveParameterizedStatements = new Set<ParameterizedStatement>(),
deleteParameterizedStatements?: Set<ParameterizedStatement>,
): Promise<void> {
return new Promise((resolveTransaction, rejectTransaction) => {
Expand All @@ -155,6 +160,7 @@ class ExpoSQLiteDatabase implements CommonSQLiteDatabase {
(_, error) => {
reject(error);
logger.warn(error);

return true;
},
);
Expand All @@ -165,7 +171,7 @@ class ExpoSQLiteDatabase implements CommonSQLiteDatabase {
await Promise.all(
[...deleteParameterizedStatements].map(
([statement, params]) =>
new Promise((resolve, reject) =>
new Promise((resolve, reject) => {
transaction.executeSql(
statement,
params,
Expand All @@ -175,10 +181,11 @@ class ExpoSQLiteDatabase implements CommonSQLiteDatabase {
(_, error) => {
reject(error);
logger.warn(error);

return true;
},
),
),
);
}),
),
);
}
Expand Down Expand Up @@ -211,6 +218,7 @@ class ExpoSQLiteDatabase implements CommonSQLiteDatabase {
(_, error) => {
reject(error);
logger.warn(error);

return true;
},
);
Expand All @@ -225,6 +233,7 @@ class ExpoSQLiteDatabase implements CommonSQLiteDatabase {
(_, error) => {
reject(error);
logger.warn(error);

return true;
},
);
Expand Down Expand Up @@ -254,6 +263,7 @@ class ExpoSQLiteDatabase implements CommonSQLiteDatabase {
},
(_, error) => {
reject(error);

return true;
},
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
import { CommonSQLiteAdapter } from '../common/CommonSQLiteAdapter';

import SQLiteDatabase from './SQLiteDatabase';

const SQLiteAdapter: CommonSQLiteAdapter = new CommonSQLiteAdapter(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import SQLite from 'react-native-sqlite-storage';
import { ConsoleLogger } from '@aws-amplify/core';
import { PersistentModel } from '@aws-amplify/datastore';

import { DB_NAME } from '../common/constants';
import { CommonSQLiteDatabase, ParameterizedStatement } from '../common/types';

Expand All @@ -16,7 +17,7 @@ if (ConsoleLogger.LOG_LEVEL === 'DEBUG') {

/*
Note:
Note:
I purposely avoided using arrow functions () => {} in this class,
Because I ran into issues with them in some of the SQLite method callbacks
Expand All @@ -41,7 +42,7 @@ class SQLiteDatabase implements CommonSQLiteDatabase {
}

public async createSchema(statements: string[]): Promise<void> {
return await this.executeStatements(statements);
await this.executeStatements(statements);
}

public async clear(): Promise<void> {
Expand All @@ -56,6 +57,7 @@ class SQLiteDatabase implements CommonSQLiteDatabase {
params: (string | number)[],
): Promise<T> {
const results: T[] = await this.getAll(statement, params);

return results[0];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,40 @@
// SPDX-License-Identifier: Apache-2.0
import { ConsoleLogger } from '@aws-amplify/core';
import {
generateSchemaStatements,
queryByIdStatement,
modelUpdateStatement,
modelInsertStatement,
queryAllStatement,
queryOneStatement,
deleteByIdStatement,
deleteByPredicateStatement,
} from '../common/SQLiteUtils';

import {
StorageAdapter,
InternalSchema,
ModelInstanceCreator,
ModelPredicate,
ModelPredicateCreator,
ModelSortPredicateCreator,
InternalSchema,
isPredicateObj,
ModelPredicate,
NamespaceResolver,
NAMESPACES,
NamespaceResolver,
OpType,
PaginationInput,
PersistentModel,
PersistentModelConstructor,
PredicateObject,
PredicatesGroup,
QueryOne,
StorageAdapter,
isPredicateObj,
utils,
} from '@aws-amplify/datastore';

import {
deleteByIdStatement,
deleteByPredicateStatement,
generateSchemaStatements,
modelInsertStatement,
modelUpdateStatement,
queryAllStatement,
queryByIdStatement,
queryOneStatement,
} from '../common/SQLiteUtils';

import {
CommonSQLiteDatabase,
ParameterizedStatement,
ModelInstanceMetadataWithId,
ParameterizedStatement,
} from './types';

const { traverseModel, validatePredicate, isModelConstructor } = utils;
Expand All @@ -49,6 +50,7 @@ export class CommonSQLiteAdapter implements StorageAdapter {
namsespaceName: string,
modelName: string,
) => PersistentModelConstructor<any>;

private db: CommonSQLiteDatabase;
private initPromise: Promise<void>;
private resolve: (value?: any) => void;
Expand All @@ -74,6 +76,7 @@ export class CommonSQLiteAdapter implements StorageAdapter {
});
} else {
await this.initPromise;

return;
}
this.schema = theSchema;
Expand Down Expand Up @@ -205,6 +208,7 @@ export class CommonSQLiteAdapter implements StorageAdapter {
for (const r of relations) {
delete record[r.fieldName];
}

return this.modelInstanceCreator(modelConstructor, record);
});
}
Expand All @@ -228,9 +232,10 @@ export class CommonSQLiteAdapter implements StorageAdapter {

const queryById = predicates && this.idFromPredicate(predicates);

const records: T[] = <T[]>await (async () => {
const records: T[] = (await (async () => {
if (queryById) {
const record = await this.getById(tableName, queryById);

return record ? [record] : [];
}

Expand All @@ -243,7 +248,7 @@ export class CommonSQLiteAdapter implements StorageAdapter {
);

return await this.db.getAll(queryStatement, params);
})();
})()) as T[];

return await this.load(namespaceName, modelConstructor.name, records);
}
Expand Down Expand Up @@ -403,7 +408,7 @@ export class CommonSQLiteAdapter implements StorageAdapter {
// create the delete statements right away
const deleteStatement = deleteByIdStatement(instance.id, tableName);
deleteStatements.add(deleteStatement);
result.push([<T>(<unknown>item), OpType.DELETE]);
result.push([item as unknown as T, OpType.DELETE]);
} else {
// query statements for the saves at first
const queryStatement = queryByIdStatement(id, tableName);
Expand All @@ -423,14 +428,14 @@ export class CommonSQLiteAdapter implements StorageAdapter {
tableName,
);
saveStatements.add(insertStatement);
result.push([<T>(<unknown>itemsToSave[idx]), OpType.INSERT]);
result.push([itemsToSave[idx] as unknown as T, OpType.INSERT]);
} else {
const updateStatement = modelUpdateStatement(
itemsToSave[idx],
tableName,
);
saveStatements.add(updateStatement);
result.push([<T>(<unknown>itemsToSave[idx]), OpType.UPDATE]);
result.push([itemsToSave[idx] as unknown as T, OpType.UPDATE]);
}
});

Expand Down
Loading

0 comments on commit d65896a

Please sign in to comment.