Skip to content

Commit

Permalink
Remove unnecesary async, alphabetic order for import statements and n…
Browse files Browse the repository at this point in the history
…pm packages, single transaction for batchSave
  • Loading branch information
chintannp committed Mar 16, 2022
1 parent 0145017 commit 55c76e6
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 27 deletions.
4 changes: 2 additions & 2 deletions packages/datastore-storage-adapter/ExpoSQLiteAdapter.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';

if (process.env.NODE_ENV === 'production') {
module.exports = require('./dist/aws-amplify-datastore-storage-adapter-expo.min.js');
module.exports = require('./dist/aws-amplify-datastore-sqlite-adapter-expo.min.js');
} else {
module.exports = require('./dist/aws-amplify-datastore-storage-adapter-expo.js');
module.exports = require('./dist/aws-amplify-datastore-sqlite-adapter-expo.js');
}
4 changes: 2 additions & 2 deletions packages/datastore-storage-adapter/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@
"@aws-amplify/core": "4.3.14",
"@aws-amplify/datastore": "3.7.6",
"@types/react-native-sqlite-storage" : "5.0.1",
"expo-file-system" : "13.1.4",
"expo-sqlite": "10.1.0",
"react-native-sqlite-storage": "5.0.0",
"expo-file-system" : "13.1.4"
"react-native-sqlite-storage": "5.0.0"
},
"jest": {
"globals": {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
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';
import { deleteAsync, documentDirectory } from 'expo-file-system';
import { openDatabase, WebSQLDatabase } from 'expo-sqlite';

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

const logger = new Logger('ExpoSQLiteDatabase');

Expand Down Expand Up @@ -33,7 +34,7 @@ class ExpoSQLiteDatabase implements CommonSQLiteDatabase {
}
}

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

Expand All @@ -60,7 +61,7 @@ class ExpoSQLiteDatabase implements CommonSQLiteDatabase {
return results[0];
}

public async getAll<T extends PersistentModel>(
public getAll<T extends PersistentModel>(
statement: string,
params: (string | number)[]
): Promise<T[]> {
Expand All @@ -82,10 +83,7 @@ class ExpoSQLiteDatabase implements CommonSQLiteDatabase {
});
}

public async save(
statement: string,
params: (string | number)[]
): Promise<void> {
public save(statement: string, params: (string | number)[]): Promise<void> {
return new Promise((resolve, reject) => {
this.db.transaction(transaction => {
transaction.executeSql(
Expand All @@ -104,7 +102,7 @@ class ExpoSQLiteDatabase implements CommonSQLiteDatabase {
});
}

public async batchQuery<T = any>(
public batchQuery<T = any>(
queryParameterizedStatements: Set<ParameterizedStatement> = new Set()
): Promise<T[]> {
return new Promise((resolveTransaction, rejectTransaction) => {
Expand Down Expand Up @@ -137,14 +135,14 @@ class ExpoSQLiteDatabase implements CommonSQLiteDatabase {
});
}

public async batchSave(
public batchSave(
saveParameterizedStatements: Set<ParameterizedStatement> = new Set(),
deleteParameterizedStatements?: Set<ParameterizedStatement>
): Promise<void> {
return new Promise((resolveTransaction, rejectTransaction) => {
try {
this.db.transaction(async transaction => {
// await for all sql statments promises to resolve
// await for all sql statements promises to resolve
await Promise.all(
[...saveParameterizedStatements].map(
([statement, params]) =>
Expand All @@ -164,9 +162,7 @@ class ExpoSQLiteDatabase implements CommonSQLiteDatabase {
)
)
);
});
if (deleteParameterizedStatements) {
this.db.transaction(async transaction => {
if (deleteParameterizedStatements) {
await Promise.all(
[...deleteParameterizedStatements].map(
([statement, params]) =>
Expand All @@ -186,16 +182,16 @@ class ExpoSQLiteDatabase implements CommonSQLiteDatabase {
)
)
);
});
}
resolveTransaction(null);
}
resolveTransaction(null);
});
} catch (error) {
rejectTransaction(error);
}
});
}

public async selectAndDelete<T = any>(
public selectAndDelete<T = any>(
queryParameterizedStatement: ParameterizedStatement,
deleteParameterizedStatement: ParameterizedStatement
): Promise<T[]> {
Expand Down Expand Up @@ -241,7 +237,7 @@ class ExpoSQLiteDatabase implements CommonSQLiteDatabase {
});
}

private async executeStatements(statements: string[]): Promise<void> {
private executeStatements(statements: string[]): Promise<void> {
return new Promise((resolveTransaction, rejectTransaction) => {
this.db.transaction(async transaction => {
try {
Expand Down
2 changes: 1 addition & 1 deletion packages/datastore-storage-adapter/webpack.config.dev.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ var config = require('./webpack.config.js');

var entry = {
'aws-amplify-datastore-storage-adapter': './lib-esm/index.js',
'aws-amplify-datastore-storage-adapter-expo':
'aws-amplify-datastore-sqlite-adapter-expo':
'./lib-esm/ExpoSQLiteAdapter/ExpoSQLiteAdapter.js',
};
module.exports = Object.assign(config, { entry, mode: 'development' });
6 changes: 3 additions & 3 deletions packages/datastore-storage-adapter/webpack.config.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
module.exports = {
entry: {
'aws-amplify-datastore-storage-adapter.min': './lib-esm/index.js',
'aws-amplify-datastore-storage-adapter-expo.min':
'aws-amplify-datastore-sqlite-adapter-expo.min':
'./lib-esm/ExpoSQLiteAdapter/ExpoSQLiteAdapter.js',
},
externals: [
'@aws-amplify/datastore',
'@aws-amplify/core',
'react-native-sqlite-storage',
'expo-sqlite',
'expo-file-system',
'expo-sqlite',
'react-native-sqlite-storage',
],
output: {
filename: '[name].js',
Expand Down

0 comments on commit 55c76e6

Please sign in to comment.