Skip to content

Commit

Permalink
0.2. Added toArray, sort, findOne, remove, forEach
Browse files Browse the repository at this point in the history
  • Loading branch information
balupton committed Jul 6, 2011
1 parent 6897ef2 commit a17fd43
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 7 deletions.
3 changes: 3 additions & 0 deletions README.md
Expand Up @@ -63,6 +63,9 @@ Query-Engine supports the exact same queries as [MongoDb](http://www.mongodb.org

## History

- v0.2 July 6, 2011
- Added toArray, sort, findOne, remove, forEach

- v0.1 July 6, 2011
- Initial commit

Expand Down
84 changes: 78 additions & 6 deletions lib/query-engine.coffee
@@ -1,26 +1,30 @@
# http://www.mongodb.org/display/DOCS/Advanced+Queries

# Array
# Has In
Array::hasIn = (options) ->
for value in @
if value in options
return true
return false

# Has All
Array::hasAll = (options) ->
@.sort().join() is options.sort().join()

# Create
Object::find = (query) ->
# Find
Object::find = (query={},next) ->
# Matches
matches = {}

# Start with entire set
for own id,record of @
# Match
match = false
empty = true

# Selectors
for own field, selector of query
empty = false
selectorType = typeof selector
exists = record[field]?
value = if exists then record[field] else false
Expand Down Expand Up @@ -116,8 +120,76 @@ Object::find = (query) ->
match = true

# Append
if match
if match or empty
matches[id] = record

# Return matches
matches
# Async
if next?
next false, matches
# Sync
else
matches

# For Each
Object::forEach ?= (callback) ->
# Prepare
for own id,record of @
callback record, id

# To Array
Object::toArray = (next) ->
# Prepare
arr = []

# Cycle
for own key,value of @
arr.push value

# Async
if next?
next false, arr
# Sync
else
arr

# Sort
Object::sort = (comparison,next) ->
# Prepare
arr = @toArray()
arr.sort(comparison)

# Async
if next?
next false, arr
# Sync
else
arr

# Find One
Object::findOne = (query={},next) ->
# Cycle
matches = @find(query).toArray()
match = if matches.length >= 1 then matches[0] else undefined

# Async
if next?
next false, match
# Sync
else
match

# Remove
Object::remove = (query={},next) ->
# Prepare
matches = @find(query)

# Delete
for own id,record of @
delete @[id]

# Async
if next?
next false, @
# Sync
else
@
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "query-engine",
"version": "0.1.1",
"version": "0.2.0",
"description": "A NoSQL (and MongoDB compliant) Query Engine coded in CoffeeScript for Server-Side use with Node.js and Client-Side use with Web-Browsers",
"homepage": "https://github.com/balupton/query-engine.npm",
"keywords": [
Expand Down
22 changes: 22 additions & 0 deletions test/query-engine.coffee
Expand Up @@ -93,5 +93,27 @@ tests =
expected = 'index': data.documents.index, 'jquery': data.documents.jquery
assert.deepEqual actual, expected

'all': ->
actual = data.documents.find({})
expected = data.documents
assert.deepEqual actual, expected

'sort': ->
actual = data.documents.find({}).sort (a,b) ->
return b.position - a.position
expected = [data.documents.history,data.documents.jquery,data.documents.index]
assert.deepEqual actual, expected

'findOne': ->
actual = data.documents.findOne()
expected = data.documents.index
assert.deepEqual actual, expected

'remove': ->
actual = data.documents.remove()
expected = {}
assert.deepEqual actual, expected
assert.deepEqual data.documents, expected

# Export
module.exports = tests

0 comments on commit a17fd43

Please sign in to comment.