GithubHelp home page GithubHelp logo

vidigami / backbone-sql Goto Github PK

View Code? Open in Web Editor NEW
14.0 14.0 8.0 929 KB

PostgreSQL, MySQL, and SQLite3 storage for BackboneORM

Home Page: http://vidigami.github.io/backbone-orm/backbone-sql.html

License: MIT License

CoffeeScript 100.00%

backbone-sql's People

Contributors

gwilymhumphreys avatar kmalakoff avatar kvnloo avatar mateomurphy avatar mlabod avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

backbone-sql's Issues

Paging broken when performing a join, when the table has a column with the same name as one of related table's columns

Prototype of a possible test case:

class A extends Backbone.Model
  schema:
    b: -> ['belongsTo', B]
    c: -> ['belongsTo', C]
  sync: require('backbone-sql').sync(A)
  url: 'postgres://test:[email protected]:3306/test/as'

class B extends Backbone.Model
  schema:
    name: 'String'
    as: -> ['hasMany', B]
    c: -> ['belongsTo', C]
  sync: require('backbone-sql').sync(B)
  url: 'postgres://test:[email protected]:3306/test/bs'

class C extends Backbone.Model
  schema:
    memberships: -> ['hasMany', A]
    roles: -> ['hasMany', B]
  sync: require('backbone-sql').sync(C)
  url: 'postgres://test:[email protected]:3306/test/cs'

A.cursor({$page: true, $offset:0, $limit:150, c_id:12}) # works fine
A.cursor({$page: true, $offset:0, $limit:150, c_id:12, 'b.name': 'foo'}) # fails
# Error: column reference "c_id" is ambiguous, sql: select count(*) from "as" left outer join "roles" on "as"."b_id" = "b"."id" where "c_id" = ? and "b"."name" = ?, bindings: 12,foo
A.cursor({$page: true, $offset:0, $limit:150, 'b.c_id':12, 'b.name': 'foo'}) # works; but suboptimal

Port number isnt passed through

Hi guys

Noticed that the port number isnt passed through to Knex in your connection.js

e.g. if I change code to add port I can now connect to my non-standard mysql (1441) otherwise I get ECONNREFUSED as it uses default port (3306)

      if (protocol === 'sqlite3') {
        connection_info = {
          filename: database_url.host || ':memory:'
        };
      } else {
        connection_info = _.extend({
          host: database_url.hostname,

This is the line I added

          port: database_url.port,
          database: database_url.database,
          charset: 'utf8'
        }, database_url.parseAuth() || {});
      }

Could be dangerous for those hoping to reach a dev DB with a dev connection string!

Let me know if you want a pull request as would be nice to contribute something small to this great project, but looks like something simple for you guys, and Im not sure how to test

$lte/$gte queries fail for Dates on MySQL

  1. Model.find (cache: false, query_cache: false) Handles $ne for dates:
    Uncaught AssertionError: all but START DATE found

  2. Model.find (cache: false, query_cache: false) Handles $lt and $lte boundary conditions:
    Uncaught AssertionError: first model found

  3. Model.find (cache: false, query_cache: false) Handles $lt and $lte boundary conditions with step:
    Uncaught AssertionError: two models found

TypeError: Object [object Object] has no method 'type'

Got this error when trying out the SQLite backend.

TypeError: Object [object Object] has no method 'type'
    at One.module.exports.One.initialize (code\node_modules\backbone-sql\node_modules\backbone-orm\backbone-orm.js:6392:70)
    at Schema.module.exports.Schema.initialize (code\node_modules\backbone-sql\node_modules\backbone-orm\backbone-orm.js:2268:17)
    at SqlSync.initialize (code\node_modules\backbone-sql\lib\sync.js:84:19)

Can you help me?

Bug with associations on empty tables

I've run into a bug, when there's an association on an empty table. My (simplified) models:

class Wall extends Backbone.Model
  schema:
    created_at: 'DateTime'
    name: 'String'
    messages: -> ['hasMany', require('./message')]

class Message extends Backbone.Model
  schema:
    created_at: 'DateTime'
    text: 'Text'
    wall: -> ['belongsTo', require('./wall')]
    user: -> ['belongsTo', require('./user')]

When the message table is empty, I get Error: HasMany.set: Unexpected type to set messages. Expecting array: null

This seems to happen because SqlCursor._joinedResultsToJSON takes

[ { walls_created_at: null,
    walls_name: 'test wall',
    walls_id: 1,
    messages_created_at: null,
    messages_text: null,
    messages_wall_id: null,
    messages_user_id: null,
    messages_id: null } ]

and converts it to

[ { created_at: null, name: 'test wall', id: 1, messages: null } ]

rather than

[ { created_at: null, name: 'test wall', id: 1, messages: [] } ]

Relation lookups should use the orm schema relation name rather than the sql table names

With a schema like the following

foo.coffee:

class Foo extends Backbone.Model
  schema:
    bar: -> ['belongsTo', require('./bar')]
  url: 'postgres://root:[email protected]:5432/example/foos'

bar.coffee:

class Bar extends Backbone.Model
  schema:
    baz: 'Integer'
    buz: 'Integer'
  url: 'postgres://root:[email protected]:5432/example/bars'

Currently, queries look like this:

Foo.find {'bars.baz': 1, $sort: 'bars.buz'}, (err, foos) -> console.log foos

Ideally, they should look like this

Foo.find {'bar.baz': 1, $sort: 'bar.buz'}, (err, foos) -> console.log foos

Model::each / eachC fails with $include

I'll be writing a test case for this in the near future

The error returned is Error: column reference "id" is ambiguous
model_each will need to be aware of joins and use the appropriate sort key, or sort will need to be aware of joins and automatically upgrade sort keys.

Join tables

I believe the following should work:

var Backbone = require('backbone');
var SQLSync = require('backbone-sql').sync;

var Room = Backbone.Model.extend({
  urlRoot: "sqlite://data.db/rooms",
  schema: {
    status: 'String',
    members: function() { return [ 'hasMany', User ] }
  }
});
Room.prototype.sync = SQLSync(Room);

var User = Backbone.Model.extend({
  urlRoot: "sqlite://data.db/users",
  schema: {
    rooms: function() { return [ 'hasMany', Room ] }
  }
});
User.prototype.sync = SQLSync(User);

var room = new Room({ status: 'test' });
room.save(function (err, ret) {
  if (err) throw err;
  console.log("save 1 finished with", err, ret);
});

However, when I run this code, it throws an error:

table rooms has no column named user_id, sql: insert into "rooms"
  ("status", "user_id") values (?, ?), bindings: test,

This implies to me that join tables aren't supported. Can you provide me some pointers as to how I might go about getting that functionality working for my project?

Thanks,
~ry

Fix test errors due to missing fields

  1. Model.cursor (cache: false, query_cache: false) "before each" hook:
    Error: ER_BAD_FIELD_ERROR: Unknown column 'foo' in 'field list', sql: insert into flats (boolean, created_at, json_data, name, updated_at) values (?, ?, ?, ?, ?), bindings: true,Wed May 21 2014 06:32:41 GMT-0700 (PDT),[object Object],flat_683,Wed May 21 2014 06:32:41 GMT-0700 (PDT)

  2. self model relations (cache: false, query_cache: false, embed: false) "before each" hook:
    Error: ER_BAD_FIELD_ERROR: Unknown column 'is_base' in 'field list', sql: insert into self_references (created_at, is_base, name, owner_id) values (?, ?, ?, ?), bindings: Wed May 21 2014 06:32:54 GMT-0700 (PDT),true,self_reference_3113,

  3. Model.cursor (cache: false, query_cache: false) "before each" hook:
    Error: column "json_data" of relation "flats" does not exist, sql: insert into "flats" ("boolean", "created_at", "json_data", "name", "updated_at") values (?, ?, ?, ?, ?) returning "id", bindings: true,2014-05-21T06:34:15.428-07:00,{"foo":{"bar":"baz"},"fizz":"buzz"},flat_14296,2014-05-21T06:34:15.428-07:00

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.