# Datomic Notes
## Event sourcing definitions
* Event Sourcing
* Event sourcing is an architectural pattern in which entities do not track their internal state by means of direct serialization or object-relational mapping, but by reading and committing events to an event store.
* Event Store
* An event store is a type of database optimized for storage of events.
* CQRS
* Command query responsibility separation (CQRS) generalises CQS to message-driven and event-driven architectures: it applies the CQS principle by using separate Query and Command messages to retrieve and modify data, respectively
* CQS
* It states that every method should either be a command that performs an action, or a query that returns data to the caller, but not both. In other words, asking a question should not change the answer.[1] More formally, methods should return a value only if they are referentially transparent and hence possess no side effects.
# Datomic Data Model
* Datom
* [Datomic official Datom Video](https://docs.datomic.com/cloud/livetutorial/datoms.html)
* 5 tuple EAVTO [EntitiyId Attribute Value Transaction Op]
* ![](https://i.imgur.com/GThNZph.png)
* jane likes both broccoli and pizza at the time of tx 1008
* jane stops liking pizza at the time of tx 1148
* No more in place updates
* Datomic is an append-only database
* How git works:
* Do we ever change the source code and forget the past?
* In datomic terms: there is transaction
* transactions are entities, can hold arbitrary data
* Resources:
* https://www.youtube.com/watch?v=ZP-E2IgqKfA
* [Datomic Data Model](https://docs.datomic.com/cloud/whatis/data-model.html)
* Notation: EDN
* [Datomic official EDN Video](https://docs.datomic.com/cloud/livetutorial/datalog.html)
## Query Types
Datomic supports multiple paradigms for querying:
* Logical/relational-like (via Datalog, which is as expressive as SQL)
* [Datomic Official Video](https://docs.datomic.com/cloud/livetutorial/datalog.html)
* It is lazy, so we don't have N+1 select problem
* Navigational/graph-like (via the Entity API)
* GraphQL-like (via the Pull API)
## Synchronization and transactions
* https://docs.datomic.com/cloud/transactions/client-synchronization.html
* [Datomic - a scalable, immutable database system by Marek Lipert
](https://www.youtube.com/watch?v=xGrCsIiiTUs)
* multi-threaded RDBMS databases spend about 75% of their time doing coordination and only 25% of their time actually doing reads and writes
* http://nms.csail.mit.edu/~stavros/pubs/OLTP_sigmod08.pdf
* > Fundamentally, the data structures in Datomic allows multiple queries to run simultaneously, without synchronization on the database server, or between the clients that are running queries. This means that queries are trivial to scale horizontally - you just add another instance of your app, and it'll run more queries in isolation, with no effect on the performance of other queries. The only bottleneck here is fetching data from storage and into the client. You can put memcached between storage and your apps to help alleviate this.
* https://augustl.com/blog/2018/datomic_look_at_all_the_things_i_am_not_doing_cont/
## Identifying entities
There are three ways to specify an entity id:
* a temporary id for a new entity being added to the database
* an existing id for an entity that's already in the database
* an identifier for an entity that's already in the database
## Schemas
* Attributes
* Each Datomic database has a schema that describes the set of attributes that can be associated with entities. A schema only defines the characteristics of the attributes themselves. It does not define which attributes can be associated with which entities. Decisions about which attributes apply to which entities are made by an application.
* Schema attributes are defined using the same data model used for application data. That is, attributes are themselves entities with associated attributes. Datomic defines a set of built-in system attributes that are used to define new attributes.
* Required schema attributes
* `:db/ident`
* name of the attribute
* `:db/valueType`
* `:db.type/string` etc
* `:db/cardinality`
* `:db.cardinality/one`
* `:db.cardinality/many`
* Optional schema attributes
* `:db/unique`
* Common examples include email, account name, product code
* `:db.unique/value`
* only one entity can have a given value for this attribute
* `:db.unique/identity`
* same as `:db.unique/value`, but also allows for "upsert" which will update if it exists, or inserts if it doesn't exist in the db
* `:db.attr/preds`
* you can ensure the value of an attribute with one or more predicates that you supply
* `:db/index`
* `:db/fulltext`
* `:db/isComponent`
* The `Entity.touch` method is a convenience that will access and cache all the attributes of an entity. In addition, touch will also access and cache the attributes of any other entities reachable through an attribute whose schema definition includes `:db/isComponent` true.
## Features
* [Datomic cloud](https://www.datomic.com/)
## How to install and use
* [Cognitect dev-tools](https://cognitect.com/dev-tools)
* Cognitect dev-tools is a set of free tools that we've created for Clojure developers. Dev-tools includes
* Datomic dev-local - a library for developing and testing Datomic applications.
* REBL - a graphical, interactive tool for browsing Clojure data.
* `bin/run -m datomic.peer-server -h localhost -p 8998 -a myaccesskey,mysecret -d hello,datomic:mem://hello`
## Testing: Speculative Writes / Simulations
* Datomic has an in-memory implementation, which means your tests need little environmental setup.
* Datomic database values provide a `with()` operation, which gives a local view of the database as if some writes were applied, but without making those writes visible globally
* the ability to obtain a past version of the database (aka `db.asOf(t)`)
* ability to reproduce the circumstances of a bug in your local environment
## Resources
* Datomic Architecture by Rich Hickey
* [Datomic Architecture PDF](http://Users/baris.aydek/Downloads/GoToCopenhagen2012-RichHickey-WritingDatomicinClojure.pdf)
* [Presentation Video](https://www.youtube.com/watch?v=7Fi-UvrRpyI)
* [Transcrition](https://github.com/matthiasn/talk-transcripts/blob/master/Hickey_Rich/WritingDatomicInClojure.md)
* [What Datomic brings to businesses Article](https://medium.com/@val.vvalval/what-datomic-brings-to-businesses-e2238a568e1c)
* [Datomic: Event Sourcing without the hassle Article](https://vvvvalvalval.github.io/posts/2018-11-12-datomic-event-sourcing-without-the-hassle.html)
* Other related
* [Datascript and Datomic: Data Modeling for Heroes - Mark Bastian Video](https://www.youtube.com/watch?v=tV4pHW_WOrY)
## To Read
* [Official documentation - From the begginning to the end](https://docs.datomic.com/on-prem/overview/introduction.html)
* Specifics
* [Memory and Caching](https://docs.datomic.com/on-prem/overview/caching.html)
* [Glossary](https://docs.datomic.com/cloud/glossary.html)
* [Simulation Testing with Datomic PDF](http://code.ouroborus.net/fp-syd/past/2013/2013-08-Gamble-Datomic.pdf)
* !!! Dynamo DB