简体   繁体   中英

Datastax Node.js Cassandra driver When to use a Mapper vs. Query

I'm working with the Datastax Node.js driver and I can't figure out when to use a mapper vs. query. Both seem to be able to perform the same CRUD operations.

With a query:

const q = SELECT * FROM mykeyspace.mytable WHERE id='12345';
client.execute(q).then(result => console.log('This is the data', result);

With mapper:

const tableRow = await tableMapper.find({ id: '12345' });

When should I use the mapper over a query and vice versa?

Mapper is a feature from cassandra-driver released in 2018. Using mapper, cassandra-driver can make a map from your cassandra table to an object in nodejs and you can handle in your nodejs application like a set of document.

Using mapper you can make selects or inserts in your database like said in this article: https://www.datastax.com/blog/2018/12/introducing-datastax-nodejs-mapper-apache-cassandra

With query method, if you need to use or reuse any property from your json you will need to make a Json.Parse().

The short answer is: whatever you find more comfortable.

The Mapper lets you deal with database data as documents (JavaScript objects), builds the CQL query for you, executes the query and maps the results.

On the other hand, the core driver only supports executing CQL queries that you have to write yourself.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM