Coder Social home page Coder Social logo

Decorator Execution Order about typegoose HOT 29 CLOSED

typegoose avatar typegoose commented on September 21, 2024
Decorator Execution Order

from typegoose.

Comments (29)

hasezoey avatar hasezoey commented on September 21, 2024 2

I think this fork will became the new "official" repo.

szokodiakos#387 got merged, it is now officially the new official

from typegoose.

hasezoey avatar hasezoey commented on September 21, 2024 1

i think its better to be called in buildSchema / _buildSchema, because a class decorator is optional

from typegoose.

fabioformosa avatar fabioformosa commented on September 21, 2024 1

It's work fine now, very nice work guy!
I owe one beer, let's add a donation button into the typegoose doc page.
Thank you!
PS: A final suggestion... IMHO, Typegoose would raise an exception if someone uses two classes with the same name, without disambiguate them by collectionName or customName.

from typegoose.

hasezoey avatar hasezoey commented on September 21, 2024

Possible Solution:

  • cache all decorator evaulations
  • cache only inner-class ones

Cache Solutions:

  • cache using Map<T, S>
  • i dont know any other

Execution Solutions:

  • execute the cached ones in buildSchema / _buildSchema
  • i dont know any other

Drawback

  • more memory consumption, because of storing functions

@B-Stefan what do you think?

from typegoose.

fabioformosa avatar fabioformosa commented on September 21, 2024

Exactly, I fell in this trap just trying to add a @TypeAlias as class decorator.
Look at this:
https://stackoverflow.com/questions/57707455/typescript-decorator-get-class-decorator-value-from-method-decorator

I disagree with the answer, I think it's better to use a buffer to cache all metadata produced by method decorator, until class decorator is called.

from typegoose.

hasezoey avatar hasezoey commented on September 21, 2024

encountered a problem: caching is good and all, but how to make differing classes with the same name when schemaOptions nor options can be accessed in @prop? i couldnt find anything that makes a class / function unique ... so for now i made an (undocumented) variable __uniqueID

after doing the change to cached version, 3 tests fail, and i cannot figure out why (because it didnt fail before the cache change, and this only wraps all into another function)

WIP branch: https://github.com/hasezoey/typegoose/tree/r6/cache-inner-class-decorators

from typegoose.

hasezoey avatar hasezoey commented on September 21, 2024

i looked into it again, didnt got it work, though about another possible solution:
"strip" the logic (that calls schemas and co) and do move them into _buildSchema

Update: found the issue, it is because the subSchemas (that dont have a call to make model, which should not be needed) got not build, and so it was "empty", i currently dont know a fix without forcing the user to always make a model out of it

from typegoose.

hasezoey avatar hasezoey commented on September 21, 2024

Update: added c8a5a18fc35b9c5461c0d78585ff566d57d550a7, which fixes the "build on subdocuments" error, but new ones show up, i think about for now (version 6) disabling the customName functions

PS: for something like this, it seems more easy to just "reinterpret"(/"recreate") mongoose into typescript

Update: could reduce it to 1 test failing (biguser)

from typegoose.

B-Stefan avatar B-Stefan commented on September 21, 2024

@hasezoey Thanks for working on this!
You are absolutely right - I ran into this issue yesterday again at another combination of nested entities.

from typegoose.

fabioformosa avatar fabioformosa commented on September 21, 2024

I'm going to pull your branch to try to solve the failing test. My proj constraint is to have more classes with the same name, this feature is strictly necessary to me. It would be a pity to give up, you're doing a very impressive work, I think this fork will became the new "official" repo.

from typegoose.

hasezoey avatar hasezoey commented on September 21, 2024

Update: could theoretically fix "all" the errors, except:

  • "_id: false" is just not working on @prop, only on @arrayProp

@fabioformosa Please know that i have 4(and more) unpushed commits, and as i said above (in this comment) i almost fixed everything

from typegoose.

fabioformosa avatar fabioformosa commented on September 21, 2024

Ok, great! Let me know when you push the new version 6.0.0-27, so I try it in my project.
It's ok at the moment not use _id: false

from typegoose.

hasezoey avatar hasezoey commented on September 21, 2024

It's ok at the moment not use _id: false

