Skip to content

Instantly share code, notes, and snippets.

@tim-rohrer
Last active July 21, 2022 21:00
Show Gist options
  • Save tim-rohrer/b33deeabc42e4a03f25919a19bf1d5d4 to your computer and use it in GitHub Desktop.
Save tim-rohrer/b33deeabc42e4a03f25919a19bf1d5d4 to your computer and use it in GitHub Desktop.
Declaration for mongo-curry
// Type definitions for mongo-curry v0.1.1
// Project: https://gitlab.com/Creplav/mongo-curry
// Definitions by: Tim Rohrer <https://github.com/tim-rohrer>
// Gratitude for: @BobobUnicorn#9999 in Discord The Coding Den
import * as MongoDB from "mongodb"
type FindAllItemsInCollection<TSchema> =
(collection: MongoDB.Colection<TSchema>) => () => Promise<MongoDB.WithId<TSchema>[]>;
type FindItemByIdInCollection<TSchema> =
(collection: MongoDB.Collection<TSchema>) => (id: MongoDB.ObjectId) => Promise<MongoDB.WithId<TSchema> | null>;
type FindItemByValueInCollection =
(collection: MongoDB.Collection<TSchema>) => (valueObject: Record<string, unknown>) => Promise<MongoDB.WithId<TSchema> | null>;
type InsertItemIntoCollection<TSchema> =
(collection: MongoDB.Collection<TSchema>) => (item: TSchema) => Promise<MongoDB.InsertOneResult<TSchema>>;
type InsertItemsIntoCollection<TSchema> =
(collection: MongoDB.Collection<TSchema>) => (validation: (items: TSchema[]) => boolean) => (items: TSchema[]) => Promise<MongoDB.InsertManyResult<TSchema> | "BAD DATA">;
type UpdateItemInCollection<TSchema> =
(collection: MongoDB.Collection<TSchema>) => (item: TSchema) => Promise<MongoDB.UpdateResult>;
type UpdateItemsInCollection<TSchema> =
(collection: MongoDB.Collection<TSchema>) => (validation: (items: TSchema[]) => boolean) => (items: TSchema[]) => Promise<MongoDB.UpdateResult>;
type DeleteItemFromCollection<TSchema> =
(collection: MongoDB.Collection<TSchema>) => (id: MongoDB.ObjectId) => Promise<MongoDB.DeleteResult>;
type ClearCollection<TSchema> =
(collection: MongoDB.Collection<TSchema>) => () => Promise<MongoDB.DeleteResult>;
/** Serves as a "lookup" function so dbRequest functions can determine the result type given a specific schema type. */
type DbRequestApply<TFunc, TSchema = MongoDB.Collection> =
TFunc extends typeof findAllItemsInCollection ? FindAllItemsInCollection<TSchema>
: TFunc extends typeof findItemByIdInCollection ? FindItemByIdInCollection<TSchema>
: TFunc extends typeof findItemByValueInCollection ? FindItemByValueInCollection<TSchema>
: TFunc extends typeof insertItemIntoCollection ? InsertItemIntoCollection<TSchema>
: TFunc extends typeof insertItemsIntoCollection ? InsertItemsIntoCollection<TSchema>
: TFunc extends typeof updateItemInCollection ? UpdateItemInCollection<TSchema>
: TFunc extends typeof updateItemsInCollection ? UpdateItemsInCollection<TSchema>
: TFunc extends typeof deleteItemFromCollection ? DeleteItemFromCollection<TSchema>
: TFunc extends typeof clearCollection ? ClearCollection<TSchema>
: never;
declare type DbRequest =
typeof findAllItemsInCollection
| typeof findItemByIdInCollection
| typeof findItemByValueInCollection
| typeof insertItemIntoCollection
| typeof insertItemsIntoCollection
| typeof updateItemInCollection
| typeof updateItemsInCollection
| typeof deleteItemFromCollection
| typeof clearCollection;
export declare function config({ DB_URL, DB_PORT, DB_NAME, TEST_DB_NAME }: {
DB_URL: string;
DB_PORT: number;
DB_NAME: string;
TEST_DB_NAME: string;
}): void;
export declare function pingDB(useTestDb?: boolean): number;
export declare function executeDBRequest(dbCollection: string, useTestDb?: boolean): <TDbRequest extends DbRequest>(dbRequest: TDbRequest) => (...requestParams: Parameters<ReturnType<DbRequestApply<TDbRequest>>>) => ReturnType<ReturnType<DbRequestApply<TDbRequest>>>;
export declare function storageActionOn<TSchema = MongoDB.Document>(collection: MongoDB.Collection<TSchema>): <TDbRequest extends DbRequest>(dbRequest: TDbRequest) => (...requestParams: Parameters<ReturnType<DbRequestApply<TDbRequest, TSchema>>>) => ReturnType<ReturnType<DbRequestApply<TDbRequest, TSchema>>>;
export declare function findAllItemsInCollection<TSchema = MongoDB.Document>(collection: MongoDB.Collection<TSchma>): () => Promise<MongoDB.WithId<TSchema>[]>;
export declare function findItemByIdInCollection<TSchema = MongoDB.Document>(collection: MongoDB.Collection<TSchema>): (id: MongoDB.ObjectId) => Promise<MongoDB.WithId<TSchema> | null>;
export declare function findItemByValueInCollection<TSchema = MongoDB.Document>(collection: MongoDB.Collection<TSchema>): (valueObject: Record<string, unknown>) => Promise<MongoDB.WithId<TSchema> | null>;
export declare function insertItemIntoCollection<TSchema = MongoDB.Document>(collection: MongoDB.Collection<TSchema>): (item: TSchema) => Promise<MongoDB.InsertOneResult<TSchema>>;
export declare function insertItemsIntoCollection<TSchema = MongoDB.Document>(collection: MongoDB.Collection<TSchema>): (validation: (items: TSchema[]) => boolean) => (items: TSchema[]) => Promise<MongoDB.InsertManyResult<TSchema> | "BAD DATA">;
export declare function updateItemInCollection<TSchema = MongoDB.Document>(collection: MongoDB.Collection<TSchema>): (item: TSchema) => Promise<MongoDB.UpdateResult>;
export declare function updateItemsInCollection<TSchema = MongoDB.Document>(collection: MongoDB.Collection<TSchema>): (validation: (items: TSchema[]) => boolean) => (items: TSchema[]) => Promise<MongoDB.UpdateResult>;
export declare function deleteItemFromCollection<TSchema = MongoDB.Document>(collection: MongoDB.Collection<TSchema>): (id: MongoDB.ObjectId) => Promise<MongoDB.DeleteResult>;
export declare function clearCollection<TSchema = MongoDB.Document>(collection: MongoDB.Collection<TSchema>): () => Promise<MongoDB.DeleteResult>;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment