Skip to Content

ai-database

npm version License: MIT

A database abstraction layer for AI applications built on top of PayloadCMS.

Installation

npm install ai-database

Usage

Standard Methods

import { db } from 'ai-database' // Find a record const user = await db.findOne({ collection: 'users', where: { email: { equals: '[email protected]' } } }) // Create a record const newUser = await db.create({ collection: 'users', data: { email: '[email protected]', password: 'password', } }) // Update a record const updatedUser = await db.update({ collection: 'users', id: 'user-id', data: { email: '[email protected]' } }) // Delete a record await db.delete({ collection: 'users', id: 'user-id' }) // Upsert a record const upsertedUser = await db.upsert({ collection: 'users', data: { email: '[email protected]' }, where: { email: { equals: '[email protected]' } } })

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