ts-mongoose icon indicating copy to clipboard operation
ts-mongoose copied to clipboard

.populateTs('') wrong and incomplete intellisense

Open zenVentzi opened this issue 5 years ago • 2 comments

Here is some example schemas.

import { createSchema, Type, typedModel } from "ts-mongoose";
import 'ts-mongoose/plugin'


const UserSchema = createSchema({
  name: Type.string()
});

const LikerSchema = createSchema({
  user: Type.string(),
  numOfLikes: Type.number()
});

const LikesSchema = createSchema({
  total: Type.number(),
  likers: Type.array().of(Type.schema().of(LikerSchema))
});

const CommentSchema = createSchema({
  value: Type.string(),
  user: Type.ref(Type.objectId()).to("User", UserSchema),
  likes: { type: LikesSchema, required: false }
});

const EditionSchema = createSchema({
  date: Type.date(),
  value: Type.string(),
  comments: Type.optionalSchema().of(CommentSchema),
  likes: Type.optionalSchema().of(LikesSchema)
});

const AnswerSchema = createSchema({
  position: Type.number(),
  userId: Type.string(),
  questionId: Type.string(),
  editions: Type.array().of(Type.schema().of(EditionSchema))
});

const AnswerModel = typedModel("Answer", AnswerSchema);

Say I want to populate some deeply nested fields inside the AnswerModel. E.g. previously I'd write AnswerModel.find().populate('editions.comments.user') if I wanted to populate the user field in Comment. Or .populate(editions.likes.likers.user') if I wanted to populate the user field from Likers.

The expected behaviour is the intellisense in populateTs to give me all the ref fields of the given Schema and recurse its nested ref fields so that I can write the above.

Actual behaviour, I am not allowed to write 'editions.comments' or any further. Also I get intellisense for things that I shouldn't be allowed to, such as 'validate', '$isDefault' and others that are not proper ref fields.

Also I expect to be able to write multiple populateTs to populate multiple ref fields. E.g. AnswerModel.find().populateTs('editions.comments.user').populateTs('editions.comments.likes.likers.user').populateTs('editions.likes.likers.user') and receive the correct answer type.

Would this be too challenging to get it working at the current stage of development? This seems to be in the core of the functionality for the app in question to have proper typings. So, without populateTs working for these examples, it renders to be useless, while otherwise the package seems to be very useful.

zenVentzi avatar Apr 19 '19 07:04 zenVentzi

+1 I also need an equivalent of

Model.find().populate({
  path: "child", populate: {
    path: "subChild", populate: {
      path: "subSubChild"
    }
  }
})

oxyii avatar Jul 01 '19 12:07 oxyii

populateTS is not a method on BookModel. This is what Typescript throws. I had another issue with cyclic references when I tried to link book to authors and vice versa. I really love the this library but it seems this is an abandoned ship

Parables avatar Apr 27 '20 03:04 Parables