Cassandra ORM wrapper for nestjs based on cassandra-driver
package.
use npm:
npm i cassandra-orm4nest --save
use yarn:
yarn add cassandra-orm4nest
use @Entity
and @Column
decorator define an entity:
// device.entity.ts
import { Column, Entity } from "cassandra-orm4nest";
@Entity({
keyspace: 'test',
table: 'device'
})
export default class Device {
@Column({name: 'serial_number'})
serialNumber: string;
@Column({name: 'create_time'})
createTime: Date;
@Column()
version: string;
@Column({name: 'is_online'})
isOnline: boolean;
}
Just like typeorm, you can use forRoot
method to configure the database and use forFeature
method to register entities:
// orm-test.module.ts
import { Module } from "@nestjs/common";
import { auth } from "cassandra-driver";
import { CassandraOrmModule } from "cassandra-orm4nest";
import DeviceController from "device.controller";
import Device from "device.entity";
import DeviceService from "device.service";
@Module({
imports: [
CassandraOrmModule.forRoot({ // database configuration
contactPoints: ['localhost'],
authProvider: new auth.PlainTextAuthProvider('username', 'password'),
localDataCenter: 'datacenter1'
}),
CassandraOrmModule.forFeature([ // register entities
Device
])
],
controllers: [DeviceController], // related controller
providers: [DeviceService] // related service
})
export default class OrmTestModule {}
Entities registered in forFeature
method will generate corresponding mapper objects witch type is mapping.ModelMapper and can be injected by @InjectMapper
decorator.
import { Injectable } from "@nestjs/common";
import { Client } from "cassandra-driver";
import { InjectClient, InjectMapper, BaseService } from "cassandra-orm4nest";
import Device from "device.entity";
@Injectable()
export default class DeviceService extends BaseService<Device> {
constructor(
@InjectMapper(Device) private readonly mapper, // inject mapper object
@InjectClient() client: Client // inject cassandra connection client
) {
super(client, mapper, Device); // inherit the parent class constructor
}
}
As you can see, we can extend the BaseService
class that implements the basic CRUD methods, includes:
saveOne
: Save a single entity.saveMany
: Save multiple entities.finadAll
: Query the full table, it is quivalent to thefindAll
method in theModelMapper
class incassandra-driver
, there is a limit on the number of results for a single query, default is 5000.findRealAll
: Query the full table, unlikefindAll
, there is no limit on the number of results for a single query, because it will converted toeachRow
method to perform query operations.findMany
: Query based on conditions, it is quivalent to thefind
method in theModelMapper
class incassandra-driver
, there is a limit on the number of results for a single query, default is 5000.findRealMany
: Query based on conditions, unlikefindMany
, there is no limit on the number of results for a single query, because it will converted toeachRow
method to perform query operations.findOne
: : Query based on conditions, return the first item that meets the condition.update
: Update based on conditions, it is quivalent to theupdate
method in theModelMapper
class incassandra-driver
.updateMany
: Perform multiple conditional update operations.remove
: Remove based on conditions, it is quivalent to theremove
method in theModelMapper
class incassandra-driver
.removeMany
: Perform multiple conditional remove operations.delete
: delete use origin cql, can not write all primary keys
Inject service directly into the control layer:
// device.controller.ts
import DeviceService from "device.service"
export default class DeviceController {
constructor(
private readonly deviceService: DeviceService // inject
){}
@Get('doSomthing')
async doSomething() {
// TODO
}
}
There is a demo in test folder.
Import schema:
cqlsh <host> -u <username> -p <password> < test/schema.cql
Run test server:
npm run test -- --host <db host> --username <db username> --password <db password> --datacenter <db datacenter>
Access URL in the browser:
http://localhost:30000/device/doSomthing
- JetBrains for free open source licenses