VR Models
vr-models is a shared database models package specifically designed for VR applications. It leverages the Sequelize ORM, providing an abstraction layer for database interactions and ensuring data consistency across various VR projects. Shipping with comprehensive TypeScript types, it facilitates a type-safe development experience. The current stable version, 1.0.52, indicates an actively maintained package within its niche domain. While its release cadence isn't explicitly defined, the version number suggests ongoing development and refinement. Its primary differentiator is its specialized focus on the VR ecosystem, offering predefined model structures that might be common in VR development, and integrates with `vr-migrations` for robust schema management.
Common errors
-
ERR_REQUIRE_ESM: Must use import to load ES Module
cause Attempting to `require()` an ES Module file, often when `"type": "module"` is set in `package.json`.fixChange `require()` calls to `import` statements or ensure that files intended to be CommonJS are named `.cjs` or reside in a package configured for CommonJS. For `sequelize-cli` migrations, consider adding `"type": "commonjs"` to a `package.json` within your `migrations` directory. -
SequelizeConnectionAcquireTimeoutError: Operation timeout
cause The database connection pool is unable to acquire a connection within the specified timeout, often due to high load or unreleased connections.fixAdjust the `pool.max` and `pool.acquire` options in your Sequelize configuration. Investigate long-running queries or uncommitted transactions in your application logic. -
TypeError: Cannot read properties of undefined (reading 'init')
cause A Sequelize model was not properly initialized with the `init` method before being used, or the `sequelize` instance was not passed correctly during model setup.fixEnsure `initializeModels(sequelize, DataTypes)` is called correctly and that your Sequelize instance and `DataTypes` are correctly passed and available to all model definitions. -
ERROR: Dialect needs to be explicitly supplied as of v4.0.0
cause The database dialect (e.g., 'mysql', 'postgres', 'sqlite') was not provided in the Sequelize constructor options.fixPass a `dialect` option to the `Sequelize` constructor, e.g., `new Sequelize({ dialect: 'sqlite', ... })`.
Warnings
- breaking Sequelize v7 introduces significant breaking changes, including renaming the main package to `@sequelize/core` and separating dialects into individual packages. If `vr-models` updates to Sequelize v7, this will require manual updates to your `sequelize` imports and dialect configurations.
- breaking Sequelize v6.19.2 included a critical SQL injection fix that inadvertently introduced a breaking change related to how replacements were handled within raw SQL strings. While this prevents security vulnerabilities, it might break existing queries that relied on the old behavior of injecting quoted replacements.
- deprecated The `sequelize.import` method for loading models was deprecated in Sequelize v5 and completely removed in v6. If `vr-models` or your consuming application uses older model loading patterns, it will break.
- gotcha Mixing ESM `import` syntax with CommonJS `require` in projects configured as ESM (e.g., `"type": "module"` in `package.json`) can lead to `ERR_REQUIRE_ESM` errors, particularly with `sequelize-cli` or older utility scripts that expect CJS.
- gotcha Frequent `ConnectionAcquireTimeoutError` indicates that the Sequelize connection pool is exhausted, often due to too many concurrent requests, long-running queries, or uncommitted/unrolled-back transactions.
- gotcha Incompatible versions of `sequelize` with `vr-models` (due to peer dependency mismatches) can lead to unexpected behavior, missing methods, or type errors, especially after major `sequelize` updates.
Install
-
npm install vr-models -
yarn add vr-models -
pnpm add vr-models
Imports
- initializeModels
const { initializeModels } = require('vr-models');import { initializeModels } from 'vr-models'; - VRUser
import VRUser from 'vr-models/VRUser';
import { VRUser } from 'vr-models'; - IVRUserAttributes
import { IVRUserAttributes } from 'vr-models';import type { IVRUserAttributes } from 'vr-models';
Quickstart
import { Sequelize, DataTypes, Model } from 'sequelize';
import { initializeModels, VRUser, VRProduct } from 'vr-models';
interface Config { database: string; username?: string; password?: string; host?: string; dialect: string; storage?: string; }
const config: Config = {
dialect: 'sqlite',
storage: process.env.DB_STORAGE ?? './vr_database.sqlite',
};
const sequelize = new Sequelize(config);
async function setupAndUseVRModels() {
try {
await sequelize.authenticate();
console.log('Database connection has been established successfully.');
// Initialize models from vr-models, passing the Sequelize instance
initializeModels(sequelize, DataTypes); // Assuming initializeModels takes sequelize and DataTypes
// Synchronize all models (in a real app, use migrations from vr-migrations)
await sequelize.sync({ alter: true });
console.log('All models were synchronized successfully.');
// Create a new VR user
const newUser = await VRUser.create({ username: 'playerOne', email: 'playerone@example.com' });
console.log('New VR user created:', newUser.toJSON());
// Create a new VR product
const newProduct = await VRProduct.create({ name: 'Virtual Headset', price: 299.99, description: 'High-fidelity VR experience.' });
console.log('New VR product created:', newProduct.toJSON());
// Find all VR users
const users = await VRUser.findAll();
console.log('All VR users:', users.map(u => u.toJSON()));
} catch (error) {
console.error('Unable to connect to the database or operate:', error);
} finally {
await sequelize.close();
console.log('Database connection closed.');
}
}
setupAndUseVRModels();