ai-database is an npm package (v2.1.3) that provides AI-powered database interface primitives with MDXLD conventions. It allows developers to define typed schemas with relationship operators (->, ~>, <-, <~) for AI-native linking, enabling cascade generation of entity graphs in a single call. Key features include promise pipelining (chain without await), batch relationship loading (eliminates N+1 queries), natural language queries, and full TypeScript support. Released on npm, it uses vitest for testing and is part of the ai-primitives ecosystem. It differs from traditional ORMs by being designed for AI-generated data, automatically matching existing records or creating new ones with fuzzy matching.
npm install ai-databaseVerified import paths — ran on the pinned version, not inferred.
Shows schema definition, cascade creation, relationship resolution, promise pipelining filtering, and natural language query.
Add { cascade: true } to create calls that should generate related entities.Use matchMode: 'fuzzy' instead of fuzzyMatch: true.
Always define the referenced type in the schema. Use TypeScript's strict mode to catch missing types.
Do not interpolate untrusted user input directly into template literal queries. Validate and sanitize input.
Convert project to ESM, or use dynamic import() if in a CommonJS context.
Use import: import { DB } from 'ai-database' or switch to dynamic import(): const { DB } = await import('ai-database')Ensure DB is called with a valid schema object: const { db } = DB({ Lead: { name: 'string' } })Define the relationship in the schema using the correct operator, e.g., company: 'Company.leads'