Registry / database / anydb-sql-2

anydb-sql-2

JSON →
library4.0.5jsnpmunverified

Minimal ORM for MySQL, PostgreSQL, and SQLite with full arbitrary SQL query support. Version 4.0.5 is the latest stable release. It is built on the node-sql query builder and node-anydb connection pool. Unlike heavier ORMs like Sequelize or TypeORM, it provides a thin layer over SQL, allowing direct SQL while adding table definitions, relationship-based joins, and deep result selection (selectDeep). It ships TypeScript types and supports both callback and promise patterns. Release cadence is low, with updates primarily addressing bug fixes.

npm install anydb-sql-2
INSTALL
IMPORT
SIG · ANYDB-SQL-2
A
anydb-sql-2
databasejavascriptv4.0.5
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

anydbsql
import anydbsql from 'anydb-sql-2'
const anydbsql = require('anydb-sql-2').default
The package is ESM-only since version 4. Use default import. CommonJS require works without .default if used with Node's require.
Table
import type { Table } from 'anydb-sql-2'
TypeScript type for table definitions. Use import type to avoid runtime overhead.
Transaction
import type { Transaction } from 'anydb-sql-2'
TypeScript type for transaction objects used with execWithin.

Initializes a database connection, defines two tables with a relationship, performs a join query, and uses selectDeep to get nested results.

import anydbsql from 'anydb-sql-2'; const db = anydbsql({ url: process.env.DATABASE_URL ?? 'postgres://user:pass@localhost:5432/mydb', connections: { min: 2, max: 20 } }); const user = db.define({ name: 'users', columns: { id: { primaryKey: true }, email: {}, password: {} } }); const post = db.define({ name: 'posts', columns: { id: { primaryKey: true }, userId: {}, content: {}, date: {} } }); user.from(user.join(post).on(user.id.equals(post.userId))) .where(post.date.gt(new Date('2020-01-01'))) .selectDeep(user.name, post.content) .all() .then((rows: any[]) => { console.log(rows); }) .catch((err: Error) => { console.error(err); });
Debug
Known issues
breakingVersion 4 changed to ESM-only. CommonJS require still works but .default is not needed; using require().default will break.
fix
If using ESM, use import anydbsql from 'anydb-sql-2'. If using CommonJS, use const anydbsql = require('anydb-sql-2') without .default.
affects: >=4.0.0
deprecatedThe callback-based methods (exec, all, get) are deprecated in favor of promises. Callbacks still work but may be removed in a future major version.
fix
Use promise-based API: e.g., .all() returns a Promise.
affects: >=3.0.0
gotchaselectDeep requires that relationships (has) are defined on the table. If not defined, the relationship name is undefined and will cause errors.
fix
Always define the has property on table definitions when using selectDeep with relationships.
affects: >=1.0.0
gotchaWhen using transactions, the transaction object must be passed explicitly to execWithin, allWithin, getWithin. Failing to do so will execute outside the transaction.
fix
Use db.begin() to get a transaction object, then pass it to the Within methods.
affects: >=1.0.0
gotchaThe sql dependency (node-sql) is a separate package. Version mismatches between anydb-sql-2 and sql can cause issues.
fix
Ensure the sql package version is compatible. Check anydb-sql-2's package.json for peerDependencies.
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: anydbsql is not a function
Using require('anydb-sql-2').default in CommonJS, or incorrect import
fix
Use import anydbsql from 'anydb-sql-2' (ESM) or const anydbsql = require('anydb-sql-2') (CJS without .default)
Cannot find module 'sql'
Missing peer dependency 'sql' (node-sql)
fix
npm install sql
Property 'selectDeep' does not exist on type 'Query'
TypeScript type definitions missing or outdated for selectDeep
fix
Update anydb-sql-2 to latest version; ensure TypeScript types are installed (the package ships types). If issue persists, use // @ts-ignore.
Error: Callback is not a function
Calling exec, all, or get with no callback and no chain to a promise
fix
Provide a callback or use promise chain (e.g., .all().then(...))
Upgrade
Version history
4.0.5latest on npm
Audit
Dependencies
any-dbrequiredProvides the database connection pool abstraction used by anydb-sql-2.
sqlrequiredUnderlying SQL query builder (node-sql) for constructing queries.
Agent activity
19 hits · last 30 days
node
16
OpenAI (training)
1
Resources