Dare: Database and REST API Generator

0.98.4 · active · verified Wed Apr 22

Dare is a JavaScript library (shipping TypeScript types) designed to streamline the creation of REST APIs from database schemas by generating SQL queries from structured JavaScript objects. It acts as an abstraction layer, allowing developers to define database interactions using a declarative object syntax rather than writing raw SQL directly. The current stable version is `0.98.4`, indicating it is still in pre-1.0 development, though it sees consistent maintenance with several bug fix and feature releases throughout 2025. Key differentiators include its 'brave API' approach to SQL generation, requiring users to define a custom `dare.execute` handler for database interaction, and its explicit support for MySQL (5.6, 5.7, 8.0) and PostgreSQL (16+), abstracting away direct driver calls.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize Dare, configure its `dare.execute` handler with a `mysql2/promise` connection pool, and perform basic `get` (SELECT) and `post` (INSERT) operations, including field aliasing.

import Dare from 'dare';
import mysql from 'mysql2/promise'; // or 'pg' for PostgreSQL

// Configure your database connection
const dbconn = mysql.createPool({
    host: process.env.DB_HOST ?? 'localhost',
    user: process.env.DB_USER ?? 'root',
    password: process.env.DB_PASSWORD ?? '',
    database: process.env.DB_NAME ?? 'testdb'
});

// Initiate Dare instance, specifying the database engine
const dare = new Dare({
	engine: 'mysql:8.0' // or 'postgres:16'
});

// Define the handler for database requests
dare.execute = async (request) => {
	console.log('Executing SQL:', request.sql, request.values);
	const [rows] = await dbconn.query(request.sql, request.values);
    // For DML operations, return an object with insertId/affectedRows
    if (request.type !== 'select') {
        return {
            insertId: rows.insertId,
            affectedRows: rows.affectedRows
        };
    }
	return rows;
};

async function runExample() {
    try {
        // Make a request to get a user
        const user = await dare.get('users', ['id', 'name', {emailAddress: 'email'}], {id: 1});
        if (user) {
            console.log(`User found: ${user.name} with email ${user.emailAddress}`);
        } else {
            console.log('User not found.');
        }

        // Example of an insert operation
        const insertResult = await dare.post('users', {
            name: 'Jane Doe',
            email: 'jane.doe@example.com'
        });
        console.log(`Inserted user with ID: ${insertResult.insertId}`);

    } catch (error) {
        console.error('Dare operation failed:', error.message);
    } finally {
        await dbconn.end(); // Close the database connection pool
    }
}

runExample();

view raw JSON →