|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const cassandra = require('cassandra-driver'); |
| 4 | +const contactPoints = process.env['CONTACT_POINTS']; |
| 5 | +const localDataCenter = process.env['LOCAL_DC']; |
| 6 | +const keyspace = 'shopping'; |
| 7 | +const table = 'catalog'; |
| 8 | + |
| 9 | +if (!contactPoints) throw new Error('Environment variable CONTACT_POINTS not set'); |
| 10 | +if (!localDataCenter) throw new Error('Environment variable LOCAL_DC not set'); |
| 11 | + |
| 12 | +// useful for determining container re-use |
| 13 | +const myuuid = cassandra.types.TimeUuid.now(); |
| 14 | +console.log('timeuuid in container startup: ' + myuuid); |
| 15 | + |
| 16 | +const client = new cassandra.Client({ |
| 17 | + contactPoints: contactPoints.split(','), |
| 18 | + localDataCenter, |
| 19 | + |
| 20 | + // AWS Lambda freezes the function execution context after the callback has been invoked. |
| 21 | + // This means that no background activity can occur between lambda invocations, |
| 22 | + // including the heartbeat that the driver uses to prevent idle disconnects in some environments. |
| 23 | + // At this time we have not done enough testing to validate if this behavior applies to Google Cloud Functions as well, |
| 24 | + // though it may be best to disable heartbeats if this is the case. This is accomplished with the setting below. |
| 25 | + // |
| 26 | + // pooling: { heartBeatInterval: 0 } |
| 27 | + |
| 28 | + // If trying to reduce Cold Start time, the driver's automatic metadata synchronization and pool warmup can be disabled |
| 29 | + // |
| 30 | + // isMetadataSyncEnabled: false, |
| 31 | + // pooling: { warmup: false } |
| 32 | +}); |
| 33 | + |
| 34 | +// Enable logging for the purpose of example |
| 35 | +client.on('log', (level, className, message) => { |
| 36 | + if (level !== 'verbose') { |
| 37 | + console.log('Driver log event', level, className, message); |
| 38 | + } |
| 39 | +}); |
| 40 | + |
| 41 | +const createKeyspace = `CREATE KEYSPACE IF NOT EXISTS ${keyspace} ` + |
| 42 | + `WITH REPLICATION = {'class':'NetworkTopologyStrategy','${localDataCenter}': 1}`; |
| 43 | + |
| 44 | +const createTable = `CREATE TABLE IF NOT EXISTS ${keyspace}.${table} (item_id int, name text, description text, price decimal, ` + |
| 45 | + `PRIMARY KEY (item_id))`; |
| 46 | + |
| 47 | +const writeQuery = `INSERT INTO ${keyspace}.${table} (item_id, name, description, price) VALUES (?, ?, ?, ?)`; |
| 48 | + |
| 49 | +const readQuery = `SELECT name, description, price FROM ${keyspace}.${table} WHERE item_id = ?`; |
| 50 | + |
| 51 | +client.connect() |
| 52 | + .then(() => console.log('Connected to the DSE cluster, discovered %d nodes', client.hosts.length)) |
| 53 | + .catch(err => console.error('There was an error trying to connect', err)); |
| 54 | + |
| 55 | +async function createSchema() { |
| 56 | + await client.execute(createKeyspace); |
| 57 | + await client.execute(createTable); |
| 58 | + return {statusCode: 200, body: `Successfully created ${keyspace}.${table} schema`}; |
| 59 | +} |
| 60 | + |
| 61 | +async function addItem(item_id, name, description, price) { |
| 62 | + const params = [ item_id, name, description, price ]; |
| 63 | + await client.execute(writeQuery, params, { prepare: true, isIdempotent: true }); |
| 64 | + return { |
| 65 | + statusCode: 200, |
| 66 | + body: JSON.stringify({ |
| 67 | + query: writeQuery, |
| 68 | + item_id: item_id, |
| 69 | + name: name, |
| 70 | + description: description, |
| 71 | + price: price |
| 72 | + }) |
| 73 | + }; |
| 74 | +} |
| 75 | + |
| 76 | +async function getItem(item_id) { |
| 77 | + const params = [ item_id ]; |
| 78 | + const result = await client.execute(readQuery, params, { prepare : true }); |
| 79 | + const row = result.first(); |
| 80 | + return { |
| 81 | + statusCode: 200, |
| 82 | + body: JSON.stringify({ |
| 83 | + query: readQuery, |
| 84 | + item_id: item_id, |
| 85 | + name: row.name, |
| 86 | + description: row.description, |
| 87 | + price: row.price |
| 88 | + }) |
| 89 | + }; |
| 90 | +} |
| 91 | + |
| 92 | +exports.createCatalog = async (req, res) => { |
| 93 | + console.log('timeuuid in createCatalog: ' + myuuid); |
| 94 | + const result = await createSchema(); |
| 95 | + res.status(result.statusCode).json(result.body); |
| 96 | +}; |
| 97 | + |
| 98 | +exports.addItem = async (req, res) => { |
| 99 | + console.log('timeuuid in addItem: ' + myuuid); |
| 100 | + const result = await addItem(req.body.item_id, req.body.name, req.body.description, req.body.price); |
| 101 | + res.status(result.statusCode).json(JSON.parse(result.body)); |
| 102 | +}; |
| 103 | + |
| 104 | +exports.getItem = async (req, res) => { |
| 105 | + console.log('timeuuid in getItem: ' + myuuid); |
| 106 | + const id = req.path.match(/\d+/g); |
| 107 | + const result = await getItem(id) |
| 108 | + res.status(result.statusCode).json(JSON.parse(result.body)); |
| 109 | +}; |
| 110 | + |
0 commit comments