Sprite

Create a Database

Edit on Github

This tutorial will as an introduction to the SpriteServer module. It strives to implement all the functionality available in the ArcadeDB Console, but in a documented TypeScript interface. It is nearly feature complete, with settings being the only thing that is not currently ready to be deployed.

This tutorial will use SpriteServer to create a database.

Overview

  1. Prerequisites
  2. Instantiating SpriteServer
  3. Async Operations
  4. Check server status
  5. Creating a database
  6. Running the Example
  7. Conclusion
  8. What’s Next?

Prerequisites

  1. Ensure you have completed the installation process, which includes installing ArcadeDB, running it, and accessing it from a TypeScript/JavaScript project with Sprite installed.

Instantiating SpriteServer

Begin by inserting the following code into your project’s index.ts file:

import { SpriteServer } from '@valence-corp/sprite';

const client = new SpriteServer({
  username: 'root',
  password: 'your_password', // replace with your password
  address: 'http://localhost:2480' // default address for ArcadeDB
});

Async Operations

Sprite uses the ArcadeDB REST API to send commands and receive responses from the ArcadeDB Server. This means that Sprite’s methods are always async. More often than not, you’ll being using async / await (or promises) in your operations.

Insert the below code following the previous snippet.

async function createDatabaseExample() {
  try {
    // code from the tutorial goes here
  } catch (error) {
    throw new Error(`There was a problem while creating the database.`, {
      cause: error
    });
  }
}

createDatabaseExample();

Checking Server Status

It might be desirable to ensure the ArcadeDB server is ready prior to sending commands. The SpriteServer.serverReady method is made for that. Update the createDatabaseExample() function to include a server status check:

async function createDatabaseExample() {
  try {
    const ready = await client.serverReady();
    if (ready) {
      // perform creation operation here
    }
  } catch (error) {
    throw new Error(`There was a problem while running the example.`, {
      cause: error
    });
  }
}

If the server is not ready, an error will be thrown with a detailed error message. It’s important to always handle errors in your code.

Creating a Database

Update the createDatabaseExample() function to create a database using the SpriteServer.createDatabase method.

The createDatabase method will return an instance of SpriteDatabase, which could be returned or used for database operations (more on that later).

async function createDatabaseExample() {
  try {
    const ready = await client.serverReady();
    if (ready) {
      const database = await client.createDatabase('ExampleDatabase');
      console.log(`${database.name} was created`);
      return database;
    }
  } catch (error) {
    throw new Error(`There was a problem while creating the database.`, {
      cause: error
    });
  }
}

Running the Example

The complete code is below. Ensure your code matches, and then execute it.

import { SpriteServer } from '@valence-corp/sprite';

const client = new SpriteServer({
  username: 'root',
  password: 'your_password', // replace with your password
  address: 'http://localhost:2480' // default address for ArcadeDB
});

async function createDatabaseExample() {
  try {
    const ready = await client.serverReady();
    if (ready) {
      const database = await client.createDatabase('ExampleDatabase');
      console.log(`${database.name} was created`);
      return database;
    }
  } catch (error) {
    throw new Error(`There was a problem while creating the database.`, {
      cause: error
    });
  }
}

Conclusion

You should now have a database on your server called “ExampleDatabase”. You can verify the existence of the database using one of the following methods:

What’s Next?

The next section will demonstrate the basics of transactional databases, and how transactions are conducted in Sprite.