{"id":16329,"library":"dare","title":"Dare: Database and REST API Generator","description":"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.","status":"active","version":"0.98.4","language":"javascript","source_language":"en","source_url":"ssh://git@github.com/MrSwitch/dare","tags":["javascript","Database","MySQL","REST","typescript"],"install":[{"cmd":"npm install dare","lang":"bash","label":"npm"},{"cmd":"yarn add dare","lang":"bash","label":"yarn"},{"cmd":"pnpm add dare","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Commonly used for MySQL/MariaDB database connections when implementing the `dare.execute` handler.","package":"mysql2","optional":true},{"reason":"Commonly used for PostgreSQL database connections when implementing the `dare.execute` handler.","package":"pg","optional":true}],"imports":[{"note":"The library primarily promotes ESM usage. While CommonJS might work with transpilation or specific Node.js configurations, ESM is the intended and idiomatic way to import since Node.js >=20 is required.","wrong":"const Dare = require('dare');","symbol":"Dare","correct":"import Dare from 'dare';"},{"note":"Type imports for `RequestOptions` or other interfaces are used for type-checking in TypeScript projects.","symbol":"RequestOptions","correct":"import type { RequestOptions } from 'dare';"},{"note":"The `DareInstance` type can be used to type a `Dare` object for enhanced type safety when extending or passing the instance around.","symbol":"DareInstance","correct":"import type { DareInstance } from 'dare';"}],"quickstart":{"code":"import Dare from 'dare';\nimport mysql from 'mysql2/promise'; // or 'pg' for PostgreSQL\n\n// Configure your database connection\nconst dbconn = mysql.createPool({\n    host: process.env.DB_HOST ?? 'localhost',\n    user: process.env.DB_USER ?? 'root',\n    password: process.env.DB_PASSWORD ?? '',\n    database: process.env.DB_NAME ?? 'testdb'\n});\n\n// Initiate Dare instance, specifying the database engine\nconst dare = new Dare({\n\tengine: 'mysql:8.0' // or 'postgres:16'\n});\n\n// Define the handler for database requests\ndare.execute = async (request) => {\n\tconsole.log('Executing SQL:', request.sql, request.values);\n\tconst [rows] = await dbconn.query(request.sql, request.values);\n    // For DML operations, return an object with insertId/affectedRows\n    if (request.type !== 'select') {\n        return {\n            insertId: rows.insertId,\n            affectedRows: rows.affectedRows\n        };\n    }\n\treturn rows;\n};\n\nasync function runExample() {\n    try {\n        // Make a request to get a user\n        const user = await dare.get('users', ['id', 'name', {emailAddress: 'email'}], {id: 1});\n        if (user) {\n            console.log(`User found: ${user.name} with email ${user.emailAddress}`);\n        } else {\n            console.log('User not found.');\n        }\n\n        // Example of an insert operation\n        const insertResult = await dare.post('users', {\n            name: 'Jane Doe',\n            email: 'jane.doe@example.com'\n        });\n        console.log(`Inserted user with ID: ${insertResult.insertId}`);\n\n    } catch (error) {\n        console.error('Dare operation failed:', error.message);\n    } finally {\n        await dbconn.end(); // Close the database connection pool\n    }\n}\n\nrunExample();","lang":"typescript","description":"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."},"warnings":[{"fix":"Upgrade your Node.js environment to version 20 or newer. Use `nvm` or your preferred Node.js version manager.","message":"Dare now requires Node.js version 20 or higher. Applications running on older Node.js versions will fail to install or run the package.","severity":"breaking","affected_versions":">=0.98.0"},{"fix":"Always assign an async function to `dare.execute` that accepts a `request` object and performs the actual database query using your chosen database driver (e.g., `mysql2`, `pg`). Ensure it returns appropriate results (array of rows for SELECT, object with `insertId`/`affectedRows` for DML).","message":"The `dare.execute` handler is mandatory and must be explicitly defined by the user. If not defined, Dare operations will throw an error indicating that `dare.execute` is not a function.","severity":"gotcha","affected_versions":">=0.95.0"},{"fix":"Inspect the `request` object within `dare.execute`. For MySQL/MariaDB connections, use `dbconn.query(request.sql, request.values)`. For PostgreSQL, use `dbconn.query(request.text, request.values)` or `dbconn.query(request.sql, request.values)` depending on your `pg` client setup if it maps `request.sql` to `text`.","message":"Careful handling of `request.sql` vs. `request.text` is crucial within the `dare.execute` handler, especially when switching between MySQL/MariaDB and PostgreSQL. MySQL uses `request.sql`, while PostgreSQL's `pg` driver typically expects `request.text`.","severity":"gotcha","affected_versions":">=0.95.0"},{"fix":"Pin `dare` to exact versions or use tilde (~) for minor version updates (`~0.98.0`) rather than caret (^) (`^0.98.0`) to avoid unexpected changes. Review changelogs carefully before upgrading.","message":"The library's versioning (`0.x.x`) indicates that it is still pre-1.0. While the project is actively maintained, users should be aware that minor version updates (e.g., 0.97.x to 0.98.x) could introduce breaking changes or significant behavioral shifts without strict adherence to semantic versioning for major releases.","severity":"gotcha","affected_versions":">=0.95.0"}],"env_vars":null,"last_verified":"2026-04-22T00:00:00.000Z","next_check":"2026-07-21T00:00:00.000Z","problems":[{"fix":"Assign an asynchronous function to `dare.execute` that takes a `request` object and performs database operations. Example: `dare.execute = async (request) => { /* ... */ };`","cause":"`dare.execute` handler was not defined or assigned correctly after `Dare` instantiation.","error":"TypeError: this.execute is not a function"},{"fix":"Enable logging for `request.sql` and `request.values` within `dare.execute` to inspect the generated query. Verify your `engine` option in the `Dare` constructor matches your database. Check the Dare documentation for complex query patterns or filters.","cause":"The generated SQL from Dare's methods (e.g., `get`, `post`) resulted in invalid syntax for the configured database engine, or an underlying database driver issue occurred.","error":"Error: SQLSTATE[HY000]: General error: 1064 You have an error in your SQL syntax..."},{"fix":"Ensure your Node.js project is configured for ESM by setting `\"type\": \"module\"` in `package.json`, or explicitly use CommonJS `require()` syntax if supported, though `dare` is primarily designed for ESM. For Node.js versions requiring `--experimental-modules`, ensure that flag is used.","cause":"Attempting to use `import Dare from 'dare';` in a CommonJS (CJS) environment without proper configuration or transpilation.","error":"SyntaxError: Cannot use import statement outside a module"},{"fix":"Pass a valid `engine` string to the `Dare` constructor, e.g., `new Dare({ engine: 'mysql:8.0' })` or `new Dare({ engine: 'postgres:16' })`.","cause":"The `engine` property was not provided or had an incorrect format during `Dare` instantiation.","error":"Error: The 'engine' option is missing or invalid. Supported engines: 'mysql:X', 'postgres:X'"}],"ecosystem":"npm"}