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

Support defining a db with jsonschema #445

Open
mrbrianevans opened this issue Jul 15, 2023 · 4 comments · May be fixed by #453
Open

Support defining a db with jsonschema #445

mrbrianevans opened this issue Jul 15, 2023 · 4 comments · May be fixed by #453

Comments

@mrbrianevans
Copy link
Contributor

Is your feature request related to a problem? Please describe.
JSON schema is a widely used and understood format for defining the shape of data structures. Existing applications already make use of it and have schemas written for entities. It would make adoption of orama search easier if a database could be created using a json schema rather than only the custom schema DDL that orama currently offers.

Describe the solution you'd like

import { create } from '@orama/orama'
 
const movieDB = await create({
  jsonschema: {
    type: 'object',
    properties: {
      title: { type: 'string' },
      director: { type: 'string' },
      plot: { type: 'string' },
      year: { type: 'number' },
      isFavorite: { type: 'boolean' }
    },
    required: [ 'title', 'director', 'plot', 'year', 'isFavorite' ]
  },
})

Describe alternatives you've considered
Writing my own custom script to try and convert my existing json schemas into oramas schema format.

Additional context
JSON schema website: https://json-schema.org/
There is widely available tooling available for json schema, and generating json schema from almost any language.
Consider the case of a very complex ecommerce site with a Golang backend, all the entities are defined in Go structs and stored in Mongodb. They want frontend in-memory search. They would need to rewrite all their schemas in oramas format to make the fields searchable.
With my feature request, they could simply run their Go structs through a jsonschema generator such as https://github.com/invopop/jsonschema and then all fields are instantly searchable with no manual schema writing.

Additional benefit
Not the main feature request but a side benefit is that typescript types can be inferred from jsonschema, meaning the database could be automatically typed after being created with a jsonschema, so insert, update and search could all have the correct types without being specified by the user, since it gets inferred from the jsonschema.

@allevo
Copy link
Collaborator

allevo commented Jul 18, 2023

Hi!
Thank you for opening this issue.
The Orama schema has been designed to be as simple as possible, which is why we started in this way.

Directly supporting JSONSchema could be technically feasible but with some limitations:

  • data type: Orama supports only some specific data type
  • an array of objects: Orama doesn't support it

The most suitable solution is to create a library (plugin?) that translates JSONSchema in Orama, skipping untranslatable properties (or throw?).

Anyway, this could be an amazing feature!

WDYT @micheleriva?

@nxf-gerdjungbluth
Copy link

We just had a team discussion about this feature request and really liked it. Maybe we will be able to contribute.

@mrbrianevans
Copy link
Contributor Author

Based on @allevo 's feedback, I suggest something like this:

import { create } from '@orama/orama'
import { schemaFromJson } from '@orama/jsonschema'
 
const movieDB = await create({
  schema: schemaFromJson({
    type: 'object',
    properties: {
      title: { type: 'string' },
      director: { type: 'string' },
      plot: { type: 'string' },
      year: { type: 'number' },
      isFavorite: { type: 'boolean' }
    },
    required: [ 'title', 'director', 'plot', 'year', 'isFavorite' ]
  }),
})

An official plugin that exports a function that converts from a json schema to orama's schema format.

@jessekrubin
Copy link
Contributor

This would pair nicely with typebox.

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

Successfully merging a pull request may close this issue.

4 participants