migrating concepts from mongoose/backbone to derby models

157 views
Skip to first unread message

Benj. N. Sugar

unread,
Sep 19, 2012, 1:53:25 PM9/19/12
to Derby
Hi,

I'm having some trouble converting some Mongoose/Backbone model
concepts to these new fangled Derby models. The following questions
all reference the project that is auto-generated from using "derby new
projectname"

1. In the project, a model is never explicitly created by using model
= store.createModel(). Is this because the model is automatically
generated from store = derby.createStore()?

2a. In a Mongoose/Backbone world, I'd probably have a collection
called Rooms which is populated by some Room models/schemas. I'd then
make modifications to the model by using the libraries' save method
like so:

Room.save({num: 507}).

In derby,js, since there can only be one model associated with a
store, and typically only one store in a project, is the convention
here that there is a single model object which is passed the name of a
group when subscribing creating something akin to a collection?

model.subscribe('rooms', function(err, room){});
model.set('rooms.0.num', 507);

2b. Does the Derby store contain all of these groupings in the single
model object like so?

{ rooms :
[ {num: 507, type: "suite"},
{num: 848, type: "single"} ],
guests:
[ {guest: "first1 last1"},
{guest: "first2 last2"} ],
}

Or does it internally have a reference to separate models, one for
rooms and another for guests which can both be accessed by using the
generic model object methods?

Monkey Patch

unread,
Oct 9, 2012, 1:11:53 PM10/9/12
to der...@googlegroups.com
Hi Benj,

I understand your confusion - it was something I 'struggled' to grasp as well. There's much mention of models in the docs but I kept asking myself "But where the HECK do you define your models?!"

I am beginning to suspect that there are no models per se (as in mongoose) but _a_ model. Correct me if I'm wrong, guys.

So, when you talk about model in derby it's like a model of the world. Which corresponds to the db in the mongodb connection:

"mongodb://user:pwd@localhost:27020/testing" (testing, in this case)

The store is where the model is persisted. (which could be just ram)


From the docs:

// Examples:
model.set('todos.id_0.completed', true);
model.set('rooms.lobby.messages.5.text', 'Call me');

collection.documentId.document


So I guess the answer to your 2b is the former.


Could we please have some definitions up in the docs? What _is_ a model in derby?
Also, any plans on opening up the wiki on github?

William Hurley

unread,
Oct 9, 2012, 5:21:05 PM10/9/12
to der...@googlegroups.com
My understanding is that you create references / subscriptions inside the current model to various store elements. So, on a page that I have multiple lists of information coming from different collections in mongo I have several calls to:

model.subscribe and then model.refList. So, for instance, on one page where I'm pulling several different types of information I have the following (note, I'm using Step because sometimes I hate all the nesting):

get('/', function(page, model) {
        step(function subscribeSites() {
                model.subscribe('sites', 'users.1.sites', this);
        },

        function subscribeServers(err, sites) {
                if (err)
                        throw err;

                model.refList('_sites', sites, 'users.1.sites');
                model.subscribe('servers', 'users.1.servers', this);
        },

        function subscribeMessages(err, servers) {
                if (err)
                        throw err;

                model.refList('_servers', servers, 'users.1.servers');
                model.subscribe('messages', 'users.1.messages', this);
        },

        function renderHome(err, messages) {
                if (err)
                        throw err;

                model.refList('_messages', messages, 'users.1.messages');

                render('home', page);
        });
})

What this does is create attributes in the model for _sites, _servers and _messages that pull the information from the appropriately named collections (e.g. sites, servers, messages) and that have referenced lists of content for the user in a collection called users. So the users collection has the following: { "_id" : "1", "sites" : [ "6aee61ed-4631-4b81-807b-aa47d2223798" ] }

greelgorke

unread,
Oct 11, 2012, 4:50:50 AM10/11/12
to der...@googlegroups.com
yep, thats right. Derby Model is not a Model but a Data Access Object, DAO, which is a pattern to abstract the data holding layers. In fact it can be an ORM if there is a plugin which maps the paths to tables.
Reply all
Reply to author
Forward
0 new messages