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
-
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 |
MySQL | mysql2 | nukak-mysql |
For example, for Postgres
:
npm install pg nukak-postgres --save
-
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';
@Entity()
export class User {
@Id({ onInsert: () => randomUUID })
id?: string;
@Field()
name?: string;
@Field({ updatable: false })
email?: string;
@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
).
import { PgQuerierPool } from 'nukak-postgres';
export const querierPool = new PgQuerierPool(
{
host: 'localhost',
user: 'theUser',
password: 'thePassword',
database: 'theDatabase',
},
{ 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();
try {
const users = await querier.findMany(User, {
$select: { id: true, name: true, email: true },
$sort: { createdAt: 'desc' },
$limit: limit,
});
return users;
} finally {
await querier.release();
}
}
async function createUser(data: User) {
const querier = await querierPool.getQuerier();
try {
const id = await querier.insertOne(User, data);
return id;
} finally {
await querier.release();
}
}
Learn more about nukak
at its website https://nukak.org