-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 215feab
Showing
13 changed files
with
413 additions
and
0 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,2 @@ | ||
/node_modules | ||
/bower_components |
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,3 @@ | ||
test/ | ||
images/ | ||
docs/ |
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,75 @@ | ||
# <img src="https://raw.githubusercontent.com/Rekord/rekord/master/images/rekord-color.png" width="60"> rekord-knex | ||
|
||
[](https://travis-ci.org/Rekord/rekord-knex | ||
[](https://david-dm.org/Rekord/rekord-knex#info=devDependencies) | ||
[](https://david-dm.org/Rekord/rekord-knex) | ||
[](https://github.com/Rekord/rekord/blob/master/LICENSE) | ||
[]() | ||
|
||
A rekord binding to [Knex.js](http://knexjs.org/). | ||
|
||
**Structure** | ||
|
||
The suggested structure for using this library is as follows: | ||
|
||
- src/connections.js: creates and exports knex objects | ||
- src/models.js: takes connection objects and defines & exports models | ||
|
||
**Example** | ||
|
||
```javascript | ||
// ============================================================================= | ||
// connections.js | ||
// ============================================================================= | ||
exports.main = require('knex')({ | ||
client: 'sqlite3', | ||
useNullAsDefault: true, | ||
connection: { | ||
filename: "./mydb.sqlite" | ||
} | ||
}); | ||
|
||
// ============================================================================= | ||
// models.js | ||
// ============================================================================= | ||
var connections = require('./connections.js'); | ||
var Rekord = require('rekord'); | ||
|
||
var Task = Rekord({ | ||
name: 'task', | ||
api: connections.main, // api must be a knex connection | ||
fields: ['name', 'done'], | ||
defaults: { | ||
done: false | ||
} | ||
}); | ||
|
||
// Load models from connections (where load option is used) | ||
Rekord.load(); | ||
|
||
// Export defined models | ||
exports.Task = Task; | ||
|
||
// ============================================================================= | ||
// example.js | ||
// ============================================================================= | ||
var Rekord = require('rekord'); | ||
var Task = require('./models.js').Task; | ||
|
||
var task = new Task({ | ||
name: 'Better Examples' | ||
}); | ||
|
||
Rekord.Promise | ||
.then(function() { | ||
return task.$save(); | ||
}) | ||
.then(function(saved) { | ||
// task saved! let the user know or return a function which returns a promise | ||
}) | ||
.catch(function(error) { | ||
// handle an error! | ||
process.exit(); | ||
}) | ||
; | ||
``` |
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,3 @@ | ||
|
||
require('./src/defaults.js'); | ||
require('./src/Rest.js'); |
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 @@ | ||
{ | ||
"name": "rekord-knex", | ||
"version": "1.5.0", | ||
"description": "A rekord binding to Knex.js - implementing Rekord.rest on the server side", | ||
"author": "Philip Diffenderfer", | ||
"license": "MIT", | ||
"main": "index.js", | ||
"repository": { | ||
"type": "git", | ||
"url": "git://github.com/Rekord/rekord-knex.git" | ||
}, | ||
"devDependencies": { | ||
"gulp": "^3.9.0", | ||
"gulp-check-filesize": "^2.0.1", | ||
"gulp-concat": "^2.6.0", | ||
"gulp-insert": "^0.5.0", | ||
"gulp-jshint": "^2.0.1", | ||
"gulp-load-plugins": "^1.1.0", | ||
"gulp-qunit": "^1.4.0", | ||
"gulp-rename": "^1.2.2", | ||
"gulp-shell": "^0.5.1", | ||
"gulp-sourcemaps": "^1.6.0", | ||
"gulp-uglify": "^1.5.3", | ||
"gulp-util": "^3.0.7", | ||
"gulp-watch": "^4.3.6", | ||
"jaguarjs-jsdoc": "1.0.0", | ||
"jsdoc": "^3.4.0", | ||
"jshint": "^2.9.2", | ||
"merge-stream": "^1.0.0", | ||
"sqlite3": "^3.1.8" | ||
}, | ||
"dependencies": { | ||
"knex": "^0.12.6", | ||
"rekord": "^1.5.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,195 @@ | ||
|
||
var Rekord = require('rekord'); | ||
var Class = Rekord.Class; | ||
|
||
function Rest(database) | ||
{ | ||
this.database = database; | ||
} | ||
|
||
Class.create( Rest, | ||
{ | ||
|
||
// success ( data[] ) | ||
// failure ( data[], status ) | ||
all: function( options, success, failure ) | ||
{ | ||
var db = this.database; | ||
var builder = db.api | ||
.select('*') | ||
.from( db.name ) | ||
; | ||
|
||
if (Rekord.isFunction(options)) | ||
{ | ||
options( builder ); | ||
} | ||
|
||
builder | ||
.then( function(rows) { | ||
success( rows ); | ||
}) | ||
.catch( function(error) { | ||
failure( [], error ); | ||
}) | ||
; | ||
}, | ||
|
||
// success( data ) | ||
// failure( data, status ) | ||
get: function( model, options, success, failure ) | ||
{ | ||
var db = this.database; | ||
var builder = db.api | ||
.select('*') | ||
.from( db.name ) | ||
.where( this.keyOf( model ) ) | ||
; | ||
|
||
if (Rekord.isFunction(options)) | ||
{ | ||
options( builder ); | ||
} | ||
|
||
builder | ||
.then( function(rows) { | ||
success( rows[0] ); | ||
}) | ||
.catch( function(error) { | ||
failure( null, error ); | ||
}) | ||
; | ||
}, | ||
|
||
// success ( data ) | ||
// failure ( data, status ) | ||
create: function( model, encoded, options, success, failure ) | ||
{ | ||
var db = this.database; | ||
var builder = db.api( db.name ) | ||
.returning( db.returning ) | ||
.insert( encoded ) | ||
; | ||
|
||
if (Rekord.isFunction(options)) | ||
{ | ||
options( builder ); | ||
} | ||
|
||
builder | ||
.then( function(rows) { | ||
success( rows[0] ); | ||
}) | ||
.catch( function(error) { | ||
failure( null, error ); | ||
}) | ||
; | ||
}, | ||
|
||
// success ( data ) | ||
// failure ( data, status ) | ||
update: function( model, encoded, options, success, failure ) | ||
{ | ||
var db = this.database; | ||
var builder = db.api( db.name ) | ||
.where( this.keyOf( model ) ) | ||
.update( encoded ) | ||
; | ||
|
||
if (Rekord.isFunction(options)) | ||
{ | ||
options( builder ); | ||
} | ||
|
||
builder | ||
.then( function() { | ||
success( {} ); | ||
}) | ||
.catch( function(error) { | ||
failure( null, error ); | ||
}) | ||
; | ||
}, | ||
|
||
// success ( data ) | ||
// failure ( data, status ) | ||
remove: function( model, options, success, failure ) | ||
{ | ||
var db = this.database; | ||
var builder = db.api( db.name ) | ||
.where( this.keyOf( model ) ) | ||
.delete() | ||
; | ||
|
||
if (Rekord.isFunction(options)) | ||
{ | ||
options( builder ); | ||
} | ||
|
||
builder | ||
.then( function() { | ||
success( {} ); | ||
}) | ||
.catch( function(error) { | ||
failure( null, error ); | ||
}) | ||
; | ||
}, | ||
|
||
// success ( data[] ) | ||
// failure ( data[], status ) | ||
query: function( url, query, options, success, failure ) | ||
{ | ||
var db = this.database; | ||
var select = options && options.select ? options.select : '*'; | ||
|
||
var builder = db.api | ||
.select( select ) | ||
.from( db.name ) | ||
; | ||
|
||
if (Rekord.isFunction(query)) | ||
{ | ||
query( builder ); | ||
} | ||
else if (Rekord.isObject(query)) | ||
{ | ||
if (query.page_size) | ||
{ | ||
builder.limit( query.page_size ); | ||
|
||
if (query.page_index) | ||
{ | ||
builder.offset( query.page_size * query.page_index ); | ||
} | ||
} | ||
} | ||
|
||
builder | ||
.then( function(rows) { | ||
success( rows ); | ||
}) | ||
.catch( function(error) { | ||
failure( [], error ); | ||
}) | ||
; | ||
}, | ||
|
||
keyOf: function(model) | ||
{ | ||
var keyHandler = this.database.keyHandler; | ||
var key = {}; | ||
|
||
keyHandler.copyFields( key, keyHandler.key, model, keyHandler.key ); | ||
|
||
return key; | ||
} | ||
|
||
}); | ||
|
||
function RestFactory(database) | ||
{ | ||
return new Rest( database ); | ||
} | ||
|
||
Rekord.setRest( RestFactory ); |
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,24 @@ | ||
|
||
var Rekord = require('rekord'); | ||
|
||
Rekord.Defaults.returning = '*'; | ||
Rekord.Defaults.cascade = Rekord.Cascade.Rest; | ||
Rekord.Defaults.cache = Rekord.Cache.None; | ||
|
||
Rekord.Relations.hasOne.auto = | ||
Rekord.Relations.belongsTo.auto = | ||
Rekord.Relations.hasMany.auto = | ||
Rekord.Relations.hasManyThrough.auto = | ||
false; | ||
|
||
Rekord.Relations.hasOne.saveCascade = | ||
Rekord.Relations.hasOne.autoCascade = | ||
Rekord.Relations.hasOne.cascade = | ||
Rekord.Relations.belongsTo.autoCascade = | ||
Rekord.Relations.belongsTo.cascade = | ||
Rekord.Relations.hasMany.autoCascade = | ||
Rekord.Relations.hasMany.saveParentCascade = | ||
Rekord.Relations.hasManyThrough.autoCascade = | ||
Rekord.Relations.hasManyThrough.cascadeSave = | ||
Rekord.Relations.hasManyThrough.saveParentCascade = | ||
Rekord.Cascade.Rest; |
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 @@ | ||
mydb.sqlite |
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,24 @@ | ||
|
||
var Rekord = require('rekord'); | ||
var Task = require('../root.js').Task; | ||
|
||
var t0 = new Task({ | ||
name: 't' + Math.random() | ||
}); | ||
|
||
Rekord.Promise | ||
.then(function() { | ||
return t0.$save(); | ||
}) | ||
.then(function() { | ||
return Task.Database.grabModel( t0.id ); | ||
}) | ||
.then(function(t1) { | ||
console.log( t1.name ); | ||
process.exit(); | ||
}) | ||
.catch(function(error) { | ||
console.log( error ); | ||
process.exit(); | ||
}) | ||
; |
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 @@ | ||
|
||
var Rekord = require('rekord'); | ||
var Task = require('../root.js').Task; | ||
|
||
Rekord.Promise | ||
.then(function() { | ||
return Task.refresh(); | ||
}) | ||
.then(function() { | ||
console.log( Task.all() ); | ||
process.exit(); | ||
}) | ||
.catch(function(error) { | ||
console.log( error ); | ||
process.exit(); | ||
}) | ||
; |
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,2 @@ | ||
#/bin/bash | ||
sqlite3 mydb.sqlite < create.sql |
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,5 @@ | ||
CREATE TABLE IF NOT EXISTS "task" ( | ||
"id" TEXT PRIMARY KEY, | ||
"name" TEXT, | ||
"done" INTEGER | ||
); |
Oops, something went wrong.