-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: SQLite driver should emit "connection" event when new connectio…
…n is created
- Loading branch information
Showing
4 changed files
with
151 additions
and
125 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
'use strict'; | ||
|
||
// SELECT users.id AS "users:id", ... | ||
// => [ { users: { id, ... } } ] | ||
function nest(rows, fields, spell) { | ||
const { Model } = spell; | ||
const { tableAlias } = Model; | ||
const results = []; | ||
|
||
for (const row of rows) { | ||
const result = {}; | ||
const qualified = Object.keys(row).some(entry => entry.includes(':')); | ||
for (const key in row) { | ||
const parts = key.split(':'); | ||
const [qualifier, column] = qualified | ||
? (parts.length > 1 ? parts : ['', key]) | ||
: [Model.attributeMap.hasOwnProperty(key) ? tableAlias : '', key]; | ||
const obj = result[qualifier] || (result[qualifier] = {}); | ||
obj[column] = row[key]; | ||
} | ||
results.push(result); | ||
} | ||
|
||
return { rows: results, fields }; | ||
} | ||
|
||
class Connection { | ||
constructor({ client, database, mode, pool }) { | ||
const { Database, OPEN_READWRITE, OPEN_CREATE } = client; | ||
if (mode == null) mode = OPEN_READWRITE | OPEN_CREATE; | ||
this.database = new Database(database, mode); | ||
this.pool = pool; | ||
} | ||
|
||
async query(query, values, spell) { | ||
const { sql, nestTables } = query.sql ? query : { sql: query }; | ||
|
||
if (/^(?:pragma|select)/i.test(sql)) { | ||
const result = await this.all(sql, values); | ||
if (nestTables) return nest(result.rows, result.fields, spell); | ||
return result; | ||
} | ||
return await this.run(sql, values); | ||
} | ||
|
||
all(sql, values) { | ||
return new Promise((resolve, reject) => { | ||
this.database.all(sql, values, (err, rows, fields) => { | ||
if (err) reject(err); | ||
else resolve({ rows, fields }); | ||
}); | ||
}); | ||
} | ||
|
||
run(sql, values) { | ||
return new Promise((resolve, reject) => { | ||
this.database.run(sql, values, function Leoric_sqliteRun(err) { | ||
if (err) reject(err); | ||
else resolve({ insertId: this.lastID, affectedRows: this.changes }); | ||
}); | ||
}); | ||
} | ||
|
||
release() { | ||
this.pool.releaseConnection(this); | ||
} | ||
|
||
async end() { | ||
const { connections } = this.pool; | ||
const index = connections.indexOf(this); | ||
if (index >= 0) connections.splice(index, 1); | ||
|
||
return await new Promise((resolve, reject) => { | ||
this.database.close(function(err) { | ||
if (err) reject(err); | ||
resolve(); | ||
}); | ||
}); | ||
} | ||
} | ||
|
||
module.exports = Connection; |
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 |
---|---|---|
@@ -0,0 +1,51 @@ | ||
'use strict'; | ||
|
||
const EventEmitter = require('events'); | ||
const Connection = require('./connection'); | ||
|
||
class Pool extends EventEmitter { | ||
constructor(opts) { | ||
super(opts); | ||
this.options = { | ||
connectionLimit: 10, | ||
...opts, | ||
client: opts.client || 'sqlite3', | ||
}; | ||
this.client = require(this.options.client); | ||
this.connections = []; | ||
this.queue = []; | ||
} | ||
|
||
async getConnection() { | ||
const { connections, queue, client, options } = this; | ||
for (const connection of connections) { | ||
if (connection.idle) { | ||
connection.idle = false; | ||
this.emit('acquire', connection); | ||
return connection; | ||
} | ||
} | ||
if (connections.length < options.connectionLimit) { | ||
const connection = new Connection({ ...options, client, pool: this }); | ||
connections.push(connection); | ||
this.emit('connection', connection); | ||
this.emit('acquire', connection); | ||
return connection; | ||
} | ||
await new Promise(resolve => queue.push(resolve)); | ||
return await this.getConnection(); | ||
} | ||
|
||
releaseConnection(connection) { | ||
connection.idle = true; | ||
this.emit('release', connection); | ||
|
||
const { queue } = this; | ||
while (queue.length > 0) { | ||
const task = queue.shift(); | ||
task(); | ||
} | ||
} | ||
} | ||
|
||
module.exports = Pool; |
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,17 @@ | ||
'use strict'; | ||
|
||
const assert = require('assert').strict; | ||
const Pool = require('../../../../src/drivers/sqlite/pool'); | ||
|
||
describe('=> SQLite driver.pool', function() { | ||
it('should emit connection', async function() { | ||
const pool = new Pool({ | ||
database: '/tmp/leoric.sqlite3', | ||
}); | ||
let result; | ||
pool.on('connection', function(connection) { | ||
result = connection; | ||
}); | ||
assert.equal(await pool.getConnection(), result); | ||
}); | ||
}); |