Replies: 2 comments
-
I faced a similar issue in Mysql when trying to handle JSON columns. Here's what I did to write once and forget about it: Model: export default class MyModel extends BaseModel {
public static latestFirst = scope((query) => {
query.orderBy('id', 'desc');
});
@column({ isPrimary: true })
public id: number;
@column()
public jsonColumn: string;
@computed()
public get parsedJsonColumn() {
const parsedJsonColumn: TransformedEntities.ParsedJsonColumn[] = JSON.parse(this.jsonColumn);
return parsedJsonColumn;
}
@beforeSave()
public static async handleJSONBeforeSave(model: MyModel) {
if (typeof model.jsonColumn === 'object') {
model.jsonColumn = JSON.stringify(model.jsonColumn);
}
}
} This way, you can directly pass a strigified JSON or an object or array to your column and this hook will handle the rest. |
Beta Was this translation helpful? Give feedback.
-
You can also use // Optionally add types for your column
type MyJsonColumn = {
foo: string,
bar: number
}
@column({
consume: (value) => JSON.parse(value),
prepare: (value) => JSON.stringify(value),
})
public jsonColumn: MyJsonColumn Be sure to make |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I already tried raising an issue, but it's been abandoned. So, I'm trying it here again. Seems like, with adonisJS it's not possible to save an array into a JSONB-field in PostgreSQL.
adonisjs/lucid#884
Beta Was this translation helpful? Give feedback.
All reactions