-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: new persistence api * add: tests * add: tests * chore: code optimizations * chore: clean tests * docs: update readme * fix: typescript * from feedback
- Loading branch information
Showing
10 changed files
with
440 additions
and
175 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 |
---|---|---|
|
@@ -147,3 +147,5 @@ orama.json | |
package-lock.json | ||
pnpm-lock.yaml | ||
yarn.lock | ||
*.msp | ||
orama_[0-9]*.json |
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
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 |
---|---|---|
@@ -1,20 +1,48 @@ | ||
import type { FastifyPluginCallback } from 'fastify' | ||
import type { Document, Orama, Results, SearchParams } from '@orama/orama' | ||
import type { Document, Orama, ProvidedTypes, Results, SearchParams, create } from '@orama/orama' | ||
|
||
type OramaInstance = { | ||
schema: Orama['schema'], | ||
interface OramaPersistence { | ||
restore: () => Promise<ReturnType<typeof create> | null> | ||
persist: (data: ReturnType<typeof create>) => Promise<any> | ||
} | ||
|
||
declare const FastifyOrama: FastifyPluginCallback<OramaInstance> | ||
declare class PersistenceInMemory implements OramaPersistence { | ||
constructor(options?: { | ||
jsonIndex?: string, | ||
}) | ||
restore: () => Promise<Promise<Orama<ProvidedTypes>> | null> | ||
persist: (data: Promise<Orama<ProvidedTypes>>) => Promise<string> | ||
} | ||
|
||
declare class PersistenceInFile implements OramaPersistence { | ||
constructor(options?: { | ||
filePath?: string, | ||
format?: string, | ||
mustExistOnStart?: boolean | ||
}) | ||
restore: () => Promise<Promise<Orama<ProvidedTypes>> | null> | ||
persist: (data: Promise<Orama<ProvidedTypes>>) => Promise<string> | ||
} | ||
|
||
type OramaPluginOptions = { | ||
persistence?: OramaPersistence | ||
} & Partial<Parameters<typeof create>[0]> | ||
|
||
declare const fastifyOrama: FastifyPluginCallback<OramaPluginOptions> | ||
|
||
declare module 'fastify' { | ||
interface FastifyInstance { | ||
orama: OramaInstance & { | ||
orama: { | ||
insert: (document: Document) => Promise<string>, | ||
search: (params: SearchParams) => Promise<Results> | ||
search: (params: SearchParams) => Promise<Results>, | ||
save?: () => Promise<any>, | ||
} | ||
} | ||
} | ||
|
||
export default FastifyOrama | ||
export { FastifyOrama } | ||
export { fastifyOrama as default } | ||
export { | ||
fastifyOrama, | ||
PersistenceInMemory, | ||
PersistenceInFile | ||
} |
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 |
---|---|---|
@@ -1,55 +1,53 @@ | ||
import fp from 'fastify-plugin' | ||
import { create, insert, search } from '@orama/orama' | ||
import path from 'path' | ||
import { existsSync } from 'fs' | ||
import { restoreFromFile, persistToFile } from '@orama/plugin-data-persistence/server' | ||
import { create, insert, search } from '@orama/orama' // todo we are limiting the api to the server side | ||
|
||
async function FastifyOrama (fastify, options) { | ||
const { | ||
schema, | ||
defaultLanguage = 'english', | ||
stemming = true, | ||
persistence = false | ||
} = options | ||
import PersistenceInMemory from './lib/persistence/in-memory.js' | ||
import PersistenceInFile from './lib/persistence/in-file.js' | ||
|
||
async function fastifyOrama (fastify, options) { | ||
if (fastify.orama) { | ||
throw new Error('fastify-orama is already registered') | ||
} | ||
|
||
const { | ||
persistence, | ||
...oramaOptions | ||
} = options | ||
|
||
let db | ||
let dbName | ||
let dbFormat | ||
|
||
const oramaApi = { | ||
insert: (...args) => insert(db, ...args), | ||
search: (...args) => search(db, ...args), | ||
save: undefined | ||
} | ||
|
||
if (persistence) { | ||
dbName = options.persistency?.name || './orama.json' | ||
dbFormat = options.persistency?.format || 'json' | ||
const databaseExists = existsSync(path.resolve(dbName)) | ||
db = await persistence.restore() | ||
|
||
if (!databaseExists) { | ||
throw new Error(`The database file ${dbName} does not exist`) | ||
oramaApi.save = /* async */ function save () { | ||
return persistence.persist(db) | ||
} | ||
} | ||
|
||
db = await restoreFromFile(dbFormat, `./${dbName}`) | ||
} else { | ||
if (!schema) { | ||
if (!db) { | ||
if (!oramaOptions.schema) { | ||
throw new Error('You must provide a schema to create a new database') | ||
} | ||
|
||
db = await create({ | ||
schema, | ||
defaultLanguage, | ||
stemming | ||
}) | ||
db = await create(oramaOptions) | ||
} | ||
|
||
fastify.decorate('orama', { | ||
insert: (...args) => insert(db, ...args), | ||
search: (...args) => search(db, ...args), | ||
save: () => persistToFile(db, dbFormat, dbName) | ||
}) | ||
fastify.decorate('orama', oramaApi) | ||
} | ||
|
||
export default fp(FastifyOrama, { | ||
export default fp(fastifyOrama, { | ||
fastify: '4.x', | ||
name: '@fastify/orama' | ||
name: 'fastify-orama' | ||
}) | ||
|
||
export { | ||
fastifyOrama, | ||
PersistenceInMemory, | ||
PersistenceInFile | ||
} |
Oops, something went wrong.