Skip to content

Commit 1b389ee

Browse files
committed
Expand DML API with tuple-based functions
This patch completes series of patches that allow to use raw tuples as input and output arguments. It was decided to use insert/replace/delete functions only for raw tuples. In contrast {insert/replace/delete}_object functions allow to put unflattened objects as it was before. Closes #6
1 parent 375dad1 commit 1b389ee

File tree

8 files changed

+250
-62
lines changed

8 files changed

+250
-62
lines changed

README.md

Lines changed: 54 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,25 +21,25 @@ across the cluster.
2121
### Insert
2222

2323
```lua
24-
local object, err = crud.insert(space_name, object, opts)
24+
-- Insert tuple
25+
local result, err = crud.insert(space_name, tuple, opts)
26+
-- Insert object
27+
local result, err = crud.insert_object(space_name, object, opts)
2528
```
2629

2730
where:
2831

2932
* `space_name` (`string`) - name of the space to insert an object
30-
* `object` (`table`) - object to insert
33+
* `tuple` / `object` (`table`) - tuple/object to insert
3134
* `opts`:
3235
* `timeout` (`?number`) - `vshard.call` timeout (in seconds)
3336

34-
Returns inserted object, error.
37+
Returns inserted rows and metadata or nil with error.
3538

3639
**Example:**
3740

3841
```lua
39-
crud.insert('customers', {
40-
id = 1, name = 'Elizabeth', age = 23,
41-
})
42-
---
42+
crud.insert('customers', {1, box.NULL, 'Elizabeth', 23})
4343
---
4444
- metadata:
4545
- {'name': 'id', 'type': 'unsigned'}
@@ -49,6 +49,18 @@ crud.insert('customers', {
4949
rows:
5050
- [1, 477, 'Elizabeth', 23]
5151
...
52+
crud.insert_object('customers', {
53+
id = 2, name = 'Elizabeth', age = 24,
54+
})
55+
---
56+
- metadata:
57+
- {'name': 'id', 'type': 'unsigned'}
58+
- {'name': 'bucket_id', 'type': 'unsigned'}
59+
- {'name': 'name', 'type': 'string'}
60+
- {'name': 'age', 'type': 'number'}
61+
rows:
62+
- [2, 401, 'Elizabeth', 24]
63+
...
5264
```
5365

5466
### Get
@@ -144,22 +156,35 @@ crud.delete('customers', 1)
144156
### Replace
145157

146158
```lua
147-
local object, err = crud.replace(space_name, object, opts)
159+
-- Replace tuple
160+
local result, err = crud.replace(space_name, tuple, opts)
161+
-- Replace object
162+
local result, err = crud.replace_object(space_name, object, opts)
148163
```
149164

150165
where:
151166

152167
* `space_name` (`string`) - name of the space
153-
* `object` (`table`) - object to insert or replace exist one
168+
* `tuple` / `object` (`table`) - tuple/object to insert or replace exist one
154169
* `opts`:
155170
* `timeout` (`?number`) - `vshard.call` timeout (in seconds)
156171

157-
Returns inserted or replaced object, error.
172+
Returns inserted or replaced rows and metadata or nil with error.
158173

159174
**Example:**
160175

161176
```lua
162-
crud.replace('customers', {
177+
crud.replace('customers', {1, box.NULL, 'Alice', 22})
178+
---
179+
- metadata:
180+
- {'name': 'id', 'type': 'unsigned'}
181+
- {'name': 'bucket_id', 'type': 'unsigned'}
182+
- {'name': 'name', 'type': 'string'}
183+
- {'name': 'age', 'type': 'number'}
184+
rows:
185+
- [1, 477, 'Alice', 22]
186+
...
187+
crud.replace_object('customers', {
163188
id = 1, name = 'Alice', age = 22,
164189
})
165190
---
@@ -176,24 +201,38 @@ crud.replace('customers', {
176201
### Upsert
177202

178203
```lua
179-
local object, err = crud.upsert(space_name, object, operations, opts)
204+
-- Upsert tuple
205+
local result, err = crud.upsert(space_name, tuple, operations, opts)
206+
-- Upsert object
207+
local result, err = crud.upsert_object(space_name, tuple, operations, opts)
180208
```
181209

182210
where:
183211

184212
* `space_name` (`string`) - name of the space
185-
* `object` (`table`) - object to insert if there is no existing tuple which matches the key fields
213+
* `tuple` / `object` (`table`) - tuple/object to insert if there is no existing tuple which matches the key fields
186214
* `operations` (`table`) - update [operations](https://www.tarantool.io/en/doc/latest/reference/reference_lua/box_space/#box-space-update) if there is an existing tuple which matches the key fields of tuple
187215
* `opts`:
188216
* `timeout` (`?number`) - `vshard.call` timeout (in seconds)
189217

190-
Returns nil, error.
218+
Returns metadata and empty array of rows or nil, error.
191219

192220
**Example:**
193221

194222
```lua
195223
crud.upsert('customers',
196-
{id = 1, name = 'Alice', age = 22,},
224+
{1, box.NULL, 'Alice', 22},
225+
{{'+', 'age', 1}})
226+
---
227+
- metadata:
228+
- {'name': 'id', 'type': 'unsigned'}
229+
- {'name': 'bucket_id', 'type': 'unsigned'}
230+
- {'name': 'name', 'type': 'string'}
231+
- {'name': 'age', 'type': 'number'}
232+
rows: []
233+
...
234+
crud.upsert_object('customers',
235+
{id = 1, name = 'Alice', age = 22},
197236
{{'+', 'age', 1}})
198237
---
199238
- metadata:

crud.lua

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,25 +25,37 @@ crud.register = registry.add
2525
--- CRUD operations.
2626
-- @section crud
2727

28-
-- @refer insert.object
28+
-- @refer insert.tuple
2929
-- @function insert
30-
crud.insert = insert.object
30+
crud.insert = insert.tuple
31+
32+
-- @refer insert.object
33+
-- @function insert_object
34+
crud.insert_object = insert.object
3135

3236
-- @refer get.call
3337
-- @function get
3438
crud.get = get.call
3539

36-
-- @refer replace.object
40+
-- @refer replace.tuple
3741
-- @function replace
38-
crud.replace = replace.object
42+
crud.replace = replace.tuple
43+
44+
-- @refer replace.object
45+
-- @function replace_object
46+
crud.replace_object = replace.object
3947

4048
-- @refer update.call
4149
-- @function update
4250
crud.update = update.call
4351

52+
-- @refer upsert.tuple
53+
-- @function upsert
54+
crud.upsert = upsert.tuple
55+
4456
-- @refer upsert.object
4557
-- @function upsert
46-
crud.upsert = upsert.object
58+
crud.upsert_object = upsert.object
4759

4860
-- @refer delete.call
4961
-- @function delete

crud/insert.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ function insert.object(space_name, obj, opts)
120120

121121
local space = utils.get_space(space_name, vshard.router.routeall())
122122
if space == nil then
123-
return nil, InsertError:new("Space %q doesn't exists", space_name)
123+
return nil, InsertError:new("Space %q doesn't exist", space_name)
124124
end
125125
local space_format = space:format()
126126

crud/replace.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ function replace.object(space_name, obj, opts)
120120

121121
local space = utils.get_space(space_name, vshard.router.routeall())
122122
if space == nil then
123-
return nil, ReplaceError:new("Space %q doesn't exists", space_name)
123+
return nil, ReplaceError:new("Space %q doesn't exist", space_name)
124124
end
125125

126126
local space_format = space:format()

crud/upsert.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ function upsert.object(space_name, obj, user_operations, opts)
134134

135135
local space = utils.get_space(space_name, vshard.router.routeall())
136136
if space == nil then
137-
return nil, UpsertError:new("Space %q doesn't exists", space_name)
137+
return nil, UpsertError:new("Space %q doesn't exist", space_name)
138138
end
139139

140140
local space_format = space:format()

test/integration/pairs_test.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ local function insert_customers(g, customers)
8686
for _, customer in ipairs(customers) do
8787
local result, err = g.cluster.main_server.net_box:eval([[
8888
local crud = require('crud')
89-
return crud.insert('customers', ...)
89+
return crud.insert_object('customers', ...)
9090
]],{customer})
9191

9292
t.assert_equals(err, nil)

test/integration/select_test.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ local function insert_customers(g, customers)
8181
for _, customer in ipairs(customers) do
8282
local result, err = g.cluster.main_server.net_box:eval([[
8383
local crud = require('crud')
84-
return crud.insert('customers', ...)
84+
return crud.insert_object('customers', ...)
8585
]],{customer})
8686

8787
t.assert_equals(err, nil)

0 commit comments

Comments
 (0)