MCPcopy
hub / github.com/apache/pouchdb / nut

Function nut

lib/index.js:4054–4164  ·  view source on GitHub ↗
(db, precodec, codec)

Source from the content-addressed store, hash-verified

4052}
4053
4054function nut(db, precodec, codec) {
4055 function encodePrefix(prefix, key, opts1, opts2) {
4056 return precodec.encode([ prefix, codec.encodeKey(key, opts1, opts2 ) ]);
4057 }
4058
4059 function addEncodings(op, prefix) {
4060 if (prefix && prefix.options) {
4061 op.keyEncoding =
4062 op.keyEncoding || prefix.options.keyEncoding;
4063 op.valueEncoding =
4064 op.valueEncoding || prefix.options.valueEncoding;
4065 }
4066 return op;
4067 }
4068
4069 db.open(function () { /* no-op */});
4070
4071 return {
4072 apply: function (ops, opts, cb) {
4073 opts = opts || {};
4074
4075 var batch = [];
4076 var i = -1;
4077 var len = ops.length;
4078
4079 while (++i < len) {
4080 var op = ops[i];
4081 addEncodings(op, op.prefix);
4082 op.prefix = getPrefix(op.prefix);
4083 batch.push({
4084 key: encodePrefix(op.prefix, op.key, opts, op),
4085 value: op.type !== 'del' && codec.encodeValue(op.value, opts, op),
4086 type: op.type
4087 });
4088 }
4089 db.db.batch(batch, opts, cb);
4090 },
4091 get: function (key, prefix, opts, cb) {
4092 opts.asBuffer = codec.valueAsBuffer(opts);
4093 return db.db.get(
4094 encodePrefix(prefix, key, opts),
4095 opts,
4096 function (err, value) {
4097 if (err) {
4098 cb(err);
4099 } else {
4100 cb(null, codec.decodeValue(value, opts));
4101 }
4102 }
4103 );
4104 },
4105 createDecoder: function (opts) {
4106 return function (key, value) {
4107 return {
4108 key: codec.decodeKey(precodec.decode(key)[1], opts),
4109 value: codec.decodeValue(value, opts)
4110 };
4111 };

Callers 1

sublevelPouchFunction · 0.70

Calls 7

addEncodingsFunction · 0.70
getPrefixFunction · 0.70
encodePrefixFunction · 0.70
clone$1Function · 0.70
wrapIteratorFunction · 0.70
batchMethod · 0.45
getMethod · 0.45

Tested by

no test coverage detected