Database is the root object of Watermelon. It owns:
- a map of
DatabaseAdapter connects Watermelon's reactive world to low-level imperative world of databases. See Adapters.
Collection manages all records of a given kind:
- it has a cache of records already fetched from the database (
- it has the public API to
- it implements fetch/update/delete operations on records
Model is an instance of a collection record. A model class describes a kind of a record.
Model is the base class for your concrete models (e.g.
- it describes the specific instance -
id+ all custom fields and actions
- it has public API to
- implements record-level observation
- static fields describe base information about a model (
associations) - See Defining models
As a general rule,
Model manages the state of a specific instance, and
Collection of the entire collection of records. So for example,
model.markAsDeleted() changes the local state of called record, but then delegates to its collection to notify collection observers and actually remove from the database
Query is a helper object that gives us a nice API to perform queries (
- created via
- encapsulates a
QueryDescriptionstructure which actually describes the query conditions
- fetch/observe methods actually delegate to
Collectionto perform database operations
Observables created by
observe/observeCountmethods so they can be reused and shared
Watermelon's objects and classes are meant to be as minimal as possible — only manage their own state and be an API for your app. Most logic should be stateless, and implemented as pure functions:
QueryDescription is a structure (object) describing the query, built using
Q.* helper functions
fieldObserver() implement query observation logic (See Observation.)
Model decorators transform simple class properties into Watermelon-aware record fields.
Much of Adapters' logic is implemented as pure functions too. See Adapters.