Socket
Socket
Sign inDemoInstall

nukak

Package Overview
Dependencies
12
Maintainers
1
Versions
61
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    nukak

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


Version published
Maintainers
1
Install size
3.27 MB
Created

Readme

Source

tests coverage status license npm version

nukak is the smart ORM for TypeScript and modern JavaScript, is designed to be fast, safe, and easy to integrate into any application. It draws inspiration from TypeORM and Mongo driver.

nukak can run in Node.js, Browser, Cordova, PhoneGap, Ionic, React Native, NativeScript, Expo, and Electron platforms.

nukak provides a consistent API for a wide variety of databases including PostgreSQL, MySQL, SQLite and MongoDB.

 

Sample code

 

Features

  • Serializable queries: its syntax is 100% valid JSON allowing the queries to be transported across platforms with ease.
  • Type-safe and Context-aware queries: squeeze the strength of TypeScript so it auto-completes, validates, and infers the appropriate operators on any level of the queries, including the relations and their fields.
  • High performance: the generated queries are fast, safe, and human-readable.
  • Combines the best elements of OOP (Object Oriented Programming) and FP (Functional Programming).
  • Declarative and imperative transactions for flexibility, and connection pooling for scalability.
  • Modern Pure ESM packages. ESM is natively supported by Node.js 12 and later.
  • soft-delete, virtual fields, repositories.
  • Supports the Data Mapper pattern for maintainability.
  • Transparent support for inheritance between entities for reusability and consistency.
  • Unified API across Databases: same query is transparently transformed according to the configured database.

 

Install

  1. Install the core package:

    npm install nukak --save
    
  2. Install one of the specific adapters for your database:

DatabaseDriverNukak Adapter
MySQLmysql2nukak-mysql
MariaDBmariadbnukak-maria
SQLitesqlite sqlite3nukak-sqlite
PostgreSQLpgnukak-postgres
MongoDBmongodbnukak-mongo (alpha)

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

/**
 * any class can be annotated with this decorator to make it works as
 * an entity.
 */
@Entity()
export class User {
  /**
   * an entity should specify an ID Field, its name and type are automatically detected.
   * the `onInsert` property can be used to specify a custom mechanism for
   * auto-generating the primary-key's value when inserting.
   */
  @Id({ onInsert: uuidv4 })
  id?: string;

  /**
   * the properties of the class can be annotated with this decorator so they
   * are interpreted as a column, its name and type are automatically detected.
   */
  @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,
    {
      $sort: { createdAt: -1 },
      $limit: limit,
    },
    ['id', 'name', 'email']
  );
  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

Keywords

FAQs

Last updated on 22 Jan 2023

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc