Welcome to AgileDB
AgileDB is a revolutionary modern database system designed for dynamic scaling, high performance, and ease of use. In this article, you will learn about the various APIs provided by AgileDB, along with practical code snippets to help you get started quickly.
Table of Contents
- Introduction
- Create Database
- Create Collection
- Insert Document
- Query Document
- Update Document
- Delete Document
- Aggregation
- Indexing
- Transactions
- App Example
Introduction
AgileDB is an advanced NoSQL database focusing on flexibility, scalability, and high performance. Designed for modern applications, AgileDB supports a wide range of operations and provides developers with a seamless experience to manage data models efficiently.
Create Database
const agileDB = require('agiledb'); async function createDatabase() { const db = await agileDB.createDatabase('myDatabase'); console.log('Database Created:', db.name); } createDatabase();
Create Collection
async function createCollection() { const db = await agileDB.connect('myDatabase'); const collection = await db.createCollection('myCollection'); console.log('Collection Created:', collection.name); } createCollection();
Insert Document
async function insertDocument() { const db = await agileDB.connect('myDatabase'); const collection = await db.collection('myCollection'); const result = await collection.insertOne({ name: 'John Doe', age: 30 }); console.log('Document Inserted:', result.insertedId); } insertDocument();
Query Document
async function queryDocument() { const db = await agileDB.connect('myDatabase'); const collection = await db.collection('myCollection'); const document = await collection.findOne({ name: 'John Doe' }); console.log('Document Found:', document); } queryDocument();
Update Document
async function updateDocument() { const db = await agileDB.connect('myDatabase'); const collection = await db.collection('myCollection'); const result = await collection.updateOne( { name: 'John Doe' }, { $set: { age: 31 } } ); console.log('Document Updated:', result.modifiedCount); } updateDocument();
Delete Document
async function deleteDocument() { const db = await agileDB.connect('myDatabase'); const collection = await db.collection('myCollection'); const result = await collection.deleteOne({ name: 'John Doe' }); console.log('Document Deleted:', result.deletedCount); } deleteDocument();
Aggregation
async function runAggregation() { const db = await agileDB.connect('myDatabase'); const collection = await db.collection('myCollection'); const result = await collection.aggregate([ { $match: { age: { $gt: 25 } } }, { $group: { _id: '$age', total: { $sum: 1 } } } ]); console.log('Aggregation Result:', result); } runAggregation();
Indexing
async function createIndex() { const db = await agileDB.connect('myDatabase'); const collection = await db.collection('myCollection'); const result = await collection.createIndex({ age: 1 }); console.log('Index Created:', result); } createIndex();
Transactions
async function runTransaction() { const db = await agileDB.connect('myDatabase'); const session = await db.startSession(); session.startTransaction(); try { const collection = await db.collection('myCollection'); await collection.insertOne({ name: 'Jane Doe', age: 28 }, { session }); await collection.updateOne({ name: 'John Doe' }, { $set: { age: 32 } }, { session }); await session.commitTransaction(); console.log('Transaction Committed'); } catch (error) { await session.abortTransaction(); console.error('Transaction Aborted:', error); } finally { session.endSession(); } } runTransaction();
App Example
Let’s integrate the aforementioned APIs into a simple application that manages user data.
const agileDB = require('agiledb'); async function manageUsers() { const db = await agileDB.connect('userManagementDB'); const users = await db.createCollection('users'); // Insert user const newUser = await users.insertOne({ name: 'Alice', age: 25 }); console.log('New User Inserted:', newUser.insertedId); // Query user const user = await users.findOne({ name: 'Alice' }); console.log('User Found:', user); // Update user await users.updateOne({ name: 'Alice' }, { $set: { age: 26 } }); console.log('User Updated'); // Create index await users.createIndex({ name: 1 }); console.log('Index Created'); // Delete user await users.deleteOne({ name: 'Alice' }); console.log('User Deleted'); // Run aggregation const aggregationResult = await users.aggregate([ { $match: { age: { $gte: 25 } } }, { $group: { _id: '$age', count: { $sum: 1 } } }, ]); console.log('Aggregation Result:', aggregationResult); // Using transactions const session = await db.startSession(); session.startTransaction(); try { await users.insertOne({ name: 'Bob', age: 30 }, { session }); await users.updateOne({ name: 'Bob' }, { $set: { age: 31 } }, { session }); await session.commitTransaction(); console.log('Transaction Committed'); } catch (error) { await session.abortTransaction(); console.error('Transaction Aborted:', error); } finally { session.endSession(); } } manageUsers().catch(console.error);
With these APIs, you can build a full-fledged application using AgileDB with minimal effort, ensuring scalability and performance along the way.
Hash: 6db721d935f26ccc17dc4fa0e42b10e19795c05ca9837561780d8cad605336d7