0.3.2 • Published 5 months ago

hysteria-orm v0.3.2

Weekly downloads
-
License
MIT
Repository
github
Last release
5 months ago

Hysteria ORM

Package is under development and not production ready by any means

Philosophy

  • Hysteria ORM is an agnostic Object-Relational Mapping (ORM) library for TypeScript, designed to simplify interactions between your application and a SQL and NoSql databases.
  • The structure of this ORM Is ispireed by some main Typescript orms like Typeorm and Lucid.
  • It's partially type safe by design, allowing you to have features like intellisense for you models interactions while maintaining the flexibility of shooting yourself in the foot!
  • The main characteristic Is that Models classes refer to the database repository allowing you to interact with It via static methods in a concise and minimal way. While Models instances do not have anything else but what you define as Columns(sql) or Properties(noSql) and are designed to be used directly in you typescript Logic without any overhead.
  • Only open source and free to use Databases are and will be supported

  • Installation

  • Prerequisites
  • Supported Databases
  • Env example with a config for each database
  • TypeScript Configuration example
  • Javascript
  • Setup Example
  • logger

Known Issues

Installation

    npm install hysteria-orm

    yarn add hysteria-orm

Prerequisites

  • A JavaScript runtime environment (e.g., Node.js, deno or bun).
  • The driver for you database, supported drivers are:
### Sql

# postgres
npm install pg
yarn add pg

# mysql or mariadb
npm install mysql2
yarn add mysql2

# sqlite
npm install sqlite3
yarn add sqlite3

### NoSql

# mongo
npm install mongodb
yarn add mongodb

# redis
npm install ioredis
yarn add ioredis
  • Driver reference versions used in development
{
  "mysql2": "^3.11.5",
  "pg": "^8.13.1",
  "sqlite3": "^5.1.7",
  "mongodb": "^6.11.0",
  "ioredis": "^5.4.1"
}

Supported Databases

Sql

Documentation For Sql Databases

  • Sql supported databases are 1) Mysql 2) MariaDB 3) Postgres 4) SQLite

NoSQl

Env example with a config for each database

# POSTGRES
DB_TYPE=postgres
DB_HOST=127.0.0.1
DB_USER=root
DB_PASSWORD=root
DB_DATABASE=test
DB_PORT=5432
DB_LOGS=true
MIGRATION_PATH=./migrations # default is database/migrations

# MYSQL
DB_TYPE=mysql
DB_HOST=127.0.0.1
DB_USER=root
DB_PASSWORD=root
DB_DATABASE=test
DB_PORT=3306
DB_LOGS=true

# MARIADB
DB_TYPE=mariadb
DB_HOST=127.0.0.1
DB_USER=root
DB_PASSWORD=root
DB_DATABASE=test
DB_PORT=3306
DB_LOGS=true

# SQLITE
DB_TYPE=sqlite
DB_DATABASE="./sqlite.db"
DB_LOGS=true

# REDIS
REDIS_HOST=127.0.0.1
REDIS_USERNAME=default
REDIS_PASSWORD=root
REDIS_PORT=6379

# MONGO
DB_TYPE=mongo
MONGO_URL=mongodb://root:root@localhost:27017
MONGO_LOGS=true

TypeScript Configuration example

{
  "compilerOptions": {
    "esModuleInterop": true,
    "forceConsistentCasingInFileNames": true,
    "module": "commonjs",
    "outDir": "lib",
    "rootDirs": ["src", "test"],
    "skipLibCheck": true,
    "strict": true,
    "target": "ES2020",
    "moduleResolution": "node",
    "declaration": true,
    // Must set decorators support
    "experimentalDecorators": true,
    "emitDecoratorMetadata": true,
  },
  "include": ["src/**/*.ts", "test/**/*.ts"],
  "exclude": ["node_modules"]
}

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
yarn add --dev reflect-metadata @babel/core @babel/cli @babel/preset-env @babel/plugin-proposal-decorators
  1. Create a babel.config.js file in the root of your project with the following content:
module.exports = {
  presets: [
    [
      "@babel/preset-env",
      {
        targets: {
          node: "current",
        },
      },
    ],
  ],
  plugins: [
    ["@babel/plugin-proposal-decorators", { legacy: true }],
  ],
};
  1. Add a build script to your package.json file:
{
  "scripts": {
    "build": "babel src --out-dir dist"
  }
}
  1. Run the build script:
npm run build
  1. Run your application:
node dist/index.js
  • Your js Model definition may look like this:
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 decorators, all other features are available in JavaScript
import {Model, SqlDataSource, getModelColumns} from 'hysteria-orm';
import Profile from './Profile';
import Post from './Post';
import Role from './Role';
import Address from './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");
  }
}

Setup Example

  • Docker compose example with the database versions used in the development
version: "3"
services:
  mysql:
    image: mysql:8.0
    environment:
      MYSQL_ROOT_PASSWORD: root
      MYSQL_DATABASE: test
    ports:
      - "3306:3306"

  postgres:
    image: postgres:13
    environment:
      POSTGRES_USER: root
      POSTGRES_PASSWORD: root
      POSTGRES_DB: test
    ports:
      - "5432:5432"

  mariadb:
    image: mariadb:10.5
    environment:
      MYSQL_ROOT_PASSWORD: root
      MYSQL_DATABASE: test
    ports:
      - "3307:3306"

  redis:
    image: redis:6
    environment:
      - REDIS_PASSWORD=root
    ports:
      - "6379:6379"

  mongo:
    image: mongo:4.4
    environment:
      MONGO_INITDB_ROOT_USERNAME: root
      MONGO_INITDB_ROOT_PASSWORD: root
    ports:
      - "27017:27017"

Logger

  • Hysteria ORM uses the winston logger by default, you can use it in your application since it's exported from the package as your logger in your application.
import { logger } from 'hysteria-orm';

logger.info('Hello World');
  • You can also customize the hysteria logger by setting a custom logger with the following methods:
    • info
    • error
    • warn
import { logger } from 'hysteria-orm';

logger.setCustomLogger({
  info: (message) => console.log(message),
  error: (message) => console.error(message),
  warn: (message) => console.warn(message),
});
0.0.40

7 months ago

0.0.37

7 months ago

0.0.38

7 months ago

0.0.39

7 months ago

2.0.0

11 months ago

0.0.30

7 months ago

0.0.31

7 months ago

0.0.32

7 months ago

0.0.33

7 months ago

0.0.34

7 months ago

0.0.35

7 months ago

0.0.36

7 months ago

0.1.0

7 months ago

0.3.0

5 months ago

0.1.2

6 months ago

0.1.1

7 months ago

0.0.26

7 months ago

0.0.27

7 months ago

0.0.28

7 months ago

0.0.29

7 months ago

0.3.2

5 months ago

0.3.1

5 months ago

0.1.3

6 months ago

0.0.20

7 months ago

0.0.21

7 months ago

0.0.22

7 months ago

0.0.23

7 months ago

0.0.24

7 months ago

0.0.25

7 months ago

0.0.15

7 months ago

0.0.16

7 months ago

0.0.17

7 months ago

0.0.18

7 months ago

0.0.19

7 months ago

0.0.10

8 months ago

0.0.11

7 months ago

0.0.12

7 months ago

0.0.13

7 months ago

0.0.14

7 months ago

0.0.1

11 months ago

0.0.0

9 months ago

0.2.1

6 months ago

0.0.3

10 months ago

0.2.0

6 months ago

0.0.2

11 months ago

0.2.7

6 months ago

0.0.9

8 months ago

0.2.6

6 months ago

0.0.8

8 months ago

0.2.8

6 months ago

0.2.3

6 months ago

0.0.5

10 months ago

0.2.2

6 months ago

0.0.4

10 months ago

0.2.5

6 months ago

0.0.7

8 months ago

0.2.4

6 months ago

1.2.0

1 year ago

1.2.2

1 year ago

1.1.3

1 year ago

1.1.2

1 year ago

1.1.1

1 year ago

1.1.0

1 year ago

1.0.3

1 year ago

1.0.2

1 year ago

1.0.1

1 year ago

1.0.0

2 years ago