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.
delete Function(base/delete db "item-key")
The delete function does not return a meaningful value, as its purpose is to remove an item from the database. If the provided key is nil or empty, the function will throw an exception. Otherwise, it attempts to delete the item corresponding to the provided key.
The delete function throws an Exception if the project key is not provided or is invalid.
(base/delete db "user-123") ; Attempts to delete the item with the key "user-123"
insert Function(base/insert db {:a 1 :b 2} "item-key")
(base/insert db {:a 1 :b 2}) ; The key is automatically generated on the server
If the data passed to the insert 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 insert 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 insert function throws an Exception if an item with the provided key already exists (status code 409), or if the data type is not supported, or if the deta-key is invalid or not provided.
(base/insert db "Hello, World!") ; Automatically wrapped in {"value" "Hello, World!"}
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 delete function is useful for removing specific items from the database. Here's an example of how you can use this function to remove an item:
(base/delete db "item-key") ; Removes the item with the key "item-key"
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, set the DETA_KEY environment variable with a valid Deta key beforehand.
set DETA_KEY=your_deta_key_here
lein test
export DETA_KEY=your_deta_key_here
lein test
Replace your_deta_key_here with your actual Deta key.
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 |