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;
}
const usersCollection: Collection<IUser> = null as any;
class UsersController extends Controller(usersCollection) { }
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?
- 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. - 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';
interface IUser {
_id: ObjectId;
name: string;
deletedAt?: Date;
}
class UsersController extends Controller({
collection: usersCollection,
useSoftDelete: true,
}) { }
(async () => {
await UsersController.deleteOne({name: 'Vladislav Kibenko'});
await UsersController.findOne({name: 'Vladislav Kibenko'});
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';
interface IUser {
_id: ObjectId;
name: string;
createdAt: Date;
updatedAt: Date;
}
class UsersController extends Controller({
collection: usersCollection,
useTimestamps: true,
}) { }
(async () => {
const entity = await UsersController.createOne({name: 'Vladislav Kibenko'});
})();