Minato Database Framework
Minato is a type-driven database framework for JavaScript and TypeScript, designed to provide a unified API across various database backends. It supports a wide range of drivers including Memory, MongoDB, MySQL, PostgreSQL, and SQLite, allowing developers to switch databases with minimal code changes. Written entirely in TypeScript, Minato offers comprehensive type safety for schema definitions and queries, aiming to eliminate common database-related errors at compile time. It distinguishes itself by providing a powerful, extensible, and modern ORM experience that can handle complex SQL operations through a JavaScript API. Currently at version 4.0.1, Minato maintains an active, demand-driven release cadence, focusing on compatibility, performance, and a consistent developer experience across diverse data stores. Its tight integration capabilities with the Cordis framework make it particularly suitable for modular and extensible application architectures.
Common errors
-
Error: Cannot find module '@minatojs/driver-mysql' or its corresponding type declarations.
cause The specific database driver package was not installed.fixInstall the required driver: `npm install @minatojs/driver-mysql` (replace `mysql` with your database). -
TypeError: minato_1.default is not a constructor
cause Attempting to instantiate `Database` using named import syntax when it's a default export.fixChange your import statement from `import { Database } from 'minato'` to `import Database from 'minato'`. -
Error: EACCES: permission denied, open '/var/run/mysqld/mysqld.sock'
cause The application lacks necessary permissions to connect to the database socket or the database server is not running or misconfigured.fixEnsure your database server is running and accessible from your application's environment. Check database user permissions and connection string details. For local development, verify Docker containers are running or local services are active. -
Error: No database driver provided.
cause The `connect` method was called without passing a database driver (e.g., `MySQLDriver`).fixPass the appropriate imported driver to the `connect` method: `await database.connect(MySQLDriver, config)`.
Warnings
- breaking Minato v4 introduces significant breaking changes from v3, particularly in API interfaces for database operations, schema definitions, and internal driver implementations. Users upgrading from v3 should carefully review the official migration guide (if available) or anticipate necessary code adjustments.
- gotcha Minato requires separate driver packages (e.g., `@minatojs/driver-mysql`, `@minatojs/driver-mongodb`) for each database type. Forgetting to install the specific driver for your target database will result in runtime errors.
- gotcha While Minato simplifies database interactions, complex data access patterns (e.g., deep nested relations, large joins) can still lead to N+1 query problems or inefficient database calls if not optimized. Always be mindful of the generated queries.
- gotcha Minato leverages TypeScript's powerful type inference. However, incorrect schema definitions or complex query constructs can sometimes lead to obscure TypeScript errors. Ensuring your types align with the actual database structure is crucial.
- gotcha The `cordis` package is a peer dependency in Minato v4. If you plan to use Minato within a Cordis application context, you must explicitly install `cordis` to avoid runtime errors related to missing modules or incompatible versions.
Install
-
npm install minato -
yarn add minato -
pnpm add minato
Imports
- Database
import { Database } from 'minato'import Database from 'minato'
- MySQLDriver
import MySQLDriver from '@minatojs/driver-mysql'
- Schema Definition (method)
database.extend('collectionName', { /* schema */ })
Quickstart
import Database from 'minato';
import MySQLDriver from '@minatojs/driver-mysql';
import type { Context } from 'cordis'; // Optional, if using Cordis
interface User {
id: number;
name: string;
age: number;
email?: string;
}
async function runMinatoExample() {
// For Cordis integration, you might pass a Context, otherwise, it's optional.
// const ctx = new Context();
// const database = new Database(ctx);
const database = new Database();
// Define the schema for the 'user' collection/table
database.extend('user', {
id: 'unsigned', // Auto-incrementing primary key
name: 'string',
age: 'integer',
email: { type: 'string', nullable: true, unique: true },
}, {
primary: 'id',
autoInc: true,
});
// Connect to the database
try {
await database.connect(MySQLDriver, {
host: 'localhost',
port: 3306,
user: 'root',
password: process.env.DB_PASSWORD ?? '', // Use environment variable for sensitive info
database: 'minato_test_db',
});
console.log('Successfully connected to MySQL database.');
// Basic CRUD operations
// Create
const newUser = await database.create('user', {
name: 'Alice',
age: 30,
email: 'alice@example.com',
});
console.log('Created user:', newUser);
// Find
const users = await database.get('user', { age: { $gt: 25 } });
console.log('Users older than 25:', users);
// Update
await database.set('user', { id: newUser.id }, { age: 31 });
const updatedUser = await database.get('user', { id: newUser.id });
console.log('Updated user:', updatedUser[0]);
// Delete
await database.remove('user', { id: newUser.id });
console.log('Deleted user with ID:', newUser.id);
} catch (error) {
console.error('Database operation failed:', error);
} finally {
await database.disconnect();
console.log('Disconnected from database.');
}
}
runMinatoExample();