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('tabla-model');
✗ import Model from 'tabla-model';
Library uses CommonJS and does not provide ESM exports. Using ESM import will fail.
new Model(name, schema)
✓ const userModel = new Model('users', { columns: [...] });
✗ const userModel = Model('users', { columns: [...] });
Model must be instantiated with 'new'; calling without 'new' will throw.
foreingKey
✓ new Model('orders', { foreingKey: [{ key: 'user_id', reference: 'users', keyReference: 'id', onDelete: 'CASCADE' }] });
✗ new Model('orders', { foreignKey: [...] });
The schema property is spelled 'foreingKey' (missing 'i' in 'foreign'). Using the correct spelling will not work.
Demonstrates creating a model, defining columns and foreign keys, adding a custom method, and generating CREATE TABLE SQL.
const Model = require('tabla-model');
const UsuarioModel = new Model('usuarios', {
colums: [
{ name: 'id', type: 'int', primary: true, autoincrement: true },
{ name: 'username', type: 'varchar(50)', defaultNull: false, unique: true },
{ name: 'email', type: 'varchar(100)' },
{ name: 'puntos', type: 'int', default: 0 }
],
foreingKey: [
{
key: 'id_perfil',
reference: 'perfiles',
keyReference: 'id',
onDelete: 'CASCADE'
}
]
});
UsuarioModel.method('getByPuntos', async (tabla, db, minPuntos, limit = 10) => {
return await tabla.select('*', `puntos > ${minPuntos}`, 'puntos DESC', limit);
});
console.log(UsuarioModel.sqlCreate());
Errors
Common errors & fixes
ERR_REQUIRE_ESM: require() of ES Module /path/to/node_modules/tabla-model/index.js from /path/to/app.js not supported.
Trying to ESM-import a CommonJS-only library using import statement.
fixChange to const Model = require('tabla-model'); TypeError: Model is not a constructor
Calling Model as a function without 'new'.
fixUse new Model(name, schema).
Cannot read properties of undefined (reading 'colums')
Schema object is missing 'colums' property (misspelled or wrong key).
fixEnsure schema has a 'colums' array (not 'columns').
Audit
Dependencies
mysql-taboptionalRequired to use models with MySQL/MariaDB
sqlite3-taboptionalRequired to use models with SQLite3
pg-tableoptionalRequired to use models with PostgreSQL