codepunkt / mongoose-patch-history

Mongoose plugin that saves a history of JSON patch operations for all documents belonging to a schema in an associated 'patches' collection
MIT License
96 stars 21 forks source link

@types/mongoose-patch-history #95

Open danielbayerlein opened 2 years ago

danielbayerlein commented 2 years ago

Has anyone created types for mongoose-patch-history? Or is something planned?

vinerich commented 2 years ago

Hey there @danielbayerlein, I created some types for my own use.

mongoose-patch-history.d.ts

declare module "mongoose-patch-history" {
  import mongoose = require("mongoose");

  export default function (schema: mongoose.Schema, opts: { mongoose: mongoose.Mongoose; name: string }): void;
}

This is the easiest to get it working quickly.

If you want to put some work in and provide typings I'd be happy to review them or extend the missing parts. Unfortunately I don't have that much time currently to implement them myself.

danielbayerlein commented 2 years ago

More types added:

declare module 'mongoose-patch-history' {
  import type { Operation } from 'fast-json-patch';
  import type { Schema } from 'mongoose';

  export type MongoosePatchHistoryPatch = {
    ops: Operation & { originalValue?: any; }[];
    ref: Types.ObjectId;
    date: Date;
  };

  export type MongoosePatchHistoryOptions = {
    mongoose: Mongoose;
    name: string;
    removePatches?: boolean;
    transforms?: [(name: string) => string, (name: string) => string];
    includes?: Record<string, Record<string, unknown>>;
    excludes?: string[];
    trackOriginalValue?: boolean;
  };

  export default function (
    schema: Schema,
    options: MongoosePatchHistoryOptions,
  ): void;
}
vinerich commented 2 years ago

Thanks for you input! Looks good.

If I might ask for you opinion:

This might be checked with @codepunkt too.

danielbayerlein commented 2 years ago

@vinerich I think both points make a lot of sense! Sounds like version 3 of mongoose-patch-history.