Skip to content

Commit

Permalink
nested objects and array of objects
Browse files Browse the repository at this point in the history
  • Loading branch information
vincic committed Nov 25, 2016
1 parent a7ed0c3 commit 77e98cf
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 6 deletions.
35 changes: 32 additions & 3 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,22 +61,51 @@ function prepareOptions (options) {
return values
}

function flatten (arr) {
let index
while ((index = arr.findIndex(el => Array.isArray(el))) > -1) {
arr.splice(index, 1, ...arr[index])
}
return arr
}

/**
* @param {string[]} options
* @returns values
* @returns values.keys
*/
function prepareOptionsInsert (options) {
function prepareOptionsInsert (options, subObject) {
var values = {}
var keys = Object.keys(options)
values = keys.map(function (i) {
var column = subObject && subObject + "['" + i + "']" || i // column['subobjcol']
var v = getValueByType(options[i])
var rv = i + " = '" + v + "'"

if (v instanceof Object && !Array.isArray(v)) {
return prepareOptionsInsert(v, i)
}

var rv = column + " = '" + v + "'"
if (v instanceof Number) {
rv = i + '=' + v
rv = column + '=' + v
} else if (Array.isArray(v)) {
// We have an array
rv = column + '= ['
v.map(function (arrValue, arrIndex, arr) {
// Check if we have objects in the array
if (arrValue instanceof Object) {
rv += '{' + prepareOptionsInsert(arrValue) + '}'
} else {
// Array of primitives
rv += "'" + arrValue + "'"
}
if (arrIndex < arr.length - 1) rv += ', '
})
rv += ']'
}
return rv
})
values = flatten(values)
return values
}

Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
"change-case": "^2.1.3",
"expect": "^1.9.0",
"mocha": "^2.3.0",
"request": "^2.40.0"
"request": "^2.40.0",
"standard": "^8.6.0"
}
}
82 changes: 80 additions & 2 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ var crate = require('../')
// it = Lab.test,
// expect = Lab.expect
var docsToInsert = 1000
crate.connect('http://127.0.0.1:4200')
crate.connect('http://127.0.0.1:8200')

describe('#node-crate', function () {
it('Create blob table', function (done) {
Expand All @@ -25,7 +25,9 @@ describe('#node-crate', function () {
})

it('Create table', function (done) {
var schema = {NodeCrateTest: {id: 'integer primary key', title: 'string'}}
var schema = {NodeCrateTest: {id: 'integer primary key', title: 'string',
farray: 'array(string)',
sub_object: 'object as (title string, sub_array array(string), sub_obj_array array(object as (soa_title string)))'}}
crate.create(schema)
.success(function (res) {
expect(res.rowcount).toBe(1)
Expand Down Expand Up @@ -178,6 +180,82 @@ describe('#node-crate', function () {
}, 4000)
})

it('Update with nested Object', function (done) {
setTimeout(function () {
crate.update('NodeCrateTest', {
title: 'TitleNewWithSub',
farray: [ 'f1', 'f2'],
sub_object: { title: 'SubTitle',
sub_array: [ 'sub1', 'sub2', 'sub3']
}
}, 'id=1')
.success(function (res) {
expect(res.rowcount).toBe(1)
done()
})
.error(function (err) {
done(err)
})
}, 2000)
})

it('Select after nested update', function (done) {
setTimeout(function () {
crate.execute('SELECT * FROM NodeCrateTest where id=1 limit 100')
.success(function (res) {
expect(res.json[0].title).toBe('TitleNewWithSub')
expect(res.json[0].farray.length).toBe(2)
expect(res.json[0].sub_object.title).toBe('SubTitle')
expect(res.json[0].sub_object.sub_array.length).toBe(3)
expect(res.json[0].numberVal).toBe(42)
done()
})
.error(function (err) {
console.log(err)
done(err)
})
}, 4000)
})

it('Update with nested array with Object', function (done) {
setTimeout(function () {
crate.update('NodeCrateTest', {
title: 'TitleNewWithSubArrayObject',
farray: [ 'f1', 'f2'],
sub_object: { title: 'SubTitle',
sub_array: [ 'sub1', 'sub2', 'sub3'],
sub_obj_array: [ { soa_title: 'SoaTitle' }]
}
}, 'id=1')
.success(function (res) {
expect(res.rowcount).toBe(1)
done()
})
.error(function (err) {
done(err)
})
}, 2000)
})

it('Select after nested update', function (done) {
setTimeout(function () {
crate.execute('SELECT * FROM NodeCrateTest where id=1 limit 100')
.success(function (res) {
expect(res.json[0].title).toBe('TitleNewWithSubArrayObject')
expect(res.json[0].farray.length).toBe(2)
expect(res.json[0].sub_object.title).toBe('SubTitle')
expect(res.json[0].sub_object.sub_obj_array[0].soa_title).toBe('SoaTitle')
expect(res.json[0].numberVal).toBe(42)
done()
})
.error(function (err) {
console.log(err)
done(err)
})
}, 4000)
})


it('getBlob', function (done) {
crate.getBlob('blobtest', hashkey)
.success(function () {
Expand Down

0 comments on commit 77e98cf

Please sign in to comment.