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

breaking: migrate to cjs #163

Merged
merged 3 commits into from
Oct 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,58 @@ app.get('/genId', async function handler (req, reply) {
})
```

## Typescript

This plugin comes with Typescript support out of the box.
Using the `withOrama` helper, you can access the `orama` decorator in your Fastify application with the correct schema.

```ts
import Fastify from 'fastify'

import { PersistenceInMemory, fastifyOrama } from 'fastify-orama'

const app = Fastify()

const mySchema = {
quote: 'string',
author: 'string'
} as const

await app.register(fastifyOrama, {
schema: mySchema,
persistence: new PersistenceInMemory()
})

const appWithOrama = app.withOrama<typeof mySchema>()
const id = await appWithOrama.orama.insert({ quote: 'Hello', author: 'World' })

appWithOrama.get('/hello', async () => {

const {orama} = appWithOrama
const result = await orama.search({ term: 'hello' })

return {
hello: result.hits
}
})
```

Usage with `fastify-plugin`:

```ts
import fp from 'fastify-plugin'

fp(function plugins(fastify) {
const fastifyWithOrama = fastify.withOrama<typeof mySchema>()

expectType<{
insert: (document: PartialSchemaDeep<TypedDocument<Orama<typeof mySchema>>>) => Promise<string>,
search: (params: SearchParams<Orama<Schema<typeof mySchema>>, typeof mySchema>) => Promise<Results<Schema<typeof mySchema>>>,
persist?: () => Promise<any>,
}>(fastifyWithOrama.orama)
})
```

## License

fastifyOrama is licensed under the [MIT](LICENSE) license.
41 changes: 23 additions & 18 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,42 +1,47 @@
import type { FastifyPluginCallback } from 'fastify'
import type { Document, Orama, ProvidedTypes, Results, SearchParams, create } from '@orama/orama'
import type { TypedDocument, insert, Orama, Results, SearchParams, create, AnyOrama, PartialSchemaDeep, Schema } from '@orama/orama'
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can insert and AnyOrama be removed here? They are no longer used.


interface OramaPersistence {
restore: () => Promise<ReturnType<typeof create> | null>
persist: (data: ReturnType<typeof create>) => Promise<any>
interface FastifyOramaPersistence<T = any, O = any> {
restore: () => Promise<Orama<T> | null>
persist: (data: Orama<T>) => Promise<O>
}

declare class PersistenceInMemory implements OramaPersistence {

declare class PersistenceInMemory<T = any, O = string | Buffer> implements FastifyOramaPersistence<T, O> {
constructor(options?: {
jsonIndex?: string,
})
restore: () => Promise<Promise<Orama<ProvidedTypes>> | null>
persist: (data: Promise<Orama<ProvidedTypes>>) => Promise<string>
restore: () => Promise<Orama<T> | null>
persist: (data: Orama<T>) => Promise<O>
}

declare class PersistenceInFile implements OramaPersistence {
declare class PersistenceInFile<T = any, O = string> implements FastifyOramaPersistence<T, O> {
constructor(options?: {
filePath?: string,
format?: string,
mustExistOnStart?: boolean
})
restore: () => Promise<Promise<Orama<ProvidedTypes>> | null>
persist: (data: Promise<Orama<ProvidedTypes>>) => Promise<string>
restore: () => Promise<Orama<T> | null>
persist: (data: Orama<T>) => Promise<O>
}

type OramaPluginOptions = {
persistence?: OramaPersistence
type FastifyOramaPluginOptions = {
persistence?: FastifyOramaPersistence
} & Partial<Parameters<typeof create>[0]>

declare const fastifyOrama: FastifyPluginCallback<OramaPluginOptions>
declare const fastifyOrama: FastifyPluginCallback<FastifyOramaPluginOptions>

interface OramaApi<T> {
insert: (document: PartialSchemaDeep<TypedDocument<Orama<T>>>) => Promise<string>,
search: (params: SearchParams<Orama<Schema<T>>, T>) => Promise<Results<Schema<T>>>,
persist?: () => Promise<any>,
}

declare module 'fastify' {
interface FastifyInstance {
orama: {
insert: (document: Document) => Promise<string>,
search: (params: SearchParams) => Promise<Results>,
persist?: () => Promise<any>,
}
withOrama<T>(): this & {
orama: OramaApi<T>
};
}
}

Expand Down
27 changes: 16 additions & 11 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import fp from 'fastify-plugin'
import * as Orama from '@orama/orama'
'use strict'

import PersistenceInMemory from './lib/persistence/in-memory.js'
import PersistenceInFile from './lib/persistence/in-file.js'
const fp = require('fastify-plugin')
const Orama = require('@orama/orama')

const PersistenceInMemory = require('./lib/persistence/in-memory.js')
const PersistenceInFile = require('./lib/persistence/in-file.js')

const SKIP_METHODS = [
'create'
Expand Down Expand Up @@ -49,17 +51,20 @@ async function fastifyOrama (fastify, options) {
db = await Orama.create(oramaOptions)
}

function withOrama () {
return this
}

fastify.decorate('orama', oramaApi)
fastify.decorate('withOrama', withOrama)
}

export default fp(fastifyOrama, {
module.exports = fp(fastifyOrama, {
fastify: '4.x',
name: 'fastify-orama'
})

export {
fastifyOrama,
PersistenceInMemory,
PersistenceInFile,
oramaInternals
}
module.exports.fastifyOrama = fastifyOrama
module.exports.PersistenceInMemory = PersistenceInMemory
module.exports.PersistenceInFile = PersistenceInFile
module.exports.oramaInternals = oramaInternals
50 changes: 0 additions & 50 deletions index.test-d.ts

This file was deleted.

10 changes: 6 additions & 4 deletions lib/persistence/in-file.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import path from 'node:path'
import { existsSync } from 'node:fs'
import { restoreFromFile, persistToFile } from '@orama/plugin-data-persistence/server'
'use strict'

const path = require('node:path')
const { existsSync } = require('node:fs')
const { restoreFromFile, persistToFile } = require('@orama/plugin-data-persistence/server')

class PersistenceInFile {
constructor (options = {}) {
Expand Down Expand Up @@ -30,4 +32,4 @@ class PersistenceInFile {
}
}

export default PersistenceInFile
module.exports = PersistenceInFile
6 changes: 4 additions & 2 deletions lib/persistence/in-memory.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { restore, persist } from '@orama/plugin-data-persistence'
'use strict'

const { restore, persist } = require('@orama/plugin-data-persistence')

class PersistenceInMemory {
constructor (options) {
Expand All @@ -18,4 +20,4 @@ class PersistenceInMemory {
}
}

export default PersistenceInMemory
module.exports = PersistenceInMemory
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
"version": "0.7.0",
"description": "Orama search-engine plugin for Fastify",
"main": "index.js",
"type": "module",
"types": "index.d.ts",
"author": {
"name": "Mateo Nunez",
Expand All @@ -14,7 +13,7 @@
"lint": "standard | snazzy",
"lint:fix": "standard --fix | snazzy",
"pretest": "npm run clean",
"test": "npm run lint && npm run unit && npm run typescript",
"test": "npm run lint && npm run unit && npm run typescript && ts-node test/types/index.ts",
"posttest": "npm run clean",
"typescript": "tsd",
"prepare": "husky install",
Expand Down Expand Up @@ -51,10 +50,14 @@
"husky": "^8.0.3",
"snazzy": "^9.0.0",
"standard": "^17.1.0",
"ts-node": "^10.9.1",
"tsd": "^0.29.0",
"typescript": "^5.2.2"
},
"publishConfig": {
"access": "public"
},
"tsd": {
"directory": "test/types"
}
}
23 changes: 19 additions & 4 deletions test/index.test.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
'use strict'

import { it } from 'node:test'
import { ok, strictEqual } from 'node:assert'
import Fastify from 'fastify'
import fastifyOrama from '../index.js'
const { it } = require('node:test')
const { ok, strictEqual } = require('node:assert')
const Fastify = require('fastify')
const fastifyOrama = require('../index.js')

it('Should register correctly fastifyOrama plugin', async () => {
const fastify = Fastify()
Expand Down Expand Up @@ -72,3 +72,18 @@ it('Should throw when trying to register multiple instances without giving a nam
strictEqual(error.message, 'fastify-orama is already registered')
}
})

it('Expose a withOrama function', async () => {
const fastify = Fastify()

await fastify.register(fastifyOrama, {
schema: {
quote: 'string',
author: 'string'
}
})

const withOrama = fastify.withOrama()

strictEqual(fastify.orama, withOrama.orama)
})
8 changes: 4 additions & 4 deletions test/orama-proxy.test.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
'use strict'

import { it } from 'node:test'
import { ok, strictEqual } from 'node:assert'
import Fastify from 'fastify'
import { fastifyOrama, oramaInternals } from '../index.js'
const { it } = require('node:test')
const { ok, strictEqual } = require('node:assert')
const Fastify = require('fastify')
const { fastifyOrama, oramaInternals } = require('../index.js')

it('Should expose all the Orama APIs', async () => {
const fastify = Fastify()
Expand Down
14 changes: 8 additions & 6 deletions test/persistence.test.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { describe, it } from 'node:test'
import { strictEqual, match } from 'node:assert'
import Fastify from 'fastify'
import { fastifyOrama, PersistenceInMemory, PersistenceInFile } from '../index.js'
import { create, insert } from '@orama/orama'
import { persistToFile } from '@orama/plugin-data-persistence/server'
'use strict'

const { describe, it } = require('node:test')
const { strictEqual, match } = require('node:assert')
const Fastify = require('fastify')
const { fastifyOrama, PersistenceInMemory, PersistenceInFile } = require('../index.js')
const { create, insert } = require('@orama/orama')
const { persistToFile } = require('@orama/plugin-data-persistence/server')

async function buildFakeDb (filePath, format) {
const db = await create({
Expand Down
Loading