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

[feature request]: automatically JSON.stringify arrays stored as jsonb columns #106

Open
dajocarter opened this issue Jun 5, 2020 · 0 comments

Comments

@dajocarter
Copy link

I'm storing an array of strings in a jsonb column and before I save it to the DB I have to JSON.stringify() the array. However, this isn't necessary when saving an object.

For example, in my model's create method, i have to look for the list and stringify it before creation

class ModelName {
  static async create (model, postgresClient) {
    if (model.someList) model.someList = JSON.stringify(model.someList)
    // I would like to avoid this ^
    const saved = await ModelName.objects.connection(postgresClient)
      .create(snakeCaseKeys(model))

My SQL table looks like

CREATE TABLE model_name (
  ...
  some_list jsonb DEFAULT NULL
  ...
)

In my model I have

ModelName.rawObjects = orm(ModelName, {
  ...
  some_list: { anyOf: [{ type: 'null' }, { items: { type: 'string' } }], default: null},
  ...
})

and in my handler decorator I have

routeName.decorators = [
  validate.body({
    type: 'object',
    required: [],
    properties: {
      ...
      someList: { anyOf: [{ type: 'null' }, { type: 'array' }], default: null },
      ...
    }
  })
]
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

1 participant