JavaScript
Hysteria ORM is written and designed for TypeScript, but it can still be used in JavaScript with some configurations:
1. Install the necessary dependencies
npm install --save-dev reflect-metadata @babel/core @babel/cli @babel/preset-env @babel/plugin-proposal-decorators
# or
yarn add --dev reflect-metadata @babel/core @babel/cli @babel/preset-env @babel/plugin-proposal-decorators
2. Create a babel.config.js
file in the root of your project:
module.exports = {
presets: [
[
"@babel/preset-env",
{
targets: {
node: "current",
},
},
],
],
plugins: [["@babel/plugin-proposal-decorators", { legacy: true }]],
};
3. Add a build script to your package.json
:
{
"scripts": {
"build": "babel src --out-dir dist"
}
}
4. Run the build script
npm run build
5. Run your application
node dist/index.js
Example: JS Model with Decorators
require("reflect-metadata");
const { Model, SqlDataSource, column } = require("hysteria-orm");
class User extends Model {
@column({ primaryKey: true })
id;
@column()
name;
@column()
email;
@column()
signupSource;
@column()
isActive;
@column()
createdAt;
@column()
updatedAt;
}
JS without decorators
If you don't want to use decorators, you can define your models like this. Aside from decorators, all other features are available in JavaScript.
const { Model, SqlDataSource } = require("hysteria-orm");
const Profile = require("./Profile");
const Post = require("./Post");
const Role = require("./Role");
const Address = require("./Address");
class User extends Model {
static {
this.column("id", { primaryKey: true });
this.column("name");
this.column("email");
this.column("signupSource");
this.column("isActive");
this.hasOne("profile", () => Profile, "userId");
this.hasMany("posts", () => Post, "userId");
this.belongsToMany("roles", () => Role, "roleId");
this.manyToMany("addresses", () => Address, "user_addresses", "userId");
}
}
Notes
- Type safety is not enforced in JavaScript.
- All features are available, but you lose development-time checks.
Next: Performance