Skip to content

Commit

Permalink
feat: add support for in-memory database
Browse files Browse the repository at this point in the history
  • Loading branch information
Bo Lingen committed Jul 25, 2017
1 parent 071aa5f commit 8587f4b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
16 changes: 12 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,26 @@ class Trilogy {
}

let obj = this.options = setup(options)
obj.connection.filename = resolve(obj.dir, path)

// ensure the directory exists
makeDirPath(dirname(obj.connection.filename))
if (path === ':memory:') {
obj.connection.filename = path
} else {
obj.connection.filename = resolve(obj.dir, path)

// ensure the directory exists
makeDirPath(dirname(obj.connection.filename))
}

this.isNative = obj.client === 'sqlite3'
this.verbose = obj.verbose

let config = { client: 'sqlite3', useNullAsDefault: true }

if (this.isNative) {
touchFile(obj.connection.filename)
if (path !== ':memory:') {
touchFile(obj.connection.filename)
}

this.knex = knex({ ...config, connection: obj.connection })
} else {
this.knex = knex(config)
Expand Down
8 changes: 7 additions & 1 deletion src/sqljs-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ export function readDatabase (instance) {
let client
let { filename } = instance.options.connection

if (filename === ':memory:') {
return new SQL.Database()
}

try {
makeDirPath(dirname(filename))
let file = readFileSync(filename)
Expand All @@ -28,9 +32,11 @@ export function readDatabase (instance) {
}

export function writeDatabase (instance, db) {
let { filename } = instance.options.connection
if (filename === ':memory:') return

let data = db.export()
let buffer = Buffer.from(data)
let { filename } = instance.options.connection

makeDirPath(dirname(filename))
writeFileSync(filename, buffer, { mode: parseInt('0777', 8) })
Expand Down

0 comments on commit 8587f4b

Please sign in to comment.