its a requirement in biguser's test currently, and clutters the database

Note:
currently a cache (that caches the build schemas (buildSchemas in data.ts), but even after re-applying with schema.set("_id", false) it didnt even change)

Update: made Automattic/mongoose#8137, as long as i dont get an update on it, i will disable the test(requirement) for it, and add a note, but as long as it persists, i cant release 6.0.0

from typegoose.

hasezoey avatar hasezoey commented on September 21, 2024

Update: i made it now commented out, and all tests pass, will squash all the WIP commits, and @fabioformosa could you evaluate/test it? (will make a pr, and update this comment)

Update: its #24

from typegoose.

B-Stefan avatar B-Stefan commented on September 21, 2024

@hasezoey This is working with my branch except for one test
expect(foundUser.job.jobType).to.not.have.property('_id'); is failing

from typegoose.

hasezoey avatar hasezoey commented on September 21, 2024

@B-Stefan even the new 58490e8?

from typegoose.

B-Stefan avatar B-Stefan commented on September 21, 2024

Did not tested this commit, yet.

from typegoose.

B-Stefan avatar B-Stefan commented on September 21, 2024

With 58490e8 all tests are green! 👍 nice work!

from typegoose.

hasezoey avatar hasezoey commented on September 21, 2024

nice work!

i would say its "not nice work" more like "hacky work" / "deactivating an existing feature" (@prop({ _id: false }) dosnt work anymore / only in arrayprop sometimes??)

PS: i really hope the mongoose issue will get resolved fast ... and that 5.7 gets finally released, tomorrow i will look into this (_id: false) once more, why it worked before, and now not, will report then again

Report from next day: i investigated it again, came to the conclusion to wait for Automattic/mongoose#8137 to either support "inline _id:false" or fix ?schema.set('_id', false) or delete schema.paths._id works...

from typegoose.

fabioformosa avatar fabioformosa commented on September 21, 2024

I pushed a commit in this sample project https://github.com/fabioformosa/typegoose-validation-error (you know it) branch typegoosev6.
Add to package.json the package of typegoose from branch r6/cache-inner-class-decorators.
One test fails :(

from typegoose.

hasezoey avatar hasezoey commented on September 21, 2024

easy fix: i made i mistake, it should have been __uniqueID, but when i set it it was _uniqueID (notice the double underscore)

fixed in 9f77b49

PS: @fabioformosa i pushed an updated version to your repo (including that @hasezoey/typegoose is now a git-npm module, if you dont use github & git with ssh, please change to https)

from typegoose.

hasezoey avatar hasezoey commented on September 21, 2024

@fabioformosa for now, the customName thing is disabled until this is fixed...

from typegoose.

B-Stefan avatar B-Stefan commented on September 21, 2024

PS: A final suggestion... IMHO, Typegoose would raise an exception if someone uses two classes with the same name, without disambiguate them by collectionName or customName.

Actually I ran into this a couple of days ago.
I had two classes with the same name and had to debug the reason why some properties where missing. An Error would be great!

from typegoose.

fabioformosa avatar fabioformosa commented on September 21, 2024

@fabioformosa for now, the customName thing is disabled until this is fixed...

Ah ok. Which is the unsolved issue?

from typegoose.

hasezoey avatar hasezoey commented on September 21, 2024

Ah ok. Which is the unsolved issue?

#23 (this) | #24 (pr for this)

from typegoose.

hasezoey avatar hasezoey commented on September 21, 2024

@fabioformosa @B-Stefan is this soft error enough? 33e5014
or should it throw instead of being "soft"?

from typegoose.

fabioformosa avatar fabioformosa commented on September 21, 2024

All tests pass! I tried also here https://github.com/fabioformosa/typegoose-validation-error and it's ok!
The only known issue is that about _id: false in @prop, isn't?

from typegoose.

hasezoey avatar hasezoey commented on September 21, 2024

yes, that is why it currently dosnt get merged, because _id: false is an existing feature, which worked, and now somehow dosnt ... and until now i didnt got a response in the mongoose issue for it

from typegoose.

hasezoey avatar hasezoey commented on September 21, 2024

PR got squash Merged in ada5cfb

from typegoose.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.