Skip to content

Commit

Permalink
Getting started: net.box - update per review
Browse files Browse the repository at this point in the history
  • Loading branch information
andreyaksenov committed Mar 6, 2024
1 parent 16bd4cc commit 1488da3
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 59 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,22 @@
function net_box_session()
local net_box = require('net.box')
local conn = net_box.connect('sampleuser:123456@127.0.0.1:3301')
net_box = require('net.box')
--[[
---
...
]]

conn = net_box.connect('sampleuser:123456@127.0.0.1:3301')
--[[
---
...
]]

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

conn.space.bands:insert { 1, 'Roxette', 1986 }
conn.space.bands:insert { 2, 'Scorpions', 1965 }
Expand All @@ -12,41 +27,41 @@ function net_box_session()
...
]]

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

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

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

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

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

conn.space.bands:delete(5)
conn.space.bands:delete({ 5 })
--[[
---
- [5, 'The Rolling Stones', 1962]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,13 @@ box.space.bands:format({
-- 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', { parts = { { 'year' } }, unique = false })
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:select({ year }, { iterator = 'LT', limit = 10 })
return box.space.bands.index.year_band:select({ year }, { iterator = 'LT', limit = 10 })
end
]]
})
88 changes: 39 additions & 49 deletions doc/how-to/getting_started_net_box.rst
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ In this tutorial, the application layout is prepared manually:

#. Inside the ``instances.enabled`` directory of the created tt environment, create the ``net_box`` directory.

#. Inside ``instances.enabled/net_box``, create the ``instances.yml``, ``config.yaml``, and ``myapp.lua`` files:
#. Inside ``instances.enabled/net_box``, create the ``instances.yml`` and ``config.yaml`` files:

* ``instances.yml`` specifies instances to run in the current environment. In this example, there is one instance:

Expand All @@ -59,16 +59,45 @@ In this tutorial, the application layout is prepared manually:

.. literalinclude:: /code_snippets/snippets/connectors/instances.enabled/net_box/config.yaml
:language: yaml
:end-at: iproto
:dedent:

* ``myapp.lua`` contains ``net.box`` requests used to interact with a :ref:`sample database <getting_started_net_box_sample_db>`. These requests are explained below in the :ref:`getting_started_net_box_developing_app` section.


.. _getting_started_net_box_interactive:

.. _getting_started_net_box_developing_app:
Making net.box requests interactively
-------------------------------------

Developing the application
--------------------------
To try out ``net.box`` requests in the interactive console, you need to start sample applications:

1. Start the :ref:`sample_db <getting_started_net_box_sample_db>` application using ``tt start``:

.. code-block:: console
$ tt start sample_db
2. Start the :ref:`net_box <getting_started_net_box_creating-app>` application:

.. code-block:: console
$ tt start net_box
3. Connect to ``net_box:instance001`` using ``tt connect``:

.. code-block:: console
$ tt connect net_box:instance001
• Connecting to the instance...
• Connected to net_box:instance001
net_box:instance001>
In the instance's console, you can create a ``net.box`` connection and try out data operations.



.. _getting_started_net_box_creating_connection:

Creating a net.box connection
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Expand All @@ -78,23 +107,23 @@ To load the ``net.box`` module, use the ``require()`` directive:
.. literalinclude:: /code_snippets/snippets/connectors/instances.enabled/net_box/myapp.lua
:language: lua
:start-at: net_box = require
:end-at: net_box = require
:end-before: net_box.connect
:dedent:

To create a connection, pass a database URI to the ``connect()`` method:

.. literalinclude:: /code_snippets/snippets/connectors/instances.enabled/net_box/myapp.lua
:language: lua
:start-at: net_box.connect
:end-at: net_box.connect
:end-before: conn:ping
:dedent:

``ping()`` can be used to check the connection status:

.. literalinclude:: /code_snippets/snippets/connectors/instances.enabled/net_box/myapp.lua
:language: lua
:start-at: conn:ping
:end-at: conn:ping
:end-before: Roxette
:dedent:


Expand Down Expand Up @@ -225,45 +254,6 @@ The ``connection:close()`` method can be used to close the connection when it is
:end-before: end
:dedent:

.. NOTE::


.. _getting_started_net_box_interactive:

Making net.box requests interactively
-------------------------------------

To try out ``net.box`` requests in the interactive console, you need to start sample applications:

1. Start the :ref:`sample_db <getting_started_net_box_sample_db>` application using ``tt start``:

.. code-block:: console
$ tt start sample_db
2. Start the :ref:`net_box <getting_started_net_box_creating-app>` application:

.. code-block:: console
$ tt start net_box
3. Connect to ``net_box:instance001`` using ``tt connect``:

.. code-block:: console
$ tt connect net_box:instance001
• Connecting to the instance...
• Connected to net_box:instance001
net_box:instance001>
In the instance's console, you can create a ``net.box`` connection and try out data operations described in :ref:`getting_started_net_box_developing_app`:

.. code-block:: console
net_box:instance001> net_box = require('net.box')
---
...
net_box:instance001> conn = net_box.connect('sampleuser:123456@127.0.0.1:3301')
---
...
You can find the example with all the requests above on GitHub: `net_box <https://github.com/tarantool/doc/tree/latest/doc/code_snippets/snippets/connectors/instances.enabled/net_box>`_.

0 comments on commit 1488da3

Please sign in to comment.