Skip to content

Commit

Permalink
Added Model.finder
Browse files Browse the repository at this point in the history
  • Loading branch information
assaf committed Nov 17, 2011
1 parent aa05378 commit 56eb0ff
Show file tree
Hide file tree
Showing 6 changed files with 285 additions and 80 deletions.
5 changes: 1 addition & 4 deletions lib/poutine/collection.coffee
Expand Up @@ -3,7 +3,7 @@

# Represents a collection and all the operation you can do on one. Database
# methods like find and insert operate through a collection.
class Collection
exports.Collection = class Collection
constructor: (@name, @database, @model)->

# -- Finders --
Expand Down Expand Up @@ -451,6 +451,3 @@ class Scope
if @_cursor
@_cursor.close()
return


exports.Collection = Collection
42 changes: 42 additions & 0 deletions lib/poutine/connect.coffee
@@ -0,0 +1,42 @@
# Database configurations. We use this to configure database access and then
# get the driver instance (Db object).
databases = {}

# Configure a database.
#
# You can call this with one argument, a database name, and it will apply the
# default configuration.
#
# You can call this with database name and an object containing configuration
# options. Supported options are:
# - host -- Database host name (defaults to 127.0.0.1)
# - port -- Database port number (defaults to 27017)
# - pool -- Connection pool size (defaults to 10)
# - name -- Actual database name if different from name argument
#
# You can also call this with an object, where each key is a database name, and
# the corresponding value the database configuration.
exports.configure = configure = (name, options)->
{ Configuration } = require("./database")
if name.constructor == Object
configs = name
for name, options of configs
configure name, options
else
throw "Already have configuration for #{name}" if databases[name]
options ||= {}
config = new Configuration(options.name || name, options)
databases[name] = config
configure.default ||= name

# Default database name. If not set, pick the first database.
configure.default = null
configure.DEFAULT = "development"

# Provides access to the specified database (null for default database).
exports.connect = connect = (name)->
{ Database } = require("./database")
name ||= configure.default || process.env.NODE_ENV || configure.DEFAULT
unless databases[name]
configure name
return new Database(databases[name])
12 changes: 4 additions & 8 deletions lib/poutine/database.coffee
Expand Up @@ -11,7 +11,7 @@ catch ex

# Database configuration. This is basically a wrapped around the Mongodb
# driver, specifically it's Db object.
class Configuration
exports.Configuration = class Configuration
constructor: (name, options = {})->
@_pool = new Pool
name: name
Expand All @@ -38,7 +38,7 @@ class Configuration
# new connection that you can use to access the database.
#
# Phytical connections are lazily initialized and pooled.
class Database extends EventEmitter
exports.Database = class Database extends EventEmitter
constructor: (@_configuration)->
@_collections = []
@ObjectID = require("mongodb").BSONPure.ObjectID
Expand Down Expand Up @@ -149,8 +149,8 @@ class Database extends EventEmitter
# mongo().find "posts", id, (err, post, db)->
# . . .
#
# query = mongo().find("posts", author_id: author._id)
# query.all (err, posts, db)->
# scope = mongo().find("posts", author_id: author._id)
# scope.all (err, posts, db)->
# . . .
find: (name, selector, options, callback)->
return @collection(name).find(selector, options, callback)
Expand Down Expand Up @@ -178,7 +178,3 @@ class Database extends EventEmitter
# . . .
distinct: (name, key, selector, callback)->
@collection(name).distinct key, selector, callback


exports.Configuration = Configuration
exports.Database = Database
45 changes: 1 addition & 44 deletions lib/poutine/index.coffee
@@ -1,48 +1,5 @@
{ Database, Configuration } = require("./database")
{ Model } = require("./model")
MongoDB = require("mongodb")

# Database configurations. We use this to configure database access and then
# get the driver instance (Db object).
databases = {}

# Configure a database.
#
# You can call this with one argument, a database name, and it will apply the
# default configuration.
#
# You can call this with database name and an object containing configuration
# options. Supported options are:
# - host -- Database host name (defaults to 127.0.0.1)
# - port -- Database port number (defaults to 27017)
# - pool -- Connection pool size (defaults to 10)
# - name -- Actual database name if different from name argument
#
# You can also call this with an object, where each key is a database name, and
# the corresponding value the database configuration.
configure = (name, options)->
if name.constructor == Object
configs = name
for name, options of configs
configure name, options
else
throw "Already have configuration for #{name}" if databases[name]
options ||= {}
config = new Configuration(options.name || name, options)
databases[name] = config
configure.default ||= name

# Default database name. If not set, pick the first database.
configure.default = null
configure.DEFAULT = "development"

# Provides access to the specified database (null for default database).
connect = (name)->
name ||= configure.default || process.env.NODE_ENV || configure.DEFAULT
unless databases[name]
configure name
return new Database(databases[name])

{ connect, configure } = require("./connect")

exports.connect = connect
exports.configure = configure
Expand Down
35 changes: 31 additions & 4 deletions lib/poutine/model.coffee
@@ -1,4 +1,34 @@
class Model
connect = require("./connect").connect


exports.Model = class Model

# Finds all objects that match the query selector.
#
# If the last argument is a callback, load all matching objects and pass them to callback.
# Callback receives error, objects and connection.
#
# Without callback, returns a new Scope object.
#
# The first argument is the query selector. You can also pass an array of identifiers to load
# specific objects, or a single identifier to load a single object. If missing, all objects
# are loaded from the collection.
#
# The second argument are query options (limit, sort, etc). If you want to specify query
# options, you must also specify a query selector.
#
# Examples:
# Post.find { author_id: author._id }, limit: 50, (err, posts, db)->
# . . .
#
# Post.find id, (err, post, db)->
# . . .
#
# scope = Post.find(author_id: author._id)
# scope.all (err, posts, db)->
# . . .
@find: (selector, options, callback)->
connect().find(this, selector, options, callback)

# Used to instantiate a new instance from a loaded object.
@instantiate: (model, values)->
Expand All @@ -18,6 +48,3 @@ class Model
@field: (name)->
@fields ||= {}
@fields[name] = true


exports.Model = Model

0 comments on commit 56eb0ff

Please sign in to comment.