Skip to content

Commit ec3ea10

Browse files
authored
refactor: use pragma method in better-sqlite3 (#10684)
1 parent c4f5d12 commit ec3ea10

2 files changed

Lines changed: 12 additions & 11 deletions

File tree

‎src/driver/better-sqlite3/BetterSqlite3Driver.ts‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ export class BetterSqlite3Driver extends AbstractSqliteDriver {
138138
nativeBinding = null,
139139
prepareDatabase,
140140
} = this.options
141-
const databaseConnection = this.sqlite(database, {
141+
const databaseConnection = new this.sqlite(database, {
142142
readonly,
143143
fileMustExist,
144144
timeout,
@@ -148,8 +148,8 @@ export class BetterSqlite3Driver extends AbstractSqliteDriver {
148148
// in the options, if encryption key for SQLCipher is setted.
149149
// Must invoke key pragma before trying to do any other interaction with the database.
150150
if (this.options.key) {
151-
databaseConnection.exec(
152-
`PRAGMA key = ${JSON.stringify(this.options.key)}`,
151+
databaseConnection.pragma(
152+
`key = ${JSON.stringify(this.options.key)}`,
153153
)
154154
}
155155

@@ -160,11 +160,11 @@ export class BetterSqlite3Driver extends AbstractSqliteDriver {
160160

161161
// we need to enable foreign keys in sqlite to make sure all foreign key related features
162162
// working properly. this also makes onDelete to work with sqlite.
163-
databaseConnection.exec(`PRAGMA foreign_keys = ON`)
163+
databaseConnection.pragma("foreign_keys = ON")
164164

165165
// turn on WAL mode to enhance performance
166166
if (this.options.enableWAL) {
167-
databaseConnection.exec(`PRAGMA journal_mode = WAL`)
167+
databaseConnection.pragma("journal_mode = WAL")
168168
}
169169

170170
return databaseConnection

‎src/driver/better-sqlite3/BetterSqlite3QueryRunner.ts‎

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,14 +62,16 @@ export class BetterSqlite3QueryRunner extends AbstractSqliteQueryRunner {
6262
* Called before migrations are run.
6363
*/
6464
async beforeMigration(): Promise<void> {
65-
await this.query(`PRAGMA foreign_keys = OFF`)
65+
const databaseConnection = await this.connect()
66+
databaseConnection.pragma("foreign_keys = OFF")
6667
}
6768

6869
/**
6970
* Called after migrations are run.
7071
*/
7172
async afterMigration(): Promise<void> {
72-
await this.query(`PRAGMA foreign_keys = ON`)
73+
const databaseConnection = await this.connect()
74+
databaseConnection.pragma("foreign_keys = ON")
7375
}
7476

7577
/**
@@ -172,10 +174,9 @@ export class BetterSqlite3QueryRunner extends AbstractSqliteQueryRunner {
172174
}
173175
protected async loadPragmaRecords(tablePath: string, pragma: string) {
174176
const [database, tableName] = this.splitTablePath(tablePath)
175-
const res = await this.query(
176-
`PRAGMA ${
177-
database ? `"${database}".` : ""
178-
}${pragma}("${tableName}")`,
177+
const databaseConnection = await this.connect()
178+
const res = databaseConnection.pragma(
179+
`${database ? `"${database}".` : ""}${pragma}("${tableName}")`,
179180
)
180181
return res
181182
}

0 commit comments

Comments
 (0)