diff --git a/README.md b/README.md index 111870d..3c1baa4 100644 --- a/README.md +++ b/README.md @@ -36,7 +36,7 @@ yarn add nodecloud | | DNS | Route53 | Google DNS | | Databases | RDBMS | RDS | - | | | NoSQL: key-value | DynamoDB | Cloud Datastore | -| | NoSQL: indexed | - | Cloud Datastore | +| | NoSQL: indexed | SimpleDB | Cloud Datastore | | Security/ Authorization | IAM | AWS IAM | - | ## Usage diff --git a/docs/database/aws-simpledb.md b/docs/database/aws-simpledb.md new file mode 100644 index 0000000..2f8768b --- /dev/null +++ b/docs/database/aws-simpledb.md @@ -0,0 +1,126 @@ +# NodeCloud database - AWS SimpleDB + +## Configure AWS credentials + +Create `config.json` as follows, + +```js +{ + "aws_access_key_id": "xxxxxxxxxxxx", + "aws_secret_access_key": "xxxxxxxxxxxx", + "region": "xxxxxxxxx" // eg : us-west-2 +} +``` + +## Initialize library + +```js +const nodeCloud = require("../../lib/"); +const ncAWS = nodeCloud.getProvider( + "AWS", + process.env.ncconf // path to config.json +); +const options = { + apiVersion: '2016-11-15', +}; +const simpleDB = ncAWS.indexedDB(options); +``` + +### Create a domain + +```js +const params = { + DomainName: 'nodeCloudTestDomain' /* required */ +}; + +simpleDB.createDomain(params) + .then((res) => { + console.log(res); + }) + .catch((err) => { + console.error(err); + }); +``` + +### Delete a domain + +```js +simpleDB.deleteDomain(params) + .then((res) => { + console.log(res); + }) + .catch((err) => { + console.error(err); + }); +``` + +### Create an Item + +```js +const params = { + Attributes: [ /* required */ + { + Name: 'STRING_VALUE', /* required */ + Value: 'STRING_VALUE', /* required */ + Replace: true || false + }, + /* more items */ + ], + DomainName: 'STRING_VALUE', /* required */ + ItemName: 'STRING_VALUE', /* required */ + Expected: { + Exists: true || false, + Name: 'STRING_VALUE', + Value: 'STRING_VALUE' + } +}; + +simpleDB.createItem(params) + .then((res) => { + console.log(res); + }) + .catch((err) => { + console.error(err); + }); +``` + +### Delete an Item + +```js +simpleDB.deleteItem(params) + .then((res) => { + console.log(res); + }) + .catch((err) => { + console.error(err); + }); +``` + +### Update an Item + +```js +simpleDB.updateItem(params) + .then((res) => { + console.log(res); + }) + .catch((err) => { + console.error(err); + }); +``` + +### Query an Item + +```js +const params = { + SelectExpression: 'STRING_VALUE', /* required */ + ConsistentRead: true || false, + NextToken: 'STRING_VALUE' +}; +simpleDB.query(params) + .then((res) => { + console.log(res); + }) + .catch((err) => { + console.error(err); + }); +``` diff --git a/examples/database/aws-simpledb.js b/examples/database/aws-simpledb.js new file mode 100644 index 0000000..38e5b91 --- /dev/null +++ b/examples/database/aws-simpledb.js @@ -0,0 +1,20 @@ +const nodeCloud = require('../../lib/'); + +const ncAWS = nodeCloud.getProvider('AWS', process.env.ncconf); +const options = { + apiVersion: '2016-11-15', +}; + +const simpleDB = ncAWS.indexedDB(options); + +const params = { + DomainName: 'nodeCloudTestDomain' /* required */ +}; + +simpleDB.createDomain(params) + .then((res) => { + console.log(res); + }) + .catch((err) => { + console.error(err); + }); diff --git a/lib/core/aws/provider.js b/lib/core/aws/provider.js index 66a8e56..714a2dd 100644 --- a/lib/core/aws/provider.js +++ b/lib/core/aws/provider.js @@ -9,6 +9,7 @@ const DirectConnect = require('../../network/aws-directconnect'); const RDS = require('../../database/aws-rds'); const DynamoDB = require('../../database/aws-dynamodb'); const IAM = require('../../security/aws-iam'); +const SimpleDB = require('../../database/aws-simpledb'); class AWS { /** @@ -42,6 +43,8 @@ class AWS { rdbms: this.RDS, nosql: this.DynamoDB, iam: this.IAM, + indexedDB: this.SimpleDB, + }; } /** @@ -158,6 +161,14 @@ class AWS { } return new IAM(this.getSDK()); } + + SimpleDB(options) { + if (options.apiVersion) { + this._apiVersion = options.apiVersion; + return new SimpleDB(this.getSDK(), options); + } + return new SimpleDB(this.getSDK()); + } } module.exports = AWS; diff --git a/lib/database/aws-simpledb.js b/lib/database/aws-simpledb.js new file mode 100644 index 0000000..f095e3e --- /dev/null +++ b/lib/database/aws-simpledb.js @@ -0,0 +1,137 @@ +const helpers = require('../core/helpers'); + +const { checkParams } = helpers; +class SimpleDB { + + /** + * SimpleDB constructor + * @constructor + * @param {object} aws - AWS SDK + * @param {object} options - { apiVersion } + */ + constructor(aws, options) { + this._AWS = aws; + if (options) { + this._apiVersion = options.apiVersion; + this._simpledb = new this._AWS.SimpleDB({ apiVersion: this._apiVersion }); + } else { + this._simpledb = new this._AWS.SimpleDB(); + } + } + + /** + * Create Domain + * @createDomain + * @param {object} params + */ + createDomain(params) { + checkParams(params); + + return new Promise((resolve, reject) => { + this._simpledb.createDomain(params, (err, data) => { + if (err) { + reject(err); + } else { + resolve(data); + } + }); + }); + } + + /** + * Delete item + * @deleteDomain + * @param {object} params + */ + deleteDomain(params) { + checkParams(params); + + return new Promise((resolve, reject) => { + this._simpledb.deleteDomain(params, (err, data) => { + if (err) { + reject(err); + } else { + resolve(data); + } + }); + }); + } + + /** + * Create item + * @createItem + * @param {object} params + */ + createItem(params) { + checkParams(params); + + return new Promise((resolve, reject) => { + this._simpledb.putAttributes(params, (err, data) => { + if (err) { + reject(err); + } else { + resolve(data); + } + }); + }); + } + + /** + * Update item + * @updateItem + * @param {object} params + */ + updateItem(params) { + checkParams(params); + + return new Promise((resolve, reject) => { + this._simpledb.putAttributes(params, (err, data) => { + if (err) { + reject(err); + } else { + resolve(data); + } + }); + }); + } + + /** + * Delete item + * @deleteItem + * @param {object} params + */ + deleteItem(params) { + checkParams(params); + + return new Promise((resolve, reject) => { + this._simpledb.deleteAttributes(params, (err, data) => { + if (err) { + reject(err); + } else { + resolve(data); + } + }); + }); + } + + /** + * Query + * @query + * @param {object} params + */ + query(params) { + checkParams(params); + + return new Promise((resolve, reject) => { + this._simpledb.getAttributes(params, (err, data) => { + if (err) { + reject(err); + } else { + resolve(data); + } + }); + }); + } +} + +module.exports = SimpleDB; \ No newline at end of file