Skip to content

Commit

Permalink
sqlite3 and pg now is optional dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
fanatid committed Oct 27, 2015
1 parent c90c55c commit 3f2784e
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 29 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ language: node_js
node_js:
- "0.10"
- "0.12"
- "4"
addons:
firefox: "38.0"
postgresql: "9.3"
Expand Down
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "odd-storage",
"version": "0.4.5",
"version": "0.4.6",
"description": "node.js and browser storage",
"keywords": [
"storage",
Expand Down Expand Up @@ -47,9 +47,7 @@
"core-decorators": "^0.8.1",
"error-system": "^1.0.0",
"make-concurrent": "^1.1.1",
"pg": "^4.4.3",
"ready-mixin": "^2.0.0",
"sqlite3": "^3.0.8"
"ready-mixin": "^2.0.0"
},
"devDependencies": {
"babel": "^5.8.21",
Expand All @@ -68,7 +66,9 @@
"karma-detect-browsers": "^2.0.2",
"karma-firefox-launcher": "^0.1.6",
"karma-mocha": "^0.2.0",
"mocha": "^2.2.5"
"mocha": "^2.2.5",
"pg": "^4.4.3",
"sqlite3": "^3.0.8"
},
"browser": {
"./lib/index.js": "./lib/index-browser.js",
Expand Down
20 changes: 15 additions & 5 deletions src/sql/postgresql.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import pg from 'pg'

import AbstractSQLStorage from './abstract'

/**
Expand All @@ -16,13 +14,20 @@ export default class PostgreSQLStorage extends AbstractSQLStorage {
super()

this._url = Object(opts).url
this._pg = Object(opts).native === true ? pg.native : pg
this._useNative = !!Object(opts).native
}

/**
* @return {boolean}
*/
static isAvailable () { return true }
static isAvailable () {
try {
require('pg')
return true
} catch (err) {
return false
}
}

/**
* @param {string} sql
Expand Down Expand Up @@ -65,7 +70,12 @@ export default class PostgreSQLStorage extends AbstractSQLStorage {
* @return {Promise}
*/
open () {
return this._query('SELECT * FROM information_schema.tables')
return Promise.resolve()
.then(() => {
let pg = require('pg')
this._pg = this._useNative ? pg.native : pg
return this._query('SELECT * FROM information_schema.tables')
})
.then(() => { this._ready(null) }, (err) => {
this._ready(err)
throw err
Expand Down
12 changes: 9 additions & 3 deletions src/sql/sqlite.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import sqlite3 from 'sqlite3'

import AbstractSQLStorage from './abstract'

/**
Expand All @@ -19,13 +17,21 @@ export default class SQLiteStorage extends AbstractSQLStorage {
/**
* @return {boolean}
*/
static isAvailable () { return true }
static isAvailable () {
try {
require('sqlite3')
return true
} catch (err) {
return false
}
}

/**
* @return {Promise}
*/
open () {
return new Promise((resolve, reject) => {
let sqlite3 = require('sqlite3')
this._db = new sqlite3.Database(this._filename, (err) => {
if (err !== null) {
return reject(err)
Expand Down
3 changes: 1 addition & 2 deletions src/sql/websql.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import AbstractSQLStorage from './abstract'
import { isFunction } from '../util'

/**
* @class WebSQLStorage
Expand All @@ -24,7 +23,7 @@ export default class WebSQLStorage extends AbstractSQLStorage {
/**
* @return {boolean}
*/
static isAvailable () { return isFunction(global.openDatabase) }
static isAvailable () { return global.openDatabase === 'function' }

/**
* @return {Promise}
Expand Down
3 changes: 1 addition & 2 deletions src/sync/indexeddb.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import AbstractSyncStorage from './abstract'
import { isFunction } from '../util'

// https://developer.mozilla.org/en-US/docs/Web/API/IDBFactory/open
var indexedDB = (global.indexedDB ||
Expand All @@ -25,7 +24,7 @@ export default class IndexedDBStorag extends AbstractSyncStorage {
* @return {boolean}
*/
static isAvailable () {
return (typeof indexedDB === 'object' && isFunction(indexedDB.open))
return (typeof indexedDB === 'object' && typeof indexedDB.open === 'function')
}

/**
Expand Down
7 changes: 3 additions & 4 deletions src/sync/localstorage.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import AbstractSyncStorage from './abstract'
import { isFunction } from '../util'

/**
* @class LocalStorage
Expand All @@ -20,9 +19,9 @@ export default class LocalStorage extends AbstractSyncStorage {
*/
static isAvailable () {
return (typeof global.localStorage === 'object' &&
isFunction(global.localStorage.getItem) &&
isFunction(global.localStorage.setItem) &&
isFunction(global.localStorage.clear))
typeof global.localStorage.getItem === 'function' &&
typeof global.localStorage.setItem === 'function' &&
typeof global.localStorage.clear === 'function')
}

/**
Expand Down
8 changes: 0 additions & 8 deletions src/util.js

This file was deleted.

0 comments on commit 3f2784e

Please sign in to comment.