Skip to content

Commit

Permalink
Merge pull request #30 from featurist/websql
Browse files Browse the repository at this point in the history
Websql
  • Loading branch information
refractalize authored Mar 13, 2017
2 parents 59ed807 + 5202d2d commit cc4361a
Show file tree
Hide file tree
Showing 7 changed files with 349 additions and 119 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ services:
- mysql
before_script:
- mysql -e 'create database sworm;'
- export DISPLAY=:99.0; sh -e /etc/init.d/xvfb start
8 changes: 6 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ var pgDriver = require("./pgDriver");
var mysqlDriver = require("./mysqlDriver");
var oracleDriver = require("./oracleDriver");
var sqliteDriver = require("./sqliteDriver");
var websqlDriver = require("./websqlDriver");
var debug = require("debug")("sworm");
var debugResults = require("debug")("sworm:results");
var redactConfig = require('./redactConfig');
Expand Down Expand Up @@ -459,7 +460,8 @@ exports.db = function(config) {
pg: pgDriver,
mysql: mysqlDriver,
oracle: oracleDriver,
sqlite: sqliteDriver
sqlite: sqliteDriver,
websql: websqlDriver
}[this.config.driver];

if (!driver) {
Expand Down Expand Up @@ -568,8 +570,10 @@ exports.escape = function(value) {
}

function configFromUrl(url) {
var isBrowser = typeof window !== 'undefined'

var parsedUrl = urlUtils.parse(url)
var protocol = parsedUrl.protocol? parsedUrl.protocol.replace(/:$/, ''): 'sqlite'
var protocol = parsedUrl.protocol? parsedUrl.protocol.replace(/:$/, ''): (isBrowser? 'websql': 'sqlite')
var driver = {
postgres: 'pg',
file: 'sqlite'
Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
"devDependencies": {
"chai": "3.5.0",
"chai-as-promised": "5.3.0",
"electron": "1.6.2",
"electron-mocha": "3.3.0",
"es6-promise": "3.2.1",
"fs-promise": "0.5.0",
"mocha": "3.2.0",
Expand All @@ -23,7 +25,7 @@
"sqlite3": "3.1.8"
},
"scripts": {
"test": "mocha"
"test": "mocha && electron-mocha --renderer test/browser"
},
"repository": {
"type": "git",
Expand Down
25 changes: 24 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ A very lightweight **write only** Node.js ORM, with support for:
* MySQL
* Oracle DB
* Sqlite 3
* Browser Web SQL

## NPM

Expand All @@ -20,6 +21,8 @@ Then install a database driver, one of:
npm install oracledb
npm install sqlite3

There's no need to install a driver for Web SQL, sworm will pick it up from the `window` object.

See [sworm](https://www.npmjs.org/package/sworm) in NPM.

## Write Only?
Expand Down Expand Up @@ -158,7 +161,7 @@ sworm.db(url)

* `url`, see urls for databases in respective section below

* `options.driver`, one of `'mssql'`, `'mysql'`, `'pg'`, `'oracle'` or `'sqlite'`.
* `options.driver`, one of `'mssql'`, `'mysql'`, `'pg'`, `'oracle'`, `'sqlite'` or `'websql'`.
* `options.config` see configuration for databases in respective section below

* `url` a connection URL, the following are supported
Expand Down Expand Up @@ -301,6 +304,25 @@ sworm.db(url)
}
```

* **websql**

URL: `websql:///db-name` or `db-name`

```js
{
driver: 'websql',
config: {
name: 'db-name',
// the `openDatabase` function to connect to the DB, defaulting to `window.openDatabase`
openDatabase: window.openDatabase,
// dababase size, defaulting to 5M
size: 5 * 1024 * 1024
}
}
```

### Close

Close the connection after use:
Expand All @@ -326,6 +348,7 @@ There are various schemes you can use:
* `sworm:pg` exact query passed to postgres
* `sworm:oracle` exact query passed to oracle
* `sworm:sqlite` exact query passed to sqlite3
* `sworm:websql` exact query passed to websql

## Models

Expand Down
85 changes: 85 additions & 0 deletions test/browser/websqlSpec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
var describeDatabase = require('../describeDatabase');
var sworm = require('../..')
var expect = require('chai').expect

var database = {
createTables: function(db, tables) {
function createTable(name, sql) {
tables.push(name);
return db.query(sql);
}

return createTable("people",
'create table if not exists people (id integer primary key, name varchar(50) NOT NULL, address_id integer NULL, photo blob null)'
).then(function() {
return createTable("people_addresses",
'create table if not exists people_addresses(address_id integer NOT NULL, person_id integer NOT NULL, rating integer NULL)'
);
}).then(function() {
return createTable("addresses",
'create table if not exists addresses(id integer primary key, address varchar(50) NOT NULL)'
);
}).then(function() {
return createTable("people_weird_id",
'create table if not exists people_weird_id(weird_id integer primary key, name varchar(50) NULL, address_weird_id integer NULL)'
);
}).then(function() {
return createTable("people_explicit_id",
'create table if not exists people_explicit_id(id integer NOT NULL, name varchar(50) NOT NULL)'
);
});
},

"true": 1,
"false": 0,

clean: function(records) {
return records;
},

driverModuleName: "websql",

transactions: false,

noModule: true
};

var config = {
driver: "websql",
config: {
name: 'test'
}
};

describeDatabase("websql", config, database, function () {
describe('connection', function () {
it('can connect with string for DB name', function () {
var db = sworm.db('test')
return db.query('select * from people')
})

it('can connect with URL', function () {
var db = sworm.db('websql:///test')
return db.query('select * from people')
})

it('can connect with openDatabase function', function () {
var called = false

var db = sworm.db({
driver: 'websql',
config: {
openDatabase: function() {
called = true
return window.openDatabase.apply(window, arguments)
},
name: 'test'
}
})

return db.query('select * from people').then(function () {
expect(called).to.be.true
})
})
})
})
Loading

0 comments on commit cc4361a

Please sign in to comment.