This documentation provides comprehensive guidance on how to use the deta library, a Clojure library designed to interact with the Deta Base, a NoSQL database service.
It covers installation, getting started, example usage, and testing. The documentation is structured to ensure users can easily navigate through the content and understand how to integrate and utilize the library in their projects.
This section provides an overview of the deta library and its purpose. It introduces the library as a tool for interacting with the Deta Base, a NoSQL database service, and outlines the benefits of using this library in Clojure projects.
Add the following to your project.clj dependencies:
[com.adaiasmagdiel/deta "0.0.1"]
Add the following to your deps.edn dependencies:
com.adaiasmagdiel/deta {:mvn/version "0.0.1"}
Add the following to your build.gradle dependencies:
implementation("com.adaiasmagdiel:deta:0.0.1")
Add the following to your pom.xml dependencies:
<dependency>
<groupId>com.adaiasmagdiel</groupId>
<artifactId>deta</artifactId>
<version>0.0.1</version>
</dependency>
To use the deta.base namespace, you need to include it in your project's dependencies and require it in your code. The namespace use clojure.string, clojure.data.json, and clj-http.client for its operations.
(ns your-namespace
(:require [deta.base :as base]))
(def db (base/base "a0abcyxzxsr_aSecretValue" "items"))
put Function(base/put db {:a 1 :b 2} "item-key")
(base/put db {:a 1 :b 2}) ; The key is automatically generated on the server
If the data passed to the put function is not a map, it is automatically wrapped in a map with the key "value". This allows for flexibility in the types of data that can be stored in the database.
The put function returns a map containing the inserted data. If a key is not provided, the server automatically generates one. The returned map includes the data and the generated key.
The put function throws an Exception if the data type is not supported or if the deta-key is invalid or not provided.
(base/put db "Hello, World!") ; Automatically wrapped in {"value" "Hello, World!"}
get Function(base/get db "item-key")
The get function returns a map containing the retrieved data if the key exists, or nil if the key does not exist or an error occurs.
The get function throws an Exception if the deta-key is invalid or not provided, or if the key is empty or not provided.
The put function's ability to accept any data type makes it versatile for different use cases. Here are examples of inserting various data types:
(base/put db {:name "John Doe" :age 30}) ; Inserting a map
(base/put db "Hello, World!") ; Inserting a string
(base/put db 42) ; Inserting an integer
(base/put db 3.14) ; Inserting a float
(base/put db true) ; Inserting a boolean
(base/put db nil) ; Inserting nil
To retrieve data from the database, you can use the get function. Here's an example of how to retrieve an item by its key:
(base/get db "user-123")
The deta.base-test namespace includes tests for both the base and put functions. These tests cover various scenarios, including valid and invalid deta-key inputs, different data types for the put function, and the behavior of the put function when a key is provided.
To run the tests, execute the following command in your project directory:
lein test
Can you improve this documentation?Edit on GitHub
cljdoc builds & hosts documentation for Clojure/Script libraries
| Ctrl+k | Jump to recent docs |
| ← | Move to previous article |
| → | Move to next article |
| Ctrl+/ | Jump to the search field |