# @svector/client

> @svector/client client

Latest version **0.0.6** (published 2024-10-27) · 0 weekly downloads

## Install

```sh
npm install @svector/client
pnpm add @svector/client
yarn add @svector/client
bun add @svector/client
```

## Health

**Score 35/100 (D)** — status: maintenance-mode.

Positive: has types; esm support; no vulnerabilities.

Warnings: low downloads; pre 1.0.

Negative: stale; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.6 |
| Published | 2024-10-27 |
| First published | 2024-02-19 |
| Weekly downloads | 0 |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Node | >=16.0.0 |
| Dependencies | 34 |
| Unpacked size | 152.9 KB |
| Known vulnerabilities | 0 (+1 in 1 direct dependencies) |
| Install scripts | no |
| Maintainers | svector |

## Links

- npm: https://www.npmjs.com/package/@svector/client
- npm.io page: https://npm.io/package/@svector/client

## Dependencies (34)

- [tslib](https://npm.io/package/tslib.md) ^2.6.2
- [@smithy/core](https://npm.io/package/@smithy/core.md) ^2.0.1
- [@aws-sdk/core](https://npm.io/package/@aws-sdk/core.md) 3.582.0
- [@smithy/types](https://npm.io/package/@smithy/types.md) ^3.0.0
- [@aws-sdk/types](https://npm.io/package/@aws-sdk/types.md) 3.577.0
- [@smithy/hash-node](https://npm.io/package/@smithy/hash-node.md) ^3.0.0
- [@smithy/util-utf8](https://npm.io/package/@smithy/util-utf8.md) ^3.0.0
- [@smithy/url-parser](https://npm.io/package/@smithy/url-parser.md) ^3.0.0
- [@smithy/util-retry](https://npm.io/package/@smithy/util-retry.md) ^3.0.0
- [@smithy/util-base64](https://npm.io/package/@smithy/util-base64.md) ^3.0.0
- [@aws-crypto/sha256-js](https://npm.io/package/@aws-crypto/sha256-js.md) 3.0.0
- [@smithy/protocol-http](https://npm.io/package/@smithy/protocol-http.md) ^4.0.0
- [@smithy/smithy-client](https://npm.io/package/@smithy/smithy-client.md) ^3.0.1
- [@smithy/config-resolver](https://npm.io/package/@smithy/config-resolver.md) ^3.0.0
- [@smithy/util-middleware](https://npm.io/package/@smithy/util-middleware.md) ^3.0.0
- [@smithy/middleware-retry](https://npm.io/package/@smithy/middleware-retry.md) ^3.0.1
- [@smithy/middleware-serde](https://npm.io/package/@smithy/middleware-serde.md) ^3.0.0
- [@smithy/middleware-stack](https://npm.io/package/@smithy/middleware-stack.md) ^3.0.0
- [@smithy/node-http-handler](https://npm.io/package/@smithy/node-http-handler.md) ^3.0.0
- [@aws-crypto/sha256-browser](https://npm.io/package/@aws-crypto/sha256-browser.md) 3.0.0
- [@aws-sdk/middleware-logger](https://npm.io/package/@aws-sdk/middleware-logger.md) 3.577.0
- [@smithy/fetch-http-handler](https://npm.io/package/@smithy/fetch-http-handler.md) ^3.0.1
- [@smithy/invalid-dependency](https://npm.io/package/@smithy/invalid-dependency.md) ^3.0.0
- [@smithy/node-config-provider](https://npm.io/package/@smithy/node-config-provider.md) ^3.0.0
- [@aws-sdk/util-user-agent-node](https://npm.io/package/@aws-sdk/util-user-agent-node.md) 3.577.0
- [@smithy/util-body-length-node](https://npm.io/package/@smithy/util-body-length-node.md) ^3.0.0
- [@aws-sdk/middleware-user-agent](https://npm.io/package/@aws-sdk/middleware-user-agent.md) 3.583.0
- [@aws-sdk/middleware-host-header](https://npm.io/package/@aws-sdk/middleware-host-header.md) 3.577.0
- [@smithy/util-defaults-mode-node](https://npm.io/package/@smithy/util-defaults-mode-node.md) ^3.0.1
- [@aws-sdk/util-user-agent-browser](https://npm.io/package/@aws-sdk/util-user-agent-browser.md) 3.577.0
- [@smithy/util-body-length-browser](https://npm.io/package/@smithy/util-body-length-browser.md) ^3.0.0
- [@smithy/middleware-content-length](https://npm.io/package/@smithy/middleware-content-length.md) ^3.0.0
- [@smithy/util-defaults-mode-browser](https://npm.io/package/@smithy/util-defaults-mode-browser.md) ^3.0.1
- [@aws-sdk/middleware-recursion-detection](https://npm.io/package/@aws-sdk/middleware-recursion-detection.md) 3.577.0

## Recent versions

- 0.0.6 (latest) — 2024-10-27
- 0.0.5 — 2024-05-29
- 0.0.4 — 2024-03-27
- 0.0.3 — 2024-02-19
- 0.0.2 — 2024-02-19
- 0.0.1 — 2024-02-19

## README

# SvectorDB Client

For more information on SvectorDB, visit the [website](https://svectordb.com/).

## Creating a database

To create a database, visit the [dashboard](https://dash.svectordb.com/) and navigate to the databases page. Click the green plus button and create a database with dimension set to 4, type set to "sandbox" and region set to "us-east-2".

Sandbox databases are free but have limitations. View the [limits](/docs/limits) page for more information.

## Connecting

To authenticate with the database, you will need to generate an API key. To do this, navigate to the [databases page](https://dash.svectordb.com/) and click the key symbol next to the database you want to access. Take note of the API key and the database ID.

Install the client library using npm:

```bash
npm install @svectordb/client
```

Next create a file called `index.js` and add the following code:

```js
const { DatabaseService } = require('@svector/client');

const region = 'us-east-2';
const apiKey = 'f4ead19a-bac7-4ff0-b379-d3f634ed180f'; // replace with your API key
const databaseId = 'f39434565486ee15'; // replace with your database ID

const client = new DatabaseService({
    endpoint: `https://${region}.api.svectordb.com`,
    apiKey: apiKey
});
```

## Writing data

To add data use the `setItem` method, this will upsert the data into the database. Let's insert 10 random documents:

```js
for (let i = 0; i < 10; i++) {
    await client.setItem({
        databaseId,
        key: `random-${i}`,
        value: Buffer.from(`Hello world #${i}!`), // Value is stored as bytes, use Buffer.from to convert a string to bytes
        vector: [Math.random(), Math.random(), Math.random(), Math.random()]
    });
}
```

Now let's see the data we just inserted:

```js
const documents = await client.listItems({databaseId});
console.log(documents);
```

You can also get a single document by key:

```js
const document = await client.getItem({databaseId, key: 'random-1'});
console.log(document);
```

To delete a document, use the `deleteItem` method:

```js
await client.deleteItem({databaseId, key: 'random-1'});
```

## Querying data

To query data, use the `queryItems` method. Let's query the 5 closest documents to the vector `[0.5, 0.5, 0.5, 0.5]`:

```js
const results = await client.query({
    databaseId,
    query: {
        vector: [0.5, 0.5, 0.5, 0.5]
    },
    maxResults: 5,
});

console.log(results);
```

You can also query for vectors closest to an existing document:

```js
const results = await client.query({
    databaseId,
    query: {
        key: 'random-0'
    },
    maxResults: 5,
});

console.log(results);
```

## Full example

Here is the full example:

```js
const { DatabaseService } = require('@svector/client');

const region = 'us-east-2';
const apiKey = 'f4ead19a-bac7-4ff0-b379-d3f634ed180f'; // replace with your API key
const databaseId = 'f39434565486ee15'; // replace with your database ID

const client = new DatabaseService({
    endpoint: `https://${region}.api.svectordb.com`,
    apiKey: apiKey
});

(async () => {
    // Insert 10 random documents
    for (let i = 0; i < 10; i++) {
        await client.setItem({
            databaseId,
            key: `random-${i}`,
            value: Buffer.from(`Hello world #${i}!`), // Value is stored as bytes, use Buffer.from to convert a string to bytes
            vector: [Math.random(), Math.random(), Math.random(), Math.random()]
        });
    }

    // List documents
    const documents = await client.listItems({databaseId});
    console.log(`Found ${documents.items.length} documents`)
    console.log(documents);

    // Get a single document
    const document = await client.getItem({databaseId, key: 'random-1'});
    console.log('Document with key "random-1"');
    console.log(document);

    // Delete a document
    await client.deleteItem({databaseId, key: 'random-1'});

    // Query documents
    const queryByVector = await client.query({
        databaseId,
        query: {
            vector: [0.5, 0.5, 0.5, 0.5]
        },
        maxResults: 5,
    });

    console.log('Query by vector');
    console.log(queryByVector);

    const queryByKey = await client.query({
        databaseId,
        query: {
            key: 'random-0'
        },
        maxResults: 5,
    });

    console.log('Query by key');
    console.log(queryByKey);
})();
```

---
_Source: https://npm.io/package/@svector/client · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
