meabed / mongoosastic-ts

Index Mongoose models into elasticsearch automatically.
MIT License
9 stars 1 forks source link

Error when using Interface with Schema #236

Open argupta23 opened 1 year ago

argupta23 commented 1 year ago

@meabed

Your Environments

Expected Behavior

migrating from mongoosastic to mongoosastic-ts generates error

Actual Behavior

In my case I have the following.


import { ObjectId } from "mongodb";
import mongoose, { Document, model, Schema, SchemaTypes, Types } from "mongoose";
import { v4 as uuidv4 } from 'uuid';
import { ESClient } from "../../libs/elasticsearch";
import { Mongoosastic } from 'mongoosastic-ts';
import { MongoosasticDocument, MongoosasticModel, MongoosasticPluginOpts } from 'mongoosastic-ts/dist/types';
// import Mongoosastic, { MongoosasticModel, MongoosasticDocument } from 'mongoosastic'

export interface IDevice extends Document {
  _id: string,
  name: string,
  description: string,
}

//Case 1  -- both mongoosastic and mongosastic-ts error out when you add interface to schema
//export const devicesSchema = new Schema<IDevice> ({

//Case 2  -- works with mongosastic but not with mongoosastic-ts
export const devicesSchema = new Schema ({
  _id: {
    type: String,
    default: () => uuidv4()
  },
  name: {
    type: String,
    index: true,
    required: true
  },
  description: {
    type: String,
  },
}

<!-- Error is generated at the below mongoosastic reference-->
devicesSchema.plugin(Mongoosastic, {
  esClient: ESClient
})

const devices = model<typeof devicesSchema, MongoosasticModel<typeof devicesSchema>>('devices', devicesSchema);
export default devices;

}

Mongoosastic generates the below error when including Interface.

Error Stack

(alias) function Mongoosastic(schema: Schema<MongoosasticDocument, MongoosasticModel<MongoosasticDocument>>, options?: Options | undefined): void
import Mongoosastic
Argument of type '(schema: Schema<MongoosasticDocument<any>, MongoosasticModel<MongoosasticDocument<any>>, {}, {}, {}, {}, "type", MongoosasticDocument<...>>, options?: Options | undefined) => void' is not assignable to parameter of type 'PluginFunction<IDevice, Model<IDevice, any, any, any, any>, any, any, any, any>'.
  Types of parameters 'schema' and 'schema' are incompatible.
    Type 'Schema<IDevice, Model<IDevice, any, any, any, any>, any, any, any, any, "type", IDevice>' is not assignable to type 'Schema<MongoosasticDocument<any>, MongoosasticModel<MongoosasticDocument<any>>, {}, {}, {}, {}, "type", MongoosasticDocument<...>>'.
      Types of property 'static' are incompatible.
        Type '{ <K extends string | number | symbol>(name: K, fn: any): Schema<IDevice, Model<IDevice, any, any, any, any>, any, any, any, any, "type", IDevice>; (obj: { ...; } & { ...; }): Schema<...>; (name: string, fn: (this: Model<...>, ...args: any[]) => any): Schema<...>; }' is not assignable to type '{ <K extends never>(name: K, fn: {}[K]): Schema<MongoosasticDocument<any>, MongoosasticModel<MongoosasticDocument<any>>, ... 5 more ..., MongoosasticDocument<...>>; (obj: {} & { ...; }): Schema<...>; (name: string, fn: (this: MongoosasticModel<...>, ...args: any[]) => any): Schema<...>; }'.
          Types of parameters 'fn' and 'fn' are incompatible.
            The 'this' types of each signature are incompatible.
              Type 'Model<IDevice, any, any, any, any>' is missing the following properties from type 'MongoosasticModel<MongoosasticDocument<any>>': bulkError, createMapping, esClient, esCount, and 9 more.ts(2345)

Please let me know what I am doing wrong.

Thanks

meabed commented 1 year ago

Could you have a look at the example folder - it has pretty much all use cases typed correctly?

argupta23 commented 1 year ago

@meabed, I did look at it earlier, but was not able to get it to work. I will close this ticket for now

meabed commented 1 year ago

Would you be able to create codesandbox with the code you tried and error so I could have a look and fix it

argupta23 commented 1 year ago

I can create a small repo and share the files with you. Please let me know if that works for you.

I do have a question in the meantime. While Mongodb supports "transaction / session" with rollback on abort, how does it work with elasticsearch?

What I mean by that is the following. When using transaction / session my understanding is that mongodb saves an interim copy of the document, waits for other processes (relation updates etc...) to finish and on success commits the transaction and updates the collection. If any step in the middle fails and the transaction aborts, all commits are rolled back and nothing gets saved to mongodb.

In the current state of the package, my understanding is that the iterim copy of the document is also saved to Elasticsearch, but when the abort occurs, mongodb cleans itself up (becuase there is no commit) but there is no mechanism within the current package to roll back / undo the document from elasticsearch. Is that correct?

thanks

meabed commented 1 year ago

Yes correct