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

node-cassandra

v0.4.2

Published

Cassandra ORM for NodeJS.

Downloads

6

Readme

node-cassandra Build Status Coverage Status

  • Provides support for current Cassandra 3.x builds.
  • Tested at scale with distribution in real production environments.
  • Mostly feature-complete, active development is underway to add remaining features.
  • Open to suggestions! Just create an issue and tag it.

Read the full API Documentation.

Examples:

  • 3.x
  • <= 7.6
  • 6.x
  • 5.x
var Cassandra = require('node-cassandra');
var config = {
    contactPoints: ['127.0.0.1:9042'],
    protocolOptions: {port: 9042},
    keyspace: {
        testkeyspace: {
            durableWrites: true,
            withReplication: {
                class: 'SimpleStrategy',
                replication_factor: 1
            }
        }
    }
};

var cassandra = Cassandra.connect(config);
cassandra.on('error', (err) => console.log(err));
cassandra.on('connect', (err) => console.log('connected'));
//create a new schema
var schema = new Cassandra.Schema({
    id: {
        type: 'uuid',
        default: Cassandra.uuid
    },
    username: {
        type: 'text',
        required: true
    },
    ageName: {
        type: 'text',
        default: (instance) => {
            //default value functions are passed the object instance
            return instance.age + instance.name;
        }
    },
    name: 'text',
    age: 'int'
}, {
    primaryKeys: ['username'],
    views: {
        byName: {
            primaryKeys: ['name'],
            orderBy: {
                name: 'asc'
            }
        },
    }
});

//create model
var TestModel = cassandra.model('testModel', schema, (err) => {
    test.save((err) => {
        if (err) {
            throw err;
        }
        console.log('test saved!');
        test.age = 29;
        test.save((err) => {...});
        //test.age updated
    });
});

var test = new TestModel({
    name: 'bar',
    username: 'foo',
    age: 30
});
//create a new schema
var schema = new Cassandra.Schema({
    name: 'text',
    usernames: {
        type: {
            list: 'text'
        }
    }
}, {
    indexes: ['usernames'], //create an index on the usernames column
    primaryKeys: ['name']
});
//create model
var TestModel = cassandra.model('testModel', schema, (err) => {
    var test = new TestModel({
        name: 'bar',
        usernames: ['foo', 'bar']
    });
    test.save(() => {
        TestModel.findOne({
            usernames: {
                $contains: 'foo'
            }
        }, (err, row) => {
            console.log(row.usernames); //['foo', 'bar']
        });
    });
});
//following above
var query = {
    name: 'bar',
    usernames: {
        $contains: 'foo'
    }
};
var updateObject = {
    usernames: {
        $filter: ['foo'], //remove items matching "foo"
        $append: ['baz'], //append item "baz"
        $prepend: ['fii'] //prepend item "fii"
    }
};
TestModel.update(query, updateObject, (err) => {
    if (err) {
        return console.log(err):
    }
    TestModel.findOne({usernames: {$contains: 'baz'}}, (err, row) => {
        console.log(row.usernames); //['fii', 'bar', 'baz']
    });
});
//following above
var query = {
    name: 'bar',
    usernames: {
        $contains: 'bar'
    }
};
var updateObject = {
    usernames: {
        $set: {
            0: 'foo'
        }
    }
};
TestModel.update(query, updateObject, (err) => {
    if (err) {
        return console.log(err):
    }
    TestModel.findOne(query, (err, row) => {
        console.log(row.usernames); //['foo', 'bar', 'baz']
    });
});
//following above
var query = {
    name: 'bar',
    usernames: {
        $contains: 'fii'
    }
};
var deleteObject = {
    usernames: [0,2] //delete first and last index
};
TestModel.delete(query, deleteObject, (err) => {
    if (err) {
        return console.log(err):
    }
    TestModel.findOne({usernames: {$contains: 'bar'}}, (err, row) => {

        console.log(row.usernames); //['bar']

        TestModel.delete({
            usernames: {
                $contains: 'bar'
            }
        }, {
            usernames: 1 //delete usernames column
        }, (err) => {...});

    });
});
//create a new schema
var schema = new Cassandra.Schema({
    name: 'text',
    usernames: {
        type: {
            set: 'text'
        }
    }
}, {
    indexes: ['usernames'], //create an index on the usernames column
    primaryKeys: ['name']
});
//create model
var TestModel = cassandra.model('testModel', schema, (err) => {
    var test = new TestModel({
        name: 'bar',
        usernames: ['foo', 'bar']
    });
    test.save(() => {
        TestModel.findOne({
            usernames: {
                $contains: 'foo'
            }
        }, (err, row) => {
            console.log(row.usernames); //['bar', 'foo']
        });
    });
});
//following above
var query = {
    name: 'bar',
    usernames: {
        $contains: 'bar'
    }
};
var updateObject = {
    usernames: {
        $add: ['aba', 'boa'],
        $filter: ['foo']
    }
};
TestModel.update(query, updateObject, (err) => {
    if (err) {
        return console.log(err):
    }
    TestModel.findOne(query, (err, row) => {
        console.log(row.usernames); //['aba', 'bar', 'boa']
    });
});
//following above
var query = {
    name: 'bar',
    usernames: {
        $contains: 'bar'
    }
};
var updateObject = {
    usernames: [] //or null
};
TestModel.update(query, updateObject, (err) => {
    if (err) {
        return console.log(err):
    }
    TestModel.findOne(query, (err, row) => {
        console.log(row.usernames); //null
    });
});
//not much you can do with deleting sets
var query = {
    name: 'bar',
    usernames: {
        $contains: 'fii'
    }
};
var deleteObject = {
    usernames: 1 //delete usernames column
};
TestModel.delete(query, deleteObject, (err) => {
    if (err) {
        return console.log(err):
    }
    TestModel.findOne({name: 'bar'}, (err, row) => {
        console.log(row.usernames); //null
    });
});
//create a new schema
var schema = new Cassandra.Schema({
    name: 'text',
    usernames: {
        type: {
            map: ['text', 'int']
        }
    }
}, {
    indexes: ['usernames'], //create an index on the usernames column keys
    primaryKeys: ['name']
});
//create model
var TestModel = cassandra.model('testModel', schema, (err) => {
    var test = new TestModel({
        name: 'bar',
        usernames: {
            $set: {
                foo: 2,
                bar: 4
            }
        }
    });
    test.save(() => {
        TestModel.findOne({
            usernames: {
                $containsKey: 'foo' //use containsKey for maps
            }
        }, (err, row) => {
            console.log(row.usernames.foo); // 2
        });
    });
});
//following above
var query = {
    name: 'bar',
    usernames: {
        $containsKey: 'bar'
    }
};
var updateObject = {
    usernames: {
        foo: 5, //update "foo" to 5
        fii: 7 /set "fii" to 7
    }
};
TestModel.update(query, updateObject, (err) => {
    if (err) {
        return console.log(err):
    }
    TestModel.findOne(query, (err, row) => {
        console.log(row.usernames.foo); // 5
    });
});
//not much you can do with deleting maps
var query = {
    name: 'bar',
    usernames: {
        $containsKey: 'bar'
    }
};
var deleteObject = {
    usernames: ['foo']
};
TestModel.delete(query, deleteObject, (err) => {
    if (err) {
        return console.log(err):
    }
    TestModel.findOne(query, (err, row) => {
        console.log(row.usernames.foo); // undefined
    });
});
//not much you can do with deleting maps
var query = {
    name: 'bar',
    usernames: {
        $containsKey: 'bar'
    }
};
var deleteObject = {
    usernames: 1
};
TestModel.delete(query, deleteObject, (err) => {
    if (err) {
        return console.log(err):
    }
    TestModel.findOne(query, (err, row) => {
        console.log(row.usernames); // null
    });
});

Table Compaction

//create a new schema
var schema = new Cassandra.Schema({
    id: 'uuid',
    name: 'text'
}, {
    primaryKeys: ['username'],
    compaction: {
        class: 'DateTieredCompactionStrategy',
        timestamp_resolution: 'MICROSECONDS',
        base_time_seconds: 3600,
        max_sstable_age_days: 365
    }
});

Static Methods

//create a new schema
var schema = new Cassandra.Schema({
    id: {
        type: 'uuid',
        default: Cassandra.uuid
    },
    username: {
        type: 'text',
        required: true
    },
    name: 'text'
}, {
    primaryKeys: ['username'],
    views: {
        byName: {
            primaryKeys: ['name']
        },
    }
});

//attach static method - fat arrow functions won't work here as we need the context to change
schema.statics.findByName = function (name, callback) {
    this.views.byName.findOne({name: name}, callback);
};

//create model
var TestModel = cassandra.model('testModel', schema, (err) => {
    TestModel.findByName('foo', (err, row) => {
        console.log(row.name); // "foo"
    });
});