Registry / database / objection-model-generator

objection-model-generator

JSON →
library1.1.4jsnpmunverified

Generates Objection.js model files from a MySQL database schema by querying information_schema. Latest version 1.1.4 supports automatic detection of tables, columns, and BelongsToOneRelations. Requires a database connection and a knex instance path. Primarily a development tool for scaffolding models, not actively maintained since 2023. Alternatives include sequelize-auto or typeorm-model-generator.

npm install objection-model-generator
INSTALL
IMPORT
SIG · OBJECTION-MODEL-GE
O
objection-model-generator
databasejavascriptv1.1.4
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.

Omg
const Omg = require('objection-model-generator');
import Omg from 'objection-model-generator';
Package is CommonJS only; ES imports will fail.
new Omg
let omg = new Omg({ host: '...', port: 3306, user: '...', password: '...' }, 'dbname', '../knex');
new Omg({ host: '...' }, 'dbname');
The third argument (knex path) is required. Missing it will cause an error when generated models try to require knex.
createModels
const ms = await omg.createModels();
const ms = omg.createModels()
createModels returns a Promise; must be awaited.

Generates Objection.js model files from a MySQL database using environment variables for connection.

require('dotenv').config(); const fs = require('fs'); const Omg = require('objection-model-generator'); const main = async () => { let omg = new Omg({ host: process.env.DB_HOST, port: process.env.DB_PORT, user: process.env.DB_USER, password: process.env.DB_PASS }, process.env.DB_NAME, '../knex'); let ms = await omg.createModels(); let path = 'src/api/models/generated.js'; fs.writeFileSync(path, ms); console.log('-> file writed:', path); process.exit(); }; main();
Debug
Known issues
gotchaKnex instance path must be relative to generated models file; default '../knex' may be wrong if output path changes.
fix
Adjust the third argument to Omg constructor to match relative path from output file to knex file.
affects: >=1.0
gotchaGenerated models require a knex instance. If knex is not installed or configured, require will fail.
fix
Ensure knex is installed and the knex file exports a knex instance.
affects: >=1.0
deprecatedPackage has not been updated since 2023. Known issues with MySQL 8+ and newer Node.js versions may arise.
fix
Consider switching to actively maintained alternatives like sequelize-auto or writing custom generation.
affects: >=1.1
gotchaRelation detection only covers BelongsToOneRelations; other relation types (HasMany, etc.) are not generated.
fix
Manually add other relations after generation.
affects: >=1.0
Errors
Common errors & fixes
Cannot find module '../knex'
The knex path provided to Omg constructor is incorrect relative to the generated file.
fix
Ensure third argument to new Omg() correctly points to your knex file, e.g., '../../knex' if models are deep.
TypeError: Omg is not a constructor
Using ES import syntax (import Omg from ... ) instead of CommonJS require.
fix
Use const Omg = require('objection-model-generator');
Cannot read properties of undefined (reading 'createModels')
Omg not properly instantiated or the constructor failed silently (e.g., missing connection details).
fix
Check that database connection parameters are correct and that the database exists.
Upgrade
Version history
1.1.4latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
2 hits · last 30 days
node
2
Resources
objection-model-generator — npm install objection-model-generator · libregistry