@klerick/json-api-nestjs-typeorm
v0.1.0-beta.16
Published
MicroOrm adapter for JsonApi Plugin for NestJs
Maintainers
Readme
json-api-nestjs-typeorm
TypeOrm adapter for json-api-nestjs
Installation
$ npm install @klerick/json-api-nestjs-typeormConfiguration params
The following interface is using for the configuration:
export type TypeOrmParam = {
useSoftDelete?: boolean // Use soft delete
runInTransaction?: <Func extends (...args: any) => any>(
isolationLevel: IsolationLevel,
fn: Func
) => ReturnType<Func> // You can use cutom function for wrapping transaction in atomic operation, example: runInTransaction from https://github.com/Aliheym/typeorm-transactional
};Resource Linkage for To-One Relations
To enable automatic resource linkage (data field in relationships) for to-one relations, use the @RelationId decorator from TypeORM.
Example:
import {
Entity,
PrimaryGeneratedColumn,
ManyToOne,
JoinColumn,
RelationId,
} from 'typeorm';
@Entity('comments')
export class Comments {
@PrimaryGeneratedColumn()
public id!: number;
@ManyToOne(() => Users)
@JoinColumn({ name: 'user_id' })
public user!: Users;
// This field will be used for resource linkage
@RelationId((comment: Comments) => comment.user)
public userId!: number;
}The @RelationId decorator creates a virtual field that contains the FK value. The library automatically detects these fields and uses them to populate relationships.{relation}.data in API responses.
