JSPM

  • Created
  • Published
  • Downloads 15
  • Score
    100M100P100Q65288F
  • License MIT

flexible and efficient ORM, with declarative JSON syntax and smart type-safety

Package Exports

  • nukak-postgres
  • nukak-postgres/index.js

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (nukak-postgres) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

nukak · license tests coverage status npm version

Learn more of nukak on its website https://nukak.org

Quick Start

nukak is a flexible and efficient ORM, with declarative JSON syntax and really smart type-safety.

The nukak queries can be safely written in the frontend (browser/mobile) and sent to the backend; or only use nukak in the backend, or even in a mobile app with an embedded database (like sqlite).

Features

Installation

  1. Install the nukak main package:
npm install nukak --save
  1. Install one of the specific adapters for your database:
Database Driver Nukak Adapter
MySQL mysql2 nukak-mysql
MariaDB mariadb nukak-maria
SQLite sqlite sqlite3 nukak-sqlite
PostgreSQL pg nukak-postgres
MongoDB mongodb nukak-mongo

For example, for Postgres:

npm install pg nukak-postgres --save
  1. Additionally, your tsconfig.json may need the following flags:
"target": "es2020",
"experimentalDecorators": true,
"emitDecoratorMetadata": true

Configuration

A default querier-pool can be set in any of the bootstrap files of your app (e.g. in the server.ts).

import { setQuerierPool } from 'nukak';
import { PgQuerierPool } from 'nukak-postgres';

const querierPool = new PgQuerierPool(
  {
    host: 'localhost',
    user: 'theUser',
    password: 'thePassword',
    database: 'theDatabase',
  },
  // optionally, a logger can be passed to log the generated SQL queries
  { logger: console.log }
);

setQuerierPool(querierPool);

Definition of Entities

Take any dump class (aka DTO) and annotate it with the decorators from nukak/entity.

import { v4 as uuidv4 } from 'uuid';
import { Field, ManyToOne, Id, OneToMany, Entity, OneToOne, ManyToMany } from 'nukak/entity/index.js';

@Entity()
export class Profile {
  /**
   * primary-key.
   * the `onInsert` callback can be used to specify a custom mechanism for auto-generating
   * the default value of a field when inserting a new record.
   */
  @Id({ onInsert: uuidv4 })
  id?: string;
  @Field()
  picture?: string;
  /**
   * foreign-keys are really simple to specify.
   */
  @Field({ reference: () => User })
  creatorId?: string;
}

@Entity()
export class User {
  @Id({ onInsert: uuidv4 })
  id?: string;
  @Field()
  name?: string;
  @Field()
  email?: string;
  @Field()
  password?: string;
  /**
   * `mappedBy` can be a callback or a string (callback is useful for auto-refactoring).
   */
  @OneToOne({ entity: () => Profile, mappedBy: (profile) => profile.creatorId, cascade: true })
  profile?: Profile;
}

@Entity()
export class MeasureUnitCategory {
  @Id({ onInsert: uuidv4 })
  id?: string;
  @Field()
  name?: string;
  @OneToMany({ entity: () => MeasureUnit, mappedBy: (measureUnit) => measureUnit.category })
  measureUnits?: MeasureUnit[];
}

@Entity()
export class MeasureUnit {
  @Id({ onInsert: uuidv4 })
  id?: string;
  @Field()
  name?: string;
  @Field({ reference: () => MeasureUnitCategory })
  categoryId?: string;
  @ManyToOne({ cascade: 'persist' })
  category?: MeasureUnitCategory;
}

Query the data

import { getQuerier } from 'nukak';
import { Transactional, InjectQuerier } from 'nukak/querier/index.js';
import { User } from './shared/models/index.js';

export class UserService {
  @Transactional()
  async getUsers(@InjectQuerier() querier?: Querier): Promise<User[]> {
    return querier.findMany(User, {
      $project: { id: true, email: true, profile: ['picture'] },
      $filter: { email: { $iendsWith: '@google.com' } },
      $sort: { createdAt: -1 },
      $limit: 100,
    });
  }
}

See more in https://nukak.org 🔆