JSPM

  • Created
  • Published
  • Downloads 7
  • Score
    100M100P100Q51706F
  • License MIT

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

Package Exports

  • nukak-browser
  • nukak-browser/nukak-browser.min.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-browser) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

code

tests coverage status license npm version

nukak is a powerful and efficient ORM, designed from the ground up to be fast, secure, and easy to use. It is inspired by other ORMs such as TypeORM and MongoDB Node.js Driver, and has been designed to rely on serializable JSON syntax for easy transport across platforms.

 

Features

  • Takes full advantage of TypeScript intelligent type-inference everywhere.
  • Serializable JSON syntax for all the queries.
  • Querying data with $project, $filter, $sort, $limit, works at multiple levels (including deep relations and their fields).
  • Supports the Data Mapper pattern.
  • The generated queries are performant, safe, and human-readable.
  • soft-delete, virtual fields, repositories, connection pooling.
  • Transparent support for inheritance between entities.
  • Modern Pure ESM approach. ESM is natively supported by Node.js 12 and later.
  • Declarative and imperative transactions.
  • Unified API for MySQL, MariaDB, SQLite, Postgres, MongoDB.

 

Install

  1. Install the core package:

    npm install nukak --save
  2. 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

 

Configure

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';

export 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 }
);

// the default querier pool that `nukak` will use
setQuerierPool(querierPool);

 

Define the entities

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

import { v4 as uuidv4 } from 'uuid';
import { Id, Field, Entity } from 'nukak/entity';

@Entity()
export class User {
  @Id({ onInsert: uuidv4 })
  id?: string;
  @Field()
  name?: string;
  @Field()
  email?: string;
  @Field()
  password?: string;
}

 

Manipulate the data

import { getQuerier } from 'nukak';
import { User } from './shared/models/index.js';

async function findLastUsers(limit = 10) {
  const querier = await getQuerier();
  const users = await querier.findMany(User, {
    $project: ['id', 'name', 'email'],
    $sort: { createdAt: -1 },
    $limit: limit,
  });
  await querier.release();
  return users;
}

async function createUser(body: User) {
  const querier = await getQuerier();
  const id = await querier.insertOne(User, body);
  await querier.release();
  return id;
}

 

Learn more about nukak at its website https://nukak.org