Cassandra Codegen

0.0.11 · active · verified Wed Apr 22

Cassandra Codegen is a utility that generates TypeScript type definitions and type-safe mappers directly from a Cassandra or ScyllaDB database schema. Its current stable version is 0.0.11. The package releases updates on an irregular but active cadence, primarily addressing bug fixes, adding support for new Cassandra types, and enhancing generated type safety. A key differentiator is its ability to produce mappers that extend the functionality of `cassandra-driver`, offering improved type annotations for partition keys, clustering columns, and query operators. It automatically maps Cassandra types like `map` to TypeScript's `Record` and handles optionality/nullability for non-primary key columns in generated insert/retrieve types, aligning with typical driver behavior. The project draws inspiration from `kysely-codegen` for its schema-to-type generation approach.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to set up `cassandra-codegen` in a TypeScript project: connecting a Cassandra client, initializing the generated mappers, and then performing basic `get` and `find` operations using a generated mapper and type-safe query operators.

import { initMappers, queryOperator } from 'cassandra-codegen';
import type { CyclistCategoryRow } from './generated/mykeyspace'; // Adjust path to your generated types
import { Client } from 'cassandra-driver';

// 1. Configure and connect your cassandra-driver client
const cassandraClient = new Client({
  contactPoints: ['127.0.0.1'], // Your Cassandra host
  localDataCenter: 'datacenter1', // Your datacenter
  credentials: { username: 'user', password: 'password' }, // Your credentials
  keyspace: 'mykeyspace', // The keyspace you generated types for
});

async function main() {
  await cassandraClient.connect();
  console.log('Cassandra client connected.');

  // 2. Initialize the generated mappers with the connected client
  await initMappers(cassandraClient);
  console.log('Mappers initialized.');

  // 3. Import and use a generated mapper (e.g., 'cyclistCategoryMapper')
  // Assuming 'cyclistCategoryMapper' and 'CyclistCategoryRow' are exported from './generated/mykeyspace.ts'
  // You must run the `cassandra-codegen` CLI tool first to generate these files.
  const cyclistCategoryMapper: {
    get: (criteria: { category: string; points: number }) => Promise<CyclistCategoryRow | null>;
    find: (criteria: { category?: string; points?: number | { $gte?: number; $lte?: number } }) => Promise<CyclistCategoryRow[]>;
    // ... other generated methods like insert, update, remove
  } = {} as any; // Placeholder for actual generated mapper

  console.log('\n--- Fetching a specific cyclist category ---');
  const specificCyclist = await cyclistCategoryMapper.get({
    category: 'GC',
    points: 100,
  });
  console.log('Retrieved:', specificCyclist);

  console.log('\n--- Finding cyclists using a query operator ---');
  const filteredCyclists = await cyclistCategoryMapper.find({
    category: 'GC',
    points: queryOperator.gte(42), // Using a type-safe query operator
  });
  console.log('Filtered results:', filteredCyclists);

  await cassandraClient.shutdown();
  console.log('Cassandra client disconnected.');
}

main().catch(console.error);

/*
To run this example:
1. Ensure Cassandra is running and your keyspace/table exists.
2. Install dependencies: `npm install cassandra-codegen cassandra-driver @types/cassandra-driver`
3. Generate types/mappers: `npm exec cassandra-codegen -- --host 127.0.0.1 --port 9042 --datacenter datacenter1 --username user --password password --keyspace mykeyspace --generate-ts-file --output-dir ./generated`
4. Adjust the import path for `CyclistCategoryRow` and the mocked `cyclistCategoryMapper` to point to your generated file (e.g., `./generated/mykeyspace`).
5. Run with `ts-node your-file.ts` (if `ts-node` is installed) or compile and run `tsc your-file.ts && node your-file.js`.
*/

view raw JSON →