-
first off: thank you for your fantastic work, i have tried many libs attempting to do similar things, but never have i felt so close to a solution as with what you have built! i am currently building a prototype for an infrastructure that should handle data migrations and version adaption. your framework gives me hope that i can do this in an object oriented manner. its in the nature of this application, that this infrastructure i'm building cannot mention the types concretely in its service implementation, so i will have code like this:
your bson implementation has a great deserialize function, but i have to explicitely state the type, which i can not do. if i had access to your type registry, i think i could search for the type that matches |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
Deepkit runtime types are based on explicit types only. This is a design decision so that not too much runtime type-related code is generated or too much overhead is generated. So, in runtime there is no information about types of I had to reread your questions multiple times to understand the use-case (what confused me was What you can do: //some file
import { entity } from '@deepkit/type';
@entity.name('Dog')
export class Dog {
name: string;
} import { typeSettings } from '@deepkit/type';
const registry = typeSettings.registeredEntities;
const rawData = JSON.parse('{ "name": "MyName" }')
const cls = "Dog"
const version = 1
const type = registry[cls];
if (!type) throw new Error(`{cls} not found`);
const o = deserialize(rawData, undefined, undefined, undefined, type)
[Dog, Animal, AnotherOne]; Depending on your build-script, you have to make sure that this line is not removed automatically (because an optimiser could say this line is not used). |
Beta Was this translation helpful? Give feedback.
-
Thanks for your quick reply! i have tried your suggestion to decorate my model classes and then access typeSettings.registeredEntities - this works nicely, i see my classes in the registry. this adds a bit of boilerplate to my code, but for the prototype this will do nicely and later i can maybe do some stuff during build. i probably misunderstood how the normal typing works - i use thanks for the hint on loading order - yes i know this problem, i will probably end up adding a build step that automatically imports all model classes somewhere. sorry for not explaining my use case very well, your answer still nailed it! |
Beta Was this translation helpful? Give feedback.
Deepkit runtime types are based on explicit types only. This is a design decision so that not too much runtime type-related code is generated or too much overhead is generated. So, in runtime there is no information about types of
rawData
orcls
available.I had to reread your questions multiple times to understand the use-case (what confused me was
cls
), but I think I understand now. Let me write it down first and let me know If I'm right: You have various classes likeDog
in your application and you have no direct access to them (you can notimport { Dog } from 'xy'
them, because maybe import path is unknown). Yet, you have a unique identifier to this class (hereDog
) and a version, and…