Skip to content

Latest commit

 

History

History
147 lines (104 loc) · 3.93 KB

README.md

File metadata and controls

147 lines (104 loc) · 3.93 KB

TarantoolScript

TarantoolScript is a tool to help you create Tarantool Lua scripts using TypeScript.

Getting started

To install Tarantoolscript add tarantoolscript npm package to your TypeScript project:

$ npm install --save-dev tarantoolscript

Also to compile your TypeScript program to Lua scripts, you need to install TypeScriptToLua:

$ npm install --save-dev typescript-to-lua

Here you can find more information about TypeScriptToLua.

Usage

Tarantoolscript provides some type declarations useful to create you own Lua scripts which may be executed by Tarantool. Here you can find more information about Tarantool.

Basic usage

Init instance

Interface Box describes all functions and submodules, included into Tarantool global variable box. To use it, just declare constant box of type Box:

import { Box, ConfigOptions } from 'tarantoolscript';

declare const box: Box;

const cfg: ConfigOptions = {
    listen: 3301,
};

box.cfg(cfg);

TypeScriptToLua will compile this TypeScript code to following Lua code:

local ____exports = {}
local cfg = {listen = 3301}
box.cfg(cfg)
return ____exports

Create space

const bands = box.schema.space.create('bands');

Format space

bands.format([
    { name: 'id', type: 'unsigned' },
    { name: 'band_name', type: 'string' },
    { name: 'year', type: 'unsigned' },
]);

Create index

bands.create_index('primary', { parts: ['id'] });

Insert data

bands.insert([1, 'Roxette', 1986]);

Tarantool modules usage

To usage some modules, which should be imported into script with Lua keyword require, you need first edit your tsconfig.json by following algorithm. At example, if you need to import module log, add it to tsconfig compilerOptions.paths:

{
    "compilerOptions": {
        "paths": {
            "log": ["tarantoolscript/src/log.d.ts"]
        }
    },
    "tstl": {
        "noResolvePaths": [
            "log"
        ]
    }
}

Or for module luatest.helpers:

{
    "compilerOptions": {
        "paths": {
            "luatest.helpers": ["tarantoolscript/src/luatest.helpers.d.ts"]
        }
    },
    "tstl": {
        "noResolvePaths": [
            "luatest.helpers"
        ]
    }
}

Now you can import and use these modules:

import * as log from 'log';
import * as luatest_helpers from 'luatest.helpers';

log.info(luatest_helpers.uuid(1, 2, 3));

After compilation:

local ____exports = {}
local log = require("log")
local luatest_helpers = require("luatest.helpers")
log.info(luatest_helpers.uuid(1, 2, 3))
return ____exports

Of course, to use luatest package or other packages which not included into Tarantool default build, you need to install them with tt rocks command.

More usage examples

This repository contains many useful samples, which are examples of using modules taken from the official Tarantool documentation. Every source file contains in its header link to the corresponding documentation page.

This repository can help you understanding how to write Tarantool scripts with TarantoolScript.

Status

Tarantoolscript is under development and not declares many useful Tarantool modules, which will be added in the near future.

Feel free to add Issue or create Pull request.