pixl-server-storage 3.1.17 → 3.2.0
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/README.md +146 -134
- package/docs/API.md +149 -144
- package/docs/Hashes.md +20 -20
- package/docs/Indexer.md +11 -14
- package/docs/Lists.md +37 -37
- package/docs/Transactions.md +11 -11
- package/engines/Redis.js +39 -36
- package/engines/RedisCluster.js +10 -13
- package/package.json +1 -1
- package/transaction.js +1 -1
package/docs/API.md
CHANGED
|
@@ -2,8 +2,8 @@
|
|
|
2
2
|
|
|
3
3
|
Here are all the public methods you can call in the storage class. These examples all assume you have your preloaded `Storage` component instance in a local variable named `storage`. The component instance can be retrieved from a running server like this:
|
|
4
4
|
|
|
5
|
-
```
|
|
6
|
-
|
|
5
|
+
```js
|
|
6
|
+
let storage = server.Storage;
|
|
7
7
|
```
|
|
8
8
|
|
|
9
9
|
## Table of Contents
|
|
@@ -89,13 +89,13 @@ var storage = server.Storage;
|
|
|
89
89
|
|
|
90
90
|
## put
|
|
91
91
|
|
|
92
|
-
```
|
|
92
|
+
```js
|
|
93
93
|
storage.put( KEY, VALUE, CALLBACK );
|
|
94
94
|
```
|
|
95
95
|
|
|
96
96
|
The `put()` method stores a key/value pair. It will create the record if it doesn't exist, or replace it if it does. All keys should be strings. The value may be an object or a `Buffer` (for binary blobs). Objects are auto-serialized to JSON. Your callback function is passed an error if one occurred. Example:
|
|
97
97
|
|
|
98
|
-
```
|
|
98
|
+
```js
|
|
99
99
|
storage.put( 'test1', { foo: 'bar1' }, function(err) {
|
|
100
100
|
if (err) throw err;
|
|
101
101
|
} );
|
|
@@ -103,9 +103,10 @@ storage.put( 'test1', { foo: 'bar1' }, function(err) {
|
|
|
103
103
|
|
|
104
104
|
For binary values, the key *must* contain a file extension, e.g. `test1.gif`. Example:
|
|
105
105
|
|
|
106
|
-
```
|
|
107
|
-
|
|
108
|
-
|
|
106
|
+
```js
|
|
107
|
+
const fs = require('fs');
|
|
108
|
+
let buffer = fs.readFileSync('picture.gif');
|
|
109
|
+
|
|
109
110
|
storage.put( 'test1.gif', buffer, function(err) {
|
|
110
111
|
if (err) throw err;
|
|
111
112
|
} );
|
|
@@ -113,18 +114,19 @@ storage.put( 'test1.gif', buffer, function(err) {
|
|
|
113
114
|
|
|
114
115
|
## putMulti
|
|
115
116
|
|
|
116
|
-
```
|
|
117
|
+
```js
|
|
117
118
|
storage.putMulti( RECORDS, CALLBACK );
|
|
118
119
|
```
|
|
119
120
|
|
|
120
121
|
The `putMulti()` method stores multiple keys/values at once, from a specified object containing both. Depending on your storage [concurrency](../README.md#concurrency) configuration, this may be significantly faster than storing the records in sequence. Example:
|
|
121
122
|
|
|
122
|
-
```
|
|
123
|
-
|
|
123
|
+
```js
|
|
124
|
+
let records = {
|
|
124
125
|
multi1: { fruit: 'apple' },
|
|
125
126
|
multi2: { fruit: 'orange' },
|
|
126
127
|
multi3: { fruit: 'banana' }
|
|
127
128
|
};
|
|
129
|
+
|
|
128
130
|
storage.putMulti( records, function(err) {
|
|
129
131
|
if (err) throw err;
|
|
130
132
|
} );
|
|
@@ -134,15 +136,16 @@ Note that if any of the individual put operations fail, the entire `putMulti()`
|
|
|
134
136
|
|
|
135
137
|
## putStream
|
|
136
138
|
|
|
137
|
-
```
|
|
139
|
+
```js
|
|
138
140
|
storage.putStream( KEY, STREAM, CALLBACK );
|
|
139
141
|
```
|
|
140
142
|
|
|
141
143
|
The `putStream()` method stores a record using a [readable stream](https://nodejs.org/api/stream.html#stream_class_stream_readable), so it doesn't have to be read into memory. This can be used to spool very large files to storage without using any RAM. Note that this is treated as a binary record, so the key *must* contain a file extension, e.g. `test1.gif`. Example:
|
|
142
144
|
|
|
143
|
-
```
|
|
144
|
-
|
|
145
|
-
|
|
145
|
+
```js
|
|
146
|
+
const fs = require('fs');
|
|
147
|
+
let stream = fs.createReadStream('picture.gif');
|
|
148
|
+
|
|
146
149
|
storage.putStream( 'test1.gif', stream, function(err) {
|
|
147
150
|
if (err) throw err;
|
|
148
151
|
} );
|
|
@@ -152,13 +155,13 @@ Please note that as of this writing, the `Couchbase`, `Redis` and `RedisCluster`
|
|
|
152
155
|
|
|
153
156
|
## get
|
|
154
157
|
|
|
155
|
-
```
|
|
158
|
+
```js
|
|
156
159
|
storage.get( KEY, CALLBACK );
|
|
157
160
|
```
|
|
158
161
|
|
|
159
162
|
The `get()` method fetches a value given a key. If the record is an object, it will be returned as such. Or, if the record is a binary blob, a `Buffer` object will be returned. Your callback function is passed an error if one occurred, and the data value for the given record. Example:
|
|
160
163
|
|
|
161
|
-
```
|
|
164
|
+
```js
|
|
162
165
|
storage.get( 'test1', function(err, data) {
|
|
163
166
|
if (err) throw err;
|
|
164
167
|
} );
|
|
@@ -166,13 +169,13 @@ storage.get( 'test1', function(err, data) {
|
|
|
166
169
|
|
|
167
170
|
## getMulti
|
|
168
171
|
|
|
169
|
-
```
|
|
172
|
+
```js
|
|
170
173
|
storage.getMulti( KEYS, CALLBACK );
|
|
171
174
|
```
|
|
172
175
|
|
|
173
176
|
The `getMulti()` method fetches multiple values at once, from a specified array of keys. Depending on your storage [concurrency](../README.md#concurrency) configuration, this may be significantly faster than fetching the records in sequence. Your callback function is passed an array of values which correspond to the specified keys. Example:
|
|
174
177
|
|
|
175
|
-
```
|
|
178
|
+
```js
|
|
176
179
|
storage.getMulti( ['test1', 'test2', 'test3'], function(err, values) {
|
|
177
180
|
if (err) throw err;
|
|
178
181
|
// values[0] will be the test1 record.
|
|
@@ -185,13 +188,13 @@ Note that if *any* of the records fail, the entire operation fails, and the firs
|
|
|
185
188
|
|
|
186
189
|
## getBuffer
|
|
187
190
|
|
|
188
|
-
```
|
|
191
|
+
```js
|
|
189
192
|
storage.getBuffer( KEY, CALLBACK );
|
|
190
193
|
```
|
|
191
194
|
|
|
192
195
|
The `getBuffer()` method retrieves a [Buffer](https://nodejs.org/api/buffer.html) to a given record's data, regardless if the key points to a JSON record or a binary record. Your callback function is passed an error if one occurred, and the buffer value for the given record. Example:
|
|
193
196
|
|
|
194
|
-
```
|
|
197
|
+
```js
|
|
195
198
|
storage.getBuffer( 'test1', function(err, buf) {
|
|
196
199
|
if (err) throw err;
|
|
197
200
|
} );
|
|
@@ -199,18 +202,19 @@ storage.getBuffer( 'test1', function(err, buf) {
|
|
|
199
202
|
|
|
200
203
|
## getStream
|
|
201
204
|
|
|
202
|
-
```
|
|
205
|
+
```js
|
|
203
206
|
storage.getStream( KEY, CALLBACK );
|
|
204
207
|
```
|
|
205
208
|
|
|
206
209
|
The `getStream()` method retrieves a [readable stream](https://nodejs.org/api/stream.html#readable-streams) to a given record's data, so it can be read or piped to a writable stream. This is for very large records, so nothing is loaded into memory. Example of spooling to a local file:
|
|
207
210
|
|
|
208
|
-
```
|
|
209
|
-
|
|
210
|
-
|
|
211
|
+
```js
|
|
212
|
+
const fs = require('fs');
|
|
213
|
+
let writeStream = fs.createWriteStream('/let/tmp/downloaded.gif');
|
|
211
214
|
|
|
212
215
|
storage.getStream( 'test1.gif', function(err, readStream, info) {
|
|
213
216
|
if (err) throw err;
|
|
217
|
+
|
|
214
218
|
writeStream.on('finish', function() {
|
|
215
219
|
// data is completely written
|
|
216
220
|
} );
|
|
@@ -224,18 +228,19 @@ Please note that as of this writing, the `Couchbase`, `Redis` and `RedisCluster`
|
|
|
224
228
|
|
|
225
229
|
## getStreamRange
|
|
226
230
|
|
|
227
|
-
```
|
|
231
|
+
```js
|
|
228
232
|
storage.getStreamRange( KEY, START, END, CALLBACK );
|
|
229
233
|
```
|
|
230
234
|
|
|
231
235
|
The `getStreamRange()` method retrieves a [readable stream](https://nodejs.org/api/stream.html#class-streamreadable) to a specific slice of a record's data, so it can be read or piped to a writable stream. The `start` and `end` arguments should be set to the starting and ending byte offset of the slice you want. Both values are *inclusive*. Example of spooling bytes `0-99` of a record to a local file:
|
|
232
236
|
|
|
233
|
-
```
|
|
234
|
-
|
|
235
|
-
|
|
237
|
+
```js
|
|
238
|
+
const fs = require('fs');
|
|
239
|
+
let writeStream = fs.createWriteStream('/let/tmp/downloaded.gif');
|
|
236
240
|
|
|
237
241
|
storage.getStreamRange( 'test1.gif', 0, 99, function(err, readStream, info) {
|
|
238
242
|
if (err) throw err;
|
|
243
|
+
|
|
239
244
|
writeStream.on('finish', function() {
|
|
240
245
|
// data is completely written
|
|
241
246
|
} );
|
|
@@ -254,7 +259,7 @@ Please note that as of this writing, the `Couchbase`, `Redis` and `RedisCluster`
|
|
|
254
259
|
|
|
255
260
|
## head
|
|
256
261
|
|
|
257
|
-
```
|
|
262
|
+
```js
|
|
258
263
|
storage.head( KEY, CALLBACK );
|
|
259
264
|
```
|
|
260
265
|
|
|
@@ -267,7 +272,7 @@ The `head()` method fetches metadata about an object given a key, without fetchi
|
|
|
267
272
|
|
|
268
273
|
Example:
|
|
269
274
|
|
|
270
|
-
```
|
|
275
|
+
```js
|
|
271
276
|
storage.head( 'test1', function(err, data) {
|
|
272
277
|
if (err) throw err;
|
|
273
278
|
// data.mod
|
|
@@ -279,13 +284,13 @@ Please note that as of this writing, the `Couchbase`, `Redis` and `RedisCluster`
|
|
|
279
284
|
|
|
280
285
|
## headMulti
|
|
281
286
|
|
|
282
|
-
```
|
|
287
|
+
```js
|
|
283
288
|
storage.headMulti( KEYS, CALLBACK );
|
|
284
289
|
```
|
|
285
290
|
|
|
286
291
|
The `headMulti()` method pings multiple records at once, from a specified array of keys. Depending on your storage [concurrency](../README.md#concurrency) configuration, this may be significantly faster than pinging the records in sequence. Your callback function is passed an array of values which correspond to the specified keys. Example:
|
|
287
292
|
|
|
288
|
-
```
|
|
293
|
+
```js
|
|
289
294
|
storage.headMulti( ['test1', 'test2', 'test3'], function(err, values) {
|
|
290
295
|
if (err) throw err;
|
|
291
296
|
// values[0] will be the test1 head info.
|
|
@@ -296,13 +301,13 @@ storage.headMulti( ['test1', 'test2', 'test3'], function(err, values) {
|
|
|
296
301
|
|
|
297
302
|
## delete
|
|
298
303
|
|
|
299
|
-
```
|
|
304
|
+
```js
|
|
300
305
|
storage.delete( KEY, CALLBACK );
|
|
301
306
|
```
|
|
302
307
|
|
|
303
308
|
The `delete()` method deletes an object given a key. Your callback function is passed an error if one occurred, otherwise it'll be falsey. Example:
|
|
304
309
|
|
|
305
|
-
```
|
|
310
|
+
```js
|
|
306
311
|
storage.delete( 'test1', function(err) {
|
|
307
312
|
if (err) throw err;
|
|
308
313
|
} );
|
|
@@ -310,13 +315,13 @@ storage.delete( 'test1', function(err) {
|
|
|
310
315
|
|
|
311
316
|
## deleteMulti
|
|
312
317
|
|
|
313
|
-
```
|
|
318
|
+
```js
|
|
314
319
|
storage.deleteMulti( KEYS, CALLBACK );
|
|
315
320
|
```
|
|
316
321
|
|
|
317
322
|
The `deleteMulti()` method deletes multiple records at once, from a specified array of keys. Depending on your storage [concurrency](../README.md#concurrency) configuration, this may be significantly faster than deleting the records in sequence. Example:
|
|
318
323
|
|
|
319
|
-
```
|
|
324
|
+
```js
|
|
320
325
|
storage.deleteMulti( ['test1', 'test2', 'test3'], function(err) {
|
|
321
326
|
if (err) throw err;
|
|
322
327
|
} );
|
|
@@ -324,13 +329,13 @@ storage.deleteMulti( ['test1', 'test2', 'test3'], function(err) {
|
|
|
324
329
|
|
|
325
330
|
## copy
|
|
326
331
|
|
|
327
|
-
```
|
|
332
|
+
```js
|
|
328
333
|
storage.copy( OLD_KEY, NEW_KEY, CALLBACK );
|
|
329
334
|
```
|
|
330
335
|
|
|
331
336
|
The `copy()` method copies a value from one key and stores it at another. If the destination record doesn't exist it is created, otherwise it is replaced. Your callback function is passed an error if one occurred, otherwise it'll be falsey. Example:
|
|
332
337
|
|
|
333
|
-
```
|
|
338
|
+
```js
|
|
334
339
|
storage.copy( 'test1', 'test2', function(err) {
|
|
335
340
|
if (err) throw err;
|
|
336
341
|
} );
|
|
@@ -340,13 +345,13 @@ storage.copy( 'test1', 'test2', function(err) {
|
|
|
340
345
|
|
|
341
346
|
## rename
|
|
342
347
|
|
|
343
|
-
```
|
|
348
|
+
```js
|
|
344
349
|
storage.rename( OLD_KEY, NEW_KEY, CALLBACK );
|
|
345
350
|
```
|
|
346
351
|
|
|
347
352
|
The `rename()` method copies a value from one key, stores it at another, and deletes the original key. If the destination record doesn't exist it is created, otherwise it is replaced. Your callback function is passed an error if one occurred, otherwise it'll be falsey. Example:
|
|
348
353
|
|
|
349
|
-
```
|
|
354
|
+
```js
|
|
350
355
|
storage.rename( 'test1', 'test2', function(err) {
|
|
351
356
|
if (err) throw err;
|
|
352
357
|
} );
|
|
@@ -356,7 +361,7 @@ storage.rename( 'test1', 'test2', function(err) {
|
|
|
356
361
|
|
|
357
362
|
## lock
|
|
358
363
|
|
|
359
|
-
```
|
|
364
|
+
```js
|
|
360
365
|
storage.lock( KEY, WAIT, CALLBACK );
|
|
361
366
|
```
|
|
362
367
|
|
|
@@ -366,7 +371,7 @@ If you pass `true` for the wait argument and the specified record is already loc
|
|
|
366
371
|
|
|
367
372
|
## unlock
|
|
368
373
|
|
|
369
|
-
```
|
|
374
|
+
```js
|
|
370
375
|
storage.unlock( KEY );
|
|
371
376
|
```
|
|
372
377
|
|
|
@@ -374,7 +379,7 @@ The `unlock()` method releases an exclusive lock on a particular record, specifi
|
|
|
374
379
|
|
|
375
380
|
## shareLock
|
|
376
381
|
|
|
377
|
-
```
|
|
382
|
+
```js
|
|
378
383
|
storage.shareLock( KEY, WAIT, CALLBACK );
|
|
379
384
|
```
|
|
380
385
|
|
|
@@ -384,7 +389,7 @@ If you pass `true` for the wait argument and the specified record is already loc
|
|
|
384
389
|
|
|
385
390
|
## shareUnlock
|
|
386
391
|
|
|
387
|
-
```
|
|
392
|
+
```js
|
|
388
393
|
storage.shareUnlock( KEY );
|
|
389
394
|
```
|
|
390
395
|
|
|
@@ -392,14 +397,14 @@ The `shareUnlock()` method releases a shared lock on a particular record, specif
|
|
|
392
397
|
|
|
393
398
|
## expire
|
|
394
399
|
|
|
395
|
-
```
|
|
400
|
+
```js
|
|
396
401
|
storage.expire( KEY, DATE );
|
|
397
402
|
```
|
|
398
403
|
|
|
399
404
|
The `expire()` method sets an expiration date on a record given its key. The date can be any string, Epoch seconds or `Date` object. The daily maintenance system will automatically deleted all expired records when it runs (assuming it is enabled -- see [Daily Maintenance](../README.md#daily-maintenance)). Example:
|
|
400
405
|
|
|
401
|
-
```
|
|
402
|
-
|
|
406
|
+
```js
|
|
407
|
+
let exp_date = ((new Date()).getTime() / 1000) + 86400; // tomorrow
|
|
403
408
|
storage.expire( 'test1', exp_date );
|
|
404
409
|
```
|
|
405
410
|
|
|
@@ -433,7 +438,7 @@ storage.getStats();
|
|
|
433
438
|
|
|
434
439
|
The `getStats()` method returns information about current system performance, including min/avg/max metrics for the last second and minute. It takes no arguments, and returns an object containing the following:
|
|
435
440
|
|
|
436
|
-
```
|
|
441
|
+
```json
|
|
437
442
|
{
|
|
438
443
|
"version": "2.0.0",
|
|
439
444
|
"engine": "Filesystem",
|
|
@@ -467,13 +472,13 @@ For details on these stats, see [Performance Metrics](../README.md#performance-m
|
|
|
467
472
|
|
|
468
473
|
## listCreate
|
|
469
474
|
|
|
470
|
-
```
|
|
475
|
+
```js
|
|
471
476
|
storage.listCreate( KEY, OPTIONS, CALLBACK );
|
|
472
477
|
```
|
|
473
478
|
|
|
474
479
|
The `listCreate()` method creates a new, empty list. Specify the desired key, options (see below) and a callback function. Your callback function is passed an error if one occurred, otherwise it'll be falsey. Example:
|
|
475
480
|
|
|
476
|
-
```
|
|
481
|
+
```js
|
|
477
482
|
storage.listCreate( 'list1', {}, function(err) {
|
|
478
483
|
if (err) throw err;
|
|
479
484
|
} );
|
|
@@ -481,7 +486,7 @@ storage.listCreate( 'list1', {}, function(err) {
|
|
|
481
486
|
|
|
482
487
|
Unless otherwise specified, the list will be created with the default [page size](../README.md#list_page_size) (number of items per page). However, you can override this in the options object by passing a `page_size` property:
|
|
483
488
|
|
|
484
|
-
```
|
|
489
|
+
```js
|
|
485
490
|
storage.listCreate( 'list1', { page_size: 100 }, function(err) {
|
|
486
491
|
if (err) throw err;
|
|
487
492
|
} );
|
|
@@ -489,13 +494,13 @@ storage.listCreate( 'list1', { page_size: 100 }, function(err) {
|
|
|
489
494
|
|
|
490
495
|
## listPush
|
|
491
496
|
|
|
492
|
-
```
|
|
497
|
+
```js
|
|
493
498
|
storage.listPush( KEY, ITEMS, [OPTIONS], CALLBACK );
|
|
494
499
|
```
|
|
495
500
|
|
|
496
501
|
Similar to the standard [Array.push()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/push), the `listPush()` method pushes one or more items onto the end of a list. The list will be created if it doesn't exist, using the default [page size](../README.md#list_page_size). `ITEMS` can be a single object, or an array of objects. Your callback function is passed an error if one occurred, otherwise it'll be falsey. Example:
|
|
497
502
|
|
|
498
|
-
```
|
|
503
|
+
```js
|
|
499
504
|
storage.listPush( 'list1', { username: 'jhuckaby', age: 38 }, function(err) {
|
|
500
505
|
if (err) throw err;
|
|
501
506
|
} );
|
|
@@ -503,7 +508,7 @@ storage.listPush( 'list1', { username: 'jhuckaby', age: 38 }, function(err) {
|
|
|
503
508
|
|
|
504
509
|
If the list doesn't exist, `listPush()` will create it. If you specify an `OPTIONS` object, this will be used in the creation of the list, i.e. to specify the [page size](../README.md#list_page_size), and add any custom params you want.
|
|
505
510
|
|
|
506
|
-
```
|
|
511
|
+
```js
|
|
507
512
|
storage.listPush( 'list1', { username: 'jhuckaby', age: 38 }, { page_size: 100 }, function(err) {
|
|
508
513
|
if (err) throw err;
|
|
509
514
|
} );
|
|
@@ -511,13 +516,13 @@ storage.listPush( 'list1', { username: 'jhuckaby', age: 38 }, { page_size: 100 }
|
|
|
511
516
|
|
|
512
517
|
## listUnshift
|
|
513
518
|
|
|
514
|
-
```
|
|
519
|
+
```js
|
|
515
520
|
storage.listUnshift( KEY, ITEMS, CALLBACK );
|
|
516
521
|
```
|
|
517
522
|
|
|
518
523
|
Similar to the standard [Array.unshift()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/unshift), the `listUnshift()` method unshifts one or more items onto the beginning of a list. The list will be created if it doesn't exist, using the default [page size](../README.md#list_page_size). `ITEMS` can be a single object, or an array of objects. Your callback function is passed an error if one occurred, otherwise it'll be falsey. Example:
|
|
519
524
|
|
|
520
|
-
```
|
|
525
|
+
```js
|
|
521
526
|
storage.listUnshift( 'list1', { username: 'jhuckaby', age: 38 }, function(err) {
|
|
522
527
|
if (err) throw err;
|
|
523
528
|
} );
|
|
@@ -525,7 +530,7 @@ storage.listUnshift( 'list1', { username: 'jhuckaby', age: 38 }, function(err) {
|
|
|
525
530
|
|
|
526
531
|
If the list doesn't exist, `listUnshift()` will create it. If you specify an `OPTIONS` object, this will be used in the creation of the list, i.e. to specify the [page size](../README.md#list_page_size), and add any custom params you want.
|
|
527
532
|
|
|
528
|
-
```
|
|
533
|
+
```js
|
|
529
534
|
storage.listUnshift( 'list1', { username: 'jhuckaby', age: 38 }, { page_size: 100 }, function(err) {
|
|
530
535
|
if (err) throw err;
|
|
531
536
|
} );
|
|
@@ -533,13 +538,13 @@ storage.listUnshift( 'list1', { username: 'jhuckaby', age: 38 }, { page_size: 10
|
|
|
533
538
|
|
|
534
539
|
## listPop
|
|
535
540
|
|
|
536
|
-
```
|
|
541
|
+
```js
|
|
537
542
|
storage.listPop( KEY, CALLBACK );
|
|
538
543
|
```
|
|
539
544
|
|
|
540
545
|
Similar to the standard [Array.pop()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/pop), the `listPop()` method pops one single item off the end of a list, and returns it. Your callback function is passed an error if one occurred, otherwise it'll be falsey. The second argument will be the popped item, if successful. Example:
|
|
541
546
|
|
|
542
|
-
```
|
|
547
|
+
```js
|
|
543
548
|
storage.listPop( 'list1', function(err, item) {
|
|
544
549
|
if (err) throw err;
|
|
545
550
|
} );
|
|
@@ -549,13 +554,13 @@ If the list is empty, an error is not generated, but the item will be `null`.
|
|
|
549
554
|
|
|
550
555
|
## listShift
|
|
551
556
|
|
|
552
|
-
```
|
|
557
|
+
```js
|
|
553
558
|
storage.listShift( KEY, CALLBACK );
|
|
554
559
|
```
|
|
555
560
|
|
|
556
561
|
Similar to the standard [Array.shift()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/shift), the `listShift()` method shifts one single item off the beginning of a list, and returns it. Your callback function is passed an error if one occurred, otherwise it'll be falsey. The second argument will be the shifted item, if successful. Example:
|
|
557
562
|
|
|
558
|
-
```
|
|
563
|
+
```js
|
|
559
564
|
storage.listShift( 'list1', function(err, item) {
|
|
560
565
|
if (err) throw err;
|
|
561
566
|
} );
|
|
@@ -565,13 +570,13 @@ If the list is empty, an error is not generated, but the item will be `null`.
|
|
|
565
570
|
|
|
566
571
|
## listGet
|
|
567
572
|
|
|
568
|
-
```
|
|
573
|
+
```js
|
|
569
574
|
storage.listGet( KEY, INDEX, LENGTH, CALLBACK );
|
|
570
575
|
```
|
|
571
576
|
|
|
572
577
|
The `listGet()` method fetches one or more items from a list, given the key, the starting index number (zero-based), the number of items to fetch (defaults to the entire list), and a callback. Your callback function is passed an error if one occurred, otherwise it'll be falsey. The second argument will be an array of the fetched items, if successful. Example:
|
|
573
578
|
|
|
574
|
-
```
|
|
579
|
+
```js
|
|
575
580
|
storage.listGet( 'list1', 40, 5, function(err, items) {
|
|
576
581
|
if (err) throw err;
|
|
577
582
|
} );
|
|
@@ -585,7 +590,7 @@ Your callback function is also passed the list info object as a 3rd argument, in
|
|
|
585
590
|
|
|
586
591
|
## listSplice
|
|
587
592
|
|
|
588
|
-
```
|
|
593
|
+
```js
|
|
589
594
|
storage.listSplice( KEY, INDEX, LENGTH, NEW_ITEMS, CALLBACK );
|
|
590
595
|
```
|
|
591
596
|
|
|
@@ -593,7 +598,7 @@ Similar to the standard [Array.splice()](https://developer.mozilla.org/en-US/doc
|
|
|
593
598
|
|
|
594
599
|
Your callback function is passed an error if one occurred, otherwise it'll be falsey. The second argument will be an array of the removed items, if successful. Example:
|
|
595
600
|
|
|
596
|
-
```
|
|
601
|
+
```js
|
|
597
602
|
storage.listSplice( 'list1', 40, 5, [], function(err, items) {
|
|
598
603
|
if (err) throw err;
|
|
599
604
|
} );
|
|
@@ -603,7 +608,7 @@ This example would remove 5 items starting at item index 40, and replace with no
|
|
|
603
608
|
|
|
604
609
|
## listFind
|
|
605
610
|
|
|
606
|
-
```
|
|
611
|
+
```js
|
|
607
612
|
storage.listFind( KEY, CRITERIA, CALLBACK );
|
|
608
613
|
```
|
|
609
614
|
|
|
@@ -611,7 +616,7 @@ The `listFind()` method will search a list for a particular item, based on a cri
|
|
|
611
616
|
|
|
612
617
|
Your callback function is passed an error if one occurred, otherwise it'll be falsey. If an item was found matching your criteria, the second argument will be the item itself, and the 3rd argument will be the item's index number (zero-based). Example:
|
|
613
618
|
|
|
614
|
-
```
|
|
619
|
+
```js
|
|
615
620
|
storage.listFind( 'list1', { username: 'jhuckaby' }, function(err, item, idx) {
|
|
616
621
|
if (err) throw err;
|
|
617
622
|
} );
|
|
@@ -621,13 +626,13 @@ If an item is not found, no error is generated. However, the `item` will be nul
|
|
|
621
626
|
|
|
622
627
|
## listFindCut
|
|
623
628
|
|
|
624
|
-
```
|
|
629
|
+
```js
|
|
625
630
|
storage.listFindCut( KEY, CRITERIA, CALLBACK );
|
|
626
631
|
```
|
|
627
632
|
|
|
628
633
|
The `listFindCut()` method will search a list for a particular item based on a criteria object, and if found, it'll delete it (remove it from the list using [listSplice()](#listsplice)). Your callback function is passed an error if one occurred, otherwise it'll be falsey. If an item was found matching your criteria, the second argument will be the item itself. Example:
|
|
629
634
|
|
|
630
|
-
```
|
|
635
|
+
```js
|
|
631
636
|
storage.listFindCut( 'list1', { username: 'jhuckaby' }, function(err, item) {
|
|
632
637
|
if (err) throw err;
|
|
633
638
|
} );
|
|
@@ -635,15 +640,15 @@ storage.listFindCut( 'list1', { username: 'jhuckaby' }, function(err, item) {
|
|
|
635
640
|
|
|
636
641
|
## listFindReplace
|
|
637
642
|
|
|
638
|
-
```
|
|
643
|
+
```js
|
|
639
644
|
storage.listFindReplace( KEY, CRITERIA, NEW_ITEM, CALLBACK );
|
|
640
645
|
```
|
|
641
646
|
|
|
642
647
|
The `listFindReplace()` method will search a list for a particular item based on a criteria object, and if found, it'll replace it with the specified item. Your callback function is passed an error if one occurred, otherwise it'll be falsey. Example:
|
|
643
648
|
|
|
644
|
-
```
|
|
645
|
-
|
|
646
|
-
|
|
649
|
+
```js
|
|
650
|
+
let criteria = { username: 'jhuckaby' };
|
|
651
|
+
let new_item = { username: 'huckabyj', foo: 'bar' };
|
|
647
652
|
|
|
648
653
|
storage.listFindReplace( 'list1', criteria, new_item, function(err) {
|
|
649
654
|
if (err) throw err;
|
|
@@ -652,15 +657,15 @@ storage.listFindReplace( 'list1', criteria, new_item, function(err) {
|
|
|
652
657
|
|
|
653
658
|
## listFindUpdate
|
|
654
659
|
|
|
655
|
-
```
|
|
660
|
+
```js
|
|
656
661
|
storage.listFindUpdate( KEY, CRITERIA, UPDATES, CALLBACK );
|
|
657
662
|
```
|
|
658
663
|
|
|
659
664
|
The `listFindUpdate()` method will search a list for a particular item based on a criteria object, and if found, it'll "update" it with the keys/values specified. Meaning, they are merged in with the existing item, adding new keys or replacing existing ones. Your callback function is passed an error if one occurred, otherwise it'll be falsey. If an item was found matching your criteria, the second argument will be the item itself, with all the updates applied. Example:
|
|
660
665
|
|
|
661
|
-
```
|
|
662
|
-
|
|
663
|
-
|
|
666
|
+
```js
|
|
667
|
+
let criteria = { username: 'jhuckaby' };
|
|
668
|
+
let updates = { gender: 'male', age: 38 };
|
|
664
669
|
|
|
665
670
|
storage.listFindUpdate( 'list1', criteria, updates, function(err, item) {
|
|
666
671
|
if (err) throw err;
|
|
@@ -669,9 +674,9 @@ storage.listFindUpdate( 'list1', criteria, updates, function(err, item) {
|
|
|
669
674
|
|
|
670
675
|
You can also increment or decrement numerical properties with this function. If an item key exists and is a number, you can set any update key to a string prefixed with `+` (increment) or `-` (decrement), followed by the delta number (int or float), e.g. `+1`. So for example, imagine a list of users, and an item property such as `number_of_logins`. When a user logs in again, you could increment this counter like this:
|
|
671
676
|
|
|
672
|
-
```
|
|
673
|
-
|
|
674
|
-
|
|
677
|
+
```js
|
|
678
|
+
let criteria = { username: 'jhuckaby' };
|
|
679
|
+
let updates = { number_of_logins: "+1" };
|
|
675
680
|
|
|
676
681
|
storage.listFindUpdate( 'list1', criteria, updates, function(err, item) {
|
|
677
682
|
if (err) throw err;
|
|
@@ -680,7 +685,7 @@ storage.listFindUpdate( 'list1', criteria, updates, function(err, item) {
|
|
|
680
685
|
|
|
681
686
|
## listFindEach
|
|
682
687
|
|
|
683
|
-
```
|
|
688
|
+
```js
|
|
684
689
|
storage.listFindEach( KEY, CRITERIA, ITERATOR, CALLBACK );
|
|
685
690
|
```
|
|
686
691
|
|
|
@@ -690,7 +695,7 @@ Your `ITERATOR` function is passed the item, the item index number, and a specia
|
|
|
690
695
|
|
|
691
696
|
Your `CALLBACK` function is called when the loop is complete and all items were iterated over, or an error occurred somewhere in the middle. It is passed an error object, or something falsey for success. Example:
|
|
692
697
|
|
|
693
|
-
```
|
|
698
|
+
```js
|
|
694
699
|
storage.listFindEach( 'list1', { username: 'jhuckaby' }, function(item, idx, callback) {
|
|
695
700
|
// do something with item, then fire callback
|
|
696
701
|
callback();
|
|
@@ -703,20 +708,20 @@ function(err) {
|
|
|
703
708
|
|
|
704
709
|
## listInsertSorted
|
|
705
710
|
|
|
706
|
-
```
|
|
711
|
+
```js
|
|
707
712
|
storage.listInsertSorted( KEY, ITEM, COMPARATOR, CALLBACK );
|
|
708
713
|
```
|
|
709
714
|
|
|
710
715
|
The `listInsertSorted()` method inserts an item into a list, while keeping it sorted. It doesn't resort the entire list every time, but rather it locates the correct position to insert the one item, based on sorting criteria, then performs a splice to insert it into place. Example:
|
|
711
716
|
|
|
712
|
-
```
|
|
713
|
-
|
|
717
|
+
```js
|
|
718
|
+
let new_user = {
|
|
714
719
|
username: 'jhuckaby',
|
|
715
720
|
age: 38,
|
|
716
721
|
gender: 'male'
|
|
717
722
|
};
|
|
718
723
|
|
|
719
|
-
|
|
724
|
+
let comparator = function(a, b) {
|
|
720
725
|
return( (a.username < b.username) ? -1 : 1 );
|
|
721
726
|
};
|
|
722
727
|
|
|
@@ -728,7 +733,7 @@ storage.listInsertSorted( 'users', new_user, comparator, function(err) {
|
|
|
728
733
|
|
|
729
734
|
If your sorting criteria is simple, i.e. a single top level property sorted ascending or descending, you can specify an array containing the key to sort by, and a direction (`1` for ascending, `-1` for descending), instead of a comparator function. Example:
|
|
730
735
|
|
|
731
|
-
```
|
|
736
|
+
```js
|
|
732
737
|
storage.listInsertSorted( 'users', new_user, ['username', 1], function(err) {
|
|
733
738
|
if (err) throw err;
|
|
734
739
|
} );
|
|
@@ -736,13 +741,13 @@ storage.listInsertSorted( 'users', new_user, ['username', 1], function(err) {
|
|
|
736
741
|
|
|
737
742
|
## listCopy
|
|
738
743
|
|
|
739
|
-
```
|
|
744
|
+
```js
|
|
740
745
|
storage.listCopy( OLD_KEY, NEW_KEY, CALLBACK );
|
|
741
746
|
```
|
|
742
747
|
|
|
743
748
|
The `listCopy()` method copies a list and all its items to a new key. Specify the existing list key, a new key, and a callback. If anything exists at the destination key, it is clobbered. Your callback function is passed an error if one occurred, otherwise it'll be falsey. Example:
|
|
744
749
|
|
|
745
|
-
```
|
|
750
|
+
```js
|
|
746
751
|
storage.listCopy( 'list1', 'list2', function(err) {
|
|
747
752
|
if (err) throw err;
|
|
748
753
|
} );
|
|
@@ -752,13 +757,13 @@ If a list already exists at the destination key, you should delete it first. It
|
|
|
752
757
|
|
|
753
758
|
## listRename
|
|
754
759
|
|
|
755
|
-
```
|
|
760
|
+
```js
|
|
756
761
|
storage.listRename( OLD_KEY, NEW_KEY, CALLBACK );
|
|
757
762
|
```
|
|
758
763
|
|
|
759
764
|
The `listRename()` method renames (moves) a list and all its items to a new key. Specify the existing list key, a new key, and a callback. If anything exists at the destination key, it is clobbered. Your callback function is passed an error if one occurred, otherwise it'll be falsey. Example:
|
|
760
765
|
|
|
761
|
-
```
|
|
766
|
+
```js
|
|
762
767
|
storage.listRename( 'list1', 'list2', function(err) {
|
|
763
768
|
if (err) throw err;
|
|
764
769
|
} );
|
|
@@ -768,7 +773,7 @@ If a list already exists at the destination key, you should delete it first. It
|
|
|
768
773
|
|
|
769
774
|
## listEach
|
|
770
775
|
|
|
771
|
-
```
|
|
776
|
+
```js
|
|
772
777
|
storage.listEach( KEY, ITERATOR, CALLBACK );
|
|
773
778
|
```
|
|
774
779
|
|
|
@@ -778,7 +783,7 @@ Your iterator function is passed the item, the item index number, and a special
|
|
|
778
783
|
|
|
779
784
|
Your `CALLBACK` function is finally called when the loop is complete and all items were iterated over, or an error occurred somewhere in the middle. It is passed an error object, or something falsey for success. Example:
|
|
780
785
|
|
|
781
|
-
```
|
|
786
|
+
```js
|
|
782
787
|
storage.listEach( 'list1', function(item, idx, callback) {
|
|
783
788
|
// do something with item, then fire callback
|
|
784
789
|
callback();
|
|
@@ -791,7 +796,7 @@ function(err) {
|
|
|
791
796
|
|
|
792
797
|
## listEachPage
|
|
793
798
|
|
|
794
|
-
```
|
|
799
|
+
```js
|
|
795
800
|
storage.listEachPage( KEY, ITERATOR, CALLBACK );
|
|
796
801
|
```
|
|
797
802
|
|
|
@@ -801,7 +806,7 @@ Your iterator function is passed each page's items as an array, and a special ca
|
|
|
801
806
|
|
|
802
807
|
Your `CALLBACK` function is finally called when the loop is complete and all items were iterated over, or an error occurred somewhere in the middle. It is passed an error object, or something falsey for success. Example:
|
|
803
808
|
|
|
804
|
-
```
|
|
809
|
+
```js
|
|
805
810
|
storage.listEachPage( 'list1', function(items, callback) {
|
|
806
811
|
// do something with items, then fire callback
|
|
807
812
|
callback();
|
|
@@ -814,7 +819,7 @@ function(err) {
|
|
|
814
819
|
|
|
815
820
|
## listEachUpdate
|
|
816
821
|
|
|
817
|
-
```
|
|
822
|
+
```js
|
|
818
823
|
storage.listEachUpdate( KEY, ITERATOR, CALLBACK );
|
|
819
824
|
```
|
|
820
825
|
|
|
@@ -824,7 +829,7 @@ Your iterator function is passed the item, the item index number, and a special
|
|
|
824
829
|
|
|
825
830
|
Your `CALLBACK` function is finally called when the loop is complete and all items were iterated over, or an error occurred somewhere in the middle. It is passed an error object, or something falsey for success. Example:
|
|
826
831
|
|
|
827
|
-
```
|
|
832
|
+
```js
|
|
828
833
|
storage.listEachUpdate( 'list1', function(item, idx, callback) {
|
|
829
834
|
// do something with item, then fire callback
|
|
830
835
|
item.something = "something new!";
|
|
@@ -838,7 +843,7 @@ function(err) {
|
|
|
838
843
|
|
|
839
844
|
## listEachPageUpdate
|
|
840
845
|
|
|
841
|
-
```
|
|
846
|
+
```js
|
|
842
847
|
storage.listEachPageUpdate( KEY, ITERATOR, CALLBACK );
|
|
843
848
|
```
|
|
844
849
|
|
|
@@ -848,7 +853,7 @@ Your iterator function is passed each page's items as an array, and a special ca
|
|
|
848
853
|
|
|
849
854
|
Your `CALLBACK` function is finally called when the loop is complete and all items were iterated over, or an error occurred somewhere in the middle. It is passed an error object, or something falsey for success. Example:
|
|
850
855
|
|
|
851
|
-
```
|
|
856
|
+
```js
|
|
852
857
|
storage.listEachPageUpdate( 'list1', function(items, callback) {
|
|
853
858
|
// do something with items, then fire callback
|
|
854
859
|
items.forEach( function(item) {
|
|
@@ -864,13 +869,13 @@ function(err) {
|
|
|
864
869
|
|
|
865
870
|
## listGetInfo
|
|
866
871
|
|
|
867
|
-
```
|
|
872
|
+
```js
|
|
868
873
|
storage.listGetInfo( KEY, CALLBACK );
|
|
869
874
|
```
|
|
870
875
|
|
|
871
876
|
The `listGetInfo()` method retrieves information about the list, without loading any items. Specifically, it fetches the list length, first and last page numbers, page size, and any custom keys you passed to the `OPTIONS` object when first creating the list. Your callback function is passed an error if one occurred, otherwise it'll be falsey. The second argument will be the list info object, if successful. Example:
|
|
872
877
|
|
|
873
|
-
```
|
|
878
|
+
```js
|
|
874
879
|
storage.listGetInfo( 'list1', function(err, list) {
|
|
875
880
|
if (err) throw err;
|
|
876
881
|
} );
|
|
@@ -888,13 +893,13 @@ Here are the keys you can expect to see in the info object:
|
|
|
888
893
|
|
|
889
894
|
## listDelete
|
|
890
895
|
|
|
891
|
-
```
|
|
896
|
+
```js
|
|
892
897
|
storage.listDelete( KEY, ENTIRE, CALLBACK );
|
|
893
898
|
```
|
|
894
899
|
|
|
895
900
|
The `listDelete()` method deletes a list. If you pass `true` for the second argument, the *entire* list will be deleted, including the header (options, page size, etc.). Otherwise the list will simply be "cleared" (all items deleted). Your callback function is passed an error if one occurred, otherwise it'll be falsey. Example:
|
|
896
901
|
|
|
897
|
-
```
|
|
902
|
+
```js
|
|
898
903
|
storage.listDelete( 'list1', true, function(err) {
|
|
899
904
|
if (err) throw err;
|
|
900
905
|
} );
|
|
@@ -904,13 +909,13 @@ storage.listDelete( 'list1', true, function(err) {
|
|
|
904
909
|
|
|
905
910
|
## hashCreate
|
|
906
911
|
|
|
907
|
-
```
|
|
912
|
+
```js
|
|
908
913
|
storage.hashCreate( PATH, OPTIONS, CALLBACK );
|
|
909
914
|
```
|
|
910
915
|
|
|
911
916
|
The `hashCreate()` method creates a new, empty hash. Specify the desired path, options (see below) and a callback function. Your callback function is passed an error if one occurred, otherwise it'll be falsey. Example:
|
|
912
917
|
|
|
913
|
-
```
|
|
918
|
+
```js
|
|
914
919
|
storage.hashCreate( 'hash1', {}, function(err) {
|
|
915
920
|
if (err) throw err;
|
|
916
921
|
} );
|
|
@@ -918,7 +923,7 @@ storage.hashCreate( 'hash1', {}, function(err) {
|
|
|
918
923
|
|
|
919
924
|
Unless otherwise specified, the hash will be created with the default [page size](../README.md#hash_page_size) (number of items per page). However, you can override this in the options object by passing a `page_size` property:
|
|
920
925
|
|
|
921
|
-
```
|
|
926
|
+
```js
|
|
922
927
|
storage.hashCreate( 'hash1', { page_size: 100 }, function(err) {
|
|
923
928
|
if (err) throw err;
|
|
924
929
|
} );
|
|
@@ -926,13 +931,13 @@ storage.hashCreate( 'hash1', { page_size: 100 }, function(err) {
|
|
|
926
931
|
|
|
927
932
|
## hashPut
|
|
928
933
|
|
|
929
|
-
```
|
|
934
|
+
```js
|
|
930
935
|
storage.hashPut( PATH, KEY, VALUE, [OPTIONS], CALLBACK );
|
|
931
936
|
```
|
|
932
937
|
|
|
933
938
|
The `hashPut()` method stores a single key/value pair in a hash. The `PATH` specifies the main storage path of the hash, and the hash key itself is identified by `KEY`, which should be a string. The `VALUE` must be an object, serializable by JSON.
|
|
934
939
|
|
|
935
|
-
```
|
|
940
|
+
```js
|
|
936
941
|
storage.hashPut( 'users', 'bsanders', { name: 'Bernie', age: 75 }, function(err) {
|
|
937
942
|
if (err) throw err;
|
|
938
943
|
} );
|
|
@@ -940,7 +945,7 @@ storage.hashPut( 'users', 'bsanders', { name: 'Bernie', age: 75 }, function(err)
|
|
|
940
945
|
|
|
941
946
|
If the hash doesn't exist, `hashPut()` will create it. If you specify an `OPTIONS` object, this will be used in the creation of the hash, i.e. to specify the [page size](../README.md#hash_page_size), and add any custom params you want.
|
|
942
947
|
|
|
943
|
-
```
|
|
948
|
+
```js
|
|
944
949
|
storage.hashPut( 'users', 'bsanders', { name: 'Bernie', age: 75 }, { page_size: 100 }, function(err) {
|
|
945
950
|
if (err) throw err;
|
|
946
951
|
} );
|
|
@@ -948,14 +953,14 @@ storage.hashPut( 'users', 'bsanders', { name: 'Bernie', age: 75 }, { page_size:
|
|
|
948
953
|
|
|
949
954
|
## hashPutMulti
|
|
950
955
|
|
|
951
|
-
```
|
|
956
|
+
```js
|
|
952
957
|
storage.hashPutMulti( PATH, RECORDS, [OPTIONS], CALLBACK );
|
|
953
958
|
```
|
|
954
959
|
|
|
955
960
|
The `hashPutMulti()` method stores multiple key/value pairs in a hash. The `PATH` specifies the main storage path of the hash, and `RECORDS` should be an object containing all the keys and values you want to store.
|
|
956
961
|
|
|
957
962
|
```js
|
|
958
|
-
|
|
963
|
+
let records = {
|
|
959
964
|
"bsanders": { name: "Bernie", age: 75 },
|
|
960
965
|
"hclinton": { name: "Hillary", age: 68 },
|
|
961
966
|
"dtrump": { name: "Donald", age: 70 }
|
|
@@ -970,13 +975,13 @@ If the hash doesn't exist, `hashPutMulti()` will create it. If you specify an `
|
|
|
970
975
|
|
|
971
976
|
## hashGet
|
|
972
977
|
|
|
973
|
-
```
|
|
978
|
+
```js
|
|
974
979
|
storage.hashGet( PATH, KEY, CALLBACK );
|
|
975
980
|
```
|
|
976
981
|
|
|
977
982
|
The `hashGet()` method fetches a single value from a hash. The `PATH` specifies the main storage path of the hash, and the hash key itself is identified by `KEY`, which should be a string. Your callback will be passed an error object (falsey on success), and the desired hash value.
|
|
978
983
|
|
|
979
|
-
```
|
|
984
|
+
```js
|
|
980
985
|
storage.hashGet( 'users', 'bsanders', function(err, value) {
|
|
981
986
|
if (err) throw err;
|
|
982
987
|
} );
|
|
@@ -984,13 +989,13 @@ storage.hashGet( 'users', 'bsanders', function(err, value) {
|
|
|
984
989
|
|
|
985
990
|
## hashGetMulti
|
|
986
991
|
|
|
987
|
-
```
|
|
992
|
+
```js
|
|
988
993
|
storage.hashGetMulti( PATH, KEYS, CALLBACK );
|
|
989
994
|
```
|
|
990
995
|
|
|
991
996
|
The `hashGetMulti()` method fetches multiple hash values at once, from a specified `PATH` and array of hash `KEYS`. Depending on your storage [concurrency](../README.md#concurrency) configuration, this may be significantly faster than fetching the values in sequence. Your callback function is passed an array of values which correspond to the specified keys. Example:
|
|
992
997
|
|
|
993
|
-
```
|
|
998
|
+
```js
|
|
994
999
|
storage.hashGetMulti( 'users', ['bsanders', 'hclinton', 'dtrump'], function(err, values) {
|
|
995
1000
|
if (err) throw err;
|
|
996
1001
|
// values[0] will be the bsanders record.
|
|
@@ -1001,13 +1006,13 @@ storage.hashGetMulti( 'users', ['bsanders', 'hclinton', 'dtrump'], function(err,
|
|
|
1001
1006
|
|
|
1002
1007
|
## hashUpdate
|
|
1003
1008
|
|
|
1004
|
-
```
|
|
1009
|
+
```js
|
|
1005
1010
|
storage.hashUpdate( PATH, KEY, UPDATES, CALLBACK );
|
|
1006
1011
|
```
|
|
1007
1012
|
|
|
1008
1013
|
The `hashUpdate()` method updates an existing key/value pair in a hash. The `PATH` specifies the main storage path of the hash, and the hash key itself is identified by `KEY`, which should be a string. The `UPDATES` must be an object, but it can contain sparse keys. Furthermore, it can contain dot or slash delimited paths, to update inner nested keys. The updates are essentially applied atop the exiting record, merging and replacing (overwriting) where appropriate. Example:
|
|
1009
1014
|
|
|
1010
|
-
```
|
|
1015
|
+
```js
|
|
1011
1016
|
storage.hashUpdate( 'users', 'bsanders', { age: 81 }, function(err) {
|
|
1012
1017
|
if (err) throw err;
|
|
1013
1018
|
} );
|
|
@@ -1017,14 +1022,14 @@ This would update Bernie's age to 81 without affecting any of the other properti
|
|
|
1017
1022
|
|
|
1018
1023
|
## hashUpdateMulti
|
|
1019
1024
|
|
|
1020
|
-
```
|
|
1025
|
+
```js
|
|
1021
1026
|
storage.hashUpdateMulti( PATH, RECORDS, CALLBACK );
|
|
1022
1027
|
```
|
|
1023
1028
|
|
|
1024
1029
|
The `hashUpdateMulti()` method updates multiple key/value pairs in a hash. The `PATH` specifies the main storage path of the hash, and `RECORDS` should be an object containing all the keys and values you want to update. See [hashUpdate()](#hashupdate) for details on the update format.
|
|
1025
1030
|
|
|
1026
1031
|
```js
|
|
1027
|
-
|
|
1032
|
+
let updates = {
|
|
1028
1033
|
"bsanders": { age: 81 },
|
|
1029
1034
|
"hclinton": { age: 75 },
|
|
1030
1035
|
"dtrump": { age: 77 }
|
|
@@ -1039,7 +1044,7 @@ This would update the `age` properties in each record, without affecting the oth
|
|
|
1039
1044
|
|
|
1040
1045
|
## hashEach
|
|
1041
1046
|
|
|
1042
|
-
```
|
|
1047
|
+
```js
|
|
1043
1048
|
storage.hashEach( PATH, ITERATOR, CALLBACK );
|
|
1044
1049
|
```
|
|
1045
1050
|
|
|
@@ -1049,7 +1054,7 @@ Your iterator function is passed the key and value of the current item, and a ca
|
|
|
1049
1054
|
|
|
1050
1055
|
The `CALLBACK` function is finally called when the loop is complete and all items were iterated over, or an error occurred somewhere in the middle. It is passed an error object, or something falsey for success. Example:
|
|
1051
1056
|
|
|
1052
|
-
```
|
|
1057
|
+
```js
|
|
1053
1058
|
storage.hashEach( 'users', function(key, value, callback) {
|
|
1054
1059
|
// do something with key/value
|
|
1055
1060
|
callback();
|
|
@@ -1062,7 +1067,7 @@ function(err) {
|
|
|
1062
1067
|
|
|
1063
1068
|
## hashEachSync
|
|
1064
1069
|
|
|
1065
|
-
```
|
|
1070
|
+
```js
|
|
1066
1071
|
storage.hashEachSync( PATH, ITERATOR, CALLBACK );
|
|
1067
1072
|
```
|
|
1068
1073
|
|
|
@@ -1072,7 +1077,7 @@ Your iterator function is passed the key and value of the current item. If you
|
|
|
1072
1077
|
|
|
1073
1078
|
The `CALLBACK` function is finally called when the loop is complete and all items were iterated over, or an error occurred somewhere in the middle. It is passed an error object, or something falsey for success. Example:
|
|
1074
1079
|
|
|
1075
|
-
```
|
|
1080
|
+
```js
|
|
1076
1081
|
storage.hashEach( 'users', function(key, value) {
|
|
1077
1082
|
// do something with key/value
|
|
1078
1083
|
// no callback here
|
|
@@ -1085,7 +1090,7 @@ function(err) {
|
|
|
1085
1090
|
|
|
1086
1091
|
## hashEachPage
|
|
1087
1092
|
|
|
1088
|
-
```
|
|
1093
|
+
```js
|
|
1089
1094
|
storage.hashEachPage( PATH, ITERATOR, CALLBACK );
|
|
1090
1095
|
```
|
|
1091
1096
|
|
|
@@ -1093,11 +1098,11 @@ The `hashEachPage()` method iterates over a hash one *page* at a time, invoking
|
|
|
1093
1098
|
|
|
1094
1099
|
Your iterator is passed exactly two arguments. An object containing all the keys and values in the current page (this may contain up to [page size](../README.md#hash_page_size) items), and a callback function that you must fire to continue the loop. Pass an error into the callback to abort the loop anywhere in the middle. The final `CALLBACK` is fired when all keys are iterated over, or an error occurs.
|
|
1095
1100
|
|
|
1096
|
-
```
|
|
1101
|
+
```js
|
|
1097
1102
|
storage.hashEachPage( 'users', function(items, callback) {
|
|
1098
1103
|
// do something with page of items
|
|
1099
|
-
for (
|
|
1100
|
-
|
|
1104
|
+
for (let key in items) {
|
|
1105
|
+
let value = items[key];
|
|
1101
1106
|
// do something with key/value pair
|
|
1102
1107
|
}
|
|
1103
1108
|
|
|
@@ -1112,19 +1117,19 @@ function(err) {
|
|
|
1112
1117
|
|
|
1113
1118
|
## hashGetAll
|
|
1114
1119
|
|
|
1115
|
-
```
|
|
1120
|
+
```js
|
|
1116
1121
|
storage.hashGetAll( PATH, CALLBACK );
|
|
1117
1122
|
```
|
|
1118
1123
|
|
|
1119
1124
|
The `hashGetAll()` method loads a hash entirely into memory as fast as possible, and fires your callback with a single in-memory object containing *all* the key/value pairs. Please use this with caution on large hashes, and keep track of your process memory usage. The `CALLBACK` is fired with two arguments: an error if one occurred (falsey if not), and a hash object containing all your keys and values.
|
|
1120
1125
|
|
|
1121
|
-
```
|
|
1126
|
+
```js
|
|
1122
1127
|
storage.hashGetAll( 'users', function(err, items) {
|
|
1123
1128
|
if (err) throw err;
|
|
1124
1129
|
|
|
1125
1130
|
// do something with all items
|
|
1126
|
-
for (
|
|
1127
|
-
|
|
1131
|
+
for (let key in items) {
|
|
1132
|
+
let value = items[key];
|
|
1128
1133
|
// do something with key/value pair
|
|
1129
1134
|
}
|
|
1130
1135
|
} );
|
|
@@ -1132,13 +1137,13 @@ storage.hashGetAll( 'users', function(err, items) {
|
|
|
1132
1137
|
|
|
1133
1138
|
## hashCopy
|
|
1134
1139
|
|
|
1135
|
-
```
|
|
1140
|
+
```js
|
|
1136
1141
|
storage.hashCopy( OLD_PATH, NEW_PATH, CALLBACK );
|
|
1137
1142
|
```
|
|
1138
1143
|
|
|
1139
1144
|
The `hashCopy()` method copies a hash and all of its items to a new path. Specify the existing hash path, a new path, and a callback. If anything exists at the destination path, it will be clobbered. Your callback function is passed an error if one occurred, otherwise it'll be falsey. Example:
|
|
1140
1145
|
|
|
1141
|
-
```
|
|
1146
|
+
```js
|
|
1142
1147
|
storage.hashCopy( 'hash1', 'hash2', function(err) {
|
|
1143
1148
|
if (err) throw err;
|
|
1144
1149
|
} );
|
|
@@ -1148,13 +1153,13 @@ If a hash already exists at the destination path, you should delete it first. I
|
|
|
1148
1153
|
|
|
1149
1154
|
## hashRename
|
|
1150
1155
|
|
|
1151
|
-
```
|
|
1156
|
+
```js
|
|
1152
1157
|
storage.hashRename( OLD_PATH, NEW_PATH, CALLBACK );
|
|
1153
1158
|
```
|
|
1154
1159
|
|
|
1155
1160
|
The `hashRename()` method renames (moves) a hash and all of its items to a new path. Specify the existing hash path, a new path, and a callback. If anything exists at the destination path, it will be clobbered. Your callback function is passed an error if one occurred, otherwise it'll be falsey. Example:
|
|
1156
1161
|
|
|
1157
|
-
```
|
|
1162
|
+
```js
|
|
1158
1163
|
storage.hashRename( 'hash1', 'hash2', function(err) {
|
|
1159
1164
|
if (err) throw err;
|
|
1160
1165
|
} );
|
|
@@ -1164,13 +1169,13 @@ If a hash already exists at the destination path, you should delete it first. I
|
|
|
1164
1169
|
|
|
1165
1170
|
## hashDelete
|
|
1166
1171
|
|
|
1167
|
-
```
|
|
1172
|
+
```js
|
|
1168
1173
|
storage.hashDelete( PATH, KEY, [ENTIRE], CALLBACK );
|
|
1169
1174
|
```
|
|
1170
1175
|
|
|
1171
1176
|
The `hashDelete()` method deletes a single key/value pair from the specified hash. Your callback function is passed an error if one occurred, otherwise it'll be falsey. Example:
|
|
1172
1177
|
|
|
1173
|
-
```
|
|
1178
|
+
```js
|
|
1174
1179
|
storage.hashDelete( 'users', 'dtrump', function(err) {
|
|
1175
1180
|
if (err) throw err;
|
|
1176
1181
|
} );
|
|
@@ -1178,7 +1183,7 @@ storage.hashDelete( 'users', 'dtrump', function(err) {
|
|
|
1178
1183
|
|
|
1179
1184
|
By default, if `hashDelete()` removes the last key from a hash, it leaves an "empty" hash in storage (i.e. one with a header, page size, options, etc.). However, if you would prefer, you can trigger a full delete if the hash becomes empty after the key is removed. To do this, pass `true` as the 3rd argument, just before your callback. Example:
|
|
1180
1185
|
|
|
1181
|
-
```
|
|
1186
|
+
```js
|
|
1182
1187
|
storage.hashDelete( 'users', 'dtrump', true, function(err) {
|
|
1183
1188
|
if (err) throw err;
|
|
1184
1189
|
} );
|
|
@@ -1186,13 +1191,13 @@ storage.hashDelete( 'users', 'dtrump', true, function(err) {
|
|
|
1186
1191
|
|
|
1187
1192
|
## hashDeleteMulti
|
|
1188
1193
|
|
|
1189
|
-
```
|
|
1194
|
+
```js
|
|
1190
1195
|
storage.hashDeleteMulti( PATH, KEYS, CALLBACK );
|
|
1191
1196
|
```
|
|
1192
1197
|
|
|
1193
1198
|
The `hashDeleteMulti()` method deletes multiple hash records at once, from a specified array of keys. Depending on your storage [concurrency](../README.md#concurrency) configuration, this may be significantly faster than deleting the records in sequence. Example:
|
|
1194
1199
|
|
|
1195
|
-
```
|
|
1200
|
+
```js
|
|
1196
1201
|
storage.hashDeleteMulti( 'users', ['bsanders', 'hclinton', 'dtrump'], function(err) {
|
|
1197
1202
|
if (err) throw err;
|
|
1198
1203
|
} );
|
|
@@ -1200,13 +1205,13 @@ storage.hashDeleteMulti( 'users', ['bsanders', 'hclinton', 'dtrump'], function(e
|
|
|
1200
1205
|
|
|
1201
1206
|
## hashDeleteAll
|
|
1202
1207
|
|
|
1203
|
-
```
|
|
1208
|
+
```js
|
|
1204
1209
|
storage.hashDeleteAll( PATH, [ENTIRE], CALLBACK );
|
|
1205
1210
|
```
|
|
1206
1211
|
|
|
1207
1212
|
The `hashDeleteAll()` method deletes a hash and all its contents. If you pass `true` for the second argument, the *entire* hash will be deleted, including the header (options, page size, etc.). Otherwise the hash will simply be "cleared" (all items deleted) but the hash header will remain. Your callback function is passed an error if one occurred, otherwise it'll be falsey. Example:
|
|
1208
1213
|
|
|
1209
|
-
```
|
|
1214
|
+
```js
|
|
1210
1215
|
storage.hashDeleteAll( 'users', true, function(err) {
|
|
1211
1216
|
if (err) throw err;
|
|
1212
1217
|
} );
|
|
@@ -1214,13 +1219,13 @@ storage.hashDeleteAll( 'users', true, function(err) {
|
|
|
1214
1219
|
|
|
1215
1220
|
## hashGetInfo
|
|
1216
1221
|
|
|
1217
|
-
```
|
|
1222
|
+
```js
|
|
1218
1223
|
storage.hashGetInfo( PATH, CALLBACK );
|
|
1219
1224
|
```
|
|
1220
1225
|
|
|
1221
1226
|
The `hashGetInfo()` method retrieves information about the hash, without loading any items. Specifically, it fetches the hash length, page size, and any custom properties you passed to the `OPTIONS` object when first creating the list. Your callback function is passed an error if one occurred, otherwise it'll be falsey. The second argument will be the hash info object, if successful. Example:
|
|
1222
1227
|
|
|
1223
|
-
```
|
|
1228
|
+
```js
|
|
1224
1229
|
storage.hashGetInfo( 'users', function(err, info) {
|
|
1225
1230
|
if (err) throw err;
|
|
1226
1231
|
console.log( "The hash has " + info.length + " keys." );
|