Skip to content

Commit 7f3fbda

Browse files
committed
feat(property): return properties as object instead of array. resolves #20
1 parent 2cc283a commit 7f3fbda

File tree

5 files changed

+18
-17
lines changed

5 files changed

+18
-17
lines changed

module/property/StatementFetch.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,13 @@ class StatementFetch extends SqliteStatement {
1616
this.error('PREPARE STATEMENT', e)
1717
}
1818
}
19+
// reduce results (convert from array to object)
20+
_transform (res) {
21+
return res.reduce((memo, item) => {
22+
memo[ item.key ] = item.value
23+
return memo
24+
}, {})
25+
}
1926
}
2027

2128
module.exports = StatementFetch

module/property/StatementFetch.test.js

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,10 @@ module.exports.tests.function = (test, common) => {
5353
})
5454

5555
// test response structure
56-
t.deepEqual(rows, [{
57-
key: 'example_key_1',
58-
value: 'example_value_1'
59-
}, {
60-
key: 'example_key_2',
61-
value: 'example_value_2'
62-
}], 'read')
56+
t.deepEqual(rows, {
57+
example_key_1: 'example_value_1',
58+
example_key_2: 'example_value_2'
59+
}, 'read')
6360

6461
t.end()
6562
})

server/demo/assets/links.js

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,8 @@ function decorateLink (el) {
2121

2222
api.property({ source: source, id: id }, {}, function (err, res) {
2323
if (err) { console.error(err) } else {
24-
(res || []).forEach(function (prop) {
25-
if (prop.key === 'name') {
26-
el.attr('href', '/demo/place/' + encodeURIComponent(source) + '/' + encodeURIComponent(id))
27-
el.text(prop.value)
28-
}
29-
})
24+
el.attr('href', '/demo/place/' + encodeURIComponent(source) + '/' + encodeURIComponent(id))
25+
el.text(_.get(res, 'name', 'unknown'))
3026
}
3127
})
3228
}

server/demo/views/pages/place.hbs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@
7272
<table class="table is-fullwidth">
7373
<tbody>
7474
{{#each params.place.property}}
75-
<tr><th>{{this.key}}</th><td>{{this.value}}</td></tr>
75+
<tr><th>{{@key}}</th><td>{{this}}</td></tr>
7676
{{/each}}
7777
</tbody>
7878
</table>

sqlite/SqliteStatement.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
const Sqlite = require('./Sqlite')
22
class SqliteStatement extends Sqlite {
33
_selectStatement () { return this.statement }
4+
_transform (res) { return res }
45
run () {
56
let stmt = this._selectStatement.apply(this, arguments)
6-
return stmt.run.apply(stmt, arguments)
7+
return this._transform(stmt.run.apply(stmt, arguments))
78
}
89
get () {
910
let stmt = this._selectStatement.apply(this, arguments)
10-
return stmt.get.apply(stmt, arguments)
11+
return this._transform(stmt.get.apply(stmt, arguments))
1112
}
1213
all () {
1314
let stmt = this._selectStatement.apply(this, arguments)
14-
return stmt.all.apply(stmt, arguments)
15+
return this._transform(stmt.all.apply(stmt, arguments))
1516
}
1617
}
1718

0 commit comments

Comments
 (0)