Skip to content

Update the net.box tutorial #4054

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Mar 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Creating your first Tarantool database

A sample application created in the [Creating your first Tarantool database](https://www.tarantool.io/en/doc/latest/how-to/getting_started_db/) tutorial.

## Running

To start an instance, execute the following command in the [config](../../../config) directory:

```console
$ tt start create_db
```
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ groups:
instance001:
iproto:
listen:
- uri: '127.0.0.1:3301'
- uri: '127.0.0.1:3301'

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Connectors

A sample application used to demonstrate how to connect to a database using connectors for different languages and execute requests for manipulating the data.

## Running

Start the application by executing the following command in the [connectors](../../../connectors) directory:

```console
$ tt start sample_db
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
credentials:
users:
sampleuser:
password: '123456'
privileges:
- permissions: [ read, write ]
spaces: [ bands ]
- permissions: [ execute ]
functions: [ get_bands_older_than ]

groups:
group001:
replicasets:
replicaset001:
instances:
instance001:
iproto:
listen:
- uri: '127.0.0.1:3301'

app:
file: 'myapp.lua'
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
instance001:
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
-- Create a space --
box.schema.space.create('bands')

-- Specify field names and types --
box.space.bands:format({
{ name = 'id', type = 'unsigned' },
{ name = 'band_name', type = 'string' },
{ name = 'year', type = 'unsigned' }
})

-- Create indexes --
box.space.bands:create_index('primary', { parts = { 'id' } })
box.space.bands:create_index('band', { parts = { 'band_name' } })
box.space.bands:create_index('year_band', { parts = { { 'year' }, { 'band_name' } } })

-- Create a stored function --
box.schema.func.create('get_bands_older_than', {
body = [[
function(year)
return box.space.bands.index.year_band:select({ year }, { iterator = 'LT', limit = 10 })
end
]]
})
16 changes: 16 additions & 0 deletions doc/code_snippets/snippets/connectors/net_box/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# net.box

A sample application containing `net.box` requests from the [Getting started with net.box](https://www.tarantool.io/en/doc/latest/how-to/getting_started_net_box/) tutorial.


## Running

Before running this sample, start an application that allows remote connections to a sample database: [sample_db](../instances.enabled/sample_db).

Then, start the interactive session by executing the following command in the [connectors](..) directory:

```console
$ tt run -i net_box/myapp.lua
```

In the console, you can use the `conn` object to execute requests for manipulating the data.
102 changes: 102 additions & 0 deletions doc/code_snippets/snippets/connectors/net_box/myapp.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
net_box = require('net.box')
--[[
---
...
]]

conn = net_box.connect('sampleuser:[email protected]:3301')
--[[
---
...
]]

conn:ping()
--[[
---
- true
...
]]

function net_box_data_operations()
-- Start net.box session
conn.space.bands:insert({ 1, 'Roxette', 1986 })
--[[
---
- - [1, 'Roxette', 1986]
...
]]
conn.space.bands:insert({ 2, 'Scorpions', 1965 })
--[[
---
- [2, 'Scorpions', 1965]
...
]]
conn.space.bands:insert({ 3, 'Ace of Base', 1987 })
--[[
---
- [3, 'Ace of Base', 1987]
...
]]
conn.space.bands:insert({ 4, 'The Beatles', 1960 })
--[[
---
- [4, 'The Beatles', 1960]
...
]]

conn.space.bands:select({ 1 })
--[[
---
- - [1, 'Roxette', 1986]
...
]]

conn.space.bands.index.band:select({ 'The Beatles' })
--[[
---
- - [4, 'The Beatles', 1960]
...
]]

conn.space.bands:update({ 2 }, { { '=', 'band_name', 'Pink Floyd' } })
--[[
---
- [2, 'Pink Floyd', 1965]
...
]]

conn.space.bands:upsert({ 5, 'The Rolling Stones', 1962 }, { { '=', 'band_name', 'The Doors' } })
--[[
---
...
]]

conn.space.bands:replace({ 1, 'Queen', 1970 })
--[[
---
- [1, 'Queen', 1970]
...
]]

conn.space.bands:delete({ 5 })
--[[
---
- [5, 'The Rolling Stones', 1962]
...
]]

conn:call('get_bands_older_than', { 1966 })
-- ---
-- - [[2, 'Pink Floyd', 1965], [4, 'The Beatles', 1960]]
-- ...
-- End net.box session
end

function net_box_close_connection()
conn:close()
--[[
---
...
]]
-- Close net.box connection
end
54 changes: 54 additions & 0 deletions doc/code_snippets/snippets/connectors/tt.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
modules:
# Directory where the external modules are stored.
directory: modules

env:
# Restart instance on failure.
restart_on_failure: false

# Directory that stores binary files.
bin_dir: bin

# Directory that stores Tarantool header files.
inc_dir: include

# Path to directory that stores all applications.
# The directory can also contain symbolic links to applications.
instances_enabled: instances.enabled

# Tarantoolctl artifacts layout compatibility: if set to true tt will not create application
# sub-directories for control socket, pid files, log files, etc.. Data files (wal, vinyl,
# snap) and multi-instance applications are not affected by this option.
tarantoolctl_layout: false

app:
# Directory that stores various instance runtime
# artifacts like console socket, PID file, etc.
run_dir: var/run

# Directory that stores log files.
log_dir: var/log

# Directory where write-ahead log (.xlog) files are stored.
wal_dir: var/lib

# Directory where memtx stores snapshot (.snap) files.
memtx_dir: var/lib

# Directory where vinyl files or subdirectories will be stored.
vinyl_dir: var/lib

# Path to file with credentials for downloading Tarantool Enterprise Edition.
# credential_path: /path/to/file
ee:
credential_path:

templates:
# The path to templates search directory.
- path: templates

repo:
# Directory where local rocks files could be found.
rocks:
# Directory that stores installation files.
distfiles: distfiles
Loading