Skip to content

Commit 5bbe1c0

Browse files
committed
Enrich CRUD API output with metadata
This patch changes output format of CRUD API from objects to rows + metadata. Before: ```lua crud.insert('customers', { id = 1, name = 'Name', age = 1, }) --- - bucket_id: 1 age: 1 name: Name id: 1 ... ``` After: ```lua crud.insert('customers', { id = 1, name = 'Name', age = 1, }) --- - - rows: - [1, 1, 1, 'Name'] metadata: - name: id type: unsigned - name: bucket_id type: unsigned - name: age type: unsigned - name: name type: string ``` It's useful when user call crud functions via connectors and (s)he doesn't really need to deserialize tuples to map. Need for #6
1 parent 33efd54 commit 5bbe1c0

File tree

15 files changed

+386
-245
lines changed

15 files changed

+386
-245
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
1818
* replace
1919
* upsert
2020

21+
* Output format for CRUD operations changed to set of rows and metadata
22+
2123
### Changed
2224

2325
* `checks` is disabled for internal functions by default

README.md

Lines changed: 57 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,14 @@ crud.insert('customers', {
4040
id = 1, name = 'Elizabeth', age = 23,
4141
})
4242
---
43-
- bucket_id: 7614
44-
age: 23
45-
name: Elizabeth
46-
id: 1
43+
---
44+
- metadata:
45+
- {'name': 'id', 'type': 'unsigned'}
46+
- {'name': 'bucket_id', 'type': 'unsigned'}
47+
- {'name': 'name', 'type': 'string'}
48+
- {'name': 'age', 'type': 'number'}
49+
rows:
50+
- [1, 477, 'Elizabeth', 23]
4751
...
4852
```
4953

@@ -67,10 +71,13 @@ Returns object, error.
6771
```lua
6872
crud.get('customers', 1)
6973
---
70-
- bucket_id: 7614
71-
age: 23
72-
name: Elizabeth
73-
id: 1
74+
- metadata:
75+
- {'name': 'id', 'type': 'unsigned'}
76+
- {'name': 'bucket_id', 'type': 'unsigned'}
77+
- {'name': 'name', 'type': 'string'}
78+
- {'name': 'age', 'type': 'number'}
79+
rows:
80+
- [1, 477, 'Elizabeth', 23]
7481
...
7582
```
7683

@@ -95,10 +102,13 @@ Returns updated object, error.
95102
```lua
96103
crud.update('customers', 1, {{'+', 'age', 1}})
97104
---
98-
- bucket_id: 7614
99-
age: 24
100-
name: Elizabeth
101-
id: 1
105+
- metadata:
106+
- {'name': 'id', 'type': 'unsigned'}
107+
- {'name': 'bucket_id', 'type': 'unsigned'}
108+
- {'name': 'name', 'type': 'string'}
109+
- {'name': 'age', 'type': 'number'}
110+
rows:
111+
- [1, 477, 'Elizabeth', 24]
102112
...
103113
```
104114

@@ -122,11 +132,13 @@ Returns deleted object, error.
122132
```lua
123133
crud.delete('customers', 1)
124134
---
125-
- bucket_id: 7614
126-
age: 24
127-
name: Elizabeth
128-
id: 1
129-
...
135+
- metadata:
136+
- {'name': 'id', 'type': 'unsigned'}
137+
- {'name': 'bucket_id', 'type': 'unsigned'}
138+
- {'name': 'name', 'type': 'string'}
139+
- {'name': 'age', 'type': 'number'}
140+
rows:
141+
- [1, 477, 'Elizabeth', 24]
130142
```
131143

132144
### Replace
@@ -151,10 +163,13 @@ crud.replace('customers', {
151163
id = 1, name = 'Alice', age = 22,
152164
})
153165
---
154-
- bucket_id: 7614
155-
age: 22
156-
name: Alice
157-
id: 1
166+
- metadata:
167+
- {'name': 'id', 'type': 'unsigned'}
168+
- {'name': 'bucket_id', 'type': 'unsigned'}
169+
- {'name': 'name', 'type': 'string'}
170+
- {'name': 'age', 'type': 'number'}
171+
rows:
172+
- [1, 477, 'Alice', 22]
158173
...
159174
```
160175

@@ -177,9 +192,16 @@ Returns nil, error.
177192
**Example:**
178193

179194
```lua
180-
crud.upsert('customers', {id = 1, name = 'Alice', age = 22,}, {{'+', 'age', 1}})
195+
crud.upsert('customers',
196+
{id = 1, name = 'Alice', age = 22,},
197+
{{'+', 'age', 1}})
181198
---
182-
- nil
199+
- metadata:
200+
- {'name': 'id', 'type': 'unsigned'}
201+
- {'name': 'bucket_id', 'type': 'unsigned'}
202+
- {'name': 'name', 'type': 'string'}
203+
- {'name': 'age', 'type': 'number'}
204+
rows: []
183205
...
184206
```
185207

@@ -222,26 +244,18 @@ Each condition is a table `{operator, field-identifier, value}`:
222244
```lua
223245
crud.select('customers', {{'<=', 'age', 35}})
224246
---
225-
- - bucket_id: 10755
226-
age: 35
227-
name: Jack
228-
id: 5
229-
- bucket_id: 8011
230-
age: 33
231-
name: David
232-
id: 3
233-
- bucket_id: 16055
234-
age: 25
235-
name: William
236-
id: 6
237-
- bucket_id: 2998
238-
age: 18
239-
name: Elizabeth
240-
id: 7
241-
- bucket_id: 7614
242-
age: 12
243-
name: Elizabeth
244-
id: 1
247+
- metadata:
248+
- {'name': 'id', 'type': 'unsigned'}
249+
- {'name': 'bucket_id', 'type': 'unsigned'}
250+
- {'name': 'name', 'type': 'string'}
251+
- {'name': 'age', 'type': 'number'}
252+
rows:
253+
- [5, 1172, 'Jack', 35]
254+
- [3, 2804, 'David', 33]
255+
- [6, 1064, 'William', 25]
256+
- [7, 693, 'Elizabeth', 18]
257+
- [1, 477, 'Elizabeth', 12]
258+
...
245259
```
246260

247261
### Pairs

crud.lua

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ local update = require('crud.update')
1111
local upsert = require('crud.upsert')
1212
local delete = require('crud.delete')
1313
local select = require('crud.select')
14+
local utils = require('crud.common.utils')
1415

1516
local crud = {}
1617

@@ -56,6 +57,10 @@ crud.select = select.call
5657
-- @function pairs
5758
crud.pairs = select.pairs
5859

60+
-- @refer utils.unflatten_rows
61+
-- @function unflatten_rows
62+
crud.unflatten_rows = utils.unflatten_rows
63+
5964
--- Initializes crud on node
6065
--
6166
-- Exports all functions that are used for calls

crud/common/utils.lua

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,4 +157,20 @@ function utils.convert_operations(user_operations, space_format)
157157
return converted_operations
158158
end
159159

160+
function utils.unflatten_rows(rows, metadata)
161+
if metadata == nil then
162+
return nil, UnflattenError:new('Metadata is not provided')
163+
end
164+
165+
local result = table.new(#rows, 0)
166+
local err
167+
for i, row in ipairs(rows) do
168+
result[i], err = utils.unflatten(row, metadata)
169+
if err ~= nil then
170+
return nil, err
171+
end
172+
end
173+
return result
174+
end
175+
160176
return utils

crud/delete.lua

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ function delete.init()
3131
})
3232
end
3333

34-
--- Deletes tuple from the specifed space by key
34+
--- Deletes tuple from the specified space by key
3535
--
3636
-- @function call
3737
--
@@ -81,12 +81,10 @@ function delete.call(space_name, key, opts)
8181
end
8282

8383
local tuple = results[replicaset.uuid]
84-
local object, err = utils.unflatten(tuple, space:format())
85-
if err ~= nil then
86-
return nil, DeleteError:new("Received tuple that doesn't match space format: %s", err)
87-
end
88-
89-
return object
84+
return {
85+
metadata = table.copy(space:format()),
86+
rows = {tuple},
87+
}
9088
end
9189

9290
return delete

crud/get.lua

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -81,12 +81,10 @@ function get.call(space_name, key, opts)
8181
end
8282

8383
local tuple = results[replicaset.uuid]
84-
local object, err = utils.unflatten(tuple, space:format())
85-
if err ~= nil then
86-
return nil, GetError:new("Received tuple that doesn't match space format: %s", err)
87-
end
88-
89-
return object
84+
return {
85+
metadata = table.copy(space:format()),
86+
rows = {tuple},
87+
}
9088
end
9189

9290
return get

crud/insert.lua

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ function insert.call(space_name, obj, opts)
6363
end
6464
local space_format = space:format()
6565

66-
-- compute default buckect_id
66+
-- compute default bucket_id
6767
local tuple, err = utils.flatten(obj, space_format)
6868
if err ~= nil then
6969
return nil, InsertError:new("Object is specified in bad format: %s", err)
@@ -92,12 +92,10 @@ function insert.call(space_name, obj, opts)
9292
end
9393

9494
local tuple = results[replicaset.uuid]
95-
local object, err = utils.unflatten(tuple, space_format)
96-
if err ~= nil then
97-
return nil, InsertError:new("Received tuple that doesn't match space format: %s", err)
98-
end
99-
100-
return object
95+
return {
96+
metadata = table.copy(space_format),
97+
rows = {tuple},
98+
}
10199
end
102100

103101
return insert

crud/replace.lua

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -89,12 +89,10 @@ function replace.call(space_name, obj, opts)
8989
end
9090

9191
local tuple = results[replicaset.uuid]
92-
local object, err = utils.unflatten(tuple, space_format)
93-
if err ~= nil then
94-
return nil, ReplaceError:new("Received tuple that doesn't match space format: %s", err)
95-
end
96-
97-
return object
92+
return {
93+
metadata = table.copy(space_format),
94+
rows = {tuple},
95+
}
9896
end
9997

10098
return replace

crud/select.lua

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -228,11 +228,16 @@ function select_module.pairs(space_name, user_conditions, opts)
228228
return nil
229229
end
230230

231-
local obj, err = iter:get()
231+
local tuple, err = iter:get()
232232
if err ~= nil then
233233
error(string.format("Failed to get next object: %s", err))
234234
end
235235

236+
local obj, err = utils.unflatten(tuple, iter.space_format)
237+
if err ~= nil then
238+
error(string.format("Failed to unflatten next object: %s", err))
239+
end
240+
236241
return iter, obj
237242
end
238243

@@ -260,7 +265,7 @@ function select_module.call(space_name, user_conditions, opts)
260265
return nil, err
261266
end
262267

263-
local objects = {}
268+
local tuples = {}
264269

265270
while iter:has_next() do
266271
local obj, err = iter:get()
@@ -272,10 +277,13 @@ function select_module.call(space_name, user_conditions, opts)
272277
break
273278
end
274279

275-
table.insert(objects, obj)
280+
table.insert(tuples, obj)
276281
end
277282

278-
return objects
283+
return {
284+
metadata = table.copy(iter.space_format),
285+
rows = tuples,
286+
}
279287
end
280288

281289
return select_module

crud/select/iterator.lua

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -191,12 +191,7 @@ function Iterator:get()
191191
end
192192
end
193193

194-
local object, err = utils.unflatten(tuple, self.space_format)
195-
if err ~= nil then
196-
return nil, GetTupleError:new("Received tuple that doesn't match space format: %s", err)
197-
end
198-
199-
return object
194+
return tuple
200195
end
201196

202197
return Iterator

0 commit comments

Comments
 (0)