Registry / database / als-model

als-model

JSON →
library0.8.27jsnpmunverified

als-model v0.8.27 provides an instrument for MySQL and SQLite database operations, including queries, migrations, and fake data generation. It offers a chaining API for building SQL statements (SELECT, WHERE, UPDATE, DELETE, INSERT) with promise or callback support. The library also includes a Migration tool for creating/dropping tables and a Types module for field type definitions. Released as a JavaScript package, it targets Node.js environments and does not bundle TypeScript types. Key differentiators include a simple object-based condition syntax supporting AND/OR logic, and a single Model class that works with both MySQL and SQLite.

npm install als-model
INSTALL
IMPORT
SIG · ALS-MODEL
A
als-model
databasejavascriptv0.8.27
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.

Model
const { Model } = require('als-model')
const Model = require('als-model')
CommonJS import; Model is a named export.
Migration
const { Migration } = require('als-model')
const Migration = require('als-model').Migration
Named export, not default. Also available as named import in ESM.
Types
const { Types } = require('als-model')
Named export. Provides field type constants (e.g., Types.INTEGER, Types.VARCHAR).

Shows how to connect to MySQL, create a table via Migration, insert a record, and query using the chaining API with callbacks.

const { Model, Migration, Types } = require('als-model'); // MySQL connection const conData = { host: 'localhost', user: 'root', password: '', database: 'myDb', }; const tableName = 'users'; const model = new Model(conData, tableName); // Create table (using Migration) const migration = new Migration(conData); migration.dropTable('temp_table'); migration.createTable('temp_table', { id: ['INT', 'NOT NULL', 'AUTO_INCREMENT'], name: ['VARCHAR(255)', 'NOT NULL'], email: ['VARCHAR(255)', 'NOT NULL'], }); // Insert record model.insert([{ name: 'John Doe', email: 'john@example.com' }]).run((result) => { if (result.err) throw result.err; console.log('Inserted:', result.rows); }); // Query records model.where({ name: 'John Doe' }).first().run((result) => { if (result.err) throw result.err; console.log('First:', result.rows); });
Debug
Known issues
gotchaMethod names like `first()` and `async()` are chainable but only available on results of `all()` and `where()`. On other methods (e.g., `insert()`, `update()`), `first()` does not exist.
fix
Only use `first()` after `all()` or `where()`. For `insert()`, `update()`, `delete()`, use `run()` or `async()` directly.
affects: >=0.0.0
gotchaThe condition object supports special prefix '|' for OR logic (e.g., {name:'|Alex'}) but this is non-standard and may be overlooked.
fix
Use explicit OR conditions with the '|' prefix as documented. For complex queries, consider writing raw SQL via `query` or `queryAsync`.
affects: >=0.0.0
gotchaThe `model()` method returns a new Model instance, not the same object. Calling it on an existing model does not modify the original.
fix
Assign the result: `let userModel = model.model('users');`
affects: >=0.0.0
deprecatedCallback-based methods (`run`) are not deprecated but promise-based (`async()`) is preferred for modern code.
fix
Use `async()` with `await` instead of callbacks for better error handling.
affects: >=0.0.0
gotchaTable names and field values are not escaped by default? The library uses simple string concatenation for SQL, which may be vulnerable to injection if inputs are unsanitized.
fix
Sanitize inputs before passing to model methods. Consider using parameterized queries when possible (currently not supported by als-model).
affects: >=0.0.0
Errors
Common errors & fixes
TypeError: Model is not a constructor
Importing as default instead of named export.
fix
Use `const { Model } = require('als-model');` instead of `const Model = require('als-model');`
Error: Cannot find module 'mysql' or 'sqlite3'
Missing required optional dependency.
fix
Install the corresponding driver: `npm install mysql` or `npm install sqlite3`
TypeError: model.all(...).first is not a function
Called `first()` on an `insert()` or `update()` chain which does not support it.
fix
Use `run()` or `async()` after `insert()`/`update()`/`delete()`. `first()` is only available after `all()` or `where()`.
Error: ER_PARSE_ERROR: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ...
Invalid condition syntax (e.g., missing quotes or incorrect operator).
fix
Ensure condition values are strings with operators like '>', '<', etc. For example: `{age: '>25'}`. Also escape special characters if needed.
Upgrade
Version history
0.8.27latest on npm
Audit
Dependencies
mysqloptionalused for MySQL database connections
sqlite3optionalused for SQLite database connections
Agent activity
21 hits · last 30 days
node
18
OpenAI (training)
1
Resources
als-model — npm install als-model · libregistry