Skip to main content
Home

latest
It is unknown whether this package works with Cloudflare Workers, Node.js, Deno, Bun, Browsers
It is unknown whether this package works with Cloudflare Workers
It is unknown whether this package works with Node.js
It is unknown whether this package works with Deno
It is unknown whether this package works with Bun
It is unknown whether this package works with Browsers
JSR Score
58%
Published
2 weeks ago (0.1.0)

Denorm

A lightweight ORM for PostgreSQL and SQLite in Deno, inspired by Drizzle ORM.

Features

  • Support for PostgreSQL and SQLite databases
  • Type-safe query building
  • Schema definition with migrations
  • Simple and intuitive API
  • Zero dependencies beyond Deno standard library

Installation

import denorm from "https://raw.githubusercontent.com/yourusername/denorm/main/denorm.ts";

Quick Start

  1. Define your database configuration:
// config.ts
import { DatabaseConfig } from "./denorm.ts";

const config: DatabaseConfig = {
  type: "sqlite", // or "postgres"
  connectionString: "./database.sqlite",
};

export default config;
  1. Define your schema:
// schema.ts
import { 
  text, 
  integer, 
  timestamp,
  TableDefinition 
} from "./denorm.ts";

export const schema: Record<string, TableDefinition> = {
  users: {
    name: "users",
    columns: {
      id: integer({ primaryKey: true, autoIncrement: true }),
      username: text({ unique: true, nullable: false }),
      email: text({ unique: true, nullable: false }),
      created_at: timestamp({ defaultValue: "CURRENT_TIMESTAMP" })
    },
    indexes: {
      users_email_idx: {
        columns: ["email"],
        unique: true
      }
    }
  }
};
  1. Initialize the database and create tables:
import denorm from "./denorm.ts";
import config from "./config.ts";
import { schema } from "./schema.ts";

const db = denorm(config);
await db.connect();

// Define tables from schema
for (const [_, table] of Object.entries(schema)) {
  db.defineTable(table);
}

// Create tables in database
await db.createTables();

// Now you can use the db
const users = await db.find("users");
console.log(users);

// Close connection when done
await db.disconnect();

Migration Workflow

Denorm includes a full-featured migration system:

  1. Initialize migrations:
deno run -A migrate.ts init
  1. Generate a migration from your schema:
deno run -A migrate.ts schema create_users_and_posts
  1. Apply migrations:
deno run -A migrate.ts up
  1. List migrations:
deno run -A migrate.ts list
  1. Rollback the last migration:
deno run -A migrate.ts down
  1. Rollback multiple migrations:
deno run -A migrate.ts down --step=3
  1. Rollback to a specific migration:
deno run -A migrate.ts down --to=20230101120000_create_users
  1. Push schema changes directly to the database:
deno run -A migrate.ts push

API Reference

Database Configuration

type DatabaseConfig = {
  type: 'postgres' | 'sqlite';
  connectionString: string;
};

Column Types

  • text(options?)
  • integer(options?)
  • real(options?)
  • boolean(options?)
  • date(options?)
  • timestamp(options?)
  • json(options?)
  • jsonb(options?) (PostgreSQL only)

Column Options

type ColumnDefinition = {
  type: ColumnType;
  primaryKey?: boolean;
  autoIncrement?: boolean;
  unique?: boolean;
  nullable?: boolean;
  defaultValue?: unknown;
  references?: {
    table: string;
    column: string;
  };
};

CRUD Operations

// Insert
const user = await db.insert("users", { 
  username: "johndoe", 
  email: "john@example.com" 
});

// Find all
const allUsers = await db.find("users");

// Find with condition
const john = await db.findOne("users", { username: "johndoe" });

// Update
await db.update(
  "users", 
  { email: "john.doe@example.com" }, 
  { id: user.id }
);

// Delete
await db.delete("users", { id: user.id });

Raw Queries

const result = await db.query(
  "SELECT * FROM users WHERE username LIKE ?", 
  ["%john%"]
);

License

MIT

New Ticket: Report package

Please provide a reason for reporting this package. We will review your report and take appropriate action.

Please review the JSR usage policy before submitting a report.

Add Package

deno add jsr:@dancaldera/denorm

Import symbol

import * as denorm from "@dancaldera/denorm";
or

Import directly with a jsr specifier

import * as denorm from "jsr:@dancaldera/denorm";

Add Package

pnpm i jsr:@dancaldera/denorm
or (using pnpm 10.8 or older)
pnpm dlx jsr add @dancaldera/denorm

Import symbol

import * as denorm from "@dancaldera/denorm";

Add Package

yarn add jsr:@dancaldera/denorm
or (using Yarn 4.8 or older)
yarn dlx jsr add @dancaldera/denorm

Import symbol

import * as denorm from "@dancaldera/denorm";

Add Package

npx jsr add @dancaldera/denorm

Import symbol

import * as denorm from "@dancaldera/denorm";

Add Package

bunx jsr add @dancaldera/denorm

Import symbol

import * as denorm from "@dancaldera/denorm";