Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

nukak

Package Overview
Dependencies
Maintainers
0
Versions
82
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

nukak

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

  • 1.4.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
64
decreased by-46.22%
Maintainers
0
Weekly downloads
 
Created
Source

nukak maku

tests coverage status license npm version

nukak is the smartest ORM for TypeScript, it is designed to be fast, safe, and easy to integrate into any application.

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

nukak has a consistent API for distinct databases, including PostgreSQL, MySQL, MariaDB, and SQLite.

 

const companyUsers = await userRepository.findMany({
  $select: { email: true, profile: ['picture'] },
  $where: { email: { $endsWith: '@example.com' } },
  $sort: { createdAt: -1 },
  $limit: 100,
});

 

Why nukak?

See this article in medium.com.

 

Features

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

 

1. Install

  1. Install the core package:

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

DatabaseDriverNukak Adapter
PostgreSQLpgnukak-postgres
SQLitesqlite sqlite3nukak-sqlite
MariaDBmariadbnukak-maria
MySQLmysql2nukak-mysql

For example, for Postgres:

npm install pg nukak-postgres --save
  1. Additionally, your tsconfig.json may need the following flags:

    "target": "es2022",
    "experimentalDecorators": true,
    "emitDecoratorMetadata": true
    

 


 

2. Define the entities

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

import { randomUUID } from 'node:crypto';
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 must 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: () => randomUUID })
  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;

  /**
   * fields are `updatable: true` by default but can also be marked as `updatable: false` so they can only be inserted and read after.
   */
  @Field({ updatable: false })
  email?: string;

  /**
   * fields are `eager: true` by default but can also be marked as `eager: false` (aka lazy fields).
   */
  @Field({ eager: false })
  password?: string;
}

 

3. Setup a querier-pool

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

// file: ./shared/orm.ts

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.debug },
);

 

4. Manipulate the data

import { querierPool } from './shared/orm.js';
import { User } from './shared/models/index.js';

async function findLastUsers(limit = 100) {
  const querier = await querierPool.getQuerier();
  const users = await querier.findMany(User, {
    $select: { id: true, name: true, email: true },
    $sort: { createdAt: 'desc' },
    $limit: limit,
  });
  return users;
}

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

 

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

Keywords

FAQs

Package last updated on 21 Aug 2024

Did you know?

Socket

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
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc