Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't load arbitrary object schemas #14

Open
fishclaws opened this issue Jul 13, 2017 · 3 comments
Open

Can't load arbitrary object schemas #14

fishclaws opened this issue Jul 13, 2017 · 3 comments

Comments

@fishclaws
Copy link

I'm dealing with an existing data-structure that looks like this:
{
'x': {#Schema A}
'y': {#Schema A}
...
}
(where 'x' and 'y' are variable strings)
and I have no way to represent, load or save this data from the Mongo DB with this ORM.
I can't use ': any' or ': Object' or ': {[key: string]: T}'
I know that no collection types are supported except Arrays but this usecase for Dictionaries seems fairly important for a Mongo ORM to handle.
Thanks!

@meirgottlieb
Copy link
Contributor

meirgottlieb commented Jul 14, 2017

You can use a PropertyConverter to handle this situation. Take a look at the tests for PropertyConverter for an example of implementing a PropertyConvert and look at documentation on the Converter decorator for information on how to apply them.

Also, make sure you are using the latest version, 1.6.2. I noticed a bug where areEqual in the PropertyConverter was not being called by the mapping. This is fixed in 1.6.2.

Let me know if you have any questions.

@fishclaws
Copy link
Author

I'm not sure how to annotate my class to use the PropertyConverter
For example:
items: {
'unknownString': {'a':0},
'unknownString2':{'a':0},
}
how would the annotations look like?

@Embeddable
class A {
  @Field()
  public a: number;
}

@Entity
@Collection('Example')
class Example {
  @Field()
  @ElementType(A)
  public items: Array<..?..>;
}

@meirgottlieb
Copy link
Contributor

meirgottlieb commented Jul 14, 2017

Let's say you have a converter called AnyConverter like in my example, you would then annotate your class as follows:

@Entity
class Example {
  
  @Converter(AnyConverter)
  public items: any;
}

You can read more about how to use a converter here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants