npm install @trailbase/sdk// trailbase.module.ts
import { Module, Global } from '@nestjs/common';
import { TrailbaseClient } from '@trailbase/sdk';
@Global()
@Module({
providers: [
{
provide: 'TRAILBASE',
useFactory: () =>
new TrailbaseClient({
apiKey: process.env.TRAILBASE_API_KEY!,
region: 'us-east-1',
}),
},
],
exports: ['TRAILBASE'],
})
export class TrailbaseModule {}Register TrailbaseModule in your AppModule imports. Use the interceptor for automatic logging or inject TRAILBASE directly into services for manual tracking.
// audit.interceptor.ts
@Injectable()
export class AuditInterceptor implements NestInterceptor {
constructor(@Inject('TRAILBASE') private trailbase: TrailbaseClient) {}
intercept(context: ExecutionContext, next: CallHandler) {
return next.handle().pipe(
tap(() => {
const req = context.switchToHttp().getRequest();
this.trailbase.track(`${req.method.toLowerCase()}.${req.path}`, {
actor: req.user?.id,
metadata: { statusCode: context.switchToHttp().getResponse().statusCode },
});
}),
);
}
}
// Or use directly in a service
@Injectable()
export class UsersService {
constructor(@Inject('TRAILBASE') private trailbase: TrailbaseClient) {}
async updateRole(actorId: string, userId: string, role: string) {
await this.trailbase.track('user.role.update', {
actor: actorId,
target: userId,
metadata: { newRole: role },
});
}
}@Controller('audit')
export class AuditController {
constructor(@Inject('TRAILBASE') private trailbase: TrailbaseClient) {}
@Get()
async search(@Query() query: SearchDto) {
return this.trailbase.search({
actor: query.userId,
from: query.from,
});
}
}Join the waitlist for early access. Five-minute setup, no infrastructure changes.
Get Early Access