corejs

CoreJS ===========

npm install corejs
3 downloads in the last day
4 downloads in the last week
71 downloads in the last month

CoreJS

A NodeJS & MySQL framework built on top of Backbone. Designed to provide a ridiculously simple structure for your back-end data-model so you can focus on other more interesting things like making pretty user interfaces.

CoreJS isn't a typical ORM framework, with rigid relational definitions between models. Instead, it allows simple updating of a model and it's children.

The framework provides 4 core functions on both models, and collections:

  • fetch
  • save
  • update
  • destroy

NOTE: This framework is still in development and not ready for alpha use.

Introduction

Models

Models directly relate to a single row within a database table.

var User = Core.Model.extend({
  name: 'user'
});

The User model above relates to the users table in the database. (Table name = name + 's').

To get a user from the database, you might run the following code:

var user = new User({ id: 1 });
user.on("fetch", function() {
  // the `user` object now contains all details of the user with id '1'
}).fetch();

Collections

Collections relate to a list of models - multiple rows in a database.

var Users = Core.Collection.extend({
  name: 'users'
});

To retreive the list of users in the database, you could run the following code:

var users = new Users();
users.on("fetch", function () {
  // user models now exist within the users collection
}).fetch();

The Users collection also relates to the users table in the database. The collection name should be the same as the table name.

Relations

The complexities arising from table relations was the primary reason for building CoreJS. The aim is to provide a simple interface to get/save/delete data in the database, without having to write a ton of nasty queries every time you want to update a few child objects/arrays.

The central control of relations is set in the model declaration, through the subObjects and subArrays parameters. For example:

var User = Core.Model.extend({
  name: 'user'.
  subObjects: {
    picture: {
      table: 'user_picture',
      on: ['id', 'user_id']
    }
  },
  subArrays: {
    apps: {
      table: 'apps',
      on: ['id', 'user_id']
    }
  }
});

The configuration above details two "children":

User picture

  1. We want to get a users' picture and assign it as an object called "picture".
  2. The picture can be found in the user_picture table.
  3. The related row from the user_picture table is when user.id = user_picture.user_id.

Apps

  1. We want to get all the apps and assign them to an array calls "apps".
  2. The apps can be found in the apps table
  3. The related row from the apps table is when user.id = apps.user_id.
var userModel = new UserModel({ id: 91 });
userModel.on('fetch', function() {

  // The userModel attributes are:
  {
    name: 'chris',
    email: 'test@testing123.com',
    password: '12356',
    id: 132,
    verified: 0,
    added: Sat Apr 27 2013 13: 09: 55 GMT + 0100(BST)
  }

}).fetch();

Models

Fetch

fetch([ options ])

var userModel = new UserModel({ id: 91 });
userModel.on('fetch', function() {
  console.log(userModel.toJSON());  // the full user with id '91' should now be available
}).fetch();

Getting children

By default fetch does not get the child arrays/objects. If there are subObjects and subArrays you'd like to get, specify withChildren: true within the options object, e.g.

fetch({ withChildren: true })

Only getting specific fields

You can choose to only get specific fields when fetching, by adding a fields array parameter to the options:

fetch({
  fields: ['id', 'name']
})

Getting a user with a parameter apart from id

By default fetch will ahem... "fetch" based on the id attribute specified in the model. But what if you want to get a user from the database with a specific email address?

fetch({
  conditions: {
    email: "my@email.com"
  }
})

The new conditions object will override the internal one that fetches by id.

Save

Saves a model to the database. If the id parameter already exists, the model is assumed to already exist within the database, so save delegates to update instead.

Example:

var userModel = new UserModel({
  email: 'my@email.com',
  name: 'Chris Houghton',
  age: 23
});
userModel.on('fetch', function() {
  console.log(userModel.toJSON());  // the full user with id '91' should now be available
}).fetch();

Update

Destroy

npm loves you