Liking cljdoc? Tell your friends :D

dgknght.json-file

Keep resources of one type, such as tasks, in a single JSON file.

A config is a map described by the ::config spec:

:path The file's path. It's created, along with any missing parent directories, the first time a resource is saved, and a missing file holds none

A component describes the type of resource stored:

:spec The spec a resource must satisfy to be saved :encode A function of a resource to JSON-ready data: a map with only the keys that are saved (see dgknght.json for dates and times) :decode A function of a map read back from JSON with string keys to a resource :noun What the resource is called in error messages, e.g. "task"

The file holds a JSON array of resources, one object each. A resource is identified by its :uid. Each change rewrites the whole file, replacing it atomically so that a reader never sees it half written. The file has no protection against several services or processes writing it at the same time, so callers serialize their changes and share one service per file.

Failures throw an ex-info whose data has a :type of:

::invalid-config The config map isn't a valid ::config ::invalid-resource The resource can't be saved: it doesn't satisfy the component's :spec, or it's being deleted without a :uid ::invalid-file The file isn't a JSON array of resources

Keep resources of one type, such as tasks, in a single JSON file.

A config is a map described by the `::config` spec:

  :path  The file's path. It's created, along with any missing parent
         directories, the first time a resource is saved, and a missing
         file holds none

A component describes the type of resource stored:

  :spec    The spec a resource must satisfy to be saved
  :encode  A function of a resource to JSON-ready data: a map with only
           the keys that are saved (see `dgknght.json` for dates and times)
  :decode  A function of a map read back from JSON with string keys to a
           resource
  :noun    What the resource is called in error messages, e.g. "task"

The file holds a JSON array of resources, one object each. A resource is
identified by its :uid. Each change rewrites the whole file, replacing it
atomically so that a reader never sees it half written. The file has no
protection against several services or processes writing it at the same
time, so callers serialize their changes and share one service per file.

Failures throw an ex-info whose data has a :type of:

  ::invalid-config    The config map isn't a valid ::config
  ::invalid-resource  The resource can't be saved: it doesn't satisfy the
                      component's :spec, or it's being deleted without a
                      :uid
  ::invalid-file      The file isn't a JSON array of resources
raw docstring

deleteclj

(delete config {:keys [noun] :as component} {:keys [uid] :as resource})

Deletes the resource with the resource's :uid, if there is one, and returns nil.

Deletes the resource with the resource's :uid, if there is one, and
returns nil.
raw docstring

fetchclj

(fetch config component uid)

Reads the resource with the :uid, or returns nil if there's none.

Reads the resource with the :uid, or returns nil if there's none.
raw docstring

fetch-allclj

(fetch-all {:keys [path]} {:keys [decode noun]})

Reads every resource in the file, or none when there's no file.

Reads every resource in the file, or none when there's no file.
raw docstring

saveclj

(save config {:keys [encode decode] :as component} resource)

Saves the resource, replacing the one with its :uid or adding it after the others when there's none, and returns it as saved. A resource without a :uid gets a new random UUID.

Saves the resource, replacing the one with its :uid or adding it after
the others when there's none, and returns it as saved. A resource without
a :uid gets a new random UUID.
raw docstring

validate-configclj

(validate-config config)

Throws an ::invalid-config unless the config is a valid ::config.

Throws an ::invalid-config unless the config is a valid ::config.
raw docstring

cljdoc builds & hosts documentation for Clojure/Script libraries

Keyboard shortcuts
Ctrl+kJump to recent docs
←Move to previous article
→Move to next article
Ctrl+/Jump to the search field
× close