UNPKG

@amazon-dax-sdk/lib-dax

Version:

Amazon DAX Document Client for JavaScript

345 lines (324 loc) 11.7 kB
/* * Copyright 2024 Amazon.com, Inc. or its affiliates. All Rights Reserved. * * Licensed under the Apache License, Version 2.0 (the "License"). You may not * use this file except in compliance with the License. A copy of the License * is located at * * http://aws.amazon.com/apache2.0/ * * or in the "license" file accompanying this file. This file is distributed on * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either * express or implied. See the License for the specific language governing * permissions and limitations under the License. */ 'use strict'; const {Dax} = require('@amazon-dax-sdk/client-dax'); const Util = require('./Util'); /** * The DaxDocument simplifies working with items in Amazon DynamoDB by * abstracting away the notion of attribute values. This abstraction annotates native * JavaScript types supplied as input parameters, as well as converts annotated * response data to native JavaScript types. * * ## Marshalling Input and Unmarshalling Response Data * * The DaxDocument affords developers the use of native JavaScript types * instead of `AttributeValue`s to simplify the JavaScript development * experience with Amazon DynamoDB. JavaScript objects passed in as parameters * are marshalled into `AttributeValue` shapes required by Amazon DynamoDB. * Responses from DynamoDB are unmarshalled into plain JavaScript objects * by the `DaxDocument`. The `DaxDocument` does not accept * `AttributeValue`s in favor of native JavaScript types. * * | JavaScript Type | DynamoDB AttributeValue | * | :-------------------------------: | ----------------------- | * | String | S | * | Number / BigInt | N | * | Boolean | BOOL | * | null | NULL | * | Array | L | * | Object | M | * | Set\<Uint8Array, Blob, ...\> | BS | * | Set\<Number, BigInt\> | NS | * | Set\<String\> | SS | * | Uint8Array, Buffer, File, Blob... | B | * * ### Example * * Here is an example list which is sent to DynamoDB in an operation: * * ```json * { "L": [{ "NULL": true }, { "BOOL": false }, { "N": 1 }, { "S": "two" }] } * ``` * * The DaxDocument abstracts the attribute values as follows in * both input and output: * * ```json * [null, false, 1, "two"] * ``` * */ // This is the source class which is used to instantiate the DaxDocument objects. class DaxDocument extends Dax { constructor(config, cluster) { super(config, cluster); } // DynamoDBDocument Supported Functions batchGet(params, optionsOrCb, callback) { params = Util.marshallInput(params, Util.inputKeyNodes('batchGet'), Util.marshallingOptions()); if(typeof optionsOrCb === 'function') { callback = optionsOrCb; optionsOrCb = undefined; } if(typeof callback === 'function') { this.batchGetItem(params, (err, data) => { if(err) { callback(err, null); } else { data = Util.unmarshallOutput(data, Util.outputKeyNodes('batchGet'), Util.unmarshallOptions()); callback(null, data); } }); } else { return this.batchGetItem(params).then((data) => { data = Util.unmarshallOutput(data, Util.outputKeyNodes('batchGet'), Util.unmarshallOptions()); return data; }).catch((err) => { throw err; }); } } batchWrite(params, optionsOrCb, callback) { params = Util.marshallInput(params, Util.inputKeyNodes('batchWrite'), Util.marshallingOptions()); if(typeof optionsOrCb === 'function') { callback = optionsOrCb; optionsOrCb = undefined; } if(typeof callback === 'function') { this.batchWriteItem(params, (err, data) => { if(err) { callback(err, null); } else { data = Util.unmarshallOutput(data, Util.outputKeyNodes('batchWrite'), Util.unmarshallOptions()); callback(null, data); } }); } else { return this.batchWriteItem(params).then((data) => { data = Util.unmarshallOutput(data, Util.outputKeyNodes('batchWrite'), Util.unmarshallOptions()); return data; }).catch((err) => { throw err; }); } } delete(params, optionsOrCb, callback) { params = Util.marshallInput(params, Util.inputKeyNodes('delete'), Util.marshallingOptions()); if(typeof optionsOrCb === 'function') { callback = optionsOrCb; optionsOrCb = undefined; } if(typeof callback === 'function') { this.deleteItem(params, (err, data) => { if(err) { callback(err, null); } else { data = Util.unmarshallOutput(data, Util.outputKeyNodes('delete'), Util.unmarshallOptions()); callback(null, data); } }); } else { return this.deleteItem(params).then((data) => { data = Util.unmarshallOutput(data, Util.outputKeyNodes('delete'), Util.unmarshallOptions()); return data; }).catch((err) => { throw err; }); } } get(params, optionsOrCb, callback) { params = Util.marshallInput(params, Util.inputKeyNodes('get'), Util.marshallingOptions()); if(typeof optionsOrCb === 'function') { callback = optionsOrCb; optionsOrCb = undefined; } if(typeof callback === 'function') { this.getItem(params, (err, data) => { if(err) { callback(err, null); } else { data = Util.unmarshallOutput(data, Util.outputKeyNodes('get'), Util.unmarshallOptions()); callback(null, data); } }); } else { return this.getItem(params).then((data) => { data = Util.unmarshallOutput(data, Util.outputKeyNodes('get'), Util.unmarshallOptions()); return data; }).catch((err)=>{ throw err; }); } } put(params, optionsOrCb, callback) { params = Util.marshallInput(params, Util.inputKeyNodes('put'), Util.marshallingOptions()); if(typeof optionsOrCb === 'function') { callback = optionsOrCb; optionsOrCb = undefined; } if(typeof callback === 'function') { this.putItem(params, (err, data) => { if(err) { callback(err, null); } else { data = Util.unmarshallOutput(data, Util.outputKeyNodes('put'), Util.unmarshallOptions()); callback(null, data); } }); } else { return this.putItem(params).then((data) => { data = Util.unmarshallOutput(data, Util.outputKeyNodes('put'), Util.unmarshallOptions()); return data; }).catch((err) => { throw err; }); } } query(params, optionsOrCb, callback) { params = Util.marshallInput(params, Util.inputKeyNodes('query'), Util.marshallingOptions()); if(typeof optionsOrCb === 'function') { callback = optionsOrCb; optionsOrCb = undefined; } if(typeof callback === 'function') { super.query(params, (err, data) => { if(err) { callback(err, null); } else { data = Util.unmarshallOutput(data, Util.outputKeyNodes('query'), Util.unmarshallOptions()); callback(null, data); } }); } else { return super.query(params).then((data) => { data = Util.unmarshallOutput(data, Util.outputKeyNodes('query'), Util.unmarshallOptions()); return data; }).catch((err) => { throw err; }); } } scan(params, optionsOrCb, callback) { params = Util.marshallInput(params, Util.inputKeyNodes('scan'), Util.marshallingOptions()); if(typeof optionsOrCb === 'function') { callback = optionsOrCb; optionsOrCb = undefined; } if(typeof callback === 'function') { super.scan(params, (err, data) => { if(err) { callback(err, null); } else { data = Util.unmarshallOutput(data, Util.outputKeyNodes('scan'), Util.unmarshallOptions()); callback(null, data); } }); } else { return super.scan(params).then((data) => { data = Util.unmarshallOutput(data, Util.outputKeyNodes('scan'), Util.unmarshallOptions()); return data; }).catch((err) => { throw err; }); } } transactGet(params, optionsOrCb, callback) { params = Util.marshallInput(params, Util.inputKeyNodes('transactGet'), Util.marshallingOptions()); if(typeof optionsOrCb === 'function') { callback = optionsOrCb; optionsOrCb = undefined; } if(typeof callback === 'function') { this.transactGetItems(params, (err, data) => { if(err) { callback(err, null); } else { data = Util.unmarshallOutput(data, Util.outputKeyNodes('transactGet'), Util.unmarshallOptions()); callback(null, data); } }); } else { return this.transactGetItems(params).then((data) => { data = Util.unmarshallOutput(data, Util.outputKeyNodes('transactGet'), Util.unmarshallOptions()); return data; }).catch((err)=>{ throw err; }); } } transactWrite(params, optionsOrCb, callback) { params = Util.marshallInput(params, Util.inputKeyNodes('transactWrite'), Util.marshallingOptions()); if(typeof optionsOrCb === 'function') { callback = optionsOrCb; optionsOrCb = undefined; } if(typeof callback === 'function') { this.transactWriteItems(params, (err, data) => { if(err) { callback(err, null); } else { data = Util.unmarshallOutput(data, Util.outputKeyNodes('transactWrite'), Util.unmarshallOptions()); callback(null, data); } }); } else { return this.transactWriteItems(params).then((data) => { data = Util.unmarshallOutput(data, Util.outputKeyNodes('transactWrite'), Util.unmarshallOptions()); return data; }).catch((err) => { throw err; }); } } update(params, optionsOrCb, callback) { params = Util.marshallInput(params, Util.inputKeyNodes('update'), Util.marshallingOptions()); if(typeof optionsOrCb === 'function') { callback = optionsOrCb; optionsOrCb = undefined; } if(typeof callback === 'function') { this.updateItem(params, (err, data) => { if(err) { callback(err, null); } else { data = Util.unmarshallOutput(data, Util.outputKeyNodes('update'), Util.unmarshallOptions()); callback(null, data); } }); } else { return this.updateItem(params).then((data) => { data = Util.unmarshallOutput(data, Util.outputKeyNodes('update'), Util.unmarshallOptions()); return data; }).catch((err)=>{ throw err; }); } } // DynamoDBDocument Unsupported Functions batchExecuteStatement(params, optionsOrCb, callback) { return super.batchExecuteStatement(params, optionsOrCb, callback); } executeStatement(params, callback) { return super.executeStatement(params, optionsOrCb, callback); } executeTransaction(params, callback) { return super.executeTransaction(params, optionsOrCb, callback); } // Unsupported DynamoDB minimal client send operation send(command, optionsOrCb, callback) { return super.send(command, optionsOrCb, callback); } } module.exports = DaxDocument;