Skip to content

Commit a65286e

Browse files
committed
Rewrite the example
1 parent d0c052c commit a65286e

File tree

1 file changed

+119
-1
lines changed

1 file changed

+119
-1
lines changed

doc/reference/reference_lua/net_box.rst

Lines changed: 119 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -972,4 +972,122 @@ And here starts the example:
972972
- [700, 'TEST700']
973973
...
974974
-- Close the connection
975-
tarantool> conn:close()
975+
tarantool> conn:close()
976+
977+
First, get the ``net.box`` object:
978+
979+
.. code-block:: tarantoolsession
980+
981+
tarantool> net_box = require('net.box')
982+
---
983+
...
984+
985+
In ``net.box``, self connection is pre-established.
986+
That is, ``conn = net_box.connect('localhost:3301')`` can be replaced with the following command:
987+
988+
.. code-block:: tarantoolsession
989+
990+
tarantool> conn = net_box.self
991+
---
992+
...
993+
994+
Then, make a ping:
995+
996+
.. code-block:: tarantoolsession
997+
998+
tarantool> conn:ping()
999+
---
1000+
- true
1001+
...
1002+
1003+
Now, let's get to work with the data in the ``tester``space.
1004+
The ``select`` command below returns all tuples where the key value is 600:
1005+
//добавить еще кортеж с ключом 800
1006+
.. code-block:: tarantoolsession
1007+
1008+
tarantool> conn.space.tester:select{800}
1009+
---
1010+
- - [800, 'TEST']
1011+
...
1012+
1013+
Now, let's insert two tuples in the space:
1014+
1015+
.. code-block:: tarantoolsession
1016+
1017+
tarantool> conn.space.tester:insert({700, 'TEST700'})
1018+
---
1019+
- [700, 'TEST700']
1020+
...
1021+
tarantool> conn.space.tester:insert({600, 'TEST600'})
1022+
---
1023+
- [600, 'TEST600']
1024+
...
1025+
1026+
After the insert, we have one tuple where the key value is ``600``.
1027+
To select this tuple, you can use the ``get()`` method.
1028+
Unlike the ``select()`` command, ``get()`` returns only one tuple that satisfies the stated condition.
1029+
1030+
.. code-block:: tarantoolsession
1031+
1032+
tarantool> conn.space.tester:get({600})
1033+
---
1034+
- [600, 'TEST600']
1035+
...
1036+
1037+
To update the existing tuple, you can use either ``update()`` or ``upsert``.
1038+
Use the first one to ...
1039+
1040+
.. code-block:: tarantoolsession
1041+
1042+
-- Update the existing tuple
1043+
tarantool> conn.space.tester:update(800, {{'=', 2, 'TEST800'}})
1044+
---
1045+
- [800, 'TEST800']
1046+
...
1047+
1048+
Use ``upsert`` to...
1049+
1050+
.. code-block:: tarantoolsession
1051+
1052+
-- Update the existing tuple
1053+
tarantool> conn.space.tester:upsert({500, 'TEST500'}, {{'=', 2, 'TEST'}})
1054+
---
1055+
...
1056+
1057+
To delete a tuple, run the method below:
1058+
1059+
.. code-block:: tarantoolsession
1060+
1061+
-- Delete tuples where the key value is 600
1062+
tarantool> conn.space.tester:delete{600}
1063+
---
1064+
- [600, 'TEST600']
1065+
...
1066+
1067+
Now, let's replace the existing tuple with a new one
1068+
1069+
.. code-block:: tarantoolsession
1070+
1071+
tarantool> conn.space.tester:replace{500, 'New data', 'Extra data'}
1072+
---
1073+
- [500, 'New data', 'Extra data']
1074+
...
1075+
1076+
Finally, let's select all tuples stored in the space:
1077+
1078+
.. code-block:: tarantoolsession
1079+
1080+
tarantool> conn.space.tester:select{}
1081+
---
1082+
- - [800, 'TEST800']
1083+
- [500, 'New data', 'Extra data']
1084+
- [700, 'TEST700']
1085+
...
1086+
1087+
In the end, close the connection when it is no longer needed:
1088+
1089+
.. code-block:: tarantoolsession
1090+
1091+
tarantool> conn:close()
1092+
---
1093+
...

0 commit comments

Comments
 (0)