Convert db module to typescript

Also had to change config module to bind `.this` value, due to
differences in setup.

Change-type: patch
Signed-off-by: Cameron Diver <cameron@resin.io>
This commit is contained in:
Cameron Diver 2018-06-11 23:18:50 +01:00
parent 4efc1de789
commit 1b0fd82f51
No known key found for this signature in database
GPG Key ID: 69264F9C923F55C1
4 changed files with 68 additions and 34 deletions

View File

@ -24,6 +24,7 @@
},
"devDependencies": {
"@types/bluebird": "^3.5.20",
"@types/knex": "^0.14.14",
"@types/lodash": "^4.14.109",
"@types/mz": "0.0.32",
"@types/node": "^10.3.1",

View File

@ -267,7 +267,7 @@ module.exports = class Config extends EventEmitter
@set({ uuid, deviceApiKey })
get: (key, trx) =>
db = trx ? @db.models
db = trx ? @db.models.bind(@db)
# Get value for "key" from config.json or db
Promise.try =>
switch @schema[key]?.source

View File

@ -1,33 +0,0 @@
Knex = require 'knex'
path = require 'path'
constants = require './lib/constants'
module.exports = class DB
constructor: ({ @databasePath } = {}) ->
@databasePath ?= constants.databasePath
@knex = Knex(
client: 'sqlite3'
connection:
filename: @databasePath
useNullAsDefault: true
)
init: =>
@knex('knex_migrations_lock').update({ is_locked: 0 })
.catch(->) # Knex doesn't return a bluebird promise here so we can't catchReturn :(
.then =>
@knex.migrate.latest(directory: path.join(__dirname, 'migrations'))
# Returns a knex object for one of the models (tables)
models: (modelName) =>
@knex(modelName)
upsertModel: (modelName, obj, id, trx) =>
knex = trx ? @knex
knex(modelName).update(obj).where(id)
.then (n) ->
if n == 0
knex(modelName).insert(obj)
transaction: (cb) =>
@knex.transaction(cb)

66
src/db.ts Normal file
View File

@ -0,0 +1,66 @@
import * as Bluebird from 'bluebird';
import * as Knex from 'knex';
import * as path from 'path';
import * as constants from './lib/constants';
interface DBOpts {
databasePath?: string;
}
type DBTransactionCallback = (trx: Knex.Transaction) => void;
class DB {
private databasePath: string;
private knex: Knex;
public constructor({ databasePath }: DBOpts = {}) {
this.databasePath = databasePath || constants.databasePath;
this.knex = Knex({
client: 'sqlite3',
connection: {
filename: this.databasePath,
},
useNullAsDefault: true,
});
}
public init(): Bluebird<void> {
return this.knex('knex_migrations_lock').update({ is_locked: 0})
.catch(() => { return; })
.then(() => {
return this.knex.migrate.latest({
directory: path.join(__dirname, 'migrations'),
});
});
}
public models(modelName: string): Knex.QueryBuilder {
return this.knex(modelName);
}
public upsertModel(
modelName: string,
obj: any,
id: number | { [key: string]: string},
trx?: Knex.Transaction,
): Bluebird<any> {
const knex = trx || this.knex;
return knex(modelName).update(obj).where(id)
.then((n: number) => {
if (n === 0) {
return knex(modelName).insert(obj);
}
});
}
public transaction(cb: DBTransactionCallback): Bluebird<Knex.Transaction> {
return this.knex.transaction(cb);
}
}
export = DB;