-
Notifications
You must be signed in to change notification settings - Fork 27
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
How to map properly different property name in the code and in the db #108
Comments
First thing's first, the As for renaming properties, you're going to want to use the export function RenameProperties(map: { [codeField: string]: string }) {
return Iridium.Transform(db => {
for (const codeField in map) {
const dbField = map[codeField]
db[codeField] = db[dbField]
delete db[dbField]
}
return db
}, code => {
for (const codeField in map) {
const dbField = map[codeField]
code[dbField] = code[codeField]
delete code[codeField]
}
return code
})
} This would allow you to rename your property like this: @Collection("houses")
@RenameProperties({
houseName: "house_name"
})
export class House extends Instance<HouseDocument, House> implements HouseDocument {
@ObjectID
public _id: string;
@Property(String)
public houseName: string;
} Please let me know if you run into any problems with that approach. Regards, |
@spartan563 thanks! This may work, but it looks like no standard decorator like @RenameProperty/@MapProperty in the library itself. |
@spartan563 after some tested this code doesn't work properly it throw this error
If I set @Property(String, false) then no issue, but how to apply @Property decorator to field which will be renamed in db to another one? |
This adds a new @rename decorator which enables you to rename a field in the DB to something else in code. It works in conjunction with @Transform and @Property (schema) functionality transparently and shouldn't introduce any backwards imcompatible changes.
I'm sorry about that @fyn-dev, I had completely forgotten about that little quirk. The issue is caused by the way that the schema is used to build the proxy instance (which forwards requests for I've instead opted to expand the underlying Iridium Instance type to support renaming natively through a new You should now be able to simply do the following (without any extra code) using @Collection("houses")
export class House extends Instance<HouseDocument, House> implements HouseDocument {
@ObjectID
public _id: string;
@Property(String)
@Rename("house_name")
public houseName: string;
} Please let me know if you run into any issues with it, and sorry about the slow turnaround time on this (holidays got in the way). |
@spartan563 |
Let's say in my document I have this structure:
but in my code I want to map like this:
problem is when I try for example insert new document is saving as houseName property but not as house_name. How to insert data using house_name but keep in the code houseName property?
TypeScript version 2.6.2
Iridium version 8.0.0-alpha.5
The text was updated successfully, but these errors were encountered: