Durcno Documentation
website·Indexed 30 days ago
https://durcno.devA type-safe SQL query builder and schema definition tool for PostgreSQL. Durcno provides a fluent API for CRUD operations, relational queries, and schema management, including support for PostgreSQL-specific features like ENUMs, indexes, PostGIS, and pgvector. It features end-to-end TypeScript type safety, Zod validation schema generation, and a migration system.
What's inside Durcno
- Durcno is a PostgreSQL Query Builder and Migration Manager for TypeScript. It is designed to provide intuitive abstractions, robust type safety, and smart migration management for TypeScript developers working with PostgreSQL.
Overview of Durcno PostgreSQL Query Builder
Durcno is a PostgreSQL Query Builder and Migration Manager for TypeScript. It is designed to provide a type-safe wrapper for PostgreSQL, focusing on intuitive abstractions that map naturally to SQL, rigorous compile-time type safety for complex operations (like joins and aggregations), and a dedicated migration system. Unlike multi-database ORMs, Durcno focuses exclusively on PostgreSQL to leverage database-specific features.Overview of Durcno query shortcut functions
Durcno provides$-prefixed utility functions that act as convenient shortcuts wrapping standard query operations. All these functions accept an optionalwhereclause as the last argument, return aQueryPromise(which can be awaited or used with.then()), and are fully type-safe with inferred return types.Function Purpose Returns $countCount rows number$existsCheck if rows exist boolean$firstGet first matching row T | null$distinctGet unique column values T[]$sumSum numeric column number | null$avgAverage of column number | null$minMinimum value number | null$maxMaximum value number | nullOverview of Durcno query shortcut functions
Durcno provides a set of$-prefixed utility functions to simplify common query patterns. All these functions accept an optionalwhereclause as the last argument, return aQueryPromisethat can be awaited, and are fully type-safe with inferred return types.Function Purpose Returns $countCount rows number$existsCheck if rows exist boolean$firstGet first matching row T | null$distinctGet unique column values T[]$sumSum numeric column number | null$avgAverage of column number | null$minMinimum value number | null$maxMaximum value number | nullOverview of Durcno PostgreSQL Query Builder
Durcno is a PostgreSQL Query Builder and Migration Manager for TypeScript. It provides a declarative way to define database schemas and perform queries with full TypeScript inference, avoiding decorators or 'magic' configurations. It supports multiple drivers includingpg,postgres,bun, andpglite, and includes first-class support for PostGIS geographic column types.Overview of Durcno PostgreSQL Query Builder and Migration Manager
Durcno is a PostgreSQL Query Builder and Migration Manager for TypeScript. It is designed to provide intuitive abstractions, robust type safety, and smart migration management for TypeScript applications interacting with PostgreSQL databases.Overview of Durcno PostgreSQL Query Builder and Migration Manager
Durcno is a PostgreSQL Query Builder and Migration Manager for TypeScript. It is designed to provide intuitive abstractions, robust type safety, and smart migration management for developers working with PostgreSQL in TypeScript environments.Durcno compatibility and runtime support
Durcno is optimized for the PostgreSQL ecosystem and is compatible with the following runtimes and databases:
- Runtimes: Node.js, Bun, Deno
- Databases: PostgreSQL, PGlite
Generate Zod schemas for database operations
Durcno provides two main functions to generate Zod validation schemas from table definitions, ensuring type safety at both compile-time and runtime. These functions automatically handle nullability, defaults, and generated columns based on the table's column definitions.import { createInsertSchema, createUpdateSchema } from "durcno/validators/zod"; import { Users } from "./schema.ts"; // Generate schemas from your table definition const insertUserSchema = createInsertSchema(Users); const updateUserSchema = createUpdateSchema(Users);Automatic camelCase to snake_case conversion in Durcno
Durcno automatically converts TypeScript camelCase identifiers to snake_case when generating SQL statements and migration files. This applies to schemas, table names, column names, enum names, and sequence names. Consecutive uppercase sequences (acronyms) are handled correctly (e.g.,
XMLParserbecomesxml_parser).Note: Enum values (e.g.,
["admin", "user"]) are NOT converted and are stored exactly as written.// TypeScript definition export const BlogPosts = table("public", "blogPosts", { id: pk(), authorId: bigint({ notNull }), publishedAt: timestamp({ notNull }), }); // Resulting generated SQL CREATE TABLE "public"."blog_posts" ( "id" bigserial PRIMARY KEY, "author_id" bigint NOT NULL, "published_at" timestamp NOT NULL );Define a database schema with Durcno
Durcno uses type-safe TypeScript code to define PostgreSQL database structures. A schema consists of Tables (core data structure), Columns (field definitions with constraints), Enum Types (constrained values), and Relations (type-safe links between tables).import { bigint, enumtype, index, many, notNull, now, one, pk, relations, table, timestamptz, unique, uniqueIndex, varchar, } from "durcno"; // Define an enum export const UserRole = enumtype("public", "userRole", ["admin", "user"]); // Define tables export const Users = table( "public", "users", { id: pk(), username: varchar({ length: 50, unique, notNull }), email: varchar({ length: 255, unique, notNull }), role: UserRole.enumed({ notNull }), createdAt: timestamptz({ notNull }).default(now()), }, { indexes: (t) => [index([t.email]), index([t.createdAt])], }, ); export const UsersRelations = relations(Users, () => ({ posts: many(Posts, Posts.userId), })); export const Posts = table( "public", "posts", { id: pk(), userId: bigint({ notNull }).references(() => Users.id), title: varchar({ length: 255, notNull }), createdAt: timestamptz({ notNull }).default(now()), }, { indexes: (t) => [index([t.userId]), index([t.userId, t.createdAt])], }, ); export const PostsRelations = relations(Posts, () => ({ author: one(Users, Users.id), }));Generate Zod schemas for database operations
Durcno provides built-in support for generating Zod validation schemas from table definitions. This ensures type safety at both compile-time and runtime by validating data before it is sent to the database. Two primary functions are provided:createInsertSchemafor INSERT operations andcreateUpdateSchemafor UPDATE operations.import { createInsertSchema, createUpdateSchema } from "durcno/validators/zod"; import { Users } from "./schema.ts"; // Generate schemas from your table definition const insertUserSchema = createInsertSchema(Users); const updateUserSchema = createUpdateSchema(Users);