devadeboye-taskly-shared-package v1.3.2
devadeboye-taskly-shared-package
This shared package centralizes common code used across services in the taskly clone project, reducing duplication and ensuring consistency.
Table of Contents
Introduction
This shared package centralizes common code used across services in the taskly clone project, reducing duplication and ensuring consistency. This package contains resources such as enums, utility functions, interfaces, services, abstract classes among many others.
Installation Instructions
To integrate the package into your project, follow these steps:
1. Add the Shared Package
Add the package to your project by running the following command:
yarn add devadeboye-taskly-shared-package
Usage
To use the package in your project, import the desired resource from the package as follows: E.g to make use of Environment enum defined in the package we write:
import { EnvironmentType } from 'devadeboye-taskly-shared-package';
// we can then use it as we like
[EnvironmentEnum.NODE_ENV]: Joi.string()
.trim()
.valid(...Object.values(EnvironmentType))
.required(),
Documentation
Note: Documentation is managed using vscode's Markdown All in One
extension.
FUNCTIONS
INTERFACES
DbInterface
The Db interface must be implement by every db service provider. It contains set of methods to ensure consistency through out our app when interacting with any type of database.
Usage
To use the db interface in your project, follow the example below:
@Injectable()
export class MongoDbService<T> implements DbInterface {
private model: Model<T>;
public constructor(model: Model<T>) {
this.model = model;
}
// implement your db method as required
public async findOne(
query: FilterQuery<T>,
select: string[],
populate: string | string[],
): Promise<T | null> {
const result = await this.model
.findOne(query)
.select(select)
.populate(populate)
.exec();
return result;
}
}
SERVICES
MongoDbService
Mongodb service abstract interaction with the mongodb database while implementing a consistent interface required by taskly database services to connect with any database.
Usage
To use the mongodb service in your project, follow the example below to inject it into a nestjs service.
@Injectable()
export class PictureService {
private dbService: MongoDbService<Picture>;
public constructor(
@InjectModel(Picture.name)
private pictureModel: Model<Picture>,
) {
this.dbService = new MongoDbService<Picture>(pictureModel);
}
// use it in your method as below
public async findProfilePics(id: string): Promise<Picture | null> {
const pics = await this.dbService.findById(id, [], []);
return pics;
}
}
Contributing
Happy coding 🥂🥂🥂