nukak is the smartest ORM for TypeScript, it 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 has a consistent API for distinct databases, including PostgreSQL, MySQL, SQLite, and MongoDB.
Features
- 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. - Serializable queries: its syntax is
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.
- Combines the best elements of
OOP
(Object Oriented Programming) and FP
(Functional 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 packages.
ESM
is natively supported by Node.js 12 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.
Install
-
Install the core package:
npm install nukak --save
-
Install one of the specific adapters for your database:
Database | Driver | Nukak Adapter |
---|
PostgreSQL | pg | nukak-postgres |
SQLite | sqlite sqlite3 | nukak-sqlite |
MariaDB | mariadb | nukak-maria |
MongoDB | mongodb | nukak-mongo |
MySQL | mysql2 | nukak-mysql |
For example, for Postgres
:
npm install pg nukak-postgres --save
-
Additionally, your tsconfig.json
may need the following flags:
"target": "es2020",
"experimentalDecorators": true,
"emitDecoratorMetadata": true
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;
}
Setup a default querier-pool
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',
},
{ logger: console.log }
);
setQuerierPool(querierPool);
Manipulate the data
import { getQuerier } from 'nukak';
import { User } from './shared/models/index.js';
async function findFirstUsers(limit = 100) {
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