-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Currently supports connect, create, load, stage, commit!, and query w/ keyword compact IRIs & contexts both in requests and responses.
- Loading branch information
1 parent
6ae337f
commit f0d42bb
Showing
8 changed files
with
798 additions
and
186 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,64 @@ | ||
(ns fluree.db.util.validation | ||
(:require [malli.core :as m])) | ||
(:require [malli.core :as m] | ||
[malli.transform :as mt])) | ||
|
||
(def value? (complement coll?)) | ||
|
||
(defn decode-iri | ||
[v] | ||
(cond | ||
(qualified-keyword? v) (str (namespace v) ":" (name v)) | ||
(keyword? v) (name v) | ||
:else v)) | ||
|
||
(def registry | ||
(merge | ||
(m/base-schemas) | ||
(m/type-schemas) | ||
(m/comparator-schemas) | ||
(m/predicate-schemas) | ||
{::iri :string | ||
::val [:fn value?] | ||
::context [:orn | ||
[:sequence [:sequential [:orn | ||
[:string :string] | ||
[:map map?]]]] | ||
[:map map?]]})) | ||
(m/base-schemas) | ||
(m/type-schemas) | ||
(m/comparator-schemas) | ||
(m/predicate-schemas) | ||
{::iri [:string {:decode/fluree decode-iri}] | ||
::val [:fn value?] | ||
::string-key [:string {:decode/fluree name | ||
:encode/fluree keyword}] | ||
::context-map [:map-of ::iri ::iri] | ||
::context [:orn | ||
[:sequence [:sequential [:orn | ||
[:string :string] | ||
[:map ::context-map]]]] | ||
[:map ::context-map]] | ||
::context-key [:= {:decode/fluree #(if (= % :context) | ||
"@context" %)} | ||
"@context"] | ||
::did [:orn | ||
[:id :string] | ||
[:map [:and | ||
[:map-of ::string-key :any] | ||
[:map | ||
["id" :string] | ||
["public" :string] | ||
["private" :string]]]]] | ||
::connect-defaults [:map | ||
[:did {:optional true} ::did] | ||
[::m/default [:map-of {:max 1} ::context-key ::context]]] | ||
::connect-opts [:and | ||
[:map-of ::string-key :any] | ||
[:map | ||
["method" {:decode/fluree name} :string] | ||
["defaults" {:optional true} ::connect-defaults]]] | ||
::create-opts [:maybe | ||
[:and | ||
[:map-of ::string-key :any] | ||
[:map | ||
["defaults" {:optional true} | ||
[:map-of {:max 1} ::context-key ::context]]]]] | ||
::create-response [:map-of ::string-key :any]})) | ||
|
||
(def fluree-transformer | ||
(mt/transformer {:name :fluree})) | ||
|
||
(def coerce-connect-opts | ||
(m/coercer ::connect-opts fluree-transformer {:registry registry})) | ||
|
||
(def coerce-create-opts | ||
(m/coercer ::create-opts fluree-transformer {:registry registry})) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
(ns fluree.sdk.clojure | ||
(:require [fluree.db.json-ld.api :as api] | ||
[fluree.db.json-ld.transact :as ftx] | ||
[fluree.db.util.log :as log] | ||
[fluree.db.util.validation :as v] | ||
[fluree.db.query.fql.syntax :as fql] | ||
[fluree.db.query.fql.parse :as fqp] | ||
[fluree.json-ld :as json-ld]) | ||
(:refer-clojure :exclude [load])) | ||
|
||
(defn connect | ||
"Forms connection to ledger, enabling automatic pulls of new updates, event | ||
services, index service. | ||
Multiple connections to same endpoint will share underlying network connection. | ||
Options include: | ||
- :defaults - (optional) with any of the following values: | ||
- :did - (optional) DiD information to use, if storing blocks as verifiable | ||
credentials, or issuing queries against a permissioned database. | ||
- :context - (optional) Default @context map to use for ledgers formed with | ||
this connection." | ||
[opts] | ||
(let [opts* (v/coerce-connect-opts opts)] | ||
(log/debug "connect opts:" opts*) | ||
(api/connect opts*))) | ||
|
||
(defn create | ||
"Creates a new json-ld ledger. A connection (conn) must always be supplied. | ||
Ledger-alias (optional) is a friendly name that is used for: | ||
- When publishing to a naming service that allows multiple pointers for the | ||
same namespace (e.g. IPNS), this becomes a sub-directory off the namespace. | ||
For multiple directories deep, use '/' for a | ||
e.g. the ledgers movies/popular, books/authors, books/best-sellers could | ||
use the same IPNS id (in this example using IPNS DNSLink): | ||
fluree:ipns://my.dns.com/books/authors | ||
fluree:ipns://my.dns.com/books/best-sellers | ||
fluree:ipns://my.dns.com/movies/top-rated | ||
- When combining multiple ledgers, each ledger becomes an individual named | ||
graph which can be referenced by name. | ||
Options map (opts) can include: | ||
- :defaults | ||
- :did - DiD information to use, if storing blocks as verifiable credentials | ||
- :context - Default @context map to use for ledgers formed with this connection" | ||
([conn] (create conn nil nil)) | ||
([conn ledger-alias] (create conn ledger-alias nil)) | ||
([conn ledger-alias opts] | ||
(let [opts* (v/coerce-create-opts opts)] | ||
(api/create conn ledger-alias opts*)))) | ||
|
||
(defn load | ||
"Loads an existing ledger by its alias (which will be converted to a | ||
connection-specific address first)." | ||
[conn ledger-alias] | ||
(api/load conn ledger-alias)) | ||
|
||
(defn exists? | ||
"Returns a promise with true if the ledger alias or address exists, false | ||
otherwise." | ||
[conn ledger-alias-or-address] | ||
(api/exists? conn ledger-alias-or-address)) | ||
|
||
(defn stage | ||
"Performs a transaction and queues change if valid (does not commit)" | ||
([db json-ld] (stage db json-ld nil)) | ||
([db json-ld opts] | ||
(let [json-ld* (ftx/coerce-txn json-ld)] | ||
(api/stage db json-ld* opts)))) | ||
|
||
(defn commit! | ||
([ledger db] (commit! ledger db nil)) | ||
([ledger db opts] (api/commit! ledger db opts))) | ||
|
||
(defn db | ||
"Retrieves latest db, or optionally a db at a moment in time | ||
and/or permissioned to a specific identity." | ||
([ledger] (db ledger nil)) | ||
([ledger opts] (api/db ledger opts))) | ||
|
||
(defn query | ||
[db query] | ||
(let [context (json-ld/parse-context (fqp/parse-context query db)) | ||
results-encoder (fql/analytical-query-results-encoder context)] | ||
(future | ||
(->> query | ||
fql/coerce-analytical-query | ||
(api/query db) | ||
deref | ||
(log/debug->>val "pre-encoded query results:") | ||
results-encoder)))) | ||
|
||
(comment | ||
;; TODO: Finish these | ||
(defn multi-query | ||
[db query] | ||
(api/multi-query db query)) | ||
|
||
(defn history | ||
[ledger query] | ||
(api/history ledger query))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.