A local database that mimics MongoDB using BSON files. This library provides a simple interface for creating collections, inserting, querying, updating, and deleting documents. It supports custom schemas and models similar to Mongoose.
You can install the package via npm:
npm install cptcr.db
cptcr.db/
├── dist/
├── node_modules/
├── src/
│ ├── collection.ts
│ ├── db.ts
│ ├── index.ts
│ ├── model.ts
│ └── schema.ts
├── test/
│ └── test.ts
├── package.json
├── tsconfig.json
└── README.md
Create a schema to define the structure of your documents.
import { Schema } from 'cptcr.db';
const userSchema = new Schema({
name: 'string',
age: 'number',
email: 'string'
});
Initialize the database and create a model.
import { Database } from 'cptcr.db';
const db = new Database('./data');
const User = db.model('User', userSchema);
Insert a document into the collection.
const newUser = { name: 'John Doe', age: 30, email: '[email protected]' };
const insertedUser = User.insert(newUser);
console.log('Inserted user:', insertedUser);
Find documents in the collection.
const foundUsers = User.find({ name: 'John Doe' });
console.log('Found users:', foundUsers);
Update a document in the collection.
const updatedCount = User.update({ name: 'John Doe' }, { age: 31 });
console.log('Updated users count:', updatedCount);
Delete a document from the collection.
const deletedCount = User.delete({ name: 'John Doe' });
console.log('Deleted users count:', deletedCount);
Count documents in the collection.
const userCount = User.count({ name: 'John Doe' });
console.log('User count:', userCount);
Get distinct values of a field in the collection.
const distinctAges = User.distinct('age');
console.log('Distinct ages:', distinctAges);
Find a document by its ID.
if (insertedUser._id) {
const foundUser = User.findById(insertedUser._id);
console.log('Found user by ID:', foundUser);
// Update by ID
const updatedUser = User.findByIdAndUpdate(insertedUser._id, { age: 32 });
console.log('Updated user by ID:', updatedUser);
// Delete by ID
const deletedUser = User.findByIdAndDelete(insertedUser._id);
console.log('Deleted user by ID:', deletedUser);
} else {
console.error('Inserted user does not have an _id.');
}
Set a custom file path for the collection.
User.setFilePath('./data/customUsers.bson');
console.log('Custom file path set.');
While this library is primarily written in TypeScript, you can also use it in a JavaScript project. The usage is similar but without type annotations.
Create a schema to define the structure of your documents.
const { Schema } = require('cptcr.db');
const userSchema = new Schema({
name: 'string',
age: 'number',
email: 'string'
});
Initialize the database and create a model.
const { Database } = require('cptcr.db');
const db = new Database('./data');
const User = db.model('User', userSchema);
Insert a document into the collection.
const newUser = { name: 'John Doe', age: 30, email: '[email protected]' };
const insertedUser = User.insert(newUser);
console.log('Inserted user:', insertedUser);
Find documents in the collection.
const foundUsers = User.find({ name: 'John Doe' });
console.log('Found users:', foundUsers);
Update a document in the collection.
const updatedCount = User.update({ name: 'John Doe' }, { age: 31 });
console.log('Updated users count:', updatedCount);
Delete a document from the collection.
const deletedCount = User.delete({ name: 'John Doe' });
console.log('Deleted users count:', deletedCount);
Count documents in the collection.
const userCount = User.count({ name: 'John Doe' });
console.log('User count:', userCount);
Get distinct values of a field in the collection.
const distinctAges = User.distinct('age');
console.log('Distinct ages:', distinctAges);
Find a document by its ID.
if (insertedUser._id) {
const foundUser = User.findById(insertedUser._id);
console.log('Found user by ID:', foundUser);
// Update by ID
const updatedUser = User.findByIdAndUpdate(insertedUser._id, { age: 32 });
console.log('Updated user by ID:', updatedUser);
// Delete by ID
const deletedUser = User.findByIdAndDelete(insertedUser._id);
console.log('Deleted user by ID:', deletedUser);
} else {
console.error('Inserted user does not have an _id.');
}
Set a custom file path for the collection.
User.setFilePath('./data/customUsers.bson');
console.log('Custom file path set.');