@juicyllama/nestjs-core
v1.1.2
Published
Core Modules for NestJS applications by JuicyLlama Studio
Downloads
200
Readme
Visit the JuicyLlama to learn more.
BaseService
BaseService wraps common TypeORM operations and (optionally) emits websocket events after database actions.
Basic usage
const service = new BaseService(query, repository)Usage The Nest Way
@Injectable()
export class BillingService extends BaseService<T> {
constructor(
readonly query: Query<T>,
@InjectRepository(E) readonly repository: Repository<T>,
) {
super(query, repository)
}
}
BaseService API
Each method delegates to the underlying Query helper and Repository and returns the corresponding result.
create(data, relations?): Create a single record (optionally with relations) and return it.createMany(qty, data): Createqtyrecords using the provided data and return the array.bulk(data, import_mode, dedup_field?): Bulk insert, upsert, or delete records based on the import mode.findAll(options?): Find all records matching the TypeORM find options.findOne(options?): Find a single record matching the TypeORM find options.findById(id, relations?): Find a record by id (optionally with relations). Throws ifidis missing.count(options?): Count records matching the TypeORM find options.sum(metric, options?): Sum a numeric metric for records matching the options.avg(metric, options?): Average a numeric metric for records matching the options.update(data, relations?): Update a record and return it (optionally with relations).remove(record): Soft delete a record (recoverable).purge(record): Hard delete a record (not recoverable).raw(sql): Execute a raw SQL query via the repository.
Websocket support
This package uses a NestJS gateway internally. Register BaseWebSocketGateway once in a module to initialize the websocket server.
import {BaseWebSocketGateway, BillingService} from '@juicyllama/nestjs-core'
@Module({
providers: [BaseWebSocketGateway, BillingService],
})
export class BillingModule {}Pass options.ws in the constructor to emit events on database actions.
const service = new BaseService(query, repository, {
ws: {
channel: 'users',
service: 'users',
namespace: '/events',
events: {
create: 'users.create',
update: 'users.update',
},
},
})Supported actions
createcreateManybulkupdateremovepurge
WebSocketOptions
enabled?: boolean- disable all websocket emits iffalsechannel?: string- default channel for emitsnamespace?: string- optional namespace for emitsservice?: string- service identifier for payload contextevents?: Partial<Record<WebSocketAction, string>>- override event namesbuildPayload?: (context) => unknown- shape payload before emitthrowOnError?: boolean- throw if emit fails or the gateway isn't registered
