jsondb-client
v1.1.0
Published
const db = require('jsondb');
Downloads
26
Readme
Here’s the updated README with the correct database name jsondb-yu03
:
jsondb-client
jsondb-client
is a server-based JSON database package designed for easy and secure CRUD operations using API requests. The database can be hosted anywhere securely with limits and custom authentication. The UI for the client is under development using Electron.js.
Overview
jsondb-client
provides a simple and efficient way to interact with JSON databases similar to traditional MongoDB, but using the jsondb-yu03
server. This package allows you to manage your databases securely and with custom authentication, enabling you to host and manage your data effectively.
Note: The server-side operations are private and protected to prevent theft or unauthorized access. Ensure that your server configurations and credentials are kept secure to prevent any potential misuse.
Installation
Install via npm:
npm install jsondb-client
Usage
Import and Setup
const dbClient = require("jsondb-client");
const db = dbClient.setConnection({
server: 'https://jsondb-yu03.onrender.com',
username: 'testuser.com',
password: '370149ecaf812899',
database: 'jsondb-yu03'
});
const collection = db.Collection('fun');
Methods
create(data)
Creates a new document in the specified collection.
Example:
const result = await collection.create({ name: "John Doe", age: 30 });
console.log(result);
find(query)
Finds all documents matching the query.
Example:
const results = await collection.find({ age: { $gt: 20 } });
console.log(results);
findOne(query)
Finds a single document matching the query.
Example:
const result = await collection.findOne({ name: "John Doe" });
console.log(result);
findById(id)
Finds a document by its unique ID.
Example:
const result = await collection.findById("12345");
console.log(result);
save(data)
Creates or updates a document based on the provided data.
Example:
const result = await collection.save({ _id: "12345", name: "Jane Doe" });
console.log(result);
update(id, data)
Updates a document by its ID.
Example:
const result = await collection.update("12345", { age: 31 });
console.log(result);
updateOne(query, data)
Updates a single document matching the query.
Example:
const result = await collection.updateOne({ name: "John Doe" }, { age: 31 });
console.log(result);
updateMany(query, data)
Updates multiple documents matching the query.
Example:
const result = await collection.updateMany({ age: { $lt: 30 } }, { active: false });
console.log(result);
findByIdAndUpdate(id, data)
Finds a document by ID and updates it.
Example:
const result = await collection.findByIdAndUpdate("12345", { age: 31 });
console.log(result);
findOneAndUpdate(query, data)
Finds a single document matching the query and updates it.
Example:
const result = await collection.findOneAndUpdate({ name: "John Doe" }, { age: 31 });
console.log(result);
deleteOne(query)
Deletes a single document matching the query.
Example:
const result = await collection.deleteOne({ name: "John Doe" });
console.log(result);
deleteMany(query)
Deletes multiple documents matching the query.
Example:
const result = await collection.deleteMany({ age: { $lt: 30 } });
console.log(result);
findByIdAndDelete(id)
Finds a document by ID and deletes it.
Example:
const result = await collection.findByIdAndDelete("12345");
console.log(result);
findOneAndDelete(query)
Finds a single document matching the query and deletes it.
Example:
const result = await collection.findOneAndDelete({ name: "John Doe" });
console.log(result);
countDocuments(query)
Counts the number of documents matching the query.
Example:
const count = await collection.countDocuments({ active: true });
console.log(count);
distinct(field, query)
Finds distinct values for a specified field.
Example:
const values = await collection.distinct("age", { active: true });
console.log(values);
aggregate(pipeline)
Performs an aggregation operation.
Example:
const results = await collection.aggregate([{ $match: { active: true } }]);
console.log(results);
populate(path, query)
Populates a field by joining related data.
Example:
const result = await collection.populate("user", { active: true });
console.log(result);
exec(command, params)
Executes a custom command with parameters.
Example:
const result = await collection.exec("customCommand", { param1: "value1" });
console.log(result);
Additional Operations
Delete User
To delete a user, make a DELETE
request to the /delete-user
endpoint.
Request:
DELETE /delete-user
Content-Type: application/json
Body:
{
"username": "user_to_delete"
}
Response:
{
"message": "User deleted successfully"
}
Error Response:
{
"message": "User not found"
}
Delete a Collection
To delete a collection, make a DELETE
request to the /:dbName/:collectionName/delete-collection
endpoint.
Request:
DELETE /:dbName/:collectionName/delete-collection
URL Parameters:
dbName
: The name of the database.collectionName
: The name of the collection to delete.
Response:
{
"message": "Collection 'collectionName' deleted successfully"
}
Error Response:
{
"message": "Collection not found"
}
Delete a Database
To delete a database, make a DELETE
request to the /delete-db/:dbName
endpoint.
Request:
DELETE /delete-db/:dbName
URL Parameters:
dbName
: The name of the database to delete.
Response:
{
"message": "Database 'dbName' deleted successfully"
}
Error Response:
{
"message": "Database not found"
}
Authentication and Setup
Creating New Credentials
To create new credentials, use the following curl
command:
curl --location 'https://jsondb-yu03.onrender.com/register' \
--header 'Content-Type: application/json' \
--header 'Authorization: Basic bWFub2pnb3dkYWJyODkuY29tOmZmNzkwNTU1Zjk3YTZkNzc=' \
--data '{
"email": "testuser.com"
}'
Response:
{
"message": "User registered successfully",
"username": "testuser.com",
"password": "370149ecaf812899"
}
Creating a Database
To create a database, use the following curl
command:
curl --location 'https://jsondb-yu03.onrender.com/add-database' \
--header 'Authorization: Basic dGVzdHVzZXIuY29tOjM3MDE0OWVjYWY4MTI4OTk=' \
--header 'Content-Type: application/json' \
--data '{
"database":"jsondb-yu03"
}'
Response:
{
"message": "Database added successfully",
"databases": [
"jsondb-yu03"
]
}
Creating a Collection
To create a collection, use the following curl
command:
curl --location --request POST 'https://jsondb-yu03.onrender.com/jsondb-yu03/testcollection' \
--header 'Authorization: Basic dGVzdHVzZXIuY29tOjM3MDE0OWVjYWY4MTI4OTk=' \
--header 'Content-Type: application/json'
Response:
{
"message": "Collection created successfully",
"collection": "testcollection"
}
License
MIT : [email protected]
Feel free to make any additional adjustments or add more details as needed!