npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

cassandra-orm4nest

v1.0.5

Published

cassandra orm support for nest.js

Downloads

353

Readme

cassandra-orm4nest

NPM version NPM monthly downloads NPM total downloads

Cassandra ORM wrapper for nestjs based on cassandra-driver package.

中文说明

Install

use npm:

npm i cassandra-orm4nest --save

use yarn:

yarn add cassandra-orm4nest

Usage

Entity Definition

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;
}

Module Definition

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 {}

Service Defination

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 the findAll method in the ModelMapper class in cassandra-driver, there is a limit on the number of results for a single query, default is 5000.
  • findRealAll: Query the full table, unlike findAll, there is no limit on the number of results for a single query, because it will converted to eachRow method to perform query operations.
  • findMany: Query based on conditions, it is quivalent to the find method in the ModelMapper class in cassandra-driver, there is a limit on the number of results for a single query, default is 5000.
  • findRealMany: Query based on conditions, unlike findMany, there is no limit on the number of results for a single query, because it will converted to eachRow 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 the update method in the ModelMapper class in cassandra-driver.
  • updateMany: Perform multiple conditional update operations.
  • remove: Remove based on conditions, it is quivalent to the remove method in the ModelMapper class in cassandra-driver.
  • removeMany: Perform multiple conditional remove operations.
  • delete: delete use origin cql, can not write all primary keys

Usage In Controller

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
    }
}

Test Demo

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

Thanks

  • JetBrains for free open source licenses