feat: add capacitor driver options for encryption & version (#7868)

This commit is contained in:
Chris 2021-07-10 23:34:56 +02:00 committed by GitHub
parent 4b45ae1e81
commit a2bd94b146
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 26 additions and 6 deletions

View File

@ -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

View File

@ -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)

View File

@ -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 &&