UNPKG

@propickler/localvectordb

Version:

A lightweight local vector database implementation in Node.js similar to ChromaDB

55 lines (47 loc) 1.78 kB
const express = require('express'); const VectorDB = require('./vectordb'); const Collection = require('./collection'); const app = express(); app.use(express.json()); const db = new VectorDB(); // Create a new collection app.post('/collections', async (req, res) => { const { name, dimension } = req.body; try { const collection = await db.createCollection(name, dimension); res.json({ status: 'success', collection: collection.name }); } catch (error) { res.status(400).json({ status: 'error', message: error.message }); } }); // Add vectors to a collection app.post('/collections/:name/add', async (req, res) => { const { ids, embeddings, metadatas } = req.body; try { const collection = db.getCollection(req.params.name); await collection.add({ ids, embeddings, metadatas }); res.json({ status: 'success' }); } catch (error) { res.status(400).json({ status: 'error', message: error.message }); } }); // Query vectors from a collection app.post('/collections/:name/query', async (req, res) => { const { queryEmbeddings, nResults } = req.body; try { const collection = db.getCollection(req.params.name); const results = await collection.query({ queryEmbeddings, nResults }); res.json({ status: 'success', results }); } catch (error) { const status = error.message.includes('not found') ? 404 : 400; res.status(status).json({ status: 'error', message: error.message }); } }); // Only start the server if this file is run directly if (require.main === module) { const PORT = process.env.PORT || 3000; app.listen(PORT, () => { console.log(`VectorDB server running on port ${PORT}`); }); } module.exports = app;