Closed briandowns closed 9 months ago
Just so I'm understanding correctly, do you mean via the use of Object.getClassRef()
/ Object.createFrom()
?
You can pass around references to the class if they are defined outside the global scope, but I have a feeling that may not help you much
Do you have an example snippet in kahless of what you're trying to do actually, that may help me grasp what's going on
Exactly, Object.createFrom.
So what I'm doing is possibly not so clever but...
For some form of select call in the ORM, the user has to pass in a 'model' (class) so it knows what it's dealing with. Kahless (Sqlite module) returns a list of lists (rows) and I need the ability to create new instances of hte given model, populate the classes with the data from the returned rows, and then return a list of classes contqining the mapped row data.
I have all of it working except for the ability to create new instanes of the given class.
In the kahless repo, you can check out the method json
that has where I'm going with it. (It won't be named that ultimately but that functionality will be distributed to the read methods).
Ah yeah I see, here: https://github.com/briandowns/kahless/blob/json_mapping/kahless.du#L551C9-L551C67 you already have reference to the class so you don't actually need to refetch it - you can simply do
const newModel = klass();
Classes are also first class citizens so you can pass references of them around, so you json call can also be .json(User)
rather than an instance too if that's easier to handle
Well... Crap. That has me working. Thank you! Once this feature is in, we'll have a basic ORM to use. :)
That’ll be so good, thanks for this! The issue is probably still valid but I’m glad your issue is solved though!
I was giving it some thought before closing and I'm not sure if it is or not. I like the idea of being able to get the class from the given ref passed and then creating new refs from it. Seems like an idiomatic approach. I'll defer to your judgement on whether to open the issue again.
Sorry I meant in your case that’s what you do yeah, but I think those functions should probably also work with classes defined in a local scope - not entirely sure on a use case yet but there may be some interesting meta programming in the local scope you could do haha
Is there an existing issue for this?
Is your feature request related to a problem?
Currently you can only get a new class instance from another class if that class has been defined in the global scope. It'd be great if we can get new class objects from a value passed into a function/method that's not in global scope.
For additional context, this feature is necessary for mapping database rows to user provided classes here: https://github.com/briandowns/kahless
Describe the solution you'd like
No response
Describe alternatives you've considered
No response
Additional context
No response