Skip to content

Commit

Permalink
feat: add Open DB Flags and URI DB Name in SQLite (#9468)
Browse files Browse the repository at this point in the history
* Add the open database flags for sqlite3.

* Check for URI before trying to create the DB dir.

* Spacing.

* Add the sqlite open connection flags.

* Spacing.

Co-authored-by: Adrian Burlacu <adrian.burlacu@live.com>
  • Loading branch information
1 parent 71efa8e commit 73148c9
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 8 deletions.
7 changes: 7 additions & 0 deletions src/driver/sqlite/SqliteConnectionOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,12 @@ export interface SqliteConnectionOptions extends BaseDataSourceOptions {
*/
readonly enableWAL?: boolean

/**
* Specifies the open file flags. By default its undefined.
* @see https://www.sqlite.org/c3ref/c_open_autoproxy.html
* @see https://github.com/TryGhost/node-sqlite3/blob/master/test/open_close.test.js
*/
readonly flags?: number

readonly poolSize?: never
}
32 changes: 24 additions & 8 deletions src/driver/sqlite/SqliteDriver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,16 +131,32 @@ export class SqliteDriver extends AbstractSqliteDriver {
* Creates connection with the database.
*/
protected async createDatabaseConnection() {
await this.createDatabaseDirectory(this.options.database)
if (
this.options.flags === undefined ||
!(this.options.flags & this.sqlite.OPEN_URI)
) {
await this.createDatabaseDirectory(this.options.database)
}

const databaseConnection: any = await new Promise((ok, fail) => {
const connection = new this.sqlite.Database(
this.options.database,
(err: any) => {
if (err) return fail(err)
ok(connection)
},
)
if (this.options.flags === undefined) {
const connection = new this.sqlite.Database(
this.options.database,
(err: any) => {
if (err) return fail(err)
ok(connection)
},
)
} else {
const connection = new this.sqlite.Database(
this.options.database,
this.options.flags,
(err: any) => {
if (err) return fail(err)
ok(connection)
},
)
}
})

// Internal function to run a command on the connection and fail if an error occured.
Expand Down
40 changes: 40 additions & 0 deletions test/functional/sqlite/file-open-flags.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import "reflect-metadata"
import { expect } from "chai"
import { DataSource } from "../../../src/data-source/DataSource"
import {
closeTestingConnections,
createTestingConnections,
reloadTestingDatabases,
} from "../../utils/test-utils"
const sqlite3 = require("sqlite3")

describe("sqlite driver > file open flags", () => {
let connections: DataSource[]
before(
async () =>
(connections = await createTestingConnections({
name: "file:./temp/sqlitedb-memory.db?mode=memory",
entities: [],
enabledDrivers: ["sqlite"],
driverSpecific: {
flags:
sqlite3.OPEN_URI |
sqlite3.OPEN_SHAREDCACHE |
sqlite3.OPEN_READWRITE |
sqlite3.OPEN_CREATE,
},
})),
)
beforeEach(() => reloadTestingDatabases(connections))
after(() => closeTestingConnections(connections))

it("should open a DB with flags as expected", () =>
Promise.all(
connections.map(async (connection) => {
// if we come this far, test was successful as a connection was established
const result = await connection.query("PRAGMA journal_mode")

expect(result).to.eql([{ journal_mode: "wal" }])
}),
))
})

0 comments on commit 73148c9

Please sign in to comment.