New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

carabao-mongo

Package Overview
Dependencies
Maintainers
0
Versions
13
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

carabao-mongo

Reusable MongoDB utilities and query logic

  • 0.2.7
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
16
increased by14.29%
Maintainers
0
Weekly downloads
 
Created
Source

Carabao-Mongo

Carabao-Mongo is a powerful TypeScript library that simplifies MongoDB operations. It provides flexible and type-safe methods for querying, inserting, updating, and deleting data, all while leveraging MongoDB's advanced capabilities like aggregation pipelines.


Features

  • Type Safety: Strong TypeScript support ensures safer and more predictable code.
  • Flexible Querying: Supports advanced filtering, joins, and projections with intuitive APIs.
  • Utility Functions: Simplified methods for common MongoDB operations.
  • UUID Integration: Automatically manages UUIDs for documents.
  • Modular Design: Designed for scalability and reusability.

Installation

Install the library via NPM:

npm install carabao-mongo

Setup

Connect to the Database

import { connectDatabase, closeDatabase } from 'carabao-mongo'

const main = async () => {
  await connectDatabase('mongodb://localhost:27017/mydb')

  // Perform operations...

  await closeDatabase()
}

main().catch(console.error)

Usage

Access a Collection

import { getCollection } from 'carabao-mongo'

interface User {
  uuid?: string
  name: string
  email: string
  createdAt: Date
  status: 'active' | 'inactive'
}

const userCollection = await getCollection<User>('users')

Insert Data

Insert a Single Document
const userId = await userCollection.insertData(
  {
    name: 'John Doe',
    email: 'john.doe@example.com',
    createdAt: new Date(),
    status: 'active'
  },
  ['email'] // mark the email field as unique (inserting a data with an already existing email will raise an exception)
)
console.log('Inserted User ID:', userId)
Insert Multiple Documents
const userIds = await userCollection.insertMultipleData([
  { name: 'Alice', email: 'alice@example.com', createdAt: new Date(), status: 'active' },
  { name: 'Bob', email: 'bob@example.com', createdAt: new Date(), status: 'inactive' }
])
console.log('Inserted User IDs:', userIds)

Query Data

Find a Single Document
const user = await userCollection.findSingleData({
  where: { email: 'john.doe@example.com' }
})
console.log('User:', user)
Find Multiple Documents
const activeUsers = await userCollection.findMultipleData({
  where: { status: 'active' },
  select: ['name', 'email'],
  sort: { createdAt: 'desc' },
  limit: 10
})
console.log('Active Users:', activeUsers)

Update Data

const updatedCount = await userCollection.updateData(
  { status: 'inactive' }, // Filter
  { status: 'active' }    // Update
)
console.log('Updated Documents:', updatedCount)

Delete Data

const deletedCount = await userCollection.deleteData({
  status: 'inactive'
})
console.log('Deleted Documents:', deletedCount)

Joins

Example: Join with Another Collection
interface Project {
  uuid?: string
  name: string
  createdBy: string // UUID of the user who created the project
  members: string[] // UUIDs of users who are members of the project
}

const projectCollection = await getCollection<Project>('projects')

const projects = await projectCollection.findMultipleData({
  join: {
    createdBy: { collectionName: 'users', select: ['name', 'email'] },
    members: { collectionName: 'users', select: ['name', 'email'] }
  },
  where: { status: 'active' }
})

// projects will now contain the joined data instead of just UUIDs

console.log('Projects with User Info:', projects)

Advanced Features

Custom Query Conditions

Leverage MongoDB operators like $gte, $regex, and $in for advanced queries:

const recentUsers = await userCollection.findMultipleData({
  where: {
    createdAt: { $gte: new Date('2024-01-01') },
    name: { $regex: /john/i }
  }
})
console.log('Recent Users:', recentUsers)

Pagination

Use limit and skip for efficient pagination:

const users = await userCollection.findMultipleData({
  where: { status: 'active' },
  limit: 10,
  skip: 20
})
console.log('Paginated Users:', users)

Error Handling

All methods throw descriptive errors on failure. Use try...catch to handle them:

try {
  const user = await userCollection.findSingleData({ where: { email: 'notfound@example.com' } })
  console.log('User:', user)
} catch (error) {
  console.error('Error:', error)
}

Contributing

  1. Clone the repository:
   git clone https://github.com/TinyRabarioelina/carabao-mongo.git
  1. Install dependencies:
    npm install
    
  2. Run tests:
    npm test
    

License

Carabao-Mongo is licensed under the MIT License. See the LICENSE file for more details.


Feedback

If you have any feedback, feature requests, or encounter issues, please open an issue.

Acknowledgments

This library was inspired by the incredible work of the Prisma team. Their approach to simplifying and streamlining database interactions served as a foundation for many of the ideas implemented in Carabao-Mongo. While Carabao-Mongo focuses specifically on MongoDB, Prisma’s design principles influenced the structure and philosophy of this project.

Thank you to the Prisma team for their contribution to the developer ecosystem!

Keywords

FAQs

Package last updated on 06 Feb 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc