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
4 changes: 4 additions & 0 deletions docs/connection-options.md
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,10 @@ See [SSL options](https://github.com/mysqljs/mysql#ssl-options).

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

* `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
14 changes: 12 additions & 2 deletions src/driver/capacitor/CapacitorConnectionOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,25 @@ 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 the mode for database encryption
*/
readonly driver: any;
readonly mode?: "no-encryption" | "encryption" | "secret" | "newsecret";

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

/**
* The SQLite journal mode (optional)
Expand Down
14 changes: 10 additions & 4 deletions src/driver/capacitor/CapacitorDriver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,17 +74,23 @@ export class CapacitorDriver extends AbstractSqliteDriver {
* Creates connection with the database.
*/
protected async createDatabaseConnection() {
const databaseMode = this.options.mode || "no-encryption";
const isDatabaseEncryted = databaseMode !== "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`, []);
await connection.query(`PRAGMA foreign_keys = ON`);

if (this.options.journalMode) {
await connection.query(`PRAGMA journal_mode = ?`, [
Expand Down