An in-memory graph data store consisting of maps with links.
This is a very basic database that stores entities. An entity is a Clojure map from attributes to values.
An attribute is a keyword, usually namespace-qualified.
An entity has exactly one unique identity attribute whose value acts as a globally-unique identifier for that entity. An entity may not have more than one unique identity attribute; if it does, the behavior is undefined.
A lookup ref is a vector pair of a unique identity attribute and a
value, such as [:user/id 1234]
. The combination of a unique
identity attribute and its value uniquely identifies a single entity
in the database.
Create a new database with new-db
.
The schema of the database is the set of all unique identity
attributes. add-id-attr
extends the schema with a new unique
identity attribute.
add
inserts entities in the database. Entities already in the
database are updated as by clojure.core/merge
. (To replace an
entity without merging, dissoc
it first.)
All entites added to a database are automatically normalized: all nested entities are replaced with lookup refs to other entities in the database.
Entity normalization recognizes the following forms:
Collection values may not mix entities and non-entities. Collections
are searched only one layer deep: add
will not recursively walk
arbitrary data structures to search for entities to normalize.
Databases can be manipulated with primitive map operations such as
get
and dissoc
. Keep in mind that there are no indexes: If you
dissoc
an entity from the database you may leave behind broken
links to that entity.
To get nested maps back out, use pull
, which follows a pull
pattern to recursively expand entity lookup refs.
An in-memory graph data store consisting of maps with links. ## Introduction This is a very basic database that stores entities. An *entity* is a Clojure map from *attributes* to values. An *attribute* is a keyword, usually namespace-qualified. An entity has exactly one *unique identity* attribute whose value acts as a globally-unique identifier for that entity. An entity may not have more than one unique identity attribute; if it does, the behavior is undefined. A *lookup ref* is a vector pair of a unique identity attribute and a value, such as `[:user/id 1234]`. The combination of a unique identity attribute and its value uniquely identifies a single entity in the database. ## Usage Create a new database with `new-db`. The schema of the database is the set of all unique identity attributes. `add-id-attr` extends the schema with a new unique identity attribute. `add` inserts entities in the database. Entities already in the database are updated as by `clojure.core/merge`. (To replace an entity without merging, `dissoc` it first.) All entites added to a database are automatically *normalized*: all nested entities are replaced with lookup refs to other entities in the database. Entity normalization recognizes the following forms: - any non-entity value, including collections - a single entity - a collection (list, vector, set) of entities - a map where keys are any type, vals are single entities Collection values may not mix entities and non-entities. Collections are searched only one layer deep: `add` will not recursively walk arbitrary data structures to search for entities to normalize. Databases can be manipulated with primitive map operations such as `get` and `dissoc`. Keep in mind that there are no indexes: If you `dissoc` an entity from the database you may leave behind broken links to that entity. To get nested maps back out, use `pull`, which follows a pull pattern to recursively expand entity lookup refs.
(add db & entities)
Returns updated db with normalized entities merged in.
Returns updated db with normalized entities merged in.
(add-id-attr db & id-keys)
Adds unique identity attributes to the db schema. Returns updated db.
Adds unique identity attributes to the db schema. Returns updated db.
(db? x)
Returns true if x is a mapgraph database.
Returns true if x is a mapgraph database.
(entity? db map)
Returns true if map is an entity according to the db schema. An entity is a map from keywords to values with exactly one identifier key.
Returns true if map is an entity according to the db schema. An entity is a map from keywords to values with exactly one identifier key.
(expr-type expr)
Returns an expression type.
Returns an expression type.
(new-db)
Returns a new, empty database value.
Returns a new, empty database value.
(pull db lookup-ref)
(pull db lookup-ref pattern)
(pull db
lookup-ref
pattern
{:as options
:keys [parser db-ref? db-get-ref]
:or {parser parse-expr db-ref? ref? db-get-ref default-get-ref}})
Returns a map representation of the entity found at lookup ref in db. Builds nested maps following a pull pattern.
A pull pattern is a vector containing any of the following forms:
:key If the entity contains :key, includes it in the result.
'* (literal symbol asterisk) Includes all keys from the entity in the result.
{ :key sub-pattern } The entity's value for key is a lookup ref or collection of lookup refs. Expands each lookup ref to the entity it refers to, then applies pull to each of those entities using the sub-pattern.
Returns a map representation of the entity found at lookup ref in db. Builds nested maps following a pull pattern. A pull pattern is a vector containing any of the following forms: :key If the entity contains :key, includes it in the result. '* (literal symbol asterisk) Includes all keys from the entity in the result. { :key sub-pattern } The entity's value for key is a lookup ref or collection of lookup refs. Expands each lookup ref to the entity it refers to, then applies pull to each of those entities using the sub-pattern.
(ref-to db entity)
Returns a lookup ref for the entity using the schema in db, or nil if not found. The db does not need to contain the entity.
Returns a lookup ref for the entity using the schema in db, or nil if not found. The db does not need to contain the entity.
(ref? db ref)
Returns true if ref is a lookup ref according to the db schema.
Returns true if ref is a lookup ref according to the db schema.
cljdoc is a website building & hosting documentation for Clojure/Script libraries
× close