Skip to content

Commit

Permalink
fix: Capacitor driver PRAGMA requests failing on Android (#7728)
Browse files Browse the repository at this point in the history
* - fix: PRAGMA needs to be run through the `query` method, since it returns data
- added WAL journal mode

* added journalMode as an optional setting

* reverted auto-format
  • Loading branch information
chriswep committed Jun 24, 2021
1 parent f4f1453 commit 9620a26
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 11 deletions.
2 changes: 2 additions & 0 deletions docs/connection-options.md
Expand Up @@ -203,6 +203,8 @@ See [SSL options](https://github.com/mysqljs/mysql#ssl-options).

* `driver` - The capacitor-sqlite instance. For example, `new SQLiteConnection(CapacitorSQLite)`.

* `journalMode` - The SQLite journal mode (optional)

## `cordova` connection options

* `database` - Database name
Expand Down
11 changes: 11 additions & 0 deletions src/driver/capacitor/CapacitorConnectionOptions.ts
Expand Up @@ -18,4 +18,15 @@ export interface CapacitorConnectionOptions extends BaseConnectionOptions {
* The capacitor-sqlite instance. For example, `new SQLiteConnection(CapacitorSQLite)`.
*/
readonly driver: any;

/**
* The SQLite journal mode (optional)
*/
readonly journalMode?:
| "DELETE"
| "TRUNCATE"
| "PERSIST"
| "MEMORY"
| "WAL"
| "OFF";
}
10 changes: 9 additions & 1 deletion src/driver/capacitor/CapacitorDriver.ts
Expand Up @@ -81,9 +81,17 @@ export class CapacitorDriver extends AbstractSqliteDriver {
1
);
await connection.open();

// we need to enable foreign keys in sqlite to make sure all foreign key related features
// working properly. this also makes onDelete to work with sqlite.
await connection.execute(`PRAGMA foreign_keys = ON;`);
await connection.query(`PRAGMA foreign_keys = ON`, []);

if (this.options.journalMode) {
await connection.query(`PRAGMA journal_mode = ?`, [
this.options.journalMode,
]);
}

return connection;
}

Expand Down
14 changes: 4 additions & 10 deletions src/driver/capacitor/CapacitorQueryRunner.ts
Expand Up @@ -47,15 +47,9 @@ export class CapacitorQueryRunner extends AbstractSqliteQueryRunner {
const command = query.substr(0, query.indexOf(" "));

if (
[
"PRAGMA",
"BEGIN",
"ROLLBACK",
"COMMIT",
"CREATE",
"ALTER",
"DROP",
].indexOf(command) !== -1
["BEGIN", "ROLLBACK", "COMMIT", "CREATE", "ALTER", "DROP"].indexOf(
command
) !== -1
) {
pResult = databaseConnection.execute(query, false);
} else if (["INSERT", "UPDATE", "DELETE"].indexOf(command) !== -1) {
Expand All @@ -70,7 +64,7 @@ export class CapacitorQueryRunner extends AbstractSqliteQueryRunner {
);
} else {
pResult = databaseConnection
.query(query, parameters)
.query(query, parameters || [])
.then(({ values }: { values: any[] }) => values);
}

Expand Down

0 comments on commit 9620a26

Please sign in to comment.