Skip to content

Commit

Permalink
Apply feedback from Caleb & Chris, Remove unused variable and separat…
Browse files Browse the repository at this point in the history
…e entrypoints
  • Loading branch information
chintannp committed Mar 10, 2022
1 parent 26bed3c commit 0145017
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 82 deletions.
7 changes: 7 additions & 0 deletions packages/datastore-storage-adapter/SQLiteAdapter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
'use strict';

if (process.env.NODE_ENV === 'production') {
module.exports = require('./dist/aws-amplify-datastore-storage-adapter.min.js');
} else {
module.exports = require('./dist/aws-amplify-datastore-storage-adapter.js');
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { openDatabase, SQLResultSet, WebSQLDatabase } from 'expo-sqlite';
import { openDatabase, WebSQLDatabase } from 'expo-sqlite';
import { ConsoleLogger as Logger } from '@aws-amplify/core';
import { PersistentModel } from '@aws-amplify/datastore';
import { CommonSQLiteDatabase, ParameterizedStatement } from '../common/types';
Expand All @@ -7,7 +7,7 @@ import { deleteAsync, documentDirectory } from 'expo-file-system';
const logger = new Logger('ExpoSQLiteDatabase');

const DB_NAME = 'AmplifyDatastore';
const DB_DISPLAYNAME = 'AWS Amplify DataStore SQLite Database';
const DB_DISPLAY_NAME = 'AWS Amplify DataStore SQLite Database';

// TODO: make these configurable
const DB_SIZE = 200000;
Expand All @@ -16,7 +16,6 @@ const DB_VERSION = '1.0';
/*
Note:
I purposely used arrow functions () => {} in this class as expo-sqlite library is not promisified.
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 All @@ -30,7 +29,7 @@ class ExpoSQLiteDatabase implements CommonSQLiteDatabase {
// only open database once.

if (!this.db) {
this.db = openDatabase(DB_NAME, DB_VERSION, DB_DISPLAYNAME, DB_SIZE);
this.db = openDatabase(DB_NAME, DB_VERSION, DB_DISPLAY_NAME, DB_SIZE);
}
}

Expand All @@ -44,7 +43,7 @@ class ExpoSQLiteDatabase implements CommonSQLiteDatabase {
await this.closeDB();
// delete database is not supported by expo-sqlite.
// Database file needs to be deleted using deleteAsync from expo-file-system
await deleteAsync(documentDirectory + 'SQLite/' + DB_NAME);
await deleteAsync(`${documentDirectory}SQLite/${DB_NAME}`);
logger.debug('Database cleared');
} catch (error) {
logger.warn('Error clearing the database.', error);
Expand All @@ -66,12 +65,12 @@ class ExpoSQLiteDatabase implements CommonSQLiteDatabase {
params: (string | number)[]
): Promise<T[]> {
return new Promise((resolve, reject) => {
this.db.readTransaction(tx => {
tx.executeSql(
this.db.readTransaction(transaction => {
transaction.executeSql(
statement,
params,
(_, res) => {
resolve(res?.rows?._array || []);
(_, result) => {
resolve(result.rows._array || []);
},
(_, error) => {
reject(error);
Expand All @@ -88,11 +87,13 @@ class ExpoSQLiteDatabase implements CommonSQLiteDatabase {
params: (string | number)[]
): Promise<void> {
return new Promise((resolve, reject) => {
this.db.transaction(tx => {
tx.executeSql(
this.db.transaction(transaction => {
transaction.executeSql(
statement,
params,
() => resolve(null),
() => {
resolve(null);
},
(_, error) => {
reject(error);
logger.warn(error);
Expand All @@ -104,20 +105,20 @@ class ExpoSQLiteDatabase implements CommonSQLiteDatabase {
}

public async batchQuery<T = any>(
queryStatements: Set<ParameterizedStatement>
queryParameterizedStatements: Set<ParameterizedStatement> = new Set()
): Promise<T[]> {
return new Promise((resolveTx, rejectTx) => {
this.db.transaction(async tx => {
return new Promise((resolveTransaction, rejectTransaction) => {
this.db.transaction(async transaction => {
try {
const results: any[] = await Promise.all(
[...queryStatements].map(
[...queryParameterizedStatements].map(
([statement, params]) =>
new Promise((resolve, reject) => {
tx.executeSql(
transaction.executeSql(
statement,
params,
(_, res) => {
resolve(res.rows._array[0]);
(_, result) => {
resolve(result.rows._array[0]);
},
(_, error) => {
reject(error);
Expand All @@ -128,30 +129,30 @@ class ExpoSQLiteDatabase implements CommonSQLiteDatabase {
})
)
);
resolveTx(results);
resolveTransaction(results);
} catch (error) {
rejectTx(error);
rejectTransaction(error);
}
});
});
}

public async batchSave(
saveStatements: Set<ParameterizedStatement>,
deleteStatements?: Set<ParameterizedStatement>
saveParameterizedStatements: Set<ParameterizedStatement> = new Set(),
deleteParameterizedStatements?: Set<ParameterizedStatement>
): Promise<void> {
return new Promise((resolveTx, rejectTx) => {
return new Promise((resolveTransaction, rejectTransaction) => {
try {
this.db.transaction(async tx => {
this.db.transaction(async transaction => {
// await for all sql statments promises to resolve
await Promise.all(
[...(saveStatements ?? [])].map(
[...saveParameterizedStatements].map(
([statement, params]) =>
new Promise((resolve, reject) =>
tx.executeSql(
transaction.executeSql(
statement,
params,
(_, result) => {
() => {
resolve(null);
},
(_, error) => {
Expand All @@ -164,30 +165,32 @@ class ExpoSQLiteDatabase implements CommonSQLiteDatabase {
)
);
});
this.db.transaction(async tx => {
await Promise.all(
[...(deleteStatements ?? [])].map(
([statement, params]) =>
new Promise((resolve, reject) =>
tx.executeSql(
statement,
params,
(_, result) => {
resolve(null);
},
(_, error) => {
reject(error);
logger.warn(error);
return true;
}
if (deleteParameterizedStatements) {
this.db.transaction(async transaction => {
await Promise.all(
[...deleteParameterizedStatements].map(
([statement, params]) =>
new Promise((resolve, reject) =>
transaction.executeSql(
statement,
params,
() => {
resolve(null);
},
(_, error) => {
reject(error);
logger.warn(error);
return true;
}
)
)
)
)
);
});
resolveTx(null);
)
);
});
}
resolveTransaction(null);
} catch (error) {
rejectTx(error);
rejectTransaction(error);
}
});
}
Expand All @@ -199,15 +202,15 @@ class ExpoSQLiteDatabase implements CommonSQLiteDatabase {
const [queryStatement, queryParams] = queryParameterizedStatement;
const [deleteStatement, deleteParams] = deleteParameterizedStatement;

return new Promise((resolveTx, rejectTx) => {
this.db.transaction(async tx => {
return new Promise((resolveTransaction, rejectTransaction) => {
this.db.transaction(async transaction => {
try {
const result: T[] = await new Promise((resolve, reject) => {
tx.executeSql(
transaction.executeSql(
queryStatement,
queryParams,
(_, res) => {
resolve(res?.rows?._array || []);
(_, result) => {
resolve(result.rows._array || []);
},
(_, error) => {
reject(error);
Expand All @@ -217,10 +220,10 @@ class ExpoSQLiteDatabase implements CommonSQLiteDatabase {
);
});
await new Promise((resolve, reject) => {
tx.executeSql(
transaction.executeSql(
deleteStatement,
deleteParams,
(_, result) => {
() => {
resolve(null);
},
(_, error) => {
Expand All @@ -230,26 +233,26 @@ class ExpoSQLiteDatabase implements CommonSQLiteDatabase {
}
);
});
resolveTx(result);
resolveTransaction(result);
} catch (error) {
rejectTx(error);
rejectTransaction(error);
}
});
});
}

private async executeStatements(statements: string[]): Promise<void> {
return new Promise((resolveTx, rejectTx) => {
this.db.transaction(async tx => {
return new Promise((resolveTransaction, rejectTransaction) => {
this.db.transaction(async transaction => {
try {
await Promise.all(
statements.map(
statement =>
new Promise((resolve, reject) => {
tx.executeSql(
transaction.executeSql(
statement,
[],
(_, result) => {
() => {
resolve(null);
},
(_, error) => {
Expand All @@ -260,9 +263,9 @@ class ExpoSQLiteDatabase implements CommonSQLiteDatabase {
})
)
);
resolveTx(null);
resolveTransaction(null);
} catch (error) {
rejectTx(error);
rejectTransaction(error);
}
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,6 @@ if (Logger.LOG_LEVEL === 'DEBUG') {
}

const DB_NAME = 'AmplifyDatastore';
const DB_DISPLAYNAME = 'AWS Amplify DataStore SQLite Database';

// TODO: make these configurable
const DB_SIZE = 200000;
const DB_VERSION = '1.0';

/*
Expand Down Expand Up @@ -86,12 +81,12 @@ class SQLiteDatabase implements CommonSQLiteDatabase {
}

public async batchQuery<T = any>(
queryStatements: Set<ParameterizedStatement>
queryParameterizedStatements: Set<ParameterizedStatement>
): Promise<T[]> {
const results = [];

await this.db.readTransaction(tx => {
for (const [statement, params] of queryStatements) {
for (const [statement, params] of queryParameterizedStatements) {
tx.executeSql(
statement,
params,
Expand All @@ -107,19 +102,21 @@ class SQLiteDatabase implements CommonSQLiteDatabase {
}

public async batchSave(
saveStatements: Set<ParameterizedStatement>,
deleteStatements?: Set<ParameterizedStatement>
saveParameterizedStatements: Set<ParameterizedStatement>,
deleteParameterizedStatements?: Set<ParameterizedStatement>
): Promise<void> {
await this.db.transaction(tx => {
for (const [statement, params] of saveStatements) {
for (const [statement, params] of saveParameterizedStatements) {
tx.executeSql(statement, params);
}
if (deleteStatements) {
for (const [statement, params] of deleteStatements) {
});
if (deleteParameterizedStatements) {
await this.db.transaction(tx => {
for (const [statement, params] of deleteParameterizedStatements) {
tx.executeSql(statement, params);
}
}
});
});
}
}

public async selectAndDelete<T = any>(
Expand Down
6 changes: 3 additions & 3 deletions packages/datastore-storage-adapter/src/common/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ export interface CommonSQLiteDatabase {
): Promise<T[]>;
save(statement: string, params: (string | number)[]): Promise<void>;
batchQuery<T = any>(
queryStatements: Set<ParameterizedStatement>
queryParameterizedStatement: Set<ParameterizedStatement>
): Promise<T[]>;
batchSave(
saveStatements: Set<ParameterizedStatement>,
deleteStatements?: Set<ParameterizedStatement>
saveParameterizedStatements: Set<ParameterizedStatement>,
deleteParameterizedStatements?: Set<ParameterizedStatement>
): Promise<void>;
selectAndDelete<T = any>(
queryParameterizedStatement: ParameterizedStatement,
Expand Down

0 comments on commit 0145017

Please sign in to comment.