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

@nielskrijger/cassandra

v0.1.0

Published

Wrapper for Node.js cassandra driver

Downloads

1

Readme

Cassandra

A wrapper for nodejs-cassandra library, providing promises and a set of convenience methods.

NOTE: this library is not very customizable nor will it be, its intent is to serve as a standard for my personal projects. There are only few tests because its use is extensively tested in component tests.

Connect

Run connect(...) before executing any statements.

cassandra.connect({
  contactPoints: ['localhost'],
});

Available options are all cassandra-driver ClientOptions.

createKeyspace(...)

cassandra.createKeyspace('test', 'NetworkTopologyStrategy')
  .then(() => console.log('Keyspace "test" created'));

A USE {keyspace} statement is executed afterwards.

dropKeyspace(...)

cassandra.dropKeyspace('test')
  .then(() => console.log('Keyspace "test" created'));

execute(...)

cassandra.execute(`SELECT * FROM users WHERE id=?`, ['a813g1e'], { prepare: true })
  .then((result) => console.log('Result', result));

preparedInsert(...)

Creates a prepared INSERT statement based on an object by mapping its keys to fields.

You can use this for update statements as well because Cassandra's INSERT and UPDATE are almost identical with the exception of counter columns.

const query = cassandra.preparedInsert('users', {
  id: 'a7509gd',
  first_name: 'John',
  last_name: 'Doe',
}, { ttl: 3600 * 24 * 31, notExists: true });

/*
{
  query: 'INSERT INTO users (first_name, id, last_name) VALUES (?, ?, ?) IF NOT EXISTS USING TTL 2678400',
  params: [ 'John', 'a7509gd', 'Doe' ]
}
*/
console.log(query);

Option | Default | Description ----------|---------|------------------ notExists | false | Adds IF NOT EXISTS clause to prepared query ttl | null | umber of seconds after which record is deleted. Set null to never expire data.

batch(..)

Batch method is a minimal wrapper around nodejs-cassandra's batch statement. It returns a promise with the result of the BATCH operation.

const queries = [{
  query: 'UPDATE users SET name=? WHERE id=?',
  params: ['John', 'ae835x'],
}, {
  query: 'INSERT INTO users (id, name, created) VALUES (?, ?, ?)',
  params: ['1g0fee', 'hendrix', new Date()],
}];

cassandra.batch(queries, { prepare: true }).catch(err => {
  assert.ifError(err);
  console.log('Data updated on cluster');
});

The most common options are listed below, you can find more here.

Option | Default | Description ----------|---------|----------------------------- prepare | false | Queries are prepared statements. logged | true | Whether batch should be written to the batchlog.

Be aware batch in Cassandra is not a performance improvement in most situations, instead it provides support for atomic transactions (either all operations succeed or none).

The one exception a batch does improve performance is when a set of writes are written to the same partition.

Logging

import * as cassandra from '@nielskrijger/cassandra';

cassandra.on('log', (level, message, object) => {
  console.log(`Log event: ${level}, ${message}, ${object}`);
});

A debug log event is emitted when executing a statement or a batch of statements. An info log event is emitted when connection was established.

Errors must be handled by the client as part of the Promise chain, no log events are emitted.