Stores

Persistence

Each Tower.Store method requires exact parameters (there's no argument overloading).

The store is used by the Tower.Model internally.

Store#find

Returns an array of models. The database is free to perform more fine-grained optimizations, such as making a collection.findOne call in mongodb if there's only one id we're searching by.

store.find { "id" : { "$in": [1, 2, 3] } }
store.find { "id" : { "$nin": [1, 2, 3] } }
store.find { "tags" : { "$all": ["ruby", "javascript"] } }
store.find { "tags" : { "$in": ["ruby", "javascript"] } }
store.find { "$or": [ { "tags" : { "$in": ["ruby", "javascript"] } }, { "id" : { "$in": [1, 2, 3] } } ] }

Store#create

Creates one model.

store.create { "tags" : ["ruby", "javascript"] }

Store#update

Updates any models matching the query.

store.update { "$set": { "tags" : ["ruby", "javascript"] } }, { "id" : { "$in": [1, 2, 3] } }

Store#destroy

Deletes any models matching the query.

store.destroy { "id" : { "$in": [1, 2, 3] } }

Querying

Operators

$eq

store.find { "status" : "active" }
store.find { "status" : "$eq" : "active" }

$ne

store.find { "status" : "$ne" : "inactive" }

<, <=, >, >=

store.find { "likeCount" : ">=" : 10 }
store.find { "likeCount" : "$gte" : 10 }

$all

store.find { "tags" : { "$all" : ["ruby", "javascript"] } }

$in

store.find { "tags" : { "$in" : ["ruby", "javascript"] } }

$nin

store.find { "tags" : { "$nin" : ["java", "asp"] } }

$match

store.find { "name" : /acme.*corp/i }

$notMatch

store.find { "name" : /acme.*corp/i }

$or

store.find { "$or": [ { "likeCount" : 1000 }, { "likeCount" : { "$gte": 1, "$lte": 100 } } ] }

$nor

store.find { "$nor": [ { "likeCount" : 1000 }, { "likeCount" : { "$gte": 1, "$lte": 100 } } ] }

$and

store.find { "$and" : [ { "a" : 1 }, { "a" : { $gt: 5 } } ] }

Sorting

sort

store.find { "tags" : { "$in" : ["ruby", "javascript"] }, "sort": [["title", "asc"]] }

Paginating

limit

store.find { "tags" : { "$in" : ["ruby", "javascript"] }, "limit": 20 }

offset

store.find { "tags" : { "$in" : ["ruby", "javascript"] }, "offset": 10 }

Selecting Specific Fields

fields

Memory Store

The memory story stores all the records in a JavaScript object. It can perform all the same advanced queries as Tower.Store.MongoDB, making it really easy to reuse server-side model code on the client.

MongoDB Store