A flexible, framework-agnostic API library for building robust backend services with built-in database and storage capabilities. This library provides a unified interface for handling database operations across various Node.js frameworks, with first-class support for NestJS.
ForgeBase API serves as a bridge between your application and ForgeBase services, providing:
- Unified Interface: A consistent way to interact with ForgeBase services.
- Framework Integrations: Built-in support for NestJS (
@forgebase/api/core/nest). - Database Access: Direct access to ForgeBase Database with full support for schema management, data operations, and row-level security.
- Type Safety: Full TypeScript support.
npm install @forgebase/api @forgebase/database kysely
# Install database driver based on your needs
npm install better-sqlite3 # for SQLite
# or
npm install pg # for PostgreSQL
# or
npm install @libsql/client # for Turso/LibSQLForgeBase API provides a dedicated module for NestJS integration, making it easy to convert your NestJS application into a ForgeBase-compatible backend.
import { Module } from '@nestjs/common';
import { ForgeBaseModule } from '@forgebase/api/core/nest';
import Database from 'better-sqlite3';
import { Kysely, SqliteDialect } from 'kysely';
// Initialize Kysely
const db = new Kysely({
dialect: new SqliteDialect({
database: new Database('mydb.sqlite'),
}),
});
@Module({
imports: [
ForgeBaseModule.register({
config: {
db: db,
enforceRls: true, // Enable Row-Level Security
},
}),
// Your other modules...
],
})
export class AppModule {}This will automatically register the DatabaseService and expose the standard ForgeBase REST API endpoints via DataController and SchemaController.
You can inject DatabaseService into your services to perform database operations programmatically.
import { Injectable } from '@nestjs/common';
import { DatabaseService } from '@forgebase/api/core';
@Injectable()
export class UsersService {
constructor(private readonly db: DatabaseService) {}
async findAll(userContext: any) {
return this.db.query(
'users',
{
select: ['id', 'name', 'email'],
limit: 10,
},
userContext,
);
}
async create(data: any, userContext: any) {
return this.db.insert(
'users',
{
tableName: 'users',
data: data,
},
userContext,
);
}
}The DatabaseService exposes the following methods:
query(tableName, queryParams, userContext): Query records.insert(tableName, data, userContext): Create new records.update(params, userContext): Update records by ID.advanceUpdate(params, userContext): Update records matching a query.delete(tableName, id, userContext): Delete a record by ID.advanceDelete(params, userContext): Delete records matching a query.
You can also manage your database schema programmatically.
// Create a new table
await db.createSchema('users', [
{ name: 'id', type: 'increments', primary: true, nullable: false },
{ name: 'email', type: 'string', unique: true, nullable: false },
]);
// Add columns
await db.addColumn('users', [{ name: 'age', type: 'integer', nullable: true }]);
// Modify permissions
// Get the permission service
const permService = db.getPermissionService();
await permService.setPermissionsForTable('users', {
operations: {
SELECT: [{ allow: 'public' }],
INSERT: [{ allow: 'role', roles: ['admin'] }],
},
});When using ForgeBaseModule, the following endpoints are automatically available:
POST /data/:tableName/query: Query recordsPOST /data/:tableName: Create recordPUT /data/:tableName/:id: Update recordPOST /data/advance-update: Advanced updatePOST /data/:tableName/:id: Delete record (via POST to support strict firewalls/proxies if needed, or check specific controller implementation)POST /data/advance-delete: Advanced delete
GET /schema: Get full schemaGET /schema/tables: Get list of tablesGET /schema/:tableName: Get specific table infoPOST /schema/:tableName: Create tableDELETE /schema/:tableName: Delete tablePOST /schema/:tableName/column: Add columnPUT /schema/:tableName/column: Update columnDELETE /schema/:tableName/column: Delete column
MIT