GitXplorerGitXplorer
k

knex-orm

public
61 stars
6 forks
11 issues

Commits

List of commits on branch master.
Unverified
699431b955ad133a99d4dc2a75c974c9928832d9

Improved class property documentation

kkripod committed 9 years ago
Unverified
6778d2fd1ff407bf2643f1c939bec377823723a0

Added a Model validation plugin for the query builder

kkripod committed 9 years ago
Unverified
34d45c8c105e9e4431fe2c8378710ddb3e71d613

Added partial support for composite primary keys

kkripod committed 9 years ago
Unverified
84de27d6604ce28a46ffb856eafc57258a134c05

Apply Plugin#beforeQuery functions before QueryBuilder#toString()

kkripod committed 9 years ago
Unverified
9f838b7eebf0e20d004e2ccb6a66dd02272da0bf

Fixed the assignment of plugin options

kkripod committed 9 years ago
Unverified
a740f4bd952963cbf5009a41d209c2cd466645eb

Added plugin initializers and options (closes #19)

kkripod committed 9 years ago

README

The README file for this repository.

Knex-based object-relational mapping for JavaScript.

Version (npm) Build Status Code Coverage Gitter

Introduction

The motivation behind this project is to combine the simplicity of Bookshelf with the power of Knex and modern ECMAScript features.

Knex-ORM aims to provide a wrapper for every significant method of Knex, while keeping the ORM code overhead as low as possible.

Getting started

Installing Knex and at least one of its supported database drivers as peer dependencies is mandatory.

$ npm install knex --save
$ npm install knex-orm --save

# Then add at least one of the following:
$ npm install pg --save
$ npm install mysql --save
$ npm install mariasql --save
$ npm install sqlite3 --save

An instance of the Knex-ORM library can be created by passing a Knex client instance to the entry class.

const knex = require('knex');
const KnexOrm = require('knex-orm');

const Database = new KnexOrm(
  knex({
    client: 'sqlite3',
    connection: {
      filename: './dev.sqlite3',
    },
  })
);

class Employee extends Database.Model {
  static get tableName() { return 'employees'; } // Redundant

  // Specify related Models which can optionally be fetched
  static get related() {
    return {
      company: this.belongsTo('Company'), // No Model cross-referencing
    };
  }
}

class Company extends Database.Model {
  // The 'tableName' property is omitted on purpose, as it gets assigned
  // automatically based on the Model's class name.

  static get primaryKey() { return 'rank'; }

  static get related() {
    return {
      employees: this.hasMany('Employee'),
    };
  }
}

// Register Models to make them relatable without cross-referencing each other
Database.register(Employee);
Database.register(Company);

Examples

Creating and storing a new Model:

const famousCompany = new Company({
  name: 'A Really Famous Company',
  email: 'info@famouscompany.example'
});

famousCompany.save()
  .then((ids) => {
    // An ordinary response of a Knex 'insert' query
    // (See http://knexjs.org/#Builder-insert)
    console.log(ids);
  });

Modifying an existing Model gathered by a query:

Company.query().where({ email: 'info@famouscompany.example' }).first()
  .then((company) => {
    // Response of a Knex 'where' query, with results parsed as Models
    // (See http://knexjs.org/#Builder-where)
    console.log(company); // Should be equal with 'famousCompany' (see above)

    company.name = 'The Most Famous Company Ever';
    return company.save();
  })
  .then((rowsCount) => {
    // An ordinary response of a Knex 'update' query
    // (See http://knexjs.org/#Builder-update)
    console.log(rowsCount); // Should be 1
  });