| name | hive-database |
| description | Database operations in Hive framework |
| globs | ["src/**/*.js"] |
| alwaysApply | false |
Database Operations
Access Service
import db from 'db';
const taskService = db.services.tasks;
Find Operations
const { results, pagesCount, count } = await service.find(
{ status: 'active' },
{ page: 1, perPage: 20, sort: '-createdOn', fields: ['_id', 'title'] }
);
const doc = await service.findOne({ _id: id });
const exists = await service.exists({ _id: id });
const total = await service.count({ status: 'active' });
const statuses = await service.distinct('status');
const { results } = await service.aggregate([
{ $match: { status: 'done' } },
{ $group: { _id: '$project._id', count: { $sum: 1 } } },
]);
Write Operations
const doc = await service.create({ title: 'New', user: ctx.state.user });
const updated = await service.updateOne(
{ _id: id },
(doc) => ({ ...doc, title: 'Updated' })
);
await service.updateMany(
{ status: 'pending' },
(doc) => ({ ...doc, status: 'active' })
);
await service.remove({ _id: id });
const newId = service.generateId();
Atomic Operations (No Events)
await service.atomic.update(
{ 'project._id': projectId },
{ $set: { 'project.name': newName } },
{ multi: true }
);
Query Patterns
import { when } from 'services/utils';
const { results } = await service.find({
...when(status, { status }),
...when(userId, { 'user._id': userId }),
status: { $in: ['active', 'pending'] },
createdOn: { $gte: startDate, $lt: endDate },
$or: [{ 'user._id': id }, { isPublic: true }],
});
Rules
updateOne requires a function, not an object
- Use
atomic.update for bulk/silent updates
- Use
when() helper for conditional query building