Skip to content

A rekord binding to Knex.js - implementing Rekord.rest on the server side

Notifications You must be signed in to change notification settings

Rekord/rekord-knex

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

rekord-knex

Build Status devDependency Status Dependency Status License Alpha

A rekord binding to Knex.js.

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

// =============================================================================
// 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!
  })
;

About

A rekord binding to Knex.js - implementing Rekord.rest on the server side

Resources

Stars

Watchers

Forks

Packages

No packages published