Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Capacitor driver additional connection options #7868

Merged
merged 9 commits into from
Jul 10, 2021
6 changes: 6 additions & 0 deletions docs/connection-options.md
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,12 @@ See [SSL options](https://github.com/mysqljs/mysql#ssl-options).

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

* `encrypted` - Set to true for database encryption

* `mode` - Set the mode for database encryption: "no-encryption" | "encryption" | "secret" | "newsecret"

* `version` - Database version

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

## `cordova` connection options
Expand Down
19 changes: 17 additions & 2 deletions src/driver/capacitor/CapacitorConnectionOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,30 @@ export interface CapacitorConnectionOptions extends BaseConnectionOptions {
*/
readonly type: "capacitor";

/**
* The capacitor-sqlite instance. For example, `new SQLiteConnection(CapacitorSQLite)`.
*/
readonly driver: any;

/**
* Database name (capacitor-sqlite will add the suffix `SQLite.db`)
*/
readonly database: string;

/**
* The capacitor-sqlite instance. For example, `new SQLiteConnection(CapacitorSQLite)`.
* Set to true for database encryption
*/
readonly driver: any;
readonly encrypted?: boolean;
imnotjames marked this conversation as resolved.
Show resolved Hide resolved

/**
* Set the mode for database encryption
*/
readonly mode?: "no-encryption" | "encryption" | "secret" | "newsecret";

/**
* Database version
*/
readonly version?: number;

/**
* The SQLite journal mode (optional)
Expand Down
29 changes: 20 additions & 9 deletions src/driver/capacitor/CapacitorDriver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,22 +74,33 @@ export class CapacitorDriver extends AbstractSqliteDriver {
* Creates connection with the database.
*/
protected async createDatabaseConnection() {
const isDatabaseEncryted = this.options.encrypted || false;
const databaseMode = this.options.mode || "no-encryption";
const databaseVersion =
typeof this.options.version === "undefined"
? 1
: this.options.version;
const connection = await this.sqlite.createConnection(
this.options.database,
false,
"no-encryption",
1
isDatabaseEncryted,
databaseMode,
databaseVersion
);
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.query(`PRAGMA foreign_keys = ON`, []);

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

if (
this.options.journalMode &&
["DELETE", "TRUNCATE", "PERSIST", "MEMORY", "WAL", "OFF"].indexOf(
imnotjames marked this conversation as resolved.
Show resolved Hide resolved
this.options.journalMode
) !== -1
) {
await connection.query(
`PRAGMA journal_mode = ${this.options.journalMode}`
imnotjames marked this conversation as resolved.
Show resolved Hide resolved
);
}

return connection;
Expand Down