UNPKG

2 kBJavaScriptView Raw
1(function() {
2 var BatchWriteStream, Errors, WriteStream, consts, defaultOptions, extend, inherits, setImmediate, util;
3
4 BatchWriteStream = require('batch-write-stream');
5
6 util = require('abstract-object/lib/util');
7
8 Errors = require('./errors');
9
10 consts = require('./consts');
11
12 inherits = util.inherits;
13
14 extend = util._extend;
15
16 setImmediate = global.setImmediate || process.nextTick;
17
18 defaultOptions = {
19 highWaterMark: 1e5,
20 maxConcurrentBatches: 4,
21 type: 'put',
22 flushWait: 10
23 };
24
25 module.exports = WriteStream = (function() {
26 inherits(WriteStream, BatchWriteStream);
27
28 function WriteStream(db, aOptions) {
29 if (!(this instanceof WriteStream)) {
30 return new WriteStream(db, aOptions);
31 }
32 this._options = extend({}, defaultOptions);
33 this._options = extend(this._options, aOptions || {});
34 if (db == null) {
35 db = aOptions.db;
36 }
37 this.db = db;
38 this._type = this._options.type;
39 BatchWriteStream.call(this, {
40 objectMode: true,
41 highWaterMark: this._options.highWaterMark,
42 maxConcurrentBatches: this._options.maxConcurrentBatches,
43 flushWait: this._options.flushWait
44 });
45 this.once('finish', (function(_this) {
46 return function() {
47 return _this.emit('close');
48 };
49 })(this));
50 }
51
52 WriteStream.prototype._writeBatch = function(aBatch, aCallback) {
53 return this.db.batch(aBatch, aCallback);
54 };
55
56 WriteStream.prototype._map = function(aItem) {
57 return {
58 type: aItem.type || this._type,
59 key: aItem.key,
60 value: aItem.value,
61 keyEncoding: aItem.keyEncoding || this._options.keyEncoding,
62 valueEncoding: aItem.valueEncoding || this.encoding || this._options.valueEncoding
63 };
64 };
65
66 WriteStream.prototype.toString = function() {
67 return 'NoSQLWriteStream';
68 };
69
70 return WriteStream;
71
72 })();
73
74}).call(this);
75
76//# sourceMappingURL=write-stream.js.map