npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@kibcode/mongodb-controllers

v1.2.3

Published

Collection controllers for MongoDB

Downloads

8

Readme

MongoDB controllers

MongoDB TypeScript controllers with soft deletion mode and timestamps.

Installation

npm i @kibcode/mongodb-controllers

or

yarn add @kibcode/mongodb-controllers

Description

The main purpose of this library is to provide the lowest (almost) control over documents placed in MongoDB. So, provided functionality is an additional layer over standard mongodb package which has its own useful modifications such as an ability to use soft deletion and ability to use creation and update timestamps.

Usage

Firstly, you have to install mongodb package.

npm i mongodb

or

yarn add mongodb

Controller

All controllers you create should extend library's Controller class. This is how it should look like:

import {Controller} from '@kibcode/mongodb-controllers';
import {ObjectId, Collection} from 'mongodb';

interface IUser {
  _id: ObjectId;
  name: string;
}

// Lets imagine, we got this collection somehow. You should not use
// "null as any" as it is shown just as an example.
// Originally, you have to create MongoDB Client and then fork
// database. Finally, fork collection from created database and use 
// it.
const usersCollection: Collection<IUser> = null as any;

// In case, you want to create controller with your own
// functionality, call "Controller" function which returns
// class with static methods and extend it.
class UsersController extends Controller(usersCollection) { }

// Otherwise you could use more simple way.
const UsersController = Controller(usersCollection);

When controller is created, we can use its methods.

UsersController
  .findOne({name: 'Vladislav Kibenko'})
  .then(user => {
    if (user === null) {
      console.log('Unable to find user :(')
    } else {
      console.log('User was found!', user);
    }
  });

Soft deletion mode

Soft deletion assumes addition of such field as deletedAt instead of physical deletion of entity.

While creating new controller, it is allowed to enable soft deletion mode for it. This mode is useful when you are afraid of losing some data without availability to recover it.

How does soft deletion mode influence controller workflow?

  1. Entities cannot be deleted until you explicitly do it. It means, when deleteOne or deleteMany are called, found entities are not being physically deleted. They just get deletedAt field which is equal to current timestamp.
  2. Entities with existing field deletedAt cannot be found with find, findOne etc. until you pass property includeDeleted = true while searching for them.
Enabling soft deletion mode
import {Controller} from '@kibcode/mongodb-controllers';
import {ObjectId, Collection} from 'mongodb';

// Pay attention to this interface. As you use soft deletion mode,
// it should contain property deletedAt?: Date. There will be no
// errors thrown due to useSoftDelete requires optional field but
// make sure you have added it.
interface IUser {
  _id: ObjectId;
  name: string;
  deletedAt?: Date;
}

// ...

// Pass useSoftDelete = true to enable soft deletion mode.
class UsersController extends Controller({
  collection: usersCollection,
  useSoftDelete: true,
}) { }

(async () => {
  // Delete entity. Physically, it will exist in database, but
  // search methods will not find it.
  await UsersController.deleteOne({name: 'Vladislav Kibenko'});
  
  // Entity will not be found.
  await UsersController.findOne({name: 'Vladislav Kibenko'});
  
  // Entity will be found as we disabled soft deletion mode for this
  // search.
  await UsersController.findOne(
    {name: 'Vladislav Kibenko'}, 
    {}, 
    {includeDeleted: true}
  );
})();

Timestamps

When using timestamps, controller works with such fields as createdAt: Date and updatedAt: Date.

Idea is rather simple - when you call createOne or createMany, controller creates entity with predefined createdAt and updatedAt fields which are equal to current timestamp.

The second thing you should know is when you are calling updateOne or updateMany, controller will set updatedAt field to current timestamp.

As a result, you will have more useful information about your entities.

Enabling timestamps
import {Controller} from '@kibcode/mongodb-controllers';
import {ObjectId, Collection} from 'mongodb';

// Unlike the soft deletion mode, it is required to pass
// createdAt: Date and updatedAt: Date fields. Otherwise,
// TypeScript should throw an error.
interface IUser {
  _id: ObjectId;
  name: string;
  createdAt: Date;
  updatedAt: Date;
}

// ...

// Pass useTimestamps = true to enable timestamps.
class UsersController extends Controller({
  collection: usersCollection,
  useTimestamps: true,
}) { }

(async () => {
  const entity = await UsersController.createOne({name: 'Vladislav Kibenko'});
  
  /*
    Entity will be:
    {
      _id: ObjectId('...'),
      name: 'Vladislav Kibenko',
      createdAt: Date('2021-01-07T21:16:17.888Z')
      updatedAt: Date('2021-01-07T21:16:17.888Z')
    }
  */
})();