Warp Server ORM
Warp Server is an Object-Relational Mapper (ORM) designed for scalable web applications, currently at version 6.8.7. It provides a structured approach to defining database schemas, including tables, columns with validation, and triggers for pre- and post-operation hooks. Developers can build complex queries, implement server-side functions for custom business logic, and enforce access restrictions based on user roles. A key differentiator is its built-in support for implementing RESTful APIs via an Express middleware. As of now, it exclusively supports MySQL, with the roadmap indicating plans for additional database adapters in future releases. The project maintains a stable release cadence, with version 6.x being the current focus, explicitly differentiating its documentation and usage from older versions 5.x and legacy releases.
Common errors
-
ReferenceError: Reflect is not defined
cause `reflect-metadata` library was not imported or `emitDecoratorMetadata` is not enabled.fixEnsure `import 'reflect-metadata';` is at the top of your main application file and `"emitDecoratorMetadata": true` is in your `tsconfig.json`. -
TypeError: Warp is not a constructor
cause Attempting to use CommonJS `require()` syntax or incorrect module resolution when `warp-server` is an ES Module.fixUse ES Module `import Warp from 'warp-server';` syntax. If using Node.js, ensure your package.json has `"type": "module"` or your file uses the `.mjs` extension, and/or your TypeScript compiler options (`moduleResolution`, `esModuleInterop`) are correctly set for ESM.
Warnings
- breaking Warp Server versions 6.x introduce significant changes and are not backward compatible with versions 5.x or legacy versions. Consult the version-specific documentation to avoid issues.
- gotcha To utilize decorators (e.g., for class and key definitions), your `tsconfig.json` must have `"experimentalDecorators": true` and `"emitDecoratorMetadata": true` configured under `"compilerOptions"`.
- gotcha The `reflect-metadata` library is a mandatory peer dependency for Warp Server when using TypeScript decorators. It must be installed and imported once at the application's entry point.
- gotcha Currently, Warp Server officially supports only MySQL databases. While other adapters are planned, direct usage with other database systems is not supported and will result in errors.
Install
-
npm install warp-server -
yarn add warp-server -
pnpm add warp-server
Imports
- Warp
const Warp = require('warp-server');import Warp from 'warp-server';
- Class
import { Class } from 'warp-server/decorators';import { Class, Key } from 'warp-server'; - reflect-metadata
import 'reflect-metadata';
Quickstart
import 'reflect-metadata';
import Warp from 'warp-server';
// Replace with your actual database connection string
const databaseURI = 'mysql://youruser:password@yourserver.com:3306/yourdatabase?charset=utf8';
const service = new Warp({ databaseURI });
async function startService() {
try {
await service.initialize();
console.log('Warp service initialized successfully. You can now define and interact with your models.');
// Example: You would define your classes and start querying here.
// @Class()
// class User {
// @Key({ primary: true, autoIncrement: true }) id: number;
// @Key() name: string;
// }
// service.define(User);
// const newUser = await service.create(User, { name: 'John Doe' });
// console.log('New user created:', newUser);
} catch (error) {
console.error('Failed to initialize Warp service:', error);
}
}
startService();