Skip to content

Commit

Permalink
feat: adds new package (Orama Switch) (#782)
Browse files Browse the repository at this point in the history
  • Loading branch information
micheleriva authored Sep 10, 2024
1 parent 0ad54e9 commit 9160a61
Show file tree
Hide file tree
Showing 7 changed files with 233 additions and 20 deletions.
13 changes: 13 additions & 0 deletions packages/switch/LICENSE.md
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.
65 changes: 65 additions & 0 deletions packages/switch/README.md
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)
40 changes: 40 additions & 0 deletions packages/switch/package.json
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"
}
}
36 changes: 36 additions & 0 deletions packages/switch/src/index.ts
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>>>
}
}
}
19 changes: 19 additions & 0 deletions packages/switch/tsconfig.json
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"]
}
12 changes: 12 additions & 0 deletions packages/switch/tsup.config.ts
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'],
})
Loading

0 comments on commit 9160a61

Please sign in to comment.