LiaMongo is a simplified MongoDB key-value store designed to make MongoDB operations easier for casual developers. It provides a straightforward interface for storing and retrieving key-value pairs in MongoDB collections.
Many developers find MongoDB's document-oriented approach flexible but sometimes overwhelming, especially for simple key-value storage scenarios. LiaMongo aims to simplify this process by abstracting away much of the complexity of interacting with MongoDB.
LiaMongo is targeted towards developers who need a simple and intuitive way to store key-value data in MongoDB without dealing with the intricacies of the MongoDB driver or schema design.
The primary advantage of LiaMongo is its ease of use. By providing a straightforward API for common MongoDB operations, developers can quickly integrate LiaMongo into their projects without needing extensive knowledge of MongoDB internals.
While the key-value schema provides simplicity and flexibility, it may not be suitable for all use cases, especially those requiring complex querying or relationships between data. Additionally, the lack of schema validation for the value field may lead to inconsistencies in data storage.
npm install lia-mongo
const LiaMongo = require('lia-mongo');
Creates a new instance of LiaMongo.
-
options
: An object containing configuration options.-
uri
: MongoDB connection URI. -
collection
: Name of the MongoDB collection to use. -
isOwnHost
: (Optional) Boolean indicating whether the MongoDB instance is hosted on the same machine. Default isfalse
. -
ignoreError
: (Optional) Boolean indicating whether to ignore connection errors. Default isfalse
. -
allowClear
: (Optional) Boolean indicating whether clearing the collection is allowed. Default isfalse
.
-
A new instance of LiaMongo.
const liaMongo = new LiaMongo({
uri: "mongodb://localhost:27017/mydb",
collection: "myCollection",
isOwnHost: true,
});
Connects to the MongoDB database.
Promise.
await liaMongo.start();
Retrieves the value associated with the given key.
-
key
: The key to retrieve the value for.
Promise | null: The value associated with the key, or null if not found.
const value = await liaMongo.get("myKey");
Stores a key-value pair in the MongoDB collection.
-
key
: The key to store. -
value
: The value to store.
Promise.
await liaMongo.put("myKey", "myValue");
Removes the key-value pair with the given key from the MongoDB collection.
-
key
: The key to remove.
Promise.
await liaMongo.remove("myKey");
Checks if the given key exists in the MongoDB collection.
-
key
: The key to check.
Promise: True if the key exists, false otherwise.
const exists = await liaMongo.containsKey("myKey");
Gets the number of key-value pairs in the MongoDB collection.
Promise: The number of key-value pairs.
const size = await liaMongo.size();
Clears all key-value pairs from the MongoDB collection.
Promise.
await liaMongo.clear();
Retrieves an array of all keys in the MongoDB collection.
Promise<string[]>: An array of keys.
const keys = await liaMongo.keys();
Retrieves an array of all values in the MongoDB collection.
Promise<any[]>: An array of values.
const values = await liaMongo.values();
Retrieves an array of all key-value pairs in the MongoDB collection.
Promise<{ key: string, value: any }[]>: An array of key-value pairs.
const entries = await liaMongo.entries();
Loads all key-value pairs from the MongoDB collection into an object.
Promise: An object containing all key-value pairs.
const data = await liaMongo.load();
Pre-processes the given data.
-
data
: The data to pre-process.
Promise: The pre-processed data.
const preProcessedData = await liaMongo.preProc(data);
Converts all key-value pairs in the MongoDB collection into an object.
Promise: An object containing all key-value pairs.
const objectData = await liaMongo.toObject();
The schema used for storing key-value pairs in MongoDB.
LiaMongo.schema;
LiaMongo npm package created by Liane Cagara.