Minato Database Framework

4.0.1 · active · verified Sun Apr 19

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

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize Minato, define a type-safe schema, connect to a MySQL database using a separate driver, and perform common CRUD (Create, Read, Update, Delete) operations.

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();

view raw JSON →