jedlik 1.0.7 → 1.0.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/index.js CHANGED
@@ -1,9 +1,10 @@
1
- function Jedlik() {
1
+ function Jedlik(booleanSupport) {
2
2
  this._data = {
3
3
  attributes: {},
4
4
  items: [],
5
5
  localSecondaryIndexes: [],
6
- expected: []
6
+ expected: [],
7
+ booleanSupport: booleanSupport
7
8
  };
8
9
  this.addIfExists = function(attributeName, fieldName, json) {
9
10
  if (this._data[fieldName]) {
@@ -97,7 +98,7 @@ Jedlik.prototype.update = function() {
97
98
  json.Key[this._data.rangekey.key][this._data.rangekey.type] = this._data.rangekey.value;
98
99
  }
99
100
 
100
- if (this._data.expected.length > 0){
101
+ if (this._data.expected.length > 0) {
101
102
  json.Expected = {};
102
103
  }
103
104
 
@@ -146,43 +147,81 @@ Jedlik.prototype.tablename = function(tablename) {
146
147
  return this;
147
148
  };
148
149
 
149
- var getType = function(value, nullable) {
150
+ Jedlik.prototype._getType = function(value, nullable, subType) {
151
+ var self = this;
152
+
150
153
  if (nullable && value == null) {
151
154
  return 'NULL';
152
155
  }
153
156
 
154
157
  if (Array.isArray(value)) {
155
- return (value.length === 0 || typeof value[0] == 'object') ? 'L' : 'SS';
158
+ if (subType || value.length === 0) {
159
+ return 'L';
160
+ }
161
+
162
+ var strings = true;
163
+
164
+ value.forEach(function(item) {
165
+ var type = self._getType(item, true, true);
166
+
167
+ if (type != 'S') {
168
+ strings = false;
169
+ }
170
+ });
171
+
172
+ return strings ? 'SS' : 'L';
173
+ }
174
+
175
+ if (this._data.booleanSupport && typeof value == 'boolean') {
176
+ return "BOOL"
156
177
  }
157
178
 
158
179
  return (typeof value == 'object') ? 'M' : Number.isFinite(value) ? 'N' : 'S';
159
180
  };
160
181
 
161
- var getValue = function(value, nullable) {
182
+ Jedlik.prototype._getValue = function(value, nullable) {
183
+ var self = this;
184
+
162
185
  if (nullable && value == null) {
163
186
  return true;
164
187
  }
165
188
 
189
+ if (self._data.booleanSupport && typeof value == 'boolean') {
190
+ return value;
191
+ }
192
+
166
193
  if (Array.isArray(value)) {
167
- if (value.length === 0 || typeof value[0] == 'object') {
168
- var result = [];
169
- value.forEach(function(item) {
170
- result.push({ M: getValue(item) });
171
- });
172
- return result;
173
- } else {
174
- return value;
194
+ if (value.length === 0) {
195
+ return [];
175
196
  }
197
+
198
+ var result = [];
199
+ var strings = true;
200
+
201
+ value.forEach(function(item) {
202
+ var type = self._getType(item, true, true);
203
+
204
+ if (type != 'S') {
205
+ strings = false;
206
+ }
207
+
208
+ var data = {};
209
+ data[type] = self._getValue(item, true);
210
+
211
+ result.push(data);
212
+ });
213
+
214
+ return strings ? value : result;
176
215
  }
177
216
 
178
217
  if (typeof value == 'object') {
179
- var result = {},
180
- itemKeys = Object.keys(value);
218
+ var result = {};
219
+ var itemKeys = Object.keys(value);
181
220
 
182
221
  itemKeys.forEach(function(key) {
183
222
  var item = value[key];
184
223
  result[key] = {};
185
- result[key][getType(item)] = getValue(item);
224
+ result[key][self._getType(item)] = self._getValue(item);
186
225
  });
187
226
  return result;
188
227
  }
@@ -194,7 +233,7 @@ Jedlik.prototype.hashkey = function(key, value, type) {
194
233
  this._data.hashkey = {
195
234
  key: key,
196
235
  value: value,
197
- type: type || getType(value)
236
+ type: type || this._getType(value)
198
237
  };
199
238
  return this;
200
239
  };
@@ -203,7 +242,7 @@ Jedlik.prototype.starthashkey = function(key, value, type) {
203
242
  this._data.starthashkey = {
204
243
  key: key,
205
244
  value: value,
206
- type: type || getType(value)
245
+ type: type || this._getType(value)
207
246
  };
208
247
  return this;
209
248
  };
@@ -212,7 +251,7 @@ Jedlik.prototype.startrangekey = function(key, value, type) {
212
251
  this._data.startrangekey = {
213
252
  key: key,
214
253
  value: value,
215
- type: type || getType(value)
254
+ type: type || this._getType(value)
216
255
  };
217
256
  return this;
218
257
  };
@@ -220,8 +259,8 @@ Jedlik.prototype.startrangekey = function(key, value, type) {
220
259
  Jedlik.prototype.rangekey = function(key, value, comparisonOp, type) {
221
260
  this._data.rangekey = {
222
261
  key: key,
223
- value: value && getValue(value),
224
- type: type || getType(value),
262
+ value: value && this._getValue(value),
263
+ type: type || this._getType(value),
225
264
  comparisonOp: comparisonOp || 'EQ'
226
265
  };
227
266
  return this;
@@ -246,9 +285,9 @@ Jedlik.prototype.localIndexName = function(indexName) {
246
285
  Jedlik.prototype.rangekeyBetween = function(key, valueFrom, valueTo) {
247
286
  this._data.rangekeyBetween = {
248
287
  key: key,
249
- valueFrom: valueFrom && getValue(valueFrom),
250
- valueTo: valueTo && getValue(valueTo),
251
- type: getType(valueFrom)
288
+ valueFrom: valueFrom && this._getValue(valueFrom),
289
+ valueTo: valueTo && this._getValue(valueTo),
290
+ type: this._getType(valueFrom)
252
291
  };
253
292
  return this;
254
293
  };
@@ -291,10 +330,10 @@ Jedlik.prototype.attribute = function(key, value, action) {
291
330
  return this.nullableAttribute(key, value, action);
292
331
  }
293
332
 
294
- Jedlik.prototype.nullableAttribute = function(key, value, action) {
333
+ Jedlik.prototype.nullableAttribute = function(key, value, action) {
295
334
  this._data.attributes[key] = {
296
- value: getValue(value, true),
297
- type: getType(value, true),
335
+ value: this._getValue(value, true),
336
+ type: this._getType(value, true),
298
337
  action: action || 'PUT'
299
338
  };
300
339
  return this;
@@ -313,8 +352,10 @@ Jedlik.prototype.del = function() {
313
352
  json.Key[this._data.hashkey.key] = {};
314
353
  json.Key[this._data.hashkey.key][this._data.hashkey.type] = this._data.hashkey.value;
315
354
 
316
- json.Key[this._data.rangekey.key] = {};
317
- json.Key[this._data.rangekey.key][this._data.rangekey.type] = this._data.rangekey.value;
355
+ if (this._data.rangekey) {
356
+ json.Key[this._data.rangekey.key] = {};
357
+ json.Key[this._data.rangekey.key][this._data.rangekey.type] = this._data.rangekey.value;
358
+ }
318
359
 
319
360
  this.addIfExists('TableName', 'tablename', json);
320
361
 
@@ -374,9 +415,9 @@ Jedlik.prototype.createTable = function() {
374
415
  });
375
416
  json.LocalSecondaryIndexes.push({
376
417
  IndexName: localSecondaryIndex.indexName,
377
- KeySchema:[
378
- {AttributeName: this._data.hashkey.key, KeyType: 'HASH' },
379
- {AttributeName: localSecondaryIndex.key, KeyType: 'RANGE' }
418
+ KeySchema: [
419
+ { AttributeName: this._data.hashkey.key, KeyType: 'HASH' },
420
+ { AttributeName: localSecondaryIndex.key, KeyType: 'RANGE' }
380
421
  ],
381
422
  Projection: {
382
423
  ProjectionType: localSecondaryIndex.projectionType
@@ -417,7 +458,7 @@ Jedlik.prototype.batchWrite = function() {
417
458
  if (key !== 'tablename') {
418
459
  var value = item[key];
419
460
  itemDDB.PutRequest.Item[key] = {};
420
- itemDDB.PutRequest.Item[key][getType(value)] = value.toString();
461
+ itemDDB.PutRequest.Item[key][that._getType(value)] = value.toString();
421
462
  }
422
463
  });
423
464
 
@@ -430,8 +471,8 @@ Jedlik.prototype.batchWrite = function() {
430
471
  Jedlik.prototype.expected = function (key, value, comparisonOp) {
431
472
  this._data.expected.push({
432
473
  key: key,
433
- value: value && getValue(value),
434
- type: getType(value),
474
+ value: value && this._getValue(value),
475
+ type: this._getType(value),
435
476
  comparisonOp: comparisonOp
436
477
  });
437
478
  return this;
@@ -457,7 +498,7 @@ Jedlik.prototype.batchGet = function() {
457
498
  if (k !== 'tablename') {
458
499
  key[k] = {};
459
500
  var value = item[k];
460
- key[k][getType(value)] = value.toString();
501
+ key[k][that._getType(value)] = value.toString();
461
502
  };
462
503
 
463
504
  });
@@ -470,8 +511,8 @@ Jedlik.prototype.batchGet = function() {
470
511
 
471
512
  Jedlik.prototype.mapItem = function(item, keysToOmit) {
472
513
  var self = this;
473
- var ret = {},
474
- keysToOmit = keysToOmit || [];
514
+ var ret = {};
515
+ var keysToOmit = keysToOmit || [];
475
516
  var itemKeys = Object.keys(item);
476
517
 
477
518
  for (var i = 0; i < itemKeys.length; i++) {
@@ -486,14 +527,14 @@ Jedlik.prototype.mapItem = function(item, keysToOmit) {
486
527
  ret[key] = [];
487
528
 
488
529
  value.forEach(function(valueObj) {
489
- var type = Object.keys(valueObj)[0],
490
- value = valueObj[type];
491
-
492
- ret[key].push(
493
- type === 'NULL' ? null :
494
- type === 'M' ? self.mapItem(value) :
495
- type === 'N' ? parseFloat(value, 10) :
496
- value);
530
+ var type = Object.keys(valueObj)[0];
531
+ var value = valueObj[type];
532
+
533
+ ret[key].push(
534
+ type === 'NULL' ? null :
535
+ type === 'M' ? self.mapItem(value) :
536
+ type === 'N' ? parseFloat(value, 10) :
537
+ value);
497
538
  });
498
539
  } else {
499
540
  ret[key] =
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "jedlik",
3
- "version": "1.0.7",
3
+ "version": "1.0.10",
4
4
  "description": "Fluent syntax generator for DynamoDB.",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -0,0 +1,8 @@
1
+ {
2
+ "Key": {
3
+ "hashkey": {
4
+ "S": "hashkeyvalue"
5
+ }
6
+ },
7
+ "TableName": "tablename"
8
+ }
File without changes
package/test/test.js CHANGED
@@ -136,12 +136,73 @@ describe('lib', function() {
136
136
  action: 'PUT'
137
137
  },
138
138
  attribute4: {
139
- value: { a: { N: '123' }, b: { S: 'STR' } },
139
+ value: { a: { N: '123' }, b: { S: 'STR' }, c: { S: 'true' }},
140
140
  type: 'M',
141
141
  action: 'PUT'
142
142
  },
143
143
  attribute5: {
144
- value: [ { M: { a: { N: '1' }, b: { S: 'S1' } } }, { M: { a: { N: '2' }, b: { S: 'S2' } } } ],
144
+ value: [ { M: { a: { N: '1' }, b: { S: 'S1' } } }, { M: { a: { N: '2' }, b: { S: 'S2' }, c: { S: 'true' } } } ],
145
+ type: 'L',
146
+ action: 'PUT'
147
+ },
148
+ attribute6: {
149
+ value: 'true',
150
+ type: 'S',
151
+ action: 'PUT'
152
+ },
153
+ attribute7: {
154
+ value: [ { NULL: true }, { S: 's1' }, { S: 's2' } ],
155
+ type: 'L',
156
+ action: 'PUT'
157
+ }
158
+ };
159
+ expect(this.jedlik
160
+ .attribute('attribute1', 'STR', 'PUT')
161
+ .attribute('attribute2', 1234)
162
+ .attribute('attribute3', ['s1', 's2'])
163
+ .attribute('attribute4', {a:123, b:'STR', c:true})
164
+ .attribute('attribute5', [{a:1, b:'S1'}, {a:2, b:'S2', c:true}])
165
+ .attribute('attribute6', true)
166
+ .attribute('attribute7', [null, 's1', 's2'])
167
+ ._data.attributes).to.deep.equal(expected);
168
+ });
169
+
170
+ it('should add attribute property if boolean support', function() {
171
+ this.jedlik = new(this.lib)(true);
172
+
173
+ var expected = {
174
+ attribute1: {
175
+ value: 'STR',
176
+ type: 'S',
177
+ action: 'PUT'
178
+ },
179
+ attribute2: {
180
+ value: '1234',
181
+ type: 'N',
182
+ action: 'PUT'
183
+ },
184
+ attribute3: {
185
+ value: ['s1', 's2'],
186
+ type: 'SS',
187
+ action: 'PUT'
188
+ },
189
+ attribute4: {
190
+ value: { a: { N: '123' }, b: { S: 'STR' }, c: { BOOL: true } },
191
+ type: 'M',
192
+ action: 'PUT'
193
+ },
194
+ attribute5: {
195
+ value: [ { M: { a: { N: '1' }, b: { S: 'S1' } } }, { M: { a: { N: '2' }, b: { S: 'S2' }, c: { BOOL: true } } } ],
196
+ type: 'L',
197
+ action: 'PUT'
198
+ },
199
+ attribute6: {
200
+ value: true,
201
+ type: 'BOOL',
202
+ action: 'PUT'
203
+ },
204
+ attribute7: {
205
+ value: [ { NULL: true }, { S: 's1' }, { S: 's2' } ],
145
206
  type: 'L',
146
207
  action: 'PUT'
147
208
  }
@@ -150,8 +211,10 @@ describe('lib', function() {
150
211
  .attribute('attribute1', 'STR', 'PUT')
151
212
  .attribute('attribute2', 1234)
152
213
  .attribute('attribute3', ['s1', 's2'])
153
- .attribute('attribute4', {a:123, b:'STR'})
154
- .attribute('attribute5', [{a:1, b:'S1'}, {a:2, b:'S2'}])
214
+ .attribute('attribute4', {a:123, b:'STR', c:true})
215
+ .attribute('attribute5', [{a:1, b:'S1'}, {a:2, b:'S2', c:true}])
216
+ .attribute('attribute6', true)
217
+ .attribute('attribute7', [null, 's1', 's2'])
155
218
  ._data.attributes).to.deep.equal(expected);
156
219
  });
157
220
 
@@ -203,12 +266,12 @@ describe('lib', function() {
203
266
  action: 'PUT'
204
267
  },
205
268
  attribute4: {
206
- value: { a: { N: '123' }, b: { S: 'STR' } },
269
+ value: { a: { N: '123' }, b: { S: 'STR' }, c: { S: 'true' } },
207
270
  type: 'M',
208
271
  action: 'PUT'
209
272
  },
210
273
  attribute5: {
211
- value: [ { M: { a: { N: '1' }, b: { S: 'S1' } } }, { M: { a: { N: '2' }, b: { S: 'S2' } } } ],
274
+ value: [ { M: { a: { N: '1' }, b: { S: 'S1' } } }, { M: { a: { N: '2' }, b: { S: 'S2' }, c: { S: 'true' } } } ],
212
275
  type: 'L',
213
276
  action: 'PUT'
214
277
  },
@@ -221,16 +284,91 @@ describe('lib', function() {
221
284
  value: true,
222
285
  type: 'NULL',
223
286
  action: 'PUT'
287
+ },
288
+ attribute8: {
289
+ value: 'true',
290
+ type: 'S',
291
+ action: 'PUT'
292
+ },
293
+ attribute9: {
294
+ value: [ { NULL: true }, { S: 's1' }, { S: 's2' } ],
295
+ type: 'L',
296
+ action: 'PUT'
224
297
  }
225
298
  };
226
299
  expect(this.jedlik
227
300
  .nullableAttribute('attribute1', 'STR', 'PUT')
228
301
  .nullableAttribute('attribute2', 1234)
229
302
  .nullableAttribute('attribute3', ['s1', 's2'])
230
- .nullableAttribute('attribute4', {a:123, b:'STR'})
231
- .nullableAttribute('attribute5', [{a:1, b:'S1'}, {a:2, b:'S2'}])
303
+ .nullableAttribute('attribute4', {a:123, b:'STR', c:true})
304
+ .nullableAttribute('attribute5', [{a:1, b:'S1'}, {a:2, b:'S2', c:true}])
232
305
  .nullableAttribute('attribute6', null)
233
306
  .nullableAttribute('attribute7', undefined)
307
+ .nullableAttribute('attribute8', true)
308
+ .nullableAttribute('attribute9', [null, 's1', 's2'])
309
+ ._data.attributes).to.deep.equal(expected);
310
+ });
311
+
312
+ it('should add nullable attribute property if boolean support', function() {
313
+ this.jedlik = new(this.lib)(true);
314
+
315
+ var expected = {
316
+ attribute1: {
317
+ value: 'STR',
318
+ type: 'S',
319
+ action: 'PUT'
320
+ },
321
+ attribute2: {
322
+ value: '1234',
323
+ type: 'N',
324
+ action: 'PUT'
325
+ },
326
+ attribute3: {
327
+ value: ['s1', 's2'],
328
+ type: 'SS',
329
+ action: 'PUT'
330
+ },
331
+ attribute4: {
332
+ value: { a: { N: '123' }, b: { S: 'STR' }, c: { BOOL: true } },
333
+ type: 'M',
334
+ action: 'PUT'
335
+ },
336
+ attribute5: {
337
+ value: [ { M: { a: { N: '1' }, b: { S: 'S1' } } }, { M: { a: { N: '2' }, b: { S: 'S2' }, c: { BOOL: true } } } ],
338
+ type: 'L',
339
+ action: 'PUT'
340
+ },
341
+ attribute6: {
342
+ value: true,
343
+ type: 'NULL',
344
+ action: 'PUT'
345
+ },
346
+ attribute7: {
347
+ value: true,
348
+ type: 'NULL',
349
+ action: 'PUT'
350
+ },
351
+ attribute8: {
352
+ value: true,
353
+ type: 'BOOL',
354
+ action: 'PUT'
355
+ },
356
+ attribute9: {
357
+ value: [ { NULL: true }, { S: 's1' }, { S: 's2' } ],
358
+ type: 'L',
359
+ action: 'PUT'
360
+ }
361
+ };
362
+ expect(this.jedlik
363
+ .nullableAttribute('attribute1', 'STR', 'PUT')
364
+ .nullableAttribute('attribute2', 1234)
365
+ .nullableAttribute('attribute3', ['s1', 's2'])
366
+ .nullableAttribute('attribute4', {a:123, b:'STR', c:true})
367
+ .nullableAttribute('attribute5', [{a:1, b:'S1'}, {a:2, b:'S2', c:true}])
368
+ .nullableAttribute('attribute6', null)
369
+ .nullableAttribute('attribute7', undefined)
370
+ .nullableAttribute('attribute8', true)
371
+ .nullableAttribute('attribute9', [null, 's1', 's2'])
234
372
  ._data.attributes).to.deep.equal(expected);
235
373
  });
236
374
  });
@@ -475,12 +613,19 @@ describe('lib', function() {
475
613
  });
476
614
  });
477
615
 
478
- it('should return a valid json for delete', function() {
616
+ it('should return a valid json for delete when only hashkey is used', function() {
617
+ expect(this.jedlik
618
+ .tablename('tablename')
619
+ .hashkey('hashkey', 'hashkeyvalue')
620
+ .del()).to.deep.equal(require('./fixtures/delete_hash'));
621
+ });
622
+
623
+ it('should return a valid json for delete when both hashkey and rangekey are used', function() {
479
624
  expect(this.jedlik
480
625
  .tablename('tablename')
481
626
  .hashkey('hashkey', 'hashkeyvalue')
482
627
  .rangekey('rangekey', 'rangekeyvalue')
483
- .del()).to.deep.equal(require('./fixtures/delete'));
628
+ .del()).to.deep.equal(require('./fixtures/delete_range'));
484
629
  });
485
630
 
486
631
  describe('getItem', function() {