ai-database
A database abstraction layer for AI applications built on top of PayloadCMS.
Installation
npm install ai-databaseUsage
Standard Methods
import { db } from 'ai-database'
// Find a record
const user = await db.findOne({
collection: 'users',
where: { email: { equals: 'user@example.com' } }
})
// Create a record
const newUser = await db.create({
collection: 'users',
data: {
email: 'user@example.com',
password: 'password',
}
})
// Update a record
const updatedUser = await db.update({
collection: 'users',
id: 'user-id',
data: {
email: 'updated@example.com'
}
})
// Delete a record
await db.delete({
collection: 'users',
id: 'user-id'
})
// Upsert a record
const upsertedUser = await db.upsert({
collection: 'users',
data: {
email: 'user@example.com'
},
where: { email: { equals: 'user@example.com' } }
})Extended Methods
getOrCreate
The getOrCreate method checks if a record exists in a specified collection using provided criteria. If it exists, it returns the record without modifying it. If it doesn’t exist, it creates a new record with the provided data and returns it.
// Find or create a record
const role = await db.getOrCreate({
collection: 'roles',
data: { id: 'admin' },
where: { id: { equals: 'admin' } }
})Dependencies
- @payloadcms/db-sqlite : ^3.33.0
- payload : ^3.33.0