| Title: | Lightweight JSON-Based Database |
|---|---|
| Description: | The goal of 'rlowdb' is to provide a lightweight, file-based JSON database. Inspired by 'LowDB' in 'JavaScript', it generates an intuitive interface for storing, retrieving, updating, and querying structured data without requiring a full-fledged database system. Ideal for prototyping, small-scale applications, and lightweight data management needs. |
| Authors: | Mohamed El Fodil Ihaddaden [aut, cre], lowdb developers [ctb, cph] (developers of the lowdb package) |
| Maintainer: | Mohamed El Fodil Ihaddaden <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 0.2.0 |
| Built: | 2026-05-10 05:18:46 UTC |
| Source: | https://github.com/feddelegrand7/rlowdb |
The 'rlowdb' class provides a lightweight, JSON-based database solution for storing and managing structured data in R. It supports CRUD operations (Create, Read, Update, Delete) and enables querying with custom functions.
new()
Initialize the database, loading data from a JSON file. If the file does not exist, an empty database is created.
rlowdb$new( file_path, default_values = list(), auto_commit = TRUE, verbose = FALSE, pretty = FALSE )
file_pathThe path to the JSON file that stores the database.
default_valuesA list of named list with the format: list(collection_name = list(key_name_1 = value, key_name_2 = value, ..., key_name_n = value)) containing the default values that will be inserted each time the 'insert' method is called. Note that the default_values will not override the existing records. Default is an empty list ('list()').
auto_commitwhether to update the DB automatically each time there's an insertion, an update or a deletion. Defaults to TRUE.s Note that you can use the 'commit' method to update the DB manually.
verboseIf TRUE, will print informative messages to the console. Defaults to FALSE
prettyUse pretty = FALSE for compact JSON, which is more efficient for data transmission and storage. TRUE for a human readable format. Defaults to FALSE.
commit()
Update the DB with the operated changes.
rlowdb$commit()
get_data()
Retrieve all stored data.
rlowdb$get_data()
A list containing all database records.
db <- rlowdb$new("database.json")
db$insert("users", list(id = 1, name = "Alice"))
db$get_data()
unlink("database.json")
get_data_collection()
Retrieve data from a specific collection.
rlowdb$get_data_collection(collection)
collectionThe name of the collection
A list containing a specific collection's records.
db <- rlowdb$new("database.json")
db$insert("users", list(id = 1, name = "Alice"))
db$get_data_collection("users")
unlink("database.json")
get_data_key()
Retrieve the records of a specific key within a collection
rlowdb$get_data_key(collection, key)
collectionThe name of the collection
keyThe key name
A vector/list containing a specific key's records.
db <- rlowdb$new("database.json")
db$insert("users", list(id = 1, name = "Alice"))
db$insert("users", list(id = 2, name = "Omar"))
db$get_data_key("users", "name")
unlink("database.json")
insert()
Insert a new record into a specified collection.
rlowdb$insert(collection, record)
collectionThe collection name (a string).
recordA named list representing the record to insert.
db <- rlowdb$new("database.json", default_values = list(
"users" = list("active" = TRUE)
)
)
db$insert("users", list(id = 1, name = "Alice"))
unlink("database.json")
find()
Find records in a collection that match a given key-value pair.
rlowdb$find(collection, key, value)
collectionThe collection name (a string).
keyThe field name to search for.
valueThe value to match.
A list of matching records. Returns an empty list if no match is found.
db <- rlowdb$new("database.json")
db$insert("users", list(id = 1, name = "Alice"))
db$find("users", "id", 1)
unlink("database.json")
update()
Update existing records in a collection.
rlowdb$update(collection, key, value, new_data)
collectionThe collection name.
keyThe field name to search for.
valueThe value to match.
new_dataA named list containing the updated data.
db <- rlowdb$new("database.json")
db$insert("users", list(id = 1, name = "Alice"))
db$update("users", "id", 1, list(name = "Alice Updated"))
unlink("database.json")
upsert()
If a record exists, update it; otherwise, insert a new record. Note that in order to use the method, the 'collection' has to exist
rlowdb$upsert(collection, key, value, new_data)
collectionThe collection name.
keyThe field name to search for.
valueThe value to match.
new_dataA named list containing the updated data.
db <- rlowdb$new("database.json")
db$insert("users", list(id = 100, name = "Coconut"))
db$upsert("users", "id", 1, list(name = "Alice Updated"))
unlink("database.json")
delete()
Delete records from a collection that match a given key-value pair.
rlowdb$delete(collection, key, value)
collectionThe collection name.
keyThe field name to search for.
valueThe value to match.
db <- rlowdb$new("database.json")
db$insert("users", list(id = 1, name = "Alice"))
db$delete("users", "id", 1)
db$get_data()
unlink("database.json")
query()
Query a collection using a condition string. This function allows filtering records from a collection using a condition string that is evaluated dynamically. The condition supports multiple logical expressions using standard R operators (e.g., '>', '<', '==', '&', '|').
rlowdb$query(collection, condition = NULL)
collectionThe collection name (a string).
conditionA string representing a logical condition for filtering records. - Supports comparisons ('>', '<', '>=', '<=', '==', '!='). - Allows logical operators ('&' for AND, '|' for OR). - Example: '"views > 200 & id > 2"'. - If 'NULL' or an empty string ('""'), returns all records.
A list of records that satisfy the condition. If no records match, returns an empty list.
db <- rlowdb$new("database.json")
db$insert("posts", list(id = 1, title = "LowDB in R", views = 100))
db$insert("posts", list(id = 2, title = "Data Management", views = 250))
db$insert("posts", list(id = 3, title = "Advanced R", views = 300))
# Query posts with views > 200 AND id > 2
db$query("posts", "views > 200 & id > 2")
# Query posts with views > 100 OR id == 1
db$query("posts", "views > 100 | id == 1")
# Query all posts (no condition)
db$query("posts", "")
unlink("database.json")
filter()
Filter Records Using a Custom Function This method applies a user-defined function to filter records in a specified collection. The function should take a record as input and return 'TRUE' for records that should be included in the result and 'FALSE' for records that should be excluded.
rlowdb$filter(collection, filter_fn)
collectionA character string specifying the name of the collection.
filter_fnA function that takes a record (a list) as input and returns 'TRUE' or 'FALSE'.
A list of records that satisfy the filtering condition.
db <- rlowdb$new("database.json")
db$insert("users", list(name = "Delta", age = 25))
db$insert("users", list(name = "Gamma", age = 36))
# Find users older than 30
db$filter("users", function(record) record$age > 30)
unlink("database.json")
drop()
Just like DROP TABLE in SQL, drops a complete collection.
rlowdb$drop(collection)
collectionThe collection name.
db <- rlowdb$new("database.json")
db$insert("users", list(name = "Delta", age = 25))
db$drop("users")
db$get_data()
unlink("database.json")
drop_all()
Drop all the collections available in your JSON file DB
rlowdb$drop_all()
db <- rlowdb$new("database.json")
db$insert("users", list(name = "Delta", age = 25))
db$insert("consumers", list(name = "Teta", age = 22))
db$drop_all()
db$get_data()
unlink("database.json")
clear()
Removes all records from a collection without deleting the collection itself
rlowdb$clear(collection)
collectionthe collection name
db <- rlowdb$new("database.json")
db$insert("users", list(name = "Delta", age = 25))
db$insert("consumers", list(name = "Teta", age = 22))
db$clear("users")
db$get_data()
unlink("database.json")
count()
Count the number of records in a collection
rlowdb$count(collection)
collectionthe collection name
numeric
db <- rlowdb$new("database.json")
db$insert("users", list(name = "Delta", age = 25))
db$insert("users", list(name = "Gamma", age = 36))
db$count("users")
unlink("database.json")
list_collections()
List the available collections
rlowdb$list_collections()
character
db <- rlowdb$new("database.json")
db$insert("users", list(name = "Delta", age = 25))
db$insert("consumers", list(name = "Teta", age = 22))
db$list_collections()
unlink("database.json")
exists_collection()
Check if a collection exists.
rlowdb$exists_collection(collection)
collectionThe collection name
TRUE if the collection exists, FALSE otherwise
db <- rlowdb$new("database.json")
db$insert("users", list(name = "Delta", age = 25))
db$insert("consumers", list(name = "Teta", age = 22))
db$exists_collection("users")
unlink("database.json")
exists_key()
Check if a key exists within a specific collection.
rlowdb$exists_key(collection, key)
collectionThe collection name
keyThe key name
TRUE if the key exists, FALSE otherwise
db <- rlowdb$new("database.json")
db$insert("users", list(name = "Delta", age = 25))
db$insert("consumers", list(name = "Teta", age = 22))
db$exists_key("users", "name")
unlink("database.json")
exists_value()
Check if a value exists within a specific collection/key combination.
rlowdb$exists_value(collection, key, value)
collectionThe collection name
keyThe key name
valueThe value to look for
TRUE if the value exists, FALSE otherwise
db <- rlowdb$new("database.json")
db$insert("users", list(name = "Delta", age = 25))
db$insert("consumers", list(name = "Teta", age = 22))
db$exists_value("users", "name", "Delta")
unlink("database.json")
transaction()
Perform a Transaction with Rollback on Failure
This method executes a sequence of operations as a transaction. If any operation fails, it rolls back all changes to maintain data integrity.
rlowdb$transaction(transaction_fn)
transaction_fnA function that performs operations on 'self'. It should not return a value.
db <- rlowdb$new("database.json")
db$insert("users", list(name = "Delta", age = 25))
db$count("users")
db$transaction(function() {
db$insert("users", list(name = "Zlatan", age = 40))
db$insert("users", list(name = "Neymar", age = 28))
# if an error is raised, a rollback will happen and
# the records won't be inserted
})
db$count("users")
unlink("database.json")
restore()
Load a JSON backup and replace the current database.
rlowdb$restore(backup_path)
backup_pathThe path of the backup JSON file. Allow users to quickly backup their database.
backup()
rlowdb$backup(backup_path)
backup_pathThe path of the backup JSON file
search()
Search Records in a Collection
This method searches for records in a collection where a specified key's value contains a given search term.
rlowdb$search(collection, key, term, ignore.case = FALSE)
collectionA character string specifying the name of the collection.
keyA character string specifying the field to search within.
termA character string specifying the term to search for.
ignore.caseA logical value indicating whether the search should be case-insensitive (default: 'FALSE').
A list of matching records. Returns an empty list if no matches are found.
db <- rlowdb$new("database.json")
db$insert("users", list(id = 1, name = "Alice"))
db$insert("users", list(id = 2, name = "Bob"))
db$insert("users", list(id = 3, name = "alice"))
# Case-sensitive search
db$search("users", "name", "Alice", ignore.case = FALSE)
# Case-insensitive search
db$search("users", "name", "alice", ignore.case = TRUE)
unlink("database.json")
bulk_insert()
Insert Multiple Records into a Collection
This method inserts multiple records into a specified collection at once. Each record should be a named list representing an entry in the collection.
rlowdb$bulk_insert(collection, records)
collectionA character string specifying the name of the collection.
recordsA list of named lists, where each named list represents a record to insert.
db <- rlowdb$new("database.json")
db$bulk_insert("users", list(
list(id = 1, name = "Alice", age = 25),
list(id = 2, name = "Bob", age = 32),
list(id = 3, name = "Charlie", age = 40)
))
db$count("users")
unlink("database.json")
status()
Provides some useful information about the database
rlowdb$status()
set_auto_commit()
Set the auto_commit value
rlowdb$set_auto_commit(auto_commit)
auto_commitTRUE will update automatically the JSON by each insertion/update/delete. If FALSE, you'll need to use the commit method whenever you want to commit your changes to the JSON DB.
set_verbose()
Set the verbose value.
rlowdb$set_verbose(verbose)
verboseIf TRUE, informative messages will be printed to the console.
rename_collection()
Allows you to rename an existing collection in the database. It checks if the specified collection exists before attempting to rename it.
rlowdb$rename_collection(collection_name, new_collection_name)
collection_nameA character string representing the current name of the collection to be renamed.
new_collection_nameA character string representing the new name for the collection.
db <- rlowdb$new("database.json")
db$bulk_insert("users", list(
list(id = 1, name = "Alice", age = 25),
list(id = 2, name = "Bob", age = 32),
list(id = 3, name = "Charlie", age = 40)
))
db$list_collections()
db$rename_collection("users", "customers")
db$list_collections()
unlink("database.json")
list_keys()
Retrieves the names (keys) of all keys within a given collection.
rlowdb$list_keys(collection)
collectionThe collection name
A character vector containing the unique keys (field names) present across all records in the collection.
db <- rlowdb$new("database.json")
db$bulk_insert("users", list(
list(id = 1, name = "Alice", age = 25),
list(id = 2, name = "Bob", age = 32),
list(id = 3, name = "Charlie")
))
db$list_keys("users")
unlink("database.json")
count_values()
Count Occurrences of a Key's Values in a Collection
rlowdb$count_values(collection, key)
collectionThe collection name
keyThe key name
A table (a frequency count) of the values associated with the specified key, showing the number of occurrences of each unique value. If the key does not exist, an error is thrown.
db <- rlowdb$new("database.json")
db$bulk_insert("users", list(
list(id = 1, name = "Alice", age = 25),
list(id = 2, name = "Bob", age = 32),
list(id = 3, name = NA),
list(id = 4, name = NA)
))
db$count_values("users", "name")
unlink("database.json")
insert_default_values()
Add Default Values to Records in a Collection
Ensures that all records in a collection have specific default values for certain keys. If a key is missing, the default value is added. Optionally, existing values can be replaced with defaults.
rlowdb$insert_default_values(collection, defaults, replace_existing = FALSE)
collectionThe collection name.
defaultsA named list of default values to add.
replace_existingLogical; if 'TRUE', replaces existing values with defaults. If 'FALSE', only adds missing keys. Defaults to 'FALSE'.
Updates the collection in place, ensuring consistency of data.
db <- rlowdb$new("database.json")
db$bulk_insert("users", list(
list(name = "Alice", age = 30),
list(name = "Bob"),
list(name = "Charlie", age = 25, role = "admin")
))
# Add defaults without replacing existing values
db$insert_default_values("users", list(role = "guest", active = TRUE))
# Add defaults and replace existing values
db$insert_default_values("users", list(role = "guest", active = TRUE), replace_existing = TRUE)
unlink("database.json")
clone_collection()
Clone an existing collection to a new collection with a different name. This creates an exact copy of the original collection's records under the new name.
rlowdb$clone_collection(from, to)
fromThe name of the source collection to clone.
toThe name of the new collection.
overwriteIf FALSE (default), will abort if target collection exists. If TRUE, will overwrite existing target collection.
db <- rlowdb$new("database.json")
db$insert("users", list(id = 1, name = "Alice"))
db$clone_collection("users", "users_backup")
db$list_collections()
unlink("database.json")
sample_records()
Randomly sample records from a collection
rlowdb$sample_records(collection, n = 1, replace = FALSE, seed = NULL)
collectionThe name of the collection to sample from
nNumber of records to sample. If n > collection size, returns all records with a warning.
replaceShould sampling be with replacement? Default FALSE
seedOptional random seed for reproducible sampling
A list of sampled records
db <- rlowdb$new("database.json")
db$insert("users", list(id = 1, name = "Alice"))
db$insert("users", list(id = 2, name = "Bob"))
db$insert("users", list(id = 3, name = "Charlie"))
# Sample 2 records without replacement
db$sample_records("users", n = 2)
# Sample with replacement
db$sample_records("users", n = 5, replace = TRUE)
# Reproducible sampling with seed
db$sample_records("users", n = 2, seed = 123)
unlink("database.json")
set_schema()
Set schema for a collection to validate future inserts/updates
rlowdb$set_schema(collection, schema)
collectionThe collection name
schemaA named list where: - Names are field names - Values can be: * A type string ("character", "numeric", etc.) * A function that returns TRUE/FALSE * A vector of allowed values * NULL to make the field optional
db <- rlowdb$new("database.json")
# Define schema before inserting
db$set_schema("users", list(
id = "numeric",
name = function(x) is.character(x) && nchar(x) > 0,
age = function(x) is.numeric(x) && x >= 0,
email = NULL # Optional
))
# This will fail validation:
try(db$insert("users", list(id = "1", name = "")))
get_schema()
Retrieve the schema for a specific collection
rlowdb$get_schema(collection)
collectionThe collection name
list
reload()
Refreshes the in-memory database by re-reading the current state from the JSON file
be aware that if auto_commit is set to FALSE, when reloading, you will lose
the modifications operated on your DB.
rlowdb$reload()
rlowdb object invisibly for chaining
clone()
The objects of this class are cloneable with this method.
rlowdb$clone(deep = FALSE)
deepWhether to make a deep clone.
## ------------------------------------------------ ## Method `rlowdb$get_data` ## ------------------------------------------------ db <- rlowdb$new("database.json") db$insert("users", list(id = 1, name = "Alice")) db$get_data() unlink("database.json") ## ------------------------------------------------ ## Method `rlowdb$get_data_collection` ## ------------------------------------------------ db <- rlowdb$new("database.json") db$insert("users", list(id = 1, name = "Alice")) db$get_data_collection("users") unlink("database.json") ## ------------------------------------------------ ## Method `rlowdb$get_data_key` ## ------------------------------------------------ db <- rlowdb$new("database.json") db$insert("users", list(id = 1, name = "Alice")) db$insert("users", list(id = 2, name = "Omar")) db$get_data_key("users", "name") unlink("database.json") ## ------------------------------------------------ ## Method `rlowdb$insert` ## ------------------------------------------------ db <- rlowdb$new("database.json", default_values = list( "users" = list("active" = TRUE) ) ) db$insert("users", list(id = 1, name = "Alice")) unlink("database.json") ## ------------------------------------------------ ## Method `rlowdb$find` ## ------------------------------------------------ db <- rlowdb$new("database.json") db$insert("users", list(id = 1, name = "Alice")) db$find("users", "id", 1) unlink("database.json") ## ------------------------------------------------ ## Method `rlowdb$update` ## ------------------------------------------------ db <- rlowdb$new("database.json") db$insert("users", list(id = 1, name = "Alice")) db$update("users", "id", 1, list(name = "Alice Updated")) unlink("database.json") ## ------------------------------------------------ ## Method `rlowdb$upsert` ## ------------------------------------------------ db <- rlowdb$new("database.json") db$insert("users", list(id = 100, name = "Coconut")) db$upsert("users", "id", 1, list(name = "Alice Updated")) unlink("database.json") ## ------------------------------------------------ ## Method `rlowdb$delete` ## ------------------------------------------------ db <- rlowdb$new("database.json") db$insert("users", list(id = 1, name = "Alice")) db$delete("users", "id", 1) db$get_data() unlink("database.json") ## ------------------------------------------------ ## Method `rlowdb$query` ## ------------------------------------------------ db <- rlowdb$new("database.json") db$insert("posts", list(id = 1, title = "LowDB in R", views = 100)) db$insert("posts", list(id = 2, title = "Data Management", views = 250)) db$insert("posts", list(id = 3, title = "Advanced R", views = 300)) # Query posts with views > 200 AND id > 2 db$query("posts", "views > 200 & id > 2") # Query posts with views > 100 OR id == 1 db$query("posts", "views > 100 | id == 1") # Query all posts (no condition) db$query("posts", "") unlink("database.json") ## ------------------------------------------------ ## Method `rlowdb$filter` ## ------------------------------------------------ db <- rlowdb$new("database.json") db$insert("users", list(name = "Delta", age = 25)) db$insert("users", list(name = "Gamma", age = 36)) # Find users older than 30 db$filter("users", function(record) record$age > 30) unlink("database.json") ## ------------------------------------------------ ## Method `rlowdb$drop` ## ------------------------------------------------ db <- rlowdb$new("database.json") db$insert("users", list(name = "Delta", age = 25)) db$drop("users") db$get_data() unlink("database.json") ## ------------------------------------------------ ## Method `rlowdb$drop_all` ## ------------------------------------------------ db <- rlowdb$new("database.json") db$insert("users", list(name = "Delta", age = 25)) db$insert("consumers", list(name = "Teta", age = 22)) db$drop_all() db$get_data() unlink("database.json") ## ------------------------------------------------ ## Method `rlowdb$clear` ## ------------------------------------------------ db <- rlowdb$new("database.json") db$insert("users", list(name = "Delta", age = 25)) db$insert("consumers", list(name = "Teta", age = 22)) db$clear("users") db$get_data() unlink("database.json") ## ------------------------------------------------ ## Method `rlowdb$count` ## ------------------------------------------------ db <- rlowdb$new("database.json") db$insert("users", list(name = "Delta", age = 25)) db$insert("users", list(name = "Gamma", age = 36)) db$count("users") unlink("database.json") ## ------------------------------------------------ ## Method `rlowdb$list_collections` ## ------------------------------------------------ db <- rlowdb$new("database.json") db$insert("users", list(name = "Delta", age = 25)) db$insert("consumers", list(name = "Teta", age = 22)) db$list_collections() unlink("database.json") ## ------------------------------------------------ ## Method `rlowdb$exists_collection` ## ------------------------------------------------ db <- rlowdb$new("database.json") db$insert("users", list(name = "Delta", age = 25)) db$insert("consumers", list(name = "Teta", age = 22)) db$exists_collection("users") unlink("database.json") ## ------------------------------------------------ ## Method `rlowdb$exists_key` ## ------------------------------------------------ db <- rlowdb$new("database.json") db$insert("users", list(name = "Delta", age = 25)) db$insert("consumers", list(name = "Teta", age = 22)) db$exists_key("users", "name") unlink("database.json") ## ------------------------------------------------ ## Method `rlowdb$exists_value` ## ------------------------------------------------ db <- rlowdb$new("database.json") db$insert("users", list(name = "Delta", age = 25)) db$insert("consumers", list(name = "Teta", age = 22)) db$exists_value("users", "name", "Delta") unlink("database.json") ## ------------------------------------------------ ## Method `rlowdb$transaction` ## ------------------------------------------------ db <- rlowdb$new("database.json") db$insert("users", list(name = "Delta", age = 25)) db$count("users") db$transaction(function() { db$insert("users", list(name = "Zlatan", age = 40)) db$insert("users", list(name = "Neymar", age = 28)) # if an error is raised, a rollback will happen and # the records won't be inserted }) db$count("users") unlink("database.json") ## ------------------------------------------------ ## Method `rlowdb$search` ## ------------------------------------------------ db <- rlowdb$new("database.json") db$insert("users", list(id = 1, name = "Alice")) db$insert("users", list(id = 2, name = "Bob")) db$insert("users", list(id = 3, name = "alice")) # Case-sensitive search db$search("users", "name", "Alice", ignore.case = FALSE) # Case-insensitive search db$search("users", "name", "alice", ignore.case = TRUE) unlink("database.json") ## ------------------------------------------------ ## Method `rlowdb$bulk_insert` ## ------------------------------------------------ db <- rlowdb$new("database.json") db$bulk_insert("users", list( list(id = 1, name = "Alice", age = 25), list(id = 2, name = "Bob", age = 32), list(id = 3, name = "Charlie", age = 40) )) db$count("users") unlink("database.json") ## ------------------------------------------------ ## Method `rlowdb$rename_collection` ## ------------------------------------------------ db <- rlowdb$new("database.json") db$bulk_insert("users", list( list(id = 1, name = "Alice", age = 25), list(id = 2, name = "Bob", age = 32), list(id = 3, name = "Charlie", age = 40) )) db$list_collections() db$rename_collection("users", "customers") db$list_collections() unlink("database.json") ## ------------------------------------------------ ## Method `rlowdb$list_keys` ## ------------------------------------------------ db <- rlowdb$new("database.json") db$bulk_insert("users", list( list(id = 1, name = "Alice", age = 25), list(id = 2, name = "Bob", age = 32), list(id = 3, name = "Charlie") )) db$list_keys("users") unlink("database.json") ## ------------------------------------------------ ## Method `rlowdb$count_values` ## ------------------------------------------------ db <- rlowdb$new("database.json") db$bulk_insert("users", list( list(id = 1, name = "Alice", age = 25), list(id = 2, name = "Bob", age = 32), list(id = 3, name = NA), list(id = 4, name = NA) )) db$count_values("users", "name") unlink("database.json") ## ------------------------------------------------ ## Method `rlowdb$insert_default_values` ## ------------------------------------------------ db <- rlowdb$new("database.json") db$bulk_insert("users", list( list(name = "Alice", age = 30), list(name = "Bob"), list(name = "Charlie", age = 25, role = "admin") )) # Add defaults without replacing existing values db$insert_default_values("users", list(role = "guest", active = TRUE)) # Add defaults and replace existing values db$insert_default_values("users", list(role = "guest", active = TRUE), replace_existing = TRUE) unlink("database.json") ## ------------------------------------------------ ## Method `rlowdb$clone_collection` ## ------------------------------------------------ db <- rlowdb$new("database.json") db$insert("users", list(id = 1, name = "Alice")) db$clone_collection("users", "users_backup") db$list_collections() unlink("database.json") ## ------------------------------------------------ ## Method `rlowdb$sample_records` ## ------------------------------------------------ db <- rlowdb$new("database.json") db$insert("users", list(id = 1, name = "Alice")) db$insert("users", list(id = 2, name = "Bob")) db$insert("users", list(id = 3, name = "Charlie")) # Sample 2 records without replacement db$sample_records("users", n = 2) # Sample with replacement db$sample_records("users", n = 5, replace = TRUE) # Reproducible sampling with seed db$sample_records("users", n = 2, seed = 123) unlink("database.json") ## ------------------------------------------------ ## Method `rlowdb$set_schema` ## ------------------------------------------------ db <- rlowdb$new("database.json") # Define schema before inserting db$set_schema("users", list( id = "numeric", name = function(x) is.character(x) && nchar(x) > 0, age = function(x) is.numeric(x) && x >= 0, email = NULL # Optional )) # This will fail validation: try(db$insert("users", list(id = "1", name = "")))## ------------------------------------------------ ## Method `rlowdb$get_data` ## ------------------------------------------------ db <- rlowdb$new("database.json") db$insert("users", list(id = 1, name = "Alice")) db$get_data() unlink("database.json") ## ------------------------------------------------ ## Method `rlowdb$get_data_collection` ## ------------------------------------------------ db <- rlowdb$new("database.json") db$insert("users", list(id = 1, name = "Alice")) db$get_data_collection("users") unlink("database.json") ## ------------------------------------------------ ## Method `rlowdb$get_data_key` ## ------------------------------------------------ db <- rlowdb$new("database.json") db$insert("users", list(id = 1, name = "Alice")) db$insert("users", list(id = 2, name = "Omar")) db$get_data_key("users", "name") unlink("database.json") ## ------------------------------------------------ ## Method `rlowdb$insert` ## ------------------------------------------------ db <- rlowdb$new("database.json", default_values = list( "users" = list("active" = TRUE) ) ) db$insert("users", list(id = 1, name = "Alice")) unlink("database.json") ## ------------------------------------------------ ## Method `rlowdb$find` ## ------------------------------------------------ db <- rlowdb$new("database.json") db$insert("users", list(id = 1, name = "Alice")) db$find("users", "id", 1) unlink("database.json") ## ------------------------------------------------ ## Method `rlowdb$update` ## ------------------------------------------------ db <- rlowdb$new("database.json") db$insert("users", list(id = 1, name = "Alice")) db$update("users", "id", 1, list(name = "Alice Updated")) unlink("database.json") ## ------------------------------------------------ ## Method `rlowdb$upsert` ## ------------------------------------------------ db <- rlowdb$new("database.json") db$insert("users", list(id = 100, name = "Coconut")) db$upsert("users", "id", 1, list(name = "Alice Updated")) unlink("database.json") ## ------------------------------------------------ ## Method `rlowdb$delete` ## ------------------------------------------------ db <- rlowdb$new("database.json") db$insert("users", list(id = 1, name = "Alice")) db$delete("users", "id", 1) db$get_data() unlink("database.json") ## ------------------------------------------------ ## Method `rlowdb$query` ## ------------------------------------------------ db <- rlowdb$new("database.json") db$insert("posts", list(id = 1, title = "LowDB in R", views = 100)) db$insert("posts", list(id = 2, title = "Data Management", views = 250)) db$insert("posts", list(id = 3, title = "Advanced R", views = 300)) # Query posts with views > 200 AND id > 2 db$query("posts", "views > 200 & id > 2") # Query posts with views > 100 OR id == 1 db$query("posts", "views > 100 | id == 1") # Query all posts (no condition) db$query("posts", "") unlink("database.json") ## ------------------------------------------------ ## Method `rlowdb$filter` ## ------------------------------------------------ db <- rlowdb$new("database.json") db$insert("users", list(name = "Delta", age = 25)) db$insert("users", list(name = "Gamma", age = 36)) # Find users older than 30 db$filter("users", function(record) record$age > 30) unlink("database.json") ## ------------------------------------------------ ## Method `rlowdb$drop` ## ------------------------------------------------ db <- rlowdb$new("database.json") db$insert("users", list(name = "Delta", age = 25)) db$drop("users") db$get_data() unlink("database.json") ## ------------------------------------------------ ## Method `rlowdb$drop_all` ## ------------------------------------------------ db <- rlowdb$new("database.json") db$insert("users", list(name = "Delta", age = 25)) db$insert("consumers", list(name = "Teta", age = 22)) db$drop_all() db$get_data() unlink("database.json") ## ------------------------------------------------ ## Method `rlowdb$clear` ## ------------------------------------------------ db <- rlowdb$new("database.json") db$insert("users", list(name = "Delta", age = 25)) db$insert("consumers", list(name = "Teta", age = 22)) db$clear("users") db$get_data() unlink("database.json") ## ------------------------------------------------ ## Method `rlowdb$count` ## ------------------------------------------------ db <- rlowdb$new("database.json") db$insert("users", list(name = "Delta", age = 25)) db$insert("users", list(name = "Gamma", age = 36)) db$count("users") unlink("database.json") ## ------------------------------------------------ ## Method `rlowdb$list_collections` ## ------------------------------------------------ db <- rlowdb$new("database.json") db$insert("users", list(name = "Delta", age = 25)) db$insert("consumers", list(name = "Teta", age = 22)) db$list_collections() unlink("database.json") ## ------------------------------------------------ ## Method `rlowdb$exists_collection` ## ------------------------------------------------ db <- rlowdb$new("database.json") db$insert("users", list(name = "Delta", age = 25)) db$insert("consumers", list(name = "Teta", age = 22)) db$exists_collection("users") unlink("database.json") ## ------------------------------------------------ ## Method `rlowdb$exists_key` ## ------------------------------------------------ db <- rlowdb$new("database.json") db$insert("users", list(name = "Delta", age = 25)) db$insert("consumers", list(name = "Teta", age = 22)) db$exists_key("users", "name") unlink("database.json") ## ------------------------------------------------ ## Method `rlowdb$exists_value` ## ------------------------------------------------ db <- rlowdb$new("database.json") db$insert("users", list(name = "Delta", age = 25)) db$insert("consumers", list(name = "Teta", age = 22)) db$exists_value("users", "name", "Delta") unlink("database.json") ## ------------------------------------------------ ## Method `rlowdb$transaction` ## ------------------------------------------------ db <- rlowdb$new("database.json") db$insert("users", list(name = "Delta", age = 25)) db$count("users") db$transaction(function() { db$insert("users", list(name = "Zlatan", age = 40)) db$insert("users", list(name = "Neymar", age = 28)) # if an error is raised, a rollback will happen and # the records won't be inserted }) db$count("users") unlink("database.json") ## ------------------------------------------------ ## Method `rlowdb$search` ## ------------------------------------------------ db <- rlowdb$new("database.json") db$insert("users", list(id = 1, name = "Alice")) db$insert("users", list(id = 2, name = "Bob")) db$insert("users", list(id = 3, name = "alice")) # Case-sensitive search db$search("users", "name", "Alice", ignore.case = FALSE) # Case-insensitive search db$search("users", "name", "alice", ignore.case = TRUE) unlink("database.json") ## ------------------------------------------------ ## Method `rlowdb$bulk_insert` ## ------------------------------------------------ db <- rlowdb$new("database.json") db$bulk_insert("users", list( list(id = 1, name = "Alice", age = 25), list(id = 2, name = "Bob", age = 32), list(id = 3, name = "Charlie", age = 40) )) db$count("users") unlink("database.json") ## ------------------------------------------------ ## Method `rlowdb$rename_collection` ## ------------------------------------------------ db <- rlowdb$new("database.json") db$bulk_insert("users", list( list(id = 1, name = "Alice", age = 25), list(id = 2, name = "Bob", age = 32), list(id = 3, name = "Charlie", age = 40) )) db$list_collections() db$rename_collection("users", "customers") db$list_collections() unlink("database.json") ## ------------------------------------------------ ## Method `rlowdb$list_keys` ## ------------------------------------------------ db <- rlowdb$new("database.json") db$bulk_insert("users", list( list(id = 1, name = "Alice", age = 25), list(id = 2, name = "Bob", age = 32), list(id = 3, name = "Charlie") )) db$list_keys("users") unlink("database.json") ## ------------------------------------------------ ## Method `rlowdb$count_values` ## ------------------------------------------------ db <- rlowdb$new("database.json") db$bulk_insert("users", list( list(id = 1, name = "Alice", age = 25), list(id = 2, name = "Bob", age = 32), list(id = 3, name = NA), list(id = 4, name = NA) )) db$count_values("users", "name") unlink("database.json") ## ------------------------------------------------ ## Method `rlowdb$insert_default_values` ## ------------------------------------------------ db <- rlowdb$new("database.json") db$bulk_insert("users", list( list(name = "Alice", age = 30), list(name = "Bob"), list(name = "Charlie", age = 25, role = "admin") )) # Add defaults without replacing existing values db$insert_default_values("users", list(role = "guest", active = TRUE)) # Add defaults and replace existing values db$insert_default_values("users", list(role = "guest", active = TRUE), replace_existing = TRUE) unlink("database.json") ## ------------------------------------------------ ## Method `rlowdb$clone_collection` ## ------------------------------------------------ db <- rlowdb$new("database.json") db$insert("users", list(id = 1, name = "Alice")) db$clone_collection("users", "users_backup") db$list_collections() unlink("database.json") ## ------------------------------------------------ ## Method `rlowdb$sample_records` ## ------------------------------------------------ db <- rlowdb$new("database.json") db$insert("users", list(id = 1, name = "Alice")) db$insert("users", list(id = 2, name = "Bob")) db$insert("users", list(id = 3, name = "Charlie")) # Sample 2 records without replacement db$sample_records("users", n = 2) # Sample with replacement db$sample_records("users", n = 5, replace = TRUE) # Reproducible sampling with seed db$sample_records("users", n = 2, seed = 123) unlink("database.json") ## ------------------------------------------------ ## Method `rlowdb$set_schema` ## ------------------------------------------------ db <- rlowdb$new("database.json") # Define schema before inserting db$set_schema("users", list( id = "numeric", name = function(x) is.character(x) && nchar(x) > 0, age = function(x) is.numeric(x) && x >= 0, email = NULL # Optional )) # This will fail validation: try(db$insert("users", list(id = "1", name = "")))