UNPKG

git-goose

Version:

a mongoose plugin that enables git like change tracking

161 lines (160 loc) 7.16 kB
import { Document, FilterQuery, HydratedDocument, Model, ProjectionType, QueryOptions, Require_id } from 'mongoose'; import { ContextualGitConfig, GitConfig, RequiredConfig } from '../config'; import { DBCommitDocument, DBCommitModel } from '../schemas'; import { Commit, CommitRef, GlobalPatcherName, Nullable, Patch, PatcherName, RefId } from '../types'; /** * Base Git manager for interacting and manipulating commits and the commit history * * @template TargetDocType - The type of the document to be generated * @template TPatcherName - The inferred name of the patcher to use (used for type hinting patches) */ export declare abstract class GitBase<TargetDocType, TPatcherName extends PatcherName = GlobalPatcherName> { /** The configuration */ protected readonly _conf?: Partial<GitConfig<TPatcherName>>; /** The Git Model */ readonly _model: DBCommitModel<TargetDocType>; /** The internal Model that this git instance is bound to */ protected readonly _referenceModel: Model<TargetDocType>; constructor(referenceModel: Model<TargetDocType>, conf?: Partial<GitConfig<TPatcherName>>); protected static staticConf<K extends keyof ContextualGitConfig>(key: K, conf?: Partial<ContextualGitConfig>): (ContextualGitConfig & Required<(typeof RequiredConfig)[number]>)[K]; /** * Restore a commit to a {@link Document} of type {@link TargetDocType} */ abstract checkout(...args: unknown[]): Promise<Nullable<Require_id<TargetDocType>>>; /** * Return the difference between two commits */ abstract diff(...args: unknown[]): Promise<Patch<TPatcherName>>; /** * Show commit logs * * Fetch all the commits that match the target id */ abstract log(...args: unknown[]): Promise<Commit[]>; /** * Restore a commit to a {@link Document} of type {@link TargetDocType} * * @param refId - The reference object id * @param commit - The commit identifier */ protected checkoutFromRefId(refId: RefId, commit: CommitRef): Promise<Nullable<HydratedDocument<TargetDocType>>>; /** * Record changes to the document in the commit store * * If no changes are detected it will skip creation */ protected abstract commit(): Promise<void>; /** * Record changes to the document in the commit store * * If no changes are detected it will skip creation * * @param refId - The reference object id * @param prev - The documents previous state * @param curr - The documents new state */ protected commitFromRefId(refId: RefId, prev: Nullable<TargetDocType>, curr: Nullable<TargetDocType>): Promise<void>; /** * Fetch the value from the config, or defaulting to the global config value * * @param key - The key to search for */ protected conf<K extends keyof ContextualGitConfig>(key: K): (ContextualGitConfig & Required<(typeof RequiredConfig)[number]>)[K]; /** * Create a patch by invoking the configured patcher * * @param committed - The documents previous state * @param active - The documents new state */ protected createPatch(committed: Nullable<TargetDocType>, active: Nullable<TargetDocType>): Promise<Nullable<Patch<TPatcherName>>>; /** * Create a patch by invoking the configured patcher * * @param committed - The documents previous state * @param active - The documents new state * @param allowNoop - Allow null patches to be returned */ protected createPatch(committed: Nullable<TargetDocType>, active: Nullable<TargetDocType>, allowNoop: true): Promise<Patch<TPatcherName>>; /** * Return the difference between two commits * * @param refId - The reference object id * @param commitA - A commit identifier * @param commitB - A commit identifier */ protected diffFromRefId(refId: RefId, commitA: CommitRef, commitB: CommitRef): Promise<Patch<TPatcherName>>; /** * Find commit using an identifier * * @param refId - The reference object id * @param commit - A commit identifier */ protected findCommitFromRefId(refId: RefId, commit: CommitRef): Promise<Nullable<DBCommitDocument<TargetDocType>>>; /** * Show commit logs * * Fetch all the commits that match the target id * * @param refId - The reference object id * @param filter - Optional filter for further constraining. * - It will always be constrained by the [target]{@link DBCommit#refId} field * regardless whether you try to override it * @param projection - Optional projection on the {@link Commit} model. * - This is scoped on the {@link Commit} type as opposed to the * actual type {@link DBCommit} to influence intent * - If you try to fetch target or snapshot fields despite the warnings, we will remove them post-processing * @param options - Optional query options for further modifications of the response data * - Unless overridden it will default sort by descending date and limit to the top 10 commits */ protected logFromRefId(refId: RefId, filter?: FilterQuery<Commit>, projection?: ProjectionType<Commit> | null, options?: QueryOptions<Commit> & { lean: true; }): Promise<Commit[]>; /** * Convert a document into its object form * * @param doc - The document to convert */ protected objectify(doc: Nullable<Document<unknown, {}, TargetDocType>>): Nullable<Require_id<TargetDocType>>; /** * Reconstruct a commit using an identifier * * @param refId - The reference object id * @param commit - A commit identifier * @param nullOnMissingCommit - Return null if the commit doesn't exist */ protected rebuildCommitFromRefId(refId: RefId, commit: CommitRef, nullOnMissingCommit?: true): Promise<Nullable<Require_id<TargetDocType>>>; /** * Find a commit by a date identifier * * Returns the latest commit that matches the date * * If the date is in the future, that is fine, it will return the HEAD commit, * effectively answering the question * * _"if I was to time travel to {@link date} what would the object look like?"_ * * This also means that going backwards in time to before the object * was first initialised, means you will get null * * @param refId - The reference object id * @param date - The date in question */ private findCommitByDateFromRefId; /** * Find a commit by its unique identifier * * @param refId - The reference object id * @param commit - The commit id */ private findCommitByIdFromRefId; /** * Find a commit in the past using a numerical offset * * If you supply a negative number, it is assumed to be a mistake as you cannot have a commit in the future. * So it will be inverted for you * * @param refId - The reference object id * @param offset - The number of commits to offset by, 0 being the HEAD commit */ private findCommitByOffsetFromRefId; }