/scylla-db-tcp

Primary LanguageRustMIT LicenseMIT

Scylla-DB TCP Server

This is a simple TCP server built with Rust and Tokio, which supports commands to interact with Scylla-DB. It's designed to be used with the NPM package @kastelapp/scylla, but you can use it independently if you wish.

Features

  • Built with Rust and Tokio for high performance and reliability (maybe I suck at rust ;3)
  • Supports a variety of commands including select, insert, update, delete, connect, and raw.
  • Can be used with the NPM package @kastelapp/scylla, or independently.

Help Needed:

Currently, it cannot serialize UDT, I'm not too sure how to do it so for now I've given up on this lol, if someone can help me with this, that would be great, besides that everything should work fine. So if you don't need to use UDT's, you should be good to go. (UDT: User Defined Types)

It CAN select them from the database, but it cannot insert them.

Installation

To install the server, checkout github releases. If you wish to build it yourself, you can do so by cloning the repository and running cargo build --release.

Usage

After building the server, you can start it by running the resulting binary in your terminal. The server will start listening on port 8080 by default (adding --port will change it).

You can then send commands to the server using a TCP client. Here's an example of how to send a select command:

{
  "command": "select",
  "table": "test",
  "keyspace": "test",
  "data": {
    "where": { "id": "1" },
    "columns": ["id", "name"],
    "limit": 1
  }
}

The server then will respond with a resonse like this:

{
  "command": "select",
  "table": null,
  "keyspace": null,
  "data": {
    "result": [
      { "id": "1", "name": "test" }
    ],
    "error": null
  }
}

Commands

The server supports the following commands:

  • connect: Connect to a Scylla-DB instance.
  • select: Select data from a table.
  • insert: Insert data into a table.
  • update: Update data in a table.
  • delete: Delete data from a table.
  • raw: Send a raw CQL query to the Scylla-DB instance (not recommended).

For more information on how to use these commands, see the section below.

Note: The objects have to match exactly in the order they are shown in src/structs, this is due to the hash verification. I don't want it to be like this but for some reason serde is weird and unless you make it exact, it will parse in random order each time. If someone knows a better way to generate the hash which doesn't depend on the order, please let me know.

Before we get started, this is a generalized payload, of an error the server may send if you are not authorized to do something:

{
    "command": "error", // Not a real command, just used for errors
    "table": null, // always null
    "keyspace": null, // always null
    "data": {
        "result": [],
        "error": "You are not authorized to do this."
    }
}
Connect

The connect command is used to connect to a Scylla-DB instance. It is required to be the first command sent to the server.

{
    "command": "connect",
    "data": {
        "contactPoints": [
            "localhost:9042",
            "1.2.3.4:9042"
        ],
        "localDataCenter": "datacenter1",
        "credentials": {
            "username": "cassandra",
            "password": "cassandra"
        },
        "keyspace": "test"
    }
}

The server will respond with a JSON object containing the result of the command:

{
    "command": "connect",
    "data": {
        "success": true,
        "error": null
    }
}
Select

The select command is used to select data from a table.

{
    "command": "select",
    "table": "test",
    "keyspace": "test",
    "data": {
        "where": { "id": "1" },
        "columns": ["id", "name"],
        "limit": 1
    }
}

The server will respond with a JSON object containing the result of the command:

{
    "command": "select",
    "table": null, // always null
    "keyspace": null, // always null
    "data": {
        "result": [
            {
                "id": "1",
                "name": "test"
            }
        ],
        "error": null
    }
}
Insert

The insert command is used to insert data into a table.

{
    "command": "insert",
    "table": "test",
    "keyspace": "test",
    "data": {
        "columns": {
            "id": "1",
            "name": "test"
        }
    }
}

The server will respond with a JSON object containing the result of the command:

{
    "command": "insert",
    "data": {
        "success": true,
        "error": null
    }
}
Update

The update command is used to update data in a table.

{
    "command": "update",
    "table": "test",
    "keyspace": "test",
    "data": {
        "where": { "id": "1" },
        "columns": {
            "name": "test2"
        }
    }
}

The server will respond with a JSON object containing the result of the command:

{
    "command": "update",
    "data": {
        "success": true,
        "error": null
    }
}
Delete

The delete command is used to delete data from a table.

{
    "command": "delete",
    "table": "test",
    "keyspace": "test",
    "data": {
        "where": { "id": "1" }
    }
}

The server will respond with a JSON object containing the result of the command:

{
    "command": "delete",
    "data": {
        "success": true,
        "error": null
    }
}
Raw

The raw command is used to send a raw CQL query to the Scylla-DB instance, this is not recommended to be used.

{
    "command": "raw",
    "keyspace": null, // should always be null
    "table": null, // should always be null
    "data": {
        "query": "SELECT * FROM test.test WHERE id = '1'"
    }
}

The server will respond with a JSON object containing the result of the command:

{
    "command": "raw",
    "data": {
        "result": [
            {
                "id": "1",
                "name": "test"
            }
        ],
        "error": null
    }
}

Contributing

We welcome contributions from the community. Please read our contributing guidelines before getting started.

License

This project is licensed under the MIT License. See the LICENSE file for details.

Contact

If you have any questions or suggestions, please feel free to open an issue.

Acknowledgements

This project uses the following third-party libraries: