Security News
tea.xyz Spam Plagues npm and RubyGems Package Registries
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
@kibcode/mongodb-controllers
Advanced tools
Readme
MongoDB TypeScript controllers with soft deletion mode and timestamps.
npm i @kibcode/mongodb-controllers
or
yarn add @kibcode/mongodb-controllers
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.
Firstly, you have to install mongodb
package.
npm i mongodb
or
yarn add mongodb
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 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?
deleteOne
or deleteMany
are called, found entities are not being physically
deleted. They just get deletedAt
field which is equal to current
timestamp.deletedAt
cannot be found with
find
, findOne
etc. until you pass property
includeDeleted = true
while searching for them.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}
);
})();
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.
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')
}
*/
})();
FAQs
Collection controllers for MongoDB
The npm package @kibcode/mongodb-controllers receives a total of 11 weekly downloads. As such, @kibcode/mongodb-controllers popularity was classified as not popular.
We found that @kibcode/mongodb-controllers demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
Security News
As cyber threats become more autonomous, AI-powered defenses are crucial for businesses to stay ahead of attackers who can exploit software vulnerabilities at scale.
Security News
UnitedHealth Group disclosed that the ransomware attack on Change Healthcare compromised protected health information for millions in the U.S., with estimated costs to the company expected to reach $1 billion.