-
-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(sql.js): add connection pool for managing sq.js backend
Use `generic-pool` to handle the `sql.js` connection in the same way `knex` internally handles its driver connections.
- Loading branch information
citycide
committed
Jan 8, 2017
1 parent
b1f4cd9
commit 2df8381
Showing
18 changed files
with
275 additions
and
289 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,40 @@ | ||
import jetpack from 'fs-jetpack' | ||
import pool from 'generic-pool' | ||
import SQL from 'sql.js' | ||
|
||
import constants from './constants' | ||
|
||
export function readDatabase (instance) { | ||
let client | ||
|
||
let atPath = instance.options.connection.filename | ||
if (jetpack.exists(atPath) === 'file') { | ||
let file = jetpack.read(atPath, 'buffer') | ||
instance.db = new SQL.Database(file) | ||
client = new SQL.Database(file) | ||
} else { | ||
instance.db = new SQL.Database() | ||
writeDatabase(instance) | ||
client = new SQL.Database() | ||
writeDatabase(instance, client) | ||
} | ||
|
||
return client | ||
} | ||
|
||
export function writeDatabase (instance) { | ||
if (!instance.db) { | ||
throw new Error(constants.ERR_NO_DATABASE) | ||
} | ||
export function writeDatabase (instance, db) { | ||
let data = db.export() | ||
let buffer = new Buffer(data) | ||
|
||
try { | ||
let data = instance.db.export() | ||
let buffer = new Buffer(data) | ||
jetpack.file(instance.options.connection.filename, { | ||
content: buffer, mode: '777' | ||
}) | ||
} | ||
|
||
let atPath = instance.options.connection.filename | ||
export function connect (instance) { | ||
return pool.createPool({ | ||
create () { | ||
return Promise.resolve(readDatabase(instance)) | ||
}, | ||
|
||
jetpack.file(atPath, { | ||
content: buffer, mode: '777' | ||
}) | ||
} catch (e) { | ||
throw new Error(e.message) | ||
} | ||
destroy (client) { | ||
client.close() | ||
return Promise.resolve() | ||
} | ||
}, { min: 1, max: 1 }) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import Trilogy from '../dist/trilogy' | ||
|
||
import test from 'ava' | ||
import { remove } from 'fs-jetpack' | ||
import { join, basename } from 'path' | ||
|
||
const filePath = join(__dirname, `${basename(__filename, '.js')}.db`) | ||
const db = new Trilogy(filePath) | ||
|
||
const schema = { | ||
first: String, | ||
second: Number | ||
} | ||
|
||
const tables = [ | ||
{ name: 'one', schema }, | ||
{ name: 'two', schema }, | ||
{ name: 'three', schema } | ||
] | ||
|
||
test.before(() => { | ||
return Promise.all(tables.map(table => { | ||
return db.model(table.name, table.schema) | ||
})) | ||
}) | ||
|
||
test.after.always('remove test database file', () => { | ||
return db.close().then(() => remove(filePath)) | ||
}) | ||
|
||
test('inserts objects into the database', async t => { | ||
let inserts = [ | ||
{ table: 'one', object: { first: 'hello', second: 1 } }, | ||
{ table: 'two', object: { first: 'hello', second: 2 } }, | ||
{ table: 'three', object: { first: 'hello', second: 3 } } | ||
] | ||
|
||
await Promise.all( | ||
inserts.map(({ table, object }) => db.create(table, object)) | ||
) | ||
|
||
inserts.forEach(async ({ table, object }) => { | ||
t.deepEqual(await db.find(table, object), [object]) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import Trilogy from '../dist/trilogy' | ||
|
||
import test from 'ava' | ||
import { remove } from 'fs-jetpack' | ||
import { join, basename } from 'path' | ||
|
||
const filePath = join(__dirname, `${basename(__filename, '.js')}.db`) | ||
const db = new Trilogy(filePath) | ||
|
||
const schema = { name: String } | ||
|
||
const tables = [ | ||
{ name: 'one', schema }, | ||
{ name: 'two', schema }, | ||
{ name: 'three', schema } | ||
] | ||
|
||
test.before(() => { | ||
return Promise.all(tables.map(table => { | ||
db.model(table.name, table.schema) | ||
})) | ||
}) | ||
|
||
test.after.always('remove test database file', () => { | ||
return db.close().then(() => remove(filePath)) | ||
}) | ||
|
||
test('removes tables from the database', async t => { | ||
let removals = await Promise.all( | ||
tables.map(({ name }) => { | ||
return db.dropModel(name).then(() => db.hasModel(name)) | ||
}) | ||
) | ||
|
||
removals.forEach(v => t.false(v)) | ||
}) |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import Trilogy from '../dist/trilogy' | ||
|
||
import test from 'ava' | ||
import { remove } from 'fs-jetpack' | ||
import { join, basename } from 'path' | ||
|
||
const filePath = join(__dirname, `${basename(__filename, '.js')}.db`) | ||
const db = new Trilogy(filePath) | ||
|
||
test.before(async () => { | ||
await db.model('first', { | ||
first: String, | ||
second: String | ||
}) | ||
|
||
await db.create('first', { | ||
first: 'fee', | ||
second: 'blah' | ||
}) | ||
}) | ||
|
||
test.after.always('remove test database file', () => { | ||
return db.close().then(() => remove(filePath)) | ||
}) | ||
|
||
test('retrieves a single object', async t => { | ||
let expected = { first: 'fee', second: 'blah' } | ||
let res = await db.findOne('first') | ||
t.deepEqual(res, expected) | ||
}) | ||
|
||
test('allows retrieving a specific property', async t => { | ||
let res = await db.findOne('first.second') | ||
t.deepEqual(res, 'blah') | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.