Database API

Manage data storage and retrieval using the built-in database functionality.

const db = new DatabaseImpl();
await db.initialize({
  type: 'indexeddb',
  config: {
    databaseName: 'MyApp-DB'
  }
});

Initialize Parameters

ParameterTypeRequiredDescription
optionsDatabaseOptionsYesDatabase configuration options

DatabaseOptions

OptionTypeDefaultDescription
type’indexeddb’ | ‘sqlite''indexeddb’Database type to use
configDatabaseConfig-Configuration object

DatabaseConfig

OptionTypeDefaultDescription
databaseNamestring’BrowserAI-DB’Database name
versionnumber1Database version
storeNamestring’BrowserAIStore’Store name
storesobject-Store configurations

Core Operations

store()

Store a document in the database.

await db.store({
  id: 'doc1',
  text: 'Sample document',
  embedding: [0.1, 0.2, 0.3]
});

Parameters

ParameterTypeRequiredDescription
documentDocumentYesDocument to store
optionsStoreOptionsNoStorage options

get()

Retrieve a document by ID.

const doc = await db.get('doc1');

Parameters

ParameterTypeRequiredDescription
idstringYesDocument ID

Returns

Promise<DatabaseDocument | null>

update()

Update an existing document.

await db.update({
  id: 'doc1',
  text: 'Updated content'
});

Parameters

ParameterTypeRequiredDescription
documentDatabaseDocumentYesUpdated document fields

delete()

Delete a document by ID.

await db.delete('doc1');

Parameters

ParameterTypeRequiredDescription
idstringYesDocument ID to delete

Vector Operations

setVectorStore()

Initialize vector store functionality.

await db.setVectorStore('default');

Parameters

ParameterTypeRequiredDescription
namestringYesVector store name
optionsVectorStoreOptionsNoVector store configuration

addVector()

Add a vector embedding for a document.

await db.addVector('doc1', [0.1, 0.2, 0.3]);

Parameters

ParameterTypeRequiredDescription
idstringYesDocument ID
vectornumber[]YesVector embedding
metadataobjectNoAdditional metadata

searchVector()

Search for similar documents using vector similarity.

const results = await db.searchVector(
  [0.1, 0.2, 0.3], 
  5, 
  { threshold: 0.8 }
);

Parameters

ParameterTypeRequiredDescription
vectornumber[]YesQuery vector
knumberYesNumber of results
optionsSearchOptionsNoSearch configuration

Returns

Promise<Array<{ id: string, score: number }>>

Types

interface Document {
  id: string;
  text?: string;
  embedding?: number[];
  [key: string]: any;
}
 
interface DatabaseConfig {
  databaseName?: string;
  version?: number;
  storeName?: string;
  stores?: Record<string, StoreConfig>;
}
 
interface StoreConfig {
  keyPath?: string;
  indexes?: Array<string>;
}
 
interface SearchOptions {
  threshold?: number;
  filter?: (doc: Document) => boolean;
}