API for karras.entity
by
Wilkes Joiner
Usage:
(ns your-namespace
(:require karras.entity))
Overview
A library for defining entities and embedded types. Entities correspond to a mongo collection.
Example:
(defembedded Address [:street :city :state :zip])
(defembedded Phone [:area-code :number]
(defentity Person
[:first-name
:last-name
:address {:type Address}
:phones {:type :list :of Phone}])
Public Variables and Functions
add-reference
function
Usage: (add-reference entity k & vs)
Add one more :_id's to a sequence of the given key
Source
collection-for
function
Usage: (collection-for entity-or-type)
Returns the DBCollection for the supplied entity instance or type.
Source
convert
multimethod
No usage documentation available
Multimethod used to convert a entity.
Takes in a field-spec and a val and returns the conver
Dispatches off of :type key of first arg.
:default returns the value unmodified
Source
count-instances
function
Usage: (count-instances type)
(count-instances type criteria)
Return the number of entities optionally matching a given where clause.
Source
create
function
Usage: (create type hmap)
Makes a new instance of type and saves it.
Source
defembedded
macro
Usage: (defembedded classname [& fields] & type-fns)
Defines a type that can be embedded into an entity.
Source
defentity
macro
Usage: (defentity classname [& fields] & type-fns)
Defines an entity that corresponds to a collection.
Source
deffetch
macro
Usage: (deffetch type fn-name [& args] & criteria)
Defines a fetch function for the given type.
The function created takes all of the options of fetch plus an :and option to append to the where clause.
Usage:
(defentity Person [:first-name :last-name :age]
(deffetch adults [] (gte :age 18))
(deffetch peope-in-age-range [min max] (within :age min max)))
Outside of defentity:
(deffetch Person teenagers [] (within :age 13 19))
Give me all the teenagers with last names A-J sorted by last name:
(teenagers :and (within :last-name "A" "J") :sort [(asc :last-name)])
Give me the youngest 10 people between the ages of 21 and 100 sorted by age and last name:
(peope-in-age-range 21 100 :limit 10 :sort [(asc :age) (asc :last-name)])
Source
deffetch-one
macro
Usage: (deffetch-one type fn-name [& args] & criteria)
Defines a fetch-one function for the given type.
The function created takes all of the options of fetch-one plus an :and option to append to the where clause.
Usage:
(defentity Person [:first-name :last-name :age]
(deffetch-one person-by-fullname
[first-name last-name]
(eq :first-name first-name)
(eq :last-name last-name)))
Fetch a person by name:
(person-by-name "John" "Smith")
Source
delete
function
Usage: (delete entity)
(delete entity & entities)
Deletes one or more entities.
Source
delete-all
function
Usage: (delete-all type)
(delete-all type where)
Deletes all entitys given an optional where clause.
Source
distinct-values
function
Usage: (distinct-values type kw)
Return the distinct values of a given type for a given key.
Source
ensure-indexes
function
Usage: (ensure-indexes)
(ensure-indexes type)
Ensure the indexes are built, optionally for a given type. Defaults to all types.
Source
ensure-type
function
Usage: (ensure-type type entity)
Force an entity to be of a given type if is not already.
Source
entity-spec
function
Usage: (entity-spec type)
Lookup the EntitySpec for a given type
Source
entity-spec-assoc
function
Usage: (entity-spec-assoc type & kvs)
Associate the suppled keys and values with the entity-spec for the given type.
Source
entity-spec-get
function
Usage: (entity-spec-get type key & [default])
Lookup the EntitySpec value for given key of the given type.
Source
entity-spec-get-in
function
Usage: (entity-spec-get-in type path)
Lookup the EntitySpec value for given key of the given type.
Source
entity-spec-of
function
Usage: (entity-spec-of type & keys)
Given a type and one or more keys,
lookup of the entity-spec for the :type of the last field supplied.
Source
entity-spec-of-item
function
Usage: (entity-spec-of-item type & keys)
Given a type and one or more keys,
lookup of the entity-spec for the :of of the last field supplied.
Source
fetch
function
Usage: (fetch type criteria & options)
Fetch a seq of entities for the given type matching the supplied parameters.
Source
fetch-all
function
Usage: (fetch-all type & options)
Fetch all of the entities for the given type.
Source
fetch-by-id
function
Usage: (fetch-by-id entity)
(fetch-by-id type id)
Fetch an enity by :_id
Source
fetch-map-reduce-values
function
Usage: (fetch-map-reduce-values map-reduce-result & fetch-options)
Takes the result of map-reduce and fetches the values. Takes the same options as fetch.
Source
fetch-one
function
Usage: (fetch-one type criteria & options)
Fetch the first entity for the given type matching the supplied criteria and options.
Source
fetch-refers-to
function
Usage: (fetch-refers-to entity referrer-type referrer-field & options)
Given an entity, a type and a field, fetch all the entities of the given type
that refer to the given entity. Takes the same options as fetch.
Source
field-spec-of
function
Usage: (field-spec-of type & keys)
Given a type and one or more keys,
returns the field spec for the last key.
Source
find-and-modify
function
Usage: (find-and-modify type criteria modifier & options)
See http://www.mongodb.org/display/DOCS/findandmodify+Command
Source
find-and-remove
function
Usage: (find-and-remove type criteria & options)
See http://www.mongodb.org/display/DOCS/findandmodify+Command
Source
get-reference
function
Usage: (get-reference entity k)
Fetch the entity or entities referrenced by the given key.
Source
grab
function
Usage: (grab parent k & [refresh])
Analogous to clojure.core/get except that it will follow references.
References are cached in a :cache atom of the references metadata.
Takes an optional refresh flag to force it to fetch the reference.
Source
grab-in
function
Usage: (grab-in parent ks & refresh)
Analogous to clojure.core/get-in except that it will follow references.
References are cached in a :cache atom of the references metadata.
Takes an optional refresh flag to force it to fetch any references
along the path.
Source
group
function
Usage: (group type keys)
(group type keys cond initial reduce)
(group type keys cond initial reduce finalize)
Fetch a seq of grouped items.
Example:
SQL: select a,b,sum(c) csum from coll where active=1 group by a,b
Karras: (group MyType
[:a :b]
{:active 1}
{:csum 0}
"function(obj,prev) { prev.csum += obj.c; }")
Source
index
function
Usage: (index type & keys)
Associate an index with a give type.
Source
list-indexes
function
Usage: (list-indexes type)
Source
make
function
Usage: (make type hmap & [no-defaults])
Converts a hashmap to the supplied type.
Source
map-reduce
function
Usage: (map-reduce type mapfn reducefn & options)
See http://www.mongodb.org/display/DOCS/MapReduce
Source
map-reduce-fetch-all
var
Composes map-reduce and fetch-map-reduce-values and returns all the results.
If you need to filter the results use:
(fetch-map-reduce-values (map-reduce ...) ...your fetch options...
Source
relate
function
Usage: (relate parent k & vs)
Relates an entity to another entity as a reference. Returns the parent with
the reference associated. No-op if the target field is not a :reference or
:references. If the the key is a list it will add the reference, otherwise it
will set the reference. References are automatically saved if they haven't
been already. References are stored on the entity as:
{:_db "db-name" :_id ObjectId :_ref "collection-name")}
Source
save
function
Usage: (save entity)
(save entity & entities)
Inserts or updates one or more entities.
Source
set-reference
function
Usage: (set-reference entity k v)
Set an :_id to the key of the given entity
Source
swap-entity-spec-in!
function
Usage: (swap-entity-spec-in! type attribute-path f & args)
Combines swap! and assoc-in to modify the attribute of a entity-spec.
Source
update
function
Usage: (update type criteria modifiers & options)
Updates one or more documents in a collection that match the criteria with the document
provided.
:upsert, performs an insert if the document doesn't have :_id
:multi, update all documents that match the criteria
If working with an instance of an entity, use the save function.
Source
update-all
function
Usage: (update-all type obj)
(update-all type criteria obj)
Shortcut for (update type criteria obj :multi)
Source
karras.entity.testing
entity-fixture
function
Usage: (entity-fixture db)
creates a test fixture that wraps tests in a with-mongo-request and drops
all the colllections for the known entities.
Source