mirror of
https://github.com/kbenestad/mdcms.git
synced 2026-06-18 15:24:32 +00:00
80 lines
2 KiB
Markdown
80 lines
2 KiB
Markdown
---
|
|
title: JavaScript SDK
|
|
sort: 110
|
|
section-id: client-sdks
|
|
keywords: JavaScript, TypeScript, SDK, Node.js, browser, npm, client
|
|
description: The NeuralDB JavaScript/TypeScript SDK for Node.js and browser environments
|
|
language: en
|
|
---
|
|
|
|
# JavaScript SDK
|
|
|
|
## Installation
|
|
|
|
```bash
|
|
npm install @neuraldb/client
|
|
```
|
|
|
|
## Basic Setup
|
|
|
|
```typescript
|
|
import { NeuralDB } from '@neuraldb/client';
|
|
|
|
const client = new NeuralDB({
|
|
connectionString: process.env.NEURALDB_URL!,
|
|
ssl: { rejectUnauthorized: true },
|
|
});
|
|
await client.connect();
|
|
```
|
|
|
|
### Connection Pool
|
|
|
|
```typescript
|
|
import { NeuralDBPool } from '@neuraldb/client';
|
|
|
|
const pool = new NeuralDBPool({
|
|
connectionString: process.env.NEURALDB_URL!,
|
|
max: 20,
|
|
idleTimeoutMillis: 30000,
|
|
});
|
|
```
|
|
|
|
## Vector Operations
|
|
|
|
```typescript
|
|
import { toVector } from '@neuraldb/client';
|
|
|
|
await client.query(
|
|
'INSERT INTO documents (content, embedding) VALUES ($1, $2)',
|
|
['My document content', toVector([0.023, -0.187, 0.412])]
|
|
);
|
|
|
|
async function semanticSearch(query: string, limit = 10) {
|
|
const embeddingResponse = await openai.embeddings.create({
|
|
model: 'text-embedding-3-small', input: query,
|
|
});
|
|
const queryVector = embeddingResponse.data[0].embedding;
|
|
const { rows } = await client.query<{ id: string; content: string; similarity: number }>(
|
|
`SELECT id, content, 1 - (embedding <=> $1) AS similarity
|
|
FROM documents WHERE embedding IS NOT NULL
|
|
ORDER BY embedding <=> $1 LIMIT $2`,
|
|
[toVector(queryVector), limit]
|
|
);
|
|
return rows;
|
|
}
|
|
```
|
|
|
|
## High-Level Document API
|
|
|
|
```typescript
|
|
import { DocumentStore } from '@neuraldb/client';
|
|
|
|
const store = new DocumentStore(client, {
|
|
table: 'documents',
|
|
embeddingColumn: 'embedding',
|
|
embeddingModel: { provider: 'openai', model: 'text-embedding-3-small', apiKey: process.env.OPENAI_API_KEY! },
|
|
});
|
|
|
|
await store.add([{ content: 'First document', metadata: { source: 'web' } }]);
|
|
const results = await store.search('query text', { limit: 10, filter: { source: 'web' } });
|
|
```
|