Coder Social home page Coder Social logo

backbone-orm's People

Contributors

acconut avatar forivall avatar gwilymhumphreys avatar kmalakoff avatar kvnloo avatar marcelklehr avatar ryansolid avatar

Stargazers

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

Watchers

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

backbone-orm's Issues

Error: missing dependency: underscore

I got this error using component.

The problem seems to be that your internal require implementation uses window.require as a fallback to require its dependencies. Component, however, does not allow "deeper" components to require their dependencies via the top-level require and instead provides every module with a specialized locally injected require variable.

A workaround would be to add the dependency to the window object, but that's not really the point of using a module require system, I think :)

A fix would be to grab the originally injected local require and save it for later e.g. as externalRequire. This could then be used inside your internal require as a fallback.

How to use

Hello,

it is not quite clear to me how to use backbone-orm and backbone-mongo correctly.

For example, I have a User model, which has a 'hasMany' relationship to 'Address'.
In turn, the address has a 'belongsTo' relationships to 'User'.

Question 1:
When I create instances, how would I go about attaching those to one another?
When I do this

user = new User { ... }
address = new Address { ... }

address.set 'user', user
user.save()

then the user gets saved properly, as does the address, but the user_id of the address is 'null'.
However, when I do address.save() before saving the user, then I have two identical address objects in my database, but the user still has an address_id of 'null'

Question 2:
How would I set up the relation from the other side?
How do I say in code 'Make it so that these address instances are attached to this user instance'?

I am generally confused, since on the documentation website there are only examples for finding stuff, so I don't even know whether my approach of using model.set(schemeProperty, value) is correct at all...

Thanks and regards,
Thomas

offline storage with refresh from server

hey, I'm trying to find a solution for an "offline-first" application where the data is stored first in the browser but can be populated from the server. will this work with your backbone-orm suite?

custom port ignored on processing URL for connecting with datasource

On trying to connect with MySQL running in a docker container, thus using non-default port number, I am observing knex trying to connect with port 3306 on proper IP address.

So I tried to find root cause for this and found backbone-orm's DatabaseURL to rely on NodeJS parsing URL resulting in host providing hostname incl. port number while hostname isn't including port number. backbone-sql is then using hostname to provide connection info for knex which is thus not including port number.

At first glance this issue seems to be more related to backbone-sql, however the code in DatabaseURL of backbone-orm looks rather strange. This is related to processing URL providing multiple hosts differently when compared with behaviour of NodeJS's URL parsing library as of 4.3.x.:

@hosts = []
for database in databases
  host = database.split(':')
  @hosts.push if host.length is 1 then {host: host[0], hostname: host[0]} else {host: host[0], hostname: "#{host[0]}:#{host[1]}", port: host[1]}

On providing multiple server addresses separated by comma this code is used and thus the resulting property per host including custom port number is hostname (different from NodeJS URL parsing, but would be matching expectation of backbone-sql) while on providing single host name only the result of NodeJS's URL parsing is taken as-is, so host is including port number rather than hostname. And basically I agree with NodeJS behaviour for the name of host doesn't include port numbers.

Backbone.Model::pick isn't schema aware

This is a minor issue and I will fix it.

Backbone = require 'backbone'

class A extends Backbone.Model
  urlRoot: '/api/as'
  schema:
    b: -> ['belongsTo', B, manual_fetch: true]
  sync: require('backbone-http').sync(A)

class B extends Backbone.Model
  urlRoot: '/api/bs'
  schema:
    a: -> ['hasOne', A, manual_fetch: true]
  sync: require('backbone-http').sync(B)

a = new A({b: {id: 1}})
a.get('b_id') # => 1
a.pick('b_id') # => {}   SHOULD BE {b_id: 1}

Relations?

Hello,

how would I work with relations between entities? E.g. I want all items that can be purchased in a certain store. How would I do that using backbone-orm?

thanks in advance

Questions on saving (with MongoDB)

I am getting this error when calling save on a model I have just changed.

Failed to create model. Error: MongoError: insertDocument :: caused by :: 11000 E11000 duplicate key error index: integration-test.users.$id dup key: { : ObjectId('5476438c9b93233f6a6414f0') }

Code:

waterfall [

        (next) ->
            User.findOne { email }, (err, user) ->
                next err, user

        (user, next) ->
            user.set { address }
            user.save (err, user) ->
                next err, user

], callback

I will provide more information or code if needed.
Isn't it supposed to work this way, and Backbone sync implementation should take care of wether it should be an update?
I also tried with Backbone's { patch: true }, but then the call to save() does not return at all.

Reconsider usage of _.isNull

In a number of places, _.isNull is used where it seems like the existential operator (?) would be a better fit (not foo? is the same as _.isNull(foo) and _.isUndefined(foo))

TypeError: this.model is undefined

the following line throws an error:
https://github.com/vidigami/backbone-orm/blob/master/src/relations/many.coffee#L120

targetProto = this.model.prototype
                           ^
backbone.Collection#set
backbone.Colleciton#add
backbone-orm.Many#add
One#_bindBacklinks.setBacklink(<Anonymous Function>)
One#_bindBacklinks
One#initializeModel
overrides.initialize
Backbone.Model
extend.child
new MyApp.MyModel

(This is a handwritten stack since the original files & line numbers are pretty arbitrary.)

I don't know why this happens, so I thought you might know more.

Update docs for unique

$unique docs

Common

  • $unique with $sort
    • Rows will contain values for the first entry matching the $unique field sorted by the $sort query.
  • $unique with $count
    • All other queries are ignored and the count of rows after grouping by the $unique fields is returned.

backbone-sql

$unique uses postgres specific functions for some functionality.

  • $unique without specifying $select or $values:
    • The documents returned will contain fields for each column. This uses postgres window functions and will not work with mysql.
  • $unique with $select or $values when $select or $values contain fields not in $unique.
    • The documents returned will contain fields for each field in ($select or $values). This uses postgres window functions and will not work with mysql.
  • $unique with $select or $values when $select or $values do not contain fields not in $unique.
    • As above, but an sql distinct query is used. This will work with mysql, etc.
  • $unique without $sort
    • Rows will contain values for the first entry matching the $unique field. The 'first entry' is the first when rows are sorted by id.

backbone-mongo

$unique uses the mongodb aggregation framework. There are limitations involved, so results are a bit different than with postgres.

  • $unique without specifying $select or $values:
    • This will only return the id field and the fields specified in $unique.
  • $unique with $select or $values:
    • The document returned with have each field in $select or $values.
  • $unique without $sort
    • Rows will contain values for the first entry matching the $unique field. The 'first entry' is random without a $sort query. Don't do this.

Add batch create to sync for graph creation

BackboneHTTP can send too many requests to the server during create and patch. It should be able to send a graph of JSON to create and return a graph of models.

Update tests to use this to try to speed up the setup step

Add support for `$distinct`

This would be to allow use of SQL's DISTINCT and mongo's collection.distinct()

Syntax would either be

{$distinct: ['column_name']}

or

{column_name: {$distinct: true}}

Change primary key?

I'd like to be able to change the type of the primary key (changing the field name would be nice to have, too), can I simply override it in my schema definition?

findOrCreate should use upsert when available

In order to avoid race conditions, findOrCreate should use available upsert mechanisms when available (db.collection.update(<query>,<update>,{upsert: true}) in mongodb, varied support in sql backends)

Are relations supposed to work with inheritance?

Hi again,

say I have a model that extends another model, like StaffUser and User for example.
Now when I set it up like the examples suggest, but also have StaffUser extends User, then, besides still having the problem, that the foreign keys for the relationships do not get set properly (#48), my 'Address' instance (when assigning a 'StaffUser' instance to its 'user' property) now has two fields, user_id and staff_user_id (both 'null' as of issue mentioned before).

I realise this is a complex issue, and coming from an object oriented background I might be expecting too much.
However your software looks very promising, and even without this feature it would save a lot of boilerplate code.

Thanks for your help!
Thomas

Promises, promises ?

Hi, it looks like a promising project!

I know its great having both the # Node style

project.save (err, project) ->
  return console.log 'Oh no an error' if err
  console.log "We're saved!"

and # Backbone style

project.save {
  success: (project) -> console.log "We're saved!"
  error: (project, err) -> console.log 'Oh no an error'
}

callback styles.

But how about returning a when.js/Promises A+ promise, when save() is called without a param ? It would greatly appeal to all promising developers out there!

Best way to have Backbone-Collections?

Hi,
i play around with some models and sqlite adapter that is working well and fits my needs so far.
What i can't figure out is a good way to crate a collection of models with backbone-orm. Unfortunately i can not find an example, or webresource. (For a short moment I wonder if i am the only one who think that a Table, or resultset is a collection of modells).

How do you build up collections of models, that relates to backbone-orm?
How can i become a collection back of a result by filtering a parameter?

Thanks a lot,
ps

Error redefining sync method

Hi, I am attempting to get started with backbone-orm. When I run the following bit of code:

var Backbone = require("backbone");

var Project = Backbone.Model.extend({
    urlRoot : "xxx"
});

Project.prototype.sync = require("backbone-orm").sync(Project);

I get this error:

 ~\Development\backbone-fs> node .\bug.js

C:\Users\pshangov\Development\backbone-fs\node_modules\backbone-orm\lib\extensions\model_stream.js:5
unction ctor() { this.constructor = child; } ctor.prototype = parent.prototype
                                                                ^
TypeError: Cannot read property 'prototype' of undefined
    at __extends (C:\Users\pshangov\Development\backbone-fs\node_modules\backbone-orm\lib\extensions\model_stream.js:5:199)
    at C:\Users\pshangov\Development\backbone-fs\node_modules\backbone-orm\lib\extensions\model_stream.js:19:5
    at Object.<anonymous> (C:\Users\pshangov\Development\backbone-fs\node_modules\backbone-orm\lib\extensions\model_stream.js:51:5)
    at Object.<anonymous> (C:\Users\pshangov\Development\backbone-fs\node_modules\backbone-orm\lib\extensions\model_stream.js:53:4)
    at Module._compile (module.js:449:26)
    at Object.Module._extensions..js (module.js:467:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Module.require (module.js:362:17)
    at require (module.js:378:17)

OS: Windows 7 64-bit
node version 0.8.21
backbone-orm version 0.5.1

The same happens on latest version from git (eb085e5)

There is no error if I comment out the "Project.prototype.sync = ..." line.

Cheers,
Peter

Nested select and values query

The tests should pass for:

  1. test -> generators -> cursor -> it 'Cursor can select fields from json', (done) ->
  2. test -> generators -> cursor -> it 'Cursor can select values from json', (done) ->

Quotes in query params uuids

I'm using Backbone in back and front end.
I configured Backbone-ORM + Backbone-HTTP in my "client" web app and used Backbone-ORM + Backbone-Mongo + Backbone-REST in my REST API.
It worked fine until I tested some queries using relationship keys from my client against the REST API.

Product.find({
  category_id: '5785228fc2ad8a23481dbb75'
}, function (err, products) {
  $("body").html(JSON.stringify(products));
});

The request that is made:
http://localhost:8080/products?category_id=%225785228fc2ad8a23481dbb75%22

That means that the category_id here is interpreted as number represented in string, and therefore must be "quoted".
But I don't think that is the expected result. Even more when the restify server, with the Backbone-REST on top of it, doesn't interpret it right and the GET returns [], even though the request without the quotes return it right.

Documentation should be better

Add conventions configuration
Add how to write and test a custom sync
Explain how to implement a REST controller in a different language
Explain how database tools work for Mongo and SQL

Good practice for lastModified timestamp / hooks in general?

Hi again!
I wanted to quickly ask, if there is a recommended way to set both 'createdAt' timestamp and 'updatedAt' timestamp. The 'created' one could be defined in the model as having a default value of 'new Date()', but how about the 'updatedAt' one?
I was just evaluating waterline, and came across this feature, which I found pretty nice:
https://github.com/balderdashy/waterline-docs/blob/master/models.md#autocreatedat

Related question:
Are there any hooks, like 'beforeSave' or similar, that could be used for that purpose?
Excuse me if that should be covered by Backbone knowledge I do not yet have ;)

Again, thanks for your help, it is very much appreciated!
Thomas

Setting up relations upon creation

So, I've got a model Apple:

var Apple = Backbone.Model.extend({
  schema: {
    origin: ['belongsTo', Farm]
  }
})

And a Juice model:

var Juice= Backbone.Model.extend({
  schema: {
    origin: ['belongsTo', Farm]
  }
})

Question: Can I simply do the following to overtake the apple's origin?

var juice = new Juice({
  origin: juicyApple.get('origin')
})
Juice.save(...)

Compatible with socket.io solutions?

Is this library compatible with socket.io-based sync solutions like backbone.io or backbone.iobind? In other words, if you dropped on of them into the sync hook, would it break backbone-orm? I'm guessing it would since the sync hook seems to be the only way the Model becomes "aware" that it's an ORM model. In any case, it would be nice if it were possible to use this in a real-time socket.io setup. It would be extra special if there was firebase option also.

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.