How to run consecutive queries with Node.js Cassandra driver?

Viewed 152

I'm trying to run this script using Cassandra driver in Node.js:

CREATE KEYSPACE IF NOT EXISTS "user"
WITH REPLICATION = {
'class': 'SimpleStrategy',
'replication_factor': 1
}
AND DURABLE_WRITES = false;

USE "user";

CREATE TYPE IF NOT EXISTS "user"."customType"(
"name" text,
"isGood" boolean,
);

But it returns me next error:

[USE]...)rror: line 8:0 mismatched input 'USE' expecting EOF (...AND DURABLE_WRITES = false;

I assume that problem is in different queries. But is it? And what's the solution?

UPDATE

Batch queries don't work. Get this Invalid statement in batch: only UPDATE, INSERT and DELETE statements are allowed

1 Answers

CQL drivers only support executing one statement at a time.

So in your case, it would be something like:

async function createSchema() {
  await client.execute('CREATE KEYSPACE user ...');
  await client.execute('USE user');
  await client.execute('CREATE TYPE IF NOT EXISTS ...');
}

or you can have a large string with your schema and do some splitting in JavaScript:

async function createSchema(schema) {
  const queries = schema.split(';');
  for (const query of queries) {
    await client.execute(query);
  }
}

Note that with the CQL drivers, semicolons at the end of each statement are not mandatory.

Related