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

[plugin]: orama schema from json schema #452

Closed
wants to merge 15 commits into from
47 changes: 47 additions & 0 deletions packages/docs/pages/plugins/plugin-json-schema.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { Tab, Tabs } from 'nextra-theme-docs'

# JSON Schema plugin

[JSON Schema](https://json-schema.org/) is a declarative language that allows you to annotate and validate JSON documents.

Orama provides its own official plugin to convert JSON Schema to Orama schema.

## Installation

You can install the plugin using any major Node.js package manager:

<Tabs items={['npm', 'yarn', 'pnpm']}>
<Tab>
```bash copy
npm install @orama/plugin-json-schema
```
</Tab>
<Tab>
```bash copy
yarn add @orama/plugin-json-schema
```
</Tab>
<Tab>
```bash copy
pnpm add @orama/plugin-json-schema
```
</Tab>
</Tabs>


## Usage

When you configure Orama, you'll also import the `schemaFromJson` function from this plguin:

```js
import { create } from '@orama/orama'
import { schemaFromJson } from '@orama/plugin-json-schema'

const jsonSchema = { ... }

const db = await create({
schema: schemaFromJson(jsonSchema),
})
```

And that's it! The Orama plugin will do the rest for you.
21 changes: 21 additions & 0 deletions packages/plugin-json-schema/LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2023 OramaSearch Inc.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
13 changes: 13 additions & 0 deletions packages/plugin-json-schema/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# JSON Schema plugin

[![Tests](https://github.com/oramasearch/orama/actions/workflows/turbo.yml/badge.svg)](https://github.com/oramasearch/orama/actions/workflows/turbo.yml)

Official plugin to convert JSON schema into Orama schema!

# Usage

For the complete usage guide, please refer to the [official plugin documentation](https://docs.oramasearch.com/plugins/plugin-json-schema).

# License

[MIT](/LICENSE.md)
58 changes: 58 additions & 0 deletions packages/plugin-json-schema/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
{
"name": "@orama/plugin-json-schema",
"version": "0.0.1",
"description": "Schema utilities for Orama",
"keywords": [
"orama",
"search",
"schema",
"json"
],
"license": "MIT",
"repository": {
"type": "git",
"url": "https://github.com/oramasearch/orama"
},
"bugs": {
"url": "https://github.com/oramasearch/orama"
},
"type": "module",
"sideEffects": false,
"main": "./dist/commonjs.cjs",
"exports": {
".": {
"types": "./dist/index.d.ts",
"import": "./dist/index.js",
"require": "./dist/commonjs.cjs"
}
},
"types": "./dist/index.d.ts",
"files": [
"dist"
],
"scripts": {
"dev": "swc --delete-dir-on-start -s -w --extensions .ts,.cts -d dist src",
"build": "swc --delete-dir-on-start --extensions .ts,.cts -d dist src",
"postbuild": "tsc -p . --emitDeclarationOnly",
"test": "c8 -c test/config/c8.json tap --rcfile=test/config/tap.yml test/*.test.ts"
},
"dependencies": {
"@orama/orama": "workspace:*"
},
"devDependencies": {
"@swc/cli": "^0.1.59",
"@swc/core": "^1.3.27",
"@types/json-schema": "^7.0.12",
"@types/node": "^18.11.18",
"@types/tap": "^15.0.7",
"c8": "^7.12.0",
"tap": "^16.3.4",
"tsx": "^3.12.2",
"typescript": "^4.9.4"
},
"config": {
"commitizen": {
"path": "cz-conventional-changelog"
}
}
}
3 changes: 3 additions & 0 deletions packages/plugin-json-schema/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { schemaFromJson } from './jsonSchema.js'

export { schemaFromJson }
45 changes: 45 additions & 0 deletions packages/plugin-json-schema/src/jsonSchema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import type { Schema, SearchableType } from '@orama/orama';
import type { JSONSchema4 } from 'json-schema';

const isJsonObject = (jsonSchema: JSONSchema4) => jsonSchema.type === 'object'

const assertTypeObject = (jsonSchema: JSONSchema4) => {
if (!isJsonObject(jsonSchema)) {
throw new Error('Provided JSON schema must be an object type');
}
}

const ORAMA_SUPPORTED_TYPES: Set<JSONSchema4['type']> = new Set(['string', 'number', 'boolean'])

const isArraySupportedByOrama = (jsonSchema: JSONSchema4): boolean => {
if (jsonSchema.type === 'array' && jsonSchema.items && !Array.isArray(jsonSchema.items)) {
return ORAMA_SUPPORTED_TYPES.has(jsonSchema.items.type);
}
return false
}

const isSupportedByOrama = (jsonSchema: JSONSchema4): boolean => {
return ORAMA_SUPPORTED_TYPES.has(jsonSchema.type) || isArraySupportedByOrama(jsonSchema);
}

const extractOramaType = (jsonSchema: JSONSchema4): SearchableType => {
const oramaType = ORAMA_SUPPORTED_TYPES.has(jsonSchema.type) ? jsonSchema.type : `${(jsonSchema.items as JSONSchema4)!.type}[]`

return oramaType as SearchableType
}

export const schemaFromJson = async (jsonSchema: JSONSchema4): Promise<Schema> => {
assertTypeObject(jsonSchema)

const oramaSchema: Schema = {}

for (const [propertyName, propertyDefinition] of Object.entries(jsonSchema.properties || {})) {
if (isSupportedByOrama(propertyDefinition)) {
oramaSchema[propertyName] = extractOramaType(propertyDefinition)
} else if (isJsonObject(propertyDefinition)) {
oramaSchema[propertyName] = await schemaFromJson(propertyDefinition)
}
}

return oramaSchema;
}
8 changes: 8 additions & 0 deletions packages/plugin-json-schema/test/config/c8.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"check-coverage": true,
"reporter": ["text", "json"],
"branches": 80,
"functions": 80,
"lines": 80,
"statements": 80
}
8 changes: 8 additions & 0 deletions packages/plugin-json-schema/test/config/tap.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
jobs: 5
timeout: 120
reporter: spec
coverage: false
node-arg:
- --loader=tsx
- --no-warnings=loader
Loading