pip-services4-cassandra-node v0.0.3
Cassandra components for Pip.Services in Node.js / ES2017
This module is a part of the Pip.Services polyglot microservices toolkit. It provides a set of components to implement Cassandra persistence.
The module contains the following packages:
- Build - Factory to create Cassandra persistence components.
- Connect - Connection component to configure Cassandra connection to database.
- Persistence - abstract persistence components to perform basic CRUD operations.
Quick links:
Use
Install the NPM package as
npm install pip-services4-cassandra-node --save
As an example, lets create persistence for the following data object.
import { IIdentifiable } from 'pip-services4-commons-node';
export class MyObject implements IIdentifiable {
public id: string;
public key: string;
public value: number;
}
The persistence component shall implement the following interface with a basic set of CRUD operations.
export interface IMyPersistence {
getPageByFilter(context: IContext, filter: FilterParams, paging: PagingParams): Promise<DataPage<MyObject>>;
getOneById(context: IContext, id: string,): Promise<MyObject>;
getOneByKey(context: IContext, key: string): Promise<MyObject>;
create(context: IContext, item: MyObject): Promise<MyObject>;
update(context: IContext, item: MyObject): Promise<MyObject>;
deleteById(context: IContext, id: string): Promise<MyObject>;
}
To implement cassandraql persistence component you shall inherit IdentifiableCassandraPersistence
.
Most CRUD operations will come from the base class. You only need to override getPageByFilter
method with a custom filter function.
And implement a getOneByKey
custom persistence method that doesn't exist in the base class.
import { IdentifiableCassandraPersistence } from 'pip-services4-cassandra-node';
export class MyCassandraPersistence extends IdentifableCassandraPersistence {
public constructor() {
super("mykeyspace.myobjects");
}
protected defineSchema(): void {
this.clearSchema();
this.ensureSchema("CREATE TABLE " + this.quotedTableName() + " (id TEXT PRIMARY KEY, key TEXT, value TEXT)");
this.ensureIndex("key", { key: 1 }, { unique: true });
}
private composeFilter(filter: FilterParams): any {
filter = filter || new FilterParams();
let criteria = [];
let id = filter.getAsNullableString('id');
if (id != null)
criteria.push("id='" + id + "'");
let tempIds = filter.getAsNullableString("ids");
if (tempIds != null) {
let ids = tempIds.split(",");
filters.push("id IN ('" + ids.join("','") + "')");
}
let key = filter.getAsNullableString("key");
if (key != null)
criteria.push("key='" + key + "'");
return criteria.length > 0 ? criteria.join(" AND ") : null;
}
public getPageByFilter(context: IContext, filter: FilterParams, paging: PagingParams): Promise<DataPage<MyObject>> {
return super.getPageByFilter(context, this.composeFilter(filter), paging, "id", null);
}
public getOneByKey(context: IContext, key: string): Promise<MyObject> {
let query = "SELECT * FROM " + this.quoteIdentifier(this._tableName) + " WHERE \"key\"=?";
let params = [ key ];
let result = this._client.execute(query, params);
let item = result && result.rows ? result.rows[0] || null : null;
if (item == null)
this._logger.trace(context, "Nothing found from %s with key = %s", this._tableName, key);
else
this._logger.trace(context, "Retrieved from %s with key = %s", this._tableName, key);
item = this.convertToPublic(item);
return item;
}
}
Configuration for your microservice that includes cassandraql persistence may look the following way.
...
{{#if CASSANDRA_ENABLED}}
- descriptor: pip-services:connection:cassandra:con1:1.0
connection:
uri: {{{CASSANDRA_SERVICE_URI}}}
host: {{{CASSANDRA_SERVICE_HOST}}}{{#unless CASSANDRA_SERVICE_HOST}}localhost{{/unless}}
port: {{CASSANDRA_SERVICE_PORT}}{{#unless CASSANDRA_SERVICE_PORT}}9042{{/unless}}
datacenter: {{CASSANDRA_DATACENTER}}{{#unless CASSANDRA_DATACENTER}}datacenter1{{/unless}}
credential:
username: {{CASSANDRA_USER}}
password: {{CASSANDRA_PASS}}
- descriptor: myservice:persistence:cassandra:default:1.0
dependencies:
connection: pip-services:connection:cassandra:con1:1.0
table: {{CASSANDRA_TABLE}}{{#unless CASSANDRA_TABLE}}mykeyspace.myobjects{{/unless}}
{{/if}}
...
Develop
For development you shall install the following prerequisites:
- Node.js 14+
- Visual Studio Code or another IDE of your choice
- Docker
- Typescript
Install dependencies:
npm install
Compile the code:
tsc
Run automated tests:
npm test
Generate API documentation:
./docgen.ps1
Before committing changes run dockerized build and test as:
./build.ps1
./test.ps1
./clear.ps1
Contacts
The library is created and maintained by Sergey Seroukhov and Danil Prisyazhniy.
The documentation is written by Mark Makarychev and Eugenio Andrieu.