nnestjs-drizzle-crud
Reference

API reference

Every method on SqlBaseCrudService, with full signatures and return types.

The class signature:

class <
  ,
   = <>,
   = <>,
   = <>,
> {}

The four generics correspond to:

GenericDefaultUse
TThe entity row type (typically typeof table.$inferSelect).
CreateDtoPartial<T>The shape accepted by create() and massCreate().
UpdateDtoPartial<T>The shape accepted by update() and massUpdate().
FilterDtoPartial<T>The shape accepted by findAll(filters) and count(filters).

Read

MethodReturnsNotes
find(id, options?)Promise<T | null>By primary key. Skips soft-deleted rows.
findOne(where, options?)Promise<T | null>where is a Partial<T> (equality only).
findAll(filters?, pagination?, options?)Promise<{ data: T[]; total: number; page: number; limit: number }>Supports options.search. See Filtering and Pagination.
exists(id, options?)Promise<boolean>Cheaper than find when you only need a yes/no.
count(filters?, options?)Promise<number>Total rows matching the filter and options.search, ignoring pagination.

find signature

const  = .(1);
const  = .(1, { : ['state'] });

findOne signature

const  = .({ : '[email protected]' });

findAll signature

import { SqlBaseCrudService } from 'nestjs-drizzle-crud';
type User = { id: number; name: string; status: string };
declare class S extends SqlBaseCrudService<User, User, Partial<User>, Partial<User>> {}
declare const s: S;

const page = s.findAll(
  { status: 'active' },            // filters
  { page: 1, limit: 20 },          // pagination
  {
    relations: ['state'],
    search: { term: 'john', columns: ['name'] },
  },                               // options
);
import { SqlBaseCrudService } from 'nestjs-drizzle-crud';
type User = { id: number; name: string; email: string; status: string };
declare class S extends SqlBaseCrudService<User, User, Partial<User>, Partial<User>> {}
declare const s: S;

const total = s.count(
  { status: 'active' },
  { search: { term: 'john', columns: ['name', 'email'] } },
);

Write

MethodReturnsNotes
create(data, options?)Promise<T>Runs validateCreatemapCreateDtoToEntitybeforeCreate → insert → afterCreate.
update(id, data, options?)Promise<T>Throws EntityNotFoundException if missing.
delete(id, options?)Promise<boolean>Hard delete.
softDelete(id, options?)Promise<boolean>Sets deleted_at to now(). Requires soft delete enabled.
restore(id, options?)Promise<T>Clears deleted_at. Throws EntityNotFoundException if missing.

Bulk (single transaction, rolls back on any failure)

MethodReturns
massCreate(data[], options?)Promise<T[]>
massUpdate(ids[], data, options?)Promise<T[]>
massSoftDelete(ids[], options?)Promise<boolean>
massRestore(ids[], options?)Promise<T[]>
massDelete(ids[], options?)Promise<boolean>

Throws BulkOperationException on failure.

MethodReturnsNotes
fullTextSearch(term, columns, pagination?, options?)Promise<{ data: T[]; total: number; page: number; limit: number }>PostgreSQL only. Same envelope as findAll. to_tsvector(...) @@ plainto_tsquery(...) with ts_rank ordering. Excludes soft-deleted rows when soft-delete is enabled.

Transaction helper

const  = .();

executeSqlTransaction(callback) opens a transaction, runs the callback with the transaction handle, commits on success, rolls back on any throw.

SqlOperationOptions

interface SqlOperationOptions {
  /** Run within an existing transaction. */
  ?: any;
  /** Return only these columns. */
  ?: string[];
  /** Eager-load these configured relations (see Relations). */
  ?: string[];
  /** Search configured table columns in findAll() and count(). */
  ?: {
    : string;
    : string[];
    ?: 'ilike' | 'fullText';
  };
  /** Skip before/after hooks for this call. */
  ?: {
    ?: boolean;
    ?: boolean;
  };
  /** Row-level lock. PostgreSQL only. See [Row-level locks](/docs/row-locks). */
  ?: 'update' | 'share' | 'none';
  /** Use FOR NO KEY UPDATE. PostgreSQL only. See [Row-level locks](/docs/row-locks). */
  ?: boolean;
}

relations eager-loads relations declared in the entity's config — see Relations.

Next

On this page