Package Exports
- ai-database
- ai-database/dist/index.js
- ai-database/dist/index.mjs
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (ai-database) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
ai-database
Direct interface to Payload CMS with database.do compatibility.
Installation
npm install ai-database
# or
yarn add ai-database
# or
pnpm add ai-databaseUsage
Node.js Environment
import { getPayload } from 'payload'
import config from '@payload-config'
import { DB } from 'ai-database'
// Initialize with Payload instance
const payload = await getPayload({ config })
const db = DB({ payload })
// Use the same interface as database.do
const posts = await db.posts.find({
where: { status: 'published' },
limit: 10,
})
const post = await db.posts.findOne('post-123')Edge Environment
import { DB } from 'ai-database'
// Initialize with REST API URL
const db = DB({
apiUrl: 'https://your-payload-api.com/api',
apiKey: 'your-api-key',
})
// Use the same interface as database.do
const posts = await db.posts.find({
where: { status: 'published' },
limit: 10,
})
const post = await db.posts.findOne('post-123')Generating Embeddings
import { generateEmbedding, calculateSimilarity } from 'ai-database'
// Generate embeddings for text
const result = await generateEmbedding('Text to embed')
if (result.success) {
console.log('Embedding:', result.embedding)
console.log('Model used:', result.model)
// Calculate similarity between two embeddings
const embedding1 = result.embedding[0]
const embedding2 = await generateEmbedding('Similar text').then((r) => r.embedding?.[0])
if (embedding1 && embedding2) {
const similarity = calculateSimilarity(embedding1, embedding2)
console.log('Similarity score:', similarity)
}
}API
The API is compatible with database.do, providing the same methods for each collection:
find(options?): Find documents in the collectionfindOne(id): Find a single document by IDcreate(data): Create a new documentupdate(id, data): Update an existing documentdelete(id): Delete a documentsearch(query, options?): Search for documents
Environment-specific Adapters
For more control over environment-specific initialization:
import { createNodeClient, createEdgeClient } from 'ai-database/adapters'
// Node.js
const nodeDb = createNodeClient({ payload })
// Edge
const edgeDb = createEdgeClient({
apiUrl: 'https://your-payload-api.com/api',
})Embedding Functions
generateEmbedding(text, options?): Generate embeddings for text using the AI SDKcalculateSimilarity(embedding1, embedding2): Calculate cosine similarity between two embeddings
License
MIT