-
-
Notifications
You must be signed in to change notification settings - Fork 296
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: adds new package (Orama Switch) (#782)
- Loading branch information
1 parent
0ad54e9
commit 9160a61
Showing
7 changed files
with
233 additions
and
20 deletions.
There are no files selected for viewing
This file contains 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,13 @@ | ||
Copyright 2024 OramaSearch Inc. | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. |
This file contains 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,65 @@ | ||
# Orama Switch | ||
|
||
Orama Switch allows you to run queries on Orama Cloud and OSS with a single interface. | ||
|
||
## Installation | ||
|
||
```sh | ||
npm i @orama/switch | ||
``` | ||
|
||
## Usage | ||
|
||
You can use the same APIs to access either Orama Cloud or Orama OSS. | ||
|
||
For instance, this is how you would interact with Orama Cloud: | ||
|
||
```js | ||
import { Switch } from '@orama/switch' | ||
import { OramaClient } from '@oramacloud/client' | ||
|
||
const client = new OramaClient({ | ||
endpoint: '<Your Orama Cloud Endpoint>', | ||
api_key: '<Your Orama Cloud API Key>', | ||
}) | ||
|
||
const orama = new Switch(client) | ||
|
||
const results = await orama.search({ | ||
term: 'noise cancelling headphones', | ||
where: { | ||
price: { | ||
lte: 99.99 | ||
} | ||
} | ||
}) | ||
``` | ||
|
||
And this is Orama OSS: | ||
|
||
```js | ||
import { Switch } from '@orama/switch' | ||
import { create } from '@orama/orama' | ||
|
||
const db = await create({ | ||
schema: { | ||
productName: 'string', | ||
price: 'number' | ||
} | ||
}) | ||
|
||
const orama = new Switch(client) | ||
|
||
const results = await orama.search({ | ||
term: 'noise cancelling headphones', | ||
where: { | ||
price: { | ||
lte: 99.99 | ||
} | ||
} | ||
}) | ||
``` | ||
|
||
## License | ||
|
||
[Apache 2.0](/LICENSE.md) |
This file contains 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,40 @@ | ||
{ | ||
"name": "@orama/switch", | ||
"version": "2.0.24", | ||
"description": "Orama Switch allows you to run queries on Orama Cloud and OSS with a single interface", | ||
"main": "./dist/index.js", | ||
"types": "./dist/index.d.ts", | ||
"exports": { | ||
".": { | ||
"default": "./dist/index.js", | ||
"import": "./dist/index.js", | ||
"require": "./dist/index.cjs", | ||
"node": "./dist/index.cjs", | ||
"types": "./dist/index.d.ts" | ||
} | ||
}, | ||
"files": [ | ||
"dist" | ||
], | ||
"scripts": { | ||
"build": "tsup" | ||
}, | ||
"keywords": [ | ||
"orama", | ||
"orama cloud" | ||
], | ||
"type": "module", | ||
"author": { | ||
"name": "Michele Riva", | ||
"email": "[email protected]", | ||
"url": "https://github.com/micheleriva" | ||
}, | ||
"peerDependencies": { | ||
"@orama/orama": "workspace:*", | ||
"@oramacloud/client": "1.3.15" | ||
}, | ||
"license": "Apache-2.0", | ||
"devDependencies": { | ||
"tsup": "^7.2.0" | ||
} | ||
} |
This file contains 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,36 @@ | ||
import type { AnyOrama, Results, SearchParams, Nullable } from '@orama/orama' | ||
import { search } from '@orama/orama' | ||
import { OramaClient, ClientSearchParams } from '@oramacloud/client' | ||
|
||
type OramaSwitchClient = AnyOrama | OramaClient | ||
|
||
type ClientType = 'oss' | 'cloud' | ||
|
||
export class Switch<T = OramaSwitchClient> { | ||
client: OramaSwitchClient | ||
clientType: ClientType | ||
isCloud: boolean = false | ||
isOSS: boolean = false | ||
|
||
constructor(client: OramaSwitchClient) { | ||
this.client = client | ||
|
||
if (client instanceof OramaClient) { | ||
this.clientType = 'cloud' | ||
this.isCloud = true | ||
} else if (typeof client === 'object' && 'id' in client && 'tokenizer' in client) { | ||
this.clientType = 'oss' | ||
this.isOSS = true | ||
} else { | ||
throw new Error('Invalid client. Expected either an OramaClient or an Orama OSS database.') | ||
} | ||
} | ||
|
||
async search<R = unknown>(params: T extends OramaClient ? ClientSearchParams : SearchParams<AnyOrama>): Promise<Nullable<Results<R>>> { | ||
if (this.isCloud) { | ||
return (this.client as OramaClient).search(params as T extends OramaClient ? ClientSearchParams : never) | ||
} else { | ||
return search(this.client as AnyOrama, params as SearchParams<AnyOrama>) as Promise<Nullable<Results<R>>> | ||
} | ||
} | ||
} |
This file contains 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,19 @@ | ||
{ | ||
"compilerOptions": { | ||
"allowJs": true, | ||
"target": "ESNext", | ||
"module": "NodeNext", | ||
"outDir": "dist", | ||
"lib": ["ESNext", "DOM"], | ||
"esModuleInterop": true, | ||
"declaration": true, | ||
"forceConsistentCasingInFileNames": true, | ||
"strict": true, | ||
"noImplicitAny": false, | ||
"skipLibCheck": true, | ||
"resolveJsonModule": true, | ||
"sourceMap": true, | ||
"moduleResolution": "nodenext" | ||
}, | ||
"include": ["src/*.ts", "src/**/*.ts"] | ||
} |
This file contains 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,12 @@ | ||
import { defineConfig } from 'tsup' | ||
|
||
export default defineConfig({ | ||
entry: ['src/index.ts'], | ||
splitting: true, | ||
sourcemap: true, | ||
clean: true, | ||
bundle: true, | ||
dts: true, | ||
minify: false, | ||
format: ['cjs', 'esm', 'iife'], | ||
}) |
Oops, something went wrong.