Registry / database / fastify-objectionjs

fastify-objectionjs

JSON →
library2.0.1jsnpmunverified

A Fastify plugin integrating objection.js ORM. Current version 2.0.1 requires Node.js ^14.x || >=16.x || >=18.x, Fastify ^4.0.0, and has a peer dependency on better-sqlite3. Provides automatic knex setup, model registration, and exposes models via fastify.objection. Uses ESM with CJS fallback. Differentiates by offering a declarative plugin interface for objection.js within Fastify, with default SQLite configuration.

npm install fastify-objectionjs
INSTALL
IMPORT
SIG · FASTIFY-OBJECTIONJ
F
fastify-objectionjs
databasejavascriptv2.0.1
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.

default
✓ import fastifyObjectionjs from 'fastify-objectionjs'
✗ const { default } = require('fastify-objectionjs')
Default export is the plugin function. Use ESM import or CJS require without destructuring.
plugin
✓ const plugin = require('fastify-objectionjs')
✗ const { plugin } = require('fastify-objectionjs')
CJS require returns the plugin function directly, not an object with a 'plugin' property.
Model
✓ import { Model } from 'objection'
✗ import { Model } from 'fastify-objectionjs'
Model is exported by objection.js, not by this plugin. Plugin only provides knex setup and model registration.

Registers the plugin with a User model and SQLite config, then queries a user from fastify.objection.

import fastify from 'fastify'; import fastifyObjectionjs from 'fastify-objectionjs'; import { Model } from 'objection'; class User extends Model { static get tableName() { return 'users'; } static get jsonSchema() { return { type: 'object', required: ['username', 'password'], properties: { id: { type: 'integer' }, username: { type: 'string', minLength: 1, maxLength: 255 }, password: { type: 'string', minLength: 1, maxLength: 255 } } }; } } const app = fastify(); await app.register(fastifyObjectionjs, { knexConfig: { client: 'better-sqlite3', connection: { filename: './test.sqlite' } }, models: [User] }); const user = await app.objection.models.user.query().findOne({ username: 'test' }); console.log(user);
Debug
Known issues
gotchaKnex instance mutation: the plugin creates a knex instance and assigns it to fastify.objection.knex; modifying it can affect global state.
fix
Avoid mutating fastify.objection.knex; create separate knex instances for custom work.
affects: >=0.0.0
gotchaModel registration mutation: models are mutated with knex instance; reusing models across plugins can cause issues.
fix
Define models directly or clone them before registration if reused.
affects: >=0.0.0
deprecatedPlugin uses CommonJS internally; ESM usage may require specific bundler configuration.
fix
Use dynamic import or ensure bundler handles CommonJS modules.
affects: >=2.0.0
gotchaDefault knex config uses better-sqlite3; if not provided, a SQLite file is created in the current directory.
fix
Explicitly pass knexConfig to avoid unintended file creation.
affects: >=0.0.0
deprecatedNo security incidents reported yet, but supply chain risks exist due to peer dependencies (better-sqlite3).
fix
Monitor vulnerability databases for better-sqlite3 and objection.js.
affects: >=0.0.0
Errors
Common errors & fixes
TypeError: Cannot read properties of undefined (reading 'models')
Plugin not registered before accessing fastify.objection.
fix
Ensure fastify.register(fastifyObjectionjs, ...) is called before any route or hook that uses fastify.objection.
Error: knex: require of 'better-sqlite3' failed
Missing peer dependency better-sqlite3.
fix
Run 'npm install better-sqlite3' to install the required database driver.
Error: Plugin must be a function or a promise
Importing the default export incorrectly (e.g., destructuring named export).
fix
Use 'import fastifyObjectionjs from 'fastify-objectionjs'' or 'const plugin = require('fastify-objectionjs')'.
UnhandledPromiseRejection: Error: Cannot find module 'objection'
objection.js not installed.
fix
Run 'npm install objection' to install the ORM dependency.
Upgrade
Version history
2.0.1latest on npm
Audit
Dependencies
fastifyrequiredpeer dependency, plugin is designed for Fastify v4
better-sqlite3optionalpeer dependency, default knex client
Agent activity
7 hits · last 30 days
node
6
Resources
fastify-objectionjs — npm install fastify-objectionjs · libregistry