diff --git a/src/backend/database/index.ts b/src/backend/database/index.ts index 536296a..c6ebdf1 100644 --- a/src/backend/database/index.ts +++ b/src/backend/database/index.ts @@ -11,25 +11,25 @@ import { ObjectId } from 'mongodb'; const Database = appConfig.database.driver === 'mongodb' ? MongoDatabaseDriver : LocalDatabaseDriver; /** - * - * @param id + * Convert a string to an EntityId (string or ObjectId depending on the database driver) + * @param id - id to convert */ export function toEntityId(id: string): EntityId { return (appConfig.database.driver === 'mongodb' ? new ObjectId(id) : id) as EntityId; } /** - * - * @param id1 - * @param id2 + * Check if provided ids are equal + * @param id1 - first id + * @param id2 - second id */ export function isEqualIds(id1?: EntityId, id2?: EntityId): boolean { return id1?.toString() === id2?.toString(); } /** - * - * @param id + * Check if provided ids are valid + * @param id - id to check */ export function isEntityId(id?: EntityId): id is EntityId { return typeof id === 'string' || id instanceof ObjectId; diff --git a/src/backend/database/local.ts b/src/backend/database/local.ts index 07f9546..99d879b 100644 --- a/src/backend/database/local.ts +++ b/src/backend/database/local.ts @@ -1,5 +1,5 @@ import Datastore from 'nedb'; -import { DatabaseDriver, Options, RejectFunction, ResolveFunction } from './types.js'; +import { DatabaseDriver, Options } from './types.js'; import path from 'path'; import appConfig from '../utils/appConfig.js'; @@ -22,6 +22,21 @@ function initDb(name: string): Datastore { }); } +/** + * Resolve function helper + */ +export interface ResolveFunction { + (value: any): void; +} + +/** + * Reject function helper + */ +export interface RejectFunction { + (reason?: unknown): void; +} + + /** * Simple decorator class to work with nedb datastore diff --git a/src/backend/database/mongodb.ts b/src/backend/database/mongodb.ts index cfa6cdf..b0b4041 100644 --- a/src/backend/database/mongodb.ts +++ b/src/backend/database/mongodb.ts @@ -6,18 +6,22 @@ const mongodbUri = appConfig.database.driver === 'mongodb' ? appConfig.database. const mongodbClient = mongodbUri ? await MongoClient.connect(mongodbUri): null; /** - * Simple decorator class to work with nedb datastore + * MongoDB driver for working with database */ export default class MongoDatabaseDriver implements DatabaseDriver { /** * Mongo client instance */ private db: MongoClient; + + /** + * Collection instance + */ private collection: Collection; /** - * - * @param collectionName + * Creates driver instance + * @param collectionName - collection to work with */ constructor(collectionName: string) { if (!mongodbClient) { diff --git a/src/backend/database/types.ts b/src/backend/database/types.ts index 5b3d618..bb86f57 100644 --- a/src/backend/database/types.ts +++ b/src/backend/database/types.ts @@ -1,13 +1,59 @@ import { ObjectId } from 'mongodb'; +/** + * Represents database driver functionality + */ export interface DatabaseDriver { + /** + * Insert new document into the database + * + * @param {object} doc - object to insert + * @returns {Promise} - inserted doc or Error object + */ insert(doc: DocType): Promise; + + /** + * Find documents that match passed query + * + * @param {object} query - query object + * @param {object} projection - projection object + * @returns {Promise | Error>} - found docs or Error object + */ find(query: Record, projection?: DocType): Promise>; + + /** + * Find one document matches passed query + * + * @param {object} query - query object + * @param {object} projection - projection object + * @returns {Promise} - found doc or Error object + */ findOne(query: Record, projection?: DocType): Promise; + + /** + * Update document matches query + * + * @param {object} query - query object + * @param {object} update - fields to update + * @param {Options} options - optional params + * @returns {Promise} - number of updated rows or affected docs or Error object + */ update(query: Record, update: DocType, options: Options): Promise> + + /** + * Remove document matches passed query + * + * @param {object} query - query object + * @param {Options} options - optional params + * @returns {Promise} - number of removed rows or Error object + */ remove(query: Record, options: Options): Promise } +/** + * Represents unique database entity id + * unique symbol to prevent type widening (read more https://todayilearned.net/2022/07/typescript-primitive-type-aliases-unique-symbols) + */ export type EntityId = (string | ObjectId) & {readonly id: unique symbol}; /** @@ -22,11 +68,3 @@ export interface Options { upsert?: boolean; returnUpdatedDocs?: boolean; } - -export interface ResolveFunction { - (value: any): void; -} - -export interface RejectFunction { - (reason?: unknown): void; -}