Skip to content

cptcr/cptcr.db

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

CPTCR Database

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.

Installation

You can install the package via npm:

npm install cptcr.db

Project Structure

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

Usage

TypeScript

Define a Schema

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

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

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);

Query Documents

Find documents in the collection.

const foundUsers = User.find({ name: 'John Doe' });
console.log('Found users:', foundUsers);

Update a Document

Update a document in the collection.

const updatedCount = User.update({ name: 'John Doe' }, { age: 31 });
console.log('Updated users count:', updatedCount);

Delete a Document

Delete a document from the collection.

const deletedCount = User.delete({ name: 'John Doe' });
console.log('Deleted users count:', deletedCount);

Count Documents

Count documents in the collection.

const userCount = User.count({ name: 'John Doe' });
console.log('User count:', userCount);

Distinct Values

Get distinct values of a field in the collection.

const distinctAges = User.distinct('age');
console.log('Distinct ages:', distinctAges);

Find by ID

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 Custom File Path

Set a custom file path for the collection.

User.setFilePath('./data/customUsers.bson');
console.log('Custom file path set.');

JavaScript

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.

Define a Schema

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

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

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);

Query Documents

Find documents in the collection.

const foundUsers = User.find({ name: 'John Doe' });
console.log('Found users:', foundUsers);

Update a Document

Update a document in the collection.

const updatedCount = User.update({ name: 'John Doe' }, { age: 31 });
console.log('Updated users count:', updatedCount);

Delete a Document

Delete a document from the collection.

const deletedCount = User.delete({ name: 'John Doe' });
console.log('Deleted users count:', deletedCount);

Count Documents

Count documents in the collection.

const userCount = User.count({ name: 'John Doe' });
console.log('User count:', userCount);

Distinct Values

Get distinct values of a field in the collection.

const distinctAges = User.distinct('age');
console.log('Distinct ages:', distinctAges);

Find by ID

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 Custom File Path

Set a custom file path for the collection.

User.setFilePath('./data/customUsers.bson');
console.log('Custom file path set.');

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published