-
-
Notifications
You must be signed in to change notification settings - Fork 340
[plugin]: orama schema from json schema #452
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
Closed
Closed
Changes from 10 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
b606b9d
fix: setup
ilteoood 02e2bd1
feat: first implementation
ilteoood cd1732a
fix: basic tests
ilteoood 14fbb5f
feat: basic tests
ilteoood 9ea7932
chore: additional object test
ilteoood 01ebfe4
chore: missing properties test
ilteoood ead2ebc
fix: mising tsconfig
ilteoood adb4df8
fix: ts types
ilteoood 7644e87
fix: tsc build
ilteoood aecdf64
feat: support for nested objects
ilteoood bf8ea3f
fix: make function async
ilteoood 9ce4015
fix: package name
ilteoood 4b46172
Merge branch 'oramasearch:main' into main
ilteoood bac4c90
fix: license and readme
ilteoood 39d2718
feat: doc
ilteoood File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
{ | ||
"name": "@orama/schema", | ||
"version": "0.0.1", | ||
"description": "Schema utilities for Orama", | ||
"keywords": [ | ||
"orama", | ||
"search", | ||
"schema" | ||
], | ||
"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" | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import { schemaFromJson } from './jsonSchema.js' | ||
|
||
export { schemaFromJson } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 = (jsonSchema: JSONSchema4) => { | ||
ilteoood marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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] = schemaFromJson(propertyDefinition) | ||
} | ||
ilteoood marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
return oramaSchema; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.