UQL ORM

0.9.0 · active · verified Wed Apr 22

UQL ORM is a fast, type-safe TypeScript Object-Relational Mapper (ORM) designed with a JSON-native query protocol. Currently at version 0.9.0, it aims for universal compatibility, running across Node.js, Bun, Deno, Cloudflare Workers, Electron, React Native, and browsers. It provides a unified API for various SQL and NoSQL databases, including PostgreSQL, MySQL, MariaDB, SQLite, LibSQL, Neon, D1, and MongoDB. Key differentiators include 100% serializable queries, deeply type-safe APIs for intelligent auto-completion, multi-level operators, and robust support for advanced features like semantic search. While still pre-1.0, the project shows active development with frequent updates and a strong focus on performance and developer experience, offering both decorator-based and imperative entity definition styles.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates defining a User entity with decorators, connecting to a PostgreSQL database using a querier pool, syncing the schema (caution: drops table), creating a new user, and performing a filtered query to fetch users by name. It also highlights necessary TypeScript compiler options.

import { Entity, Id, Field } from 'uql-orm';
import { PgQuerierPool } from 'uql-orm/postgres';

// 1. Define your Entity (User.ts)
@Entity()
export class User {
  @Id({ type: 'uuid' })
  id?: string;

  @Field({ unique: true })
  email?: string;

  @Field()
  name?: string;
}

// 2. Set up the Pool and Query (app.ts)
async function runQuery() {
  const pool = new PgQuerierPool({
    host: process.env.DB_HOST ?? 'localhost',
    port: parseInt(process.env.DB_PORT ?? '5432', 10),
    database: process.env.DB_NAME ?? 'app_db',
    user: process.env.DB_USER ?? 'postgres',
    password: process.env.DB_PASSWORD ?? 'password',
  });

  try {
    await pool.withQuerier(async (querier) => {
      // Ensure schema is in sync (for development/testing)
      // In production, use migrations CLI
      await querier.sync(User, { drop: true }); // DANGER: drops table

      // Create a user
      const newUser = await querier.createOne(User, { email: 'test@example.com', name: 'John Doe' });
      console.log('Created user:', newUser);

      // Find users
      const users = await querier.findMany(User, {
        $where: { name: { $istartsWith: 'John' } },
        $select: { id: true, email: true },
      });
      console.log('Found users:', users);
    });
  } catch (error) {
    console.error('Database operation failed:', error);
  } finally {
    await pool.end(); // Don't forget to release connections
  }
}

runQuery();

// tsconfig.json configuration for decorators:
// {
//   "compilerOptions": {
//     "experimentalDecorators": true,
//     "emitDecoratorMetadata": true,
//     "moduleResolution": "NodeNext",
//     "module": "NodeNext",
//     "target": "ES2022"
//   }
// }

view raw JSON →