npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

@the-forgebase/sdk

v0.0.12

Published

A powerful, type-safe TypeScript SDK for interacting with ForgeBase services, providing comprehensive database operations, real-time features, and advanced query capabilities.

Readme

ForgeBase TypeScript SDK

A powerful, type-safe TypeScript SDK for interacting with ForgeBase services, providing comprehensive database operations, real-time features, and advanced query capabilities.

Core Features

  • Type-Safe Query Builder:

    • Fluent API design
    • Advanced filtering
    • Complex joins and relations
    • Aggregations and window functions
    • Transaction support
    • Raw query support
    • Query optimization
  • Database Operations:

    • CRUD operations
    • Batch operations
    • Pagination
    • Sorting
    • Custom queries
    • Schema validation
    • Error handling
  • Security Features:

    • Input sanitization
    • Type validation
    • Error boundaries
  • Advanced Querying:

    • Window functions
    • Common Table Expressions (CTEs)
    • Recursive queries
    • Complex filtering
    • Advanced joins
    • Subqueries
    • Aggregations

Installation

npm install @the-forgebase/sdk
# or
yarn add @the-forgebase/sdk
# or
pnpm add @the-forgebase/sdk

Basic Usage

Database Operations

import { DatabaseSDK } from '@the-forgebase/sdk/client';

// Initialize with your API URL
const db = new DatabaseSDK('http://localhost:3000', {
  credentials: 'include',
  headers: {
    'Content-Type': 'application/json',
  },
});

// Basic CRUD Operations
const users = await db
  .table('users')
  .select('id', 'name', 'email')
  .where('status', 'active')
  .execute({
    headers: {
      'some-stuff': 'true',
    },
  });

// Create a new record
const newUser = await db.table('users').create({
  name: 'John Doe',
  email: '[email protected]',
  role: 'user',
});

// Update a record
await db.table('users').update(1, {
  status: 'inactive',
});

// Delete a record
await db.table('users').delete(1);

Advanced Queries

// Complex filtering with type safety
interface User {
  id: number;
  name: string;
  email: string;
  role: string;
  department: string;
  salary: number;
}

const results = await db
  .table<User>('users')
  .where('status', 'active')
  .andWhere((query) => {
    query.where('role', 'manager').where('department', 'IT').orWhere('salary', '>', 100000);
  })
  .orderBy('name', 'asc')
  .limit(10)
  .execute();

// Aggregations
const stats = await db.table<User>('users').groupBy('department').select('department').count('id', 'total_users').avg('salary', 'avg_salary').having('total_users', '>', 5).execute();

// Window Functions
const rankedUsers = await db
  .table<User>('users')
  .select('name', 'department', 'salary')
  .window('rank', 'salary_rank', {
    partitionBy: ['department'],
    orderBy: [{ field: 'salary', direction: 'desc' }],
  })
  .execute();

// Advanced Window Functions
const analysis = await db
  .table<User>('users')
  .windowAdvanced('sum', 'running_total', {
    field: 'salary',
    over: {
      partitionBy: ['department'],
      orderBy: [{ field: 'hire_date', direction: 'asc' }],
      frame: {
        type: 'ROWS',
        start: 'UNBOUNDED PRECEDING',
        end: 'CURRENT ROW',
      },
    },
  })
  .execute();

CTEs and Recursive Queries

// Simple CTE
const highPaidUsers = db.table<User>('users').where('salary', '>', 100000);

const result = await db.table<User>('users').with('high_paid', highPaidUsers).execute();

// Recursive CTE
interface Category {
  id: number;
  parent_id: number | null;
  name: string;
}

const categories = await db
  .table<Category>('categories')
  .withRecursive(
    'category_tree',
    // Initial query
    db.table('categories').where('parent_id', null),
    // Recursive query
    db.table('categories').join('category_tree', 'parent_id', 'id'),
    { unionAll: true },
  )
  .execute();

Error Handling

try {
  const result = await db.table('users').where('id', 1).execute();
} catch (error) {
  if (error instanceof QueryError) {
    // Handle query-related errors
    console.error('Query Error:', error.message);
  } else if (error instanceof ValidationError) {
    // Handle validation errors
    console.error('Validation Error:', error.details);
  } else if (error instanceof AuthorizationError) {
    // Handle authorization errors
    console.error('Authorization Error:', error.message);
  } else {
    // Handle other errors
    console.error('Unknown Error:', error);
  }
}

Type Safety

The SDK provides full TypeScript support with generic types:

interface User {
  id: number;
  name: string;
  email: string;
  role: string;
}

interface Order {
  id: number;
  userId: number;
  total: number;
  status: string;
}

// Type-safe queries
const users = await db.table<User>('users').select('id', 'name', 'email').where('role', 'admin').execute();

// Type-safe joins
const orders = await db.table<Order>('orders').join<User>('users', 'userId', 'id').select('orders.id', 'users.name', 'orders.total').execute();

Performance Optimization

Query Optimization

// Use select to limit returned fields
const users = await db.table('users').select('id', 'name').where('active', true).execute();

// Use indexes effectively
const result = await db
  .table('users')
  .where('email', '[email protected]') // Assuming email is indexed
  .first()
  .execute();

// Batch operations (WIP*)
await db.table('users').createMany([
  { name: 'User 1', email: '[email protected]' },
  { name: 'User 2', email: '[email protected]' },
]);

Building

Run nx build sdk to build the library.

Running Tests

# Run unit tests
nx test sdk

# Run integration tests
nx test sdk --config=integration

# Run tests with coverage
nx test sdk --coverage

License

MIT