-
Notifications
You must be signed in to change notification settings - Fork 120
Creating a Custom Store in Tower
To create a custom store in Tower, implement the following methods:
class Tower.MyCustomStore extends Tower.Store
find: (cursor, callback) ->
insert: (cursor, callback) ->
update: (cursor, callback) ->
destroy: (cursor, callback) ->
count: (cursor, callback) ->
exists: (cursor, callback) ->
# setup the database
@initialize: (callback) ->
# completely clear the database
@clean: (callback) ->
Implementing those methods will allow you to save models with simple fields. To support associations, you can implement before/after hooks for find/insert/update/destroy, or if you can do joins you may just be able to handle them without any hooks (mongodb needs to use hooks though, for example, to implement hasMany associations, while SQL can use joins).
class Tower.MyCustomStore extends Tower.Store
# ...
runBeforeInsert: (cursor, callback) ->
runAfterInsert: (cursor, callback) ->
runBeforeUpdate: (cursor, callback) ->
runAfterUpdate: (cursor, callback) ->
runBeforeDestroy: (cursor, callback) ->
runAfterDestroy: (cursor, callback) ->
runBeforeFind: (cursor, callback) ->
runAfterFind: (cursor, callback, records) ->
The first argument to every method is a cursor, which contains all the information the database needs. You can call cursor.toParams()
to get it in a compiled JSON object:
{
"sort": [],
"conditions": {
},
"page": 2
}
All that's necessary is to iterate through each item in the cursor's criteria, serialize it to whatever form it needs to be, then generate the database-specific query string/object, and make the database call.