loukoum icon indicating copy to clipboard operation
loukoum copied to clipboard

Support JSON/JSONB ?

Open kevinschoon opened this issue 6 years ago • 2 comments

Do you had any plans to support Postgres JSON/JSONB data types? One example would be a "containment" query against JSONB structures.

Currently I use a work around like this:

type Tag struct {
    Key string
    Value string
    ....
}
raw, _ := json.Marshal(Tag{Key: "some-key", "Value": "some-value"})
lk.Select().From("table").Where(lk.Raw(fmt.Sprintf(`tags @> '%s'`, string(raw))))

Thank you for your work on this wonderful library!

kevinschoon avatar Mar 09 '18 01:03 kevinschoon

At the moment, I'm afraid not... But I'll be glad to give you some pointers and any help you require to implement this type.

https://github.com/ulule/loukoum/blob/b658af15b7c4b97df2c993875bda0e9fc5cc18fe/stmt/expression.go#L293

You could add below a Json method that accept a JsonOp interface. Something like that:

func (identifier Identifier) Json(op JsonOp) InfixExpression {
	return NewInfixExpression(identifier, op.Operator(), op.Expression())
}

Where JsonOp are all kind of JSON operation that you want to support, like containment, existence or even path traversal...

Hope that helps.

novln avatar Mar 09 '18 13:03 novln

@novln Thanks for the suggestions, I will give this a shot as time permits and create a PR.

kevinschoon avatar Mar 09 '18 16:03 kevinschoon