data-api-client
Advanced tools
Comparing version 1.3.0 to 1.3.1
{ | ||
"name": "data-api-client", | ||
"version": "1.3.0", | ||
"version": "1.3.1", | ||
"description": "A lightweight wrapper that simplifies working with the Amazon Aurora Serverless Data API", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
192
README.md
![Aurora Serverless Data API Client](https://user-images.githubusercontent.com/2053544/79285017-44053500-7e8a-11ea-8515-998ccf9c2d2e.png) | ||
[![Build Status](https://travis-ci.org/jeremydaly/data-api-client.svg?branch=master)](https://travis-ci.org/jeremydaly/data-api-client) | ||
[![npm](https://img.shields.io/npm/v/data-api-client.svg)](https://www.npmjs.com/package/data-api-client) | ||
[![npm](https://img.shields.io/npm/l/data-api-client.svg)](https://www.npmjs.com/package/data-api-client) | ||
> #### Project Update: October 7, 2024 | ||
> | ||
> With the recent announcement that Amazon Aurora MySQL-Compatible Edition now supports a redesigned [RDS Data API for Aurora Serverless v2 and Aurora provisioned database instances](https://aws.amazon.com/about-aws/whats-new/2024/09/amazon-aurora-mysql-rds-data-api/), there have been several requests to add support to this project. The new RDS Data API also supports [Amazon Aurora PostgreSQL-Compatible Edition](https://aws.amazon.com/about-aws/whats-new/2023/12/amazon-aurora-postgresql-rds-data-api/) (more detail [here](https://aws.amazon.com/blogs/database/introducing-the-data-api-for-amazon-aurora-serverless-v2-and-amazon-aurora-provisioned-clusters/)). | ||
> | ||
> Star and watch the project for the 2.0 branch updates. | ||
The **Data API Client** is a lightweight wrapper that simplifies working with the Amazon Aurora Serverless Data API by abstracting away the notion of field values. This abstraction annotates native JavaScript types supplied as input parameters, as well as converts annotated response data to native JavaScript types. It's basically a [DocumentClient](https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/DynamoDB/DocumentClient.html) for the Data API. It also promisifies the `AWS.RDSDataService` client to make working with `async/await` or Promise chains easier AND dramatically simplifies **transactions**. | ||
@@ -36,30 +41,22 @@ | ||
// SELECT with named parameters | ||
let resultParams = await data.query( | ||
`SELECT * FROM myTable WHERE id = :id`, | ||
{ id: 2 } | ||
) | ||
let resultParams = await data.query(`SELECT * FROM myTable WHERE id = :id`, { id: 2 }) | ||
// { records: [ { id: 2, name: 'Mike', age: 52 } ] } | ||
// INSERT with named parameters | ||
let insert = await data.query( | ||
`INSERT INTO myTable (name,age,has_curls) VALUES(:name,:age,:curls)`, | ||
{ name: 'Greg', age: 18, curls: false } | ||
) | ||
let insert = await data.query(`INSERT INTO myTable (name,age,has_curls) VALUES(:name,:age,:curls)`, { | ||
name: 'Greg', | ||
age: 18, | ||
curls: false | ||
}) | ||
// BATCH INSERT with named parameters | ||
let batchInsert = await data.query( | ||
`INSERT INTO myTable (name,age,has_curls) VALUES(:name,:age,:curls)`, | ||
[ | ||
[{ name: 'Marcia', age: 17, curls: false }], | ||
[{ name: 'Peter', age: 15, curls: false }], | ||
[{ name: 'Jan', age: 15, curls: false }], | ||
[{ name: 'Cindy', age: 12, curls: true }], | ||
[{ name: 'Bobby', age: 12, curls: false }] | ||
] | ||
) | ||
let batchInsert = await data.query(`INSERT INTO myTable (name,age,has_curls) VALUES(:name,:age,:curls)`, [ | ||
[{ name: 'Marcia', age: 17, curls: false }], | ||
[{ name: 'Peter', age: 15, curls: false }], | ||
[{ name: 'Jan', age: 15, curls: false }], | ||
[{ name: 'Cindy', age: 12, curls: true }], | ||
[{ name: 'Bobby', age: 12, curls: false }] | ||
]) | ||
// Update with named parameters | ||
let update = await data.query( | ||
`UPDATE myTable SET age = :age WHERE id = :id`, | ||
{ age: 13, id: 5 } | ||
) | ||
let update = await data.query(`UPDATE myTable SET age = :age WHERE id = :id`, { age: 13, id: 5 }) | ||
@@ -77,6 +74,3 @@ // Delete with named parameters | ||
database: 'myOtherDatabase', | ||
parameters: [ | ||
{ id: 123}, | ||
{ name: 'isActive', value: { booleanValue: true } } | ||
] | ||
parameters: [{ id: 123 }, { name: 'isActive', value: { booleanValue: true } }] | ||
}) | ||
@@ -86,2 +80,3 @@ ``` | ||
## Why do I need this? | ||
The [Data API](https://docs.aws.amazon.com/AmazonRDS/latest/AuroraUserGuide/data-api.html) requires you to specify data types when passing in parameters. The basic `INSERT` example above would look like this using the native `AWS.RDSDataService` class: | ||
@@ -125,5 +120,7 @@ | ||
``` | ||
Not only are there no column names, but you have to pull the value from the data type field. Lots of extra work that the **Data API Client** handles automatically for you. 😀 | ||
## Installation and Setup | ||
``` | ||
@@ -139,17 +136,18 @@ npm i data-api-client | ||
| Property | Type | Description | Default | | ||
| -------- | ---- | ----------- | ------- | | ||
| AWS | `AWS` | A custom `aws-sdk` instance | | | ||
| resourceArn | `string` | The ARN of your Aurora Serverless Cluster. This value is *required*, but can be overridden when querying. | | | ||
| secretArn | `string` | The ARN of the secret associated with your database credentials. This is *required*, but can be overridden when querying. | | | ||
| database | `string` | *Optional* default database to use with queries. Can be overridden when querying. | | | ||
| engine | `mysql` or `pg` | The type of database engine you're connecting to (MySQL or Postgres). | `mysql` | | ||
| hydrateColumnNames | `boolean` | When `true`, results will be returned as objects with column names as keys. If `false`, results will be returned as an array of values. | `true` | | ||
| ~~keepAlive~~ (deprecated) | `boolean` | See [Connection Reuse](#connection-reuse) below. | | | ||
| ~~sslEnabled~~ (deprecated) | `boolean` | Set this in the `options` | `true` | | ||
| options | `object` | An *optional* configuration object that is passed directly into the RDSDataService constructor. See [here](https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/RDSDataService.html#constructor-property) for available options. | `{}` | | ||
| ~~region~~ (deprecated) | `string` | Set this in the `options` | | | ||
| formatOptions | `object` | Formatting options to auto parse dates and coerce native JavaScript date objects to MySQL supported date formats. Valid keys are `deserializeDate` and `treatAsLocalDate`. Both accept boolean values. | Both `false` | | ||
| Property | Type | Description | Default | | ||
| --------------------------- | --------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------ | | ||
| AWS | `AWS` | A custom `aws-sdk` instance | | | ||
| resourceArn | `string` | The ARN of your Aurora Serverless Cluster. This value is _required_, but can be overridden when querying. | | | ||
| secretArn | `string` | The ARN of the secret associated with your database credentials. This is _required_, but can be overridden when querying. | | | ||
| database | `string` | _Optional_ default database to use with queries. Can be overridden when querying. | | | ||
| engine | `mysql` or `pg` | The type of database engine you're connecting to (MySQL or Postgres). | `mysql` | | ||
| hydrateColumnNames | `boolean` | When `true`, results will be returned as objects with column names as keys. If `false`, results will be returned as an array of values. | `true` | | ||
| ~~keepAlive~~ (deprecated) | `boolean` | See [Connection Reuse](#connection-reuse) below. | | | ||
| ~~sslEnabled~~ (deprecated) | `boolean` | Set this in the `options` | `true` | | ||
| options | `object` | An _optional_ configuration object that is passed directly into the RDSDataService constructor. See [here](https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/RDSDataService.html#constructor-property) for available options. | `{}` | | ||
| ~~region~~ (deprecated) | `string` | Set this in the `options` | | | ||
| formatOptions | `object` | Formatting options to auto parse dates and coerce native JavaScript date objects to MySQL supported date formats. Valid keys are `deserializeDate` and `treatAsLocalDate`. Both accept boolean values. | Both `false` | | ||
### Connection Reuse | ||
It is recommended to enable connection reuse as this dramatically decreases the latency of subsequent calls to the AWS API. This can be done by setting an environment variable | ||
@@ -174,2 +172,3 @@ `AWS_NODEJS_CONNECTION_REUSE_ENABLED=1`. For more information see the [AWS SDK documentation](https://docs.aws.amazon.com/sdk-for-javascript/v2/developer-guide/node-reusing-connections.html). | ||
### Running a query | ||
Once initialized, running a query is super simple. Use the `query()` method and pass in your SQL statement: | ||
@@ -182,4 +181,5 @@ | ||
By default, this will return your rows as an array of objects with column names as property names: | ||
```javascript | ||
[ | ||
;[ | ||
{ id: 1, name: 'Alice', age: null }, | ||
@@ -194,3 +194,4 @@ { id: 2, name: 'Mike', age: 52 }, | ||
```javascript | ||
let result = await data.query(` | ||
let result = await data.query( | ||
` | ||
SELECT * FROM myTable WHERE id = :id AND created > :createDate`, | ||
@@ -204,10 +205,8 @@ { id: 2, createDate: '2019-06-01' } | ||
```javascript | ||
let result = await data.query( | ||
`SELECT * FROM myTable WHERE id = :id AND created > :createDate`, | ||
[ // An array of objects is totally cool, too. We'll merge them for you. | ||
{ id: 2 }, | ||
// Data API Client just passes this straight on through | ||
{ name: 'createDate', value: { blobValue: new Buffer('2019-06-01') } } | ||
] | ||
) | ||
let result = await data.query(`SELECT * FROM myTable WHERE id = :id AND created > :createDate`, [ | ||
// An array of objects is totally cool, too. We'll merge them for you. | ||
{ id: 2 }, | ||
// Data API Client just passes this straight on through | ||
{ name: 'createDate', value: { blobValue: new Buffer('2019-06-01') } } | ||
]) | ||
``` | ||
@@ -230,16 +229,14 @@ | ||
Sometimes you might want to have *dynamic identifiers* in your SQL statements. Unfortunately, the `RDSDataService` doesn't do this, but the **Data API Client** does! We're using the [sqlstring](https://github.com/mysqljs/sqlstring) module under the hood, so as long as [NO_BACKSLASH_ESCAPES](https://dev.mysql.com/doc/refman/5.7/en/sql-mode.html#sqlmode_no_backslash_escapes) SQL mode is disabled (which is the default state for Aurora Serverless), you're good to go. Use a double colon (`::`) prefix to create *named identifiers* and you can do cool things like this: | ||
Sometimes you might want to have _dynamic identifiers_ in your SQL statements. Unfortunately, the `RDSDataService` doesn't do this, but the **Data API Client** does! We're using the [sqlstring](https://github.com/mysqljs/sqlstring) module under the hood, so as long as [NO_BACKSLASH_ESCAPES](https://dev.mysql.com/doc/refman/5.7/en/sql-mode.html#sqlmode_no_backslash_escapes) SQL mode is disabled (which is the default state for Aurora Serverless), you're good to go. Use a double colon (`::`) prefix to create _named identifiers_ and you can do cool things like this: | ||
```javascript | ||
let result = await data.query( | ||
`SELECT ::fields FROM ::table WHERE id > :id`, | ||
{ | ||
fields: ['id','name','created'], | ||
table: 'table_' + someScaryUserInput, // someScaryUserInput = 123abc | ||
id: 1 | ||
} | ||
) | ||
let result = await data.query(`SELECT ::fields FROM ::table WHERE id > :id`, { | ||
fields: ['id', 'name', 'created'], | ||
table: 'table_' + someScaryUserInput, // someScaryUserInput = 123abc | ||
id: 1 | ||
}) | ||
``` | ||
Which will produce a query like this: | ||
```sql | ||
@@ -249,5 +246,6 @@ SELECT `id`, `name`, `created` FROM `table_123abc` WHERE id > :id LIMIT 10 | ||
You'll notice that we leave the *named parameters* alone. Anything that Data API and the `RDSDataService` Class currently handles, we defer to them. | ||
You'll notice that we leave the _named parameters_ alone. Anything that Data API and the `RDSDataService` Class currently handles, we defer to them. | ||
### Type-Casting | ||
The Aurora Data API can sometimes give you trouble with certain data types, such as uuid, unless you explicitly cast them. While you can certainly do this manually in your SQL string, the Data API Client offers a really easy way to handle this for you. | ||
@@ -282,17 +280,15 @@ | ||
### Batch Queries | ||
The `RDSDataService` Class provides a `batchExecuteStatement` method that allows you to execute a prepared statement multiple times using different parameter sets. This is only allowed for `INSERT`, `UPDATE` and `DELETE` queries, but is much more efficient than issuing multiple `executeStatement` calls. The Data API Client handles the switching for you based on *how* you send in your parameters. | ||
The `RDSDataService` Class provides a `batchExecuteStatement` method that allows you to execute a prepared statement multiple times using different parameter sets. This is only allowed for `INSERT`, `UPDATE` and `DELETE` queries, but is much more efficient than issuing multiple `executeStatement` calls. The Data API Client handles the switching for you based on _how_ you send in your parameters. | ||
To issue a batch query, use the `query()` method (either by passing an object or using the two arity form), and provide multiple parameter sets as nested arrays. For example, if you wanted to update multiple records at once, your query might look like this: | ||
```javascript | ||
let result = await data.query( | ||
`UPDATE myTable SET name = :newName WHERE id = :id`, | ||
[ | ||
[ { id: 1, newName: 'Alice Franklin' } ], | ||
[ { id: 7, newName: 'Jan Glass' } ] | ||
] | ||
) | ||
let result = await data.query(`UPDATE myTable SET name = :newName WHERE id = :id`, [ | ||
[{ id: 1, newName: 'Alice Franklin' }], | ||
[{ id: 7, newName: 'Jan Glass' }] | ||
]) | ||
``` | ||
You can also use *named identifiers* in batch queries, which will update and escape your SQL statement. **ONLY** parameters from the first parameter set will be used to update the query. Subsequent parameter sets will only update *named parameters* supported by the Data API. | ||
You can also use _named identifiers_ in batch queries, which will update and escape your SQL statement. **ONLY** parameters from the first parameter set will be used to update the query. Subsequent parameter sets will only update _named parameters_ supported by the Data API. | ||
@@ -302,5 +298,7 @@ Whenever a batch query is executed, it returns an `updateResults` field. Other than for `INSERT` statements, however, there is no useful feedback provided by this field. | ||
### Retrieving Insert IDs | ||
The Data API returns a `generatedFields` array that contains the value of auto-incrementing primary keys. If this value is returned, the Data API Client will parse this and return it as the `insertId`. This also works for batch queries as well. | ||
## Transaction Support | ||
Transaction support in the Data API Client has been dramatically simplified. Start a new transaction using the `transaction()` method, and then chain queries using the `query()` method. The `query()` method supports all standard query options. Alternatively, you can specify a function as the only argument in a `query()` method call and return the arguments as an array of values. The function receives two arguments, the result of the last query executed, and an array containing all the previous query results. This is useful if you need values from a previous query as part of your transaction. | ||
@@ -321,6 +319,9 @@ | ||
```javascript | ||
let results = await mysql.transaction() | ||
let results = await mysql | ||
.transaction() | ||
.query('INSERT INTO myTable (name) VALUES(:name)', { name: 'Tiger' }) | ||
.query((r) => [ 'UPDATE myTable SET age = :age WHERE id = :id', { age: 4, id: r.insertId } ]) | ||
.rollback((e,status) => { /* do something with the error */ }) // optional | ||
.query((r) => ['UPDATE myTable SET age = :age WHERE id = :id', { age: 4, id: r.insertId }]) | ||
.rollback((e, status) => { | ||
/* do something with the error */ | ||
}) // optional | ||
.commit() // execute the queries | ||
@@ -335,3 +336,4 @@ ``` | ||
The Data API Client exposes *promisified* versions of the five RDSDataService methods. These are: | ||
The Data API Client exposes _promisified_ versions of the five RDSDataService methods. These are: | ||
- `batchExecuteStatement` | ||
@@ -380,5 +382,7 @@ - `beginTransaction` | ||
## Data API Limitations / Wonkiness | ||
The first GA release of the Data API has *a lot* of promise, unfortunately, there are still quite a few things that make it a bit wonky and may require you to implement some workarounds. I've outlined some of my findings below. | ||
The first GA release of the Data API has _a lot_ of promise, unfortunately, there are still quite a few things that make it a bit wonky and may require you to implement some workarounds. I've outlined some of my findings below. | ||
### You can't send in an array of values | ||
The GitHub repo for RDSDataService mentions something about `arrayValues`, but I've been unable to get arrays (including TypedArrays and Buffers) to be used for parameters with `IN` clauses. For example, the following query will **NOT** work: | ||
@@ -401,5 +405,7 @@ | ||
### ~~Named parameters MUST be sent in order~~ | ||
~~Read that again if you need to. So parameters have to be **BOTH** named and *in order*, otherwise the query **may** fail. I stress **may**, because if you send in two fields of compatible type in the wrong order, the query will work, just with your values flipped. 🤦🏻♂️ Watch out for this one.~~ 👈This was fixed! | ||
~~Read that again if you need to. So parameters have to be **BOTH** named and _in order_, otherwise the query **may** fail. I stress **may**, because if you send in two fields of compatible type in the wrong order, the query will work, just with your values flipped. 🤦🏻♂️ Watch out for this one.~~ 👈This was fixed! | ||
### You can't parameterize identifiers | ||
If you want to use dynamic column or field names, there is no way to do it automatically with the Data API. The `mysql` package, for example, lets you use `??` to dynamically insert escaped identifiers. Something like the example below is currently not possible. | ||
@@ -423,7 +429,8 @@ | ||
### Batch statements do not give you updated record counts | ||
### Batch statements do not give you updated record counts | ||
This one is a bit frustrating. If you execute a standard `executeStatement`, then it will return a `numberOfRecordsUpdated` field for `UPDATE` and `DELETE` queries. This is handy for knowing if your query succeeded. Unfortunately, a `batchExecuteStatement` does not return this field for you. | ||
## Enabling Data API | ||
In order to use the Data API, you must enable it on your Aurora Serverless Cluster and create a Secret. You also must grant your execution environment a number of permission as outlined in the following sections. | ||
@@ -435,3 +442,3 @@ | ||
You need to modify your Aurora Serverless cluster by clicking “ACTIONS” and then “Modify Cluster”. Just check the Data API box in the *Network & Security* section and you’re good to go. Remember that your Aurora Serverless cluster still runs in a VPC, even though you don’t need to run your Lambdas in a VPC to access it via the Data API. | ||
You need to modify your Aurora Serverless cluster by clicking “ACTIONS” and then “Modify Cluster”. Just check the Data API box in the _Network & Security_ section and you’re good to go. Remember that your Aurora Serverless cluster still runs in a VPC, even though you don’t need to run your Lambdas in a VPC to access it via the Data API. | ||
@@ -444,3 +451,2 @@ ### Set up a secret in the Secrets Manager | ||
Next we give it a name, this is important, because this will be part of the arn when we set up permissions later. You can give it a description as well so you don’t forget what this secret is about when you look at it in a few weeks. | ||
@@ -459,20 +465,22 @@ | ||
**YAML:** | ||
```yaml | ||
Statement: | ||
- Effect: "Allow" | ||
- Effect: 'Allow' | ||
Action: | ||
- "rds-data:ExecuteSql" | ||
- "rds-data:ExecuteStatement" | ||
- "rds-data:BatchExecuteStatement" | ||
- "rds-data:BeginTransaction" | ||
- "rds-data:RollbackTransaction" | ||
- "rds-data:CommitTransaction" | ||
Resource: "*" | ||
- Effect: "Allow" | ||
- 'rds-data:ExecuteSql' | ||
- 'rds-data:ExecuteStatement' | ||
- 'rds-data:BatchExecuteStatement' | ||
- 'rds-data:BeginTransaction' | ||
- 'rds-data:RollbackTransaction' | ||
- 'rds-data:CommitTransaction' | ||
Resource: '*' | ||
- Effect: 'Allow' | ||
Action: | ||
- "secretsmanager:GetSecretValue" | ||
Resource: "arn:aws:secretsmanager:{REGION}:{ACCOUNT-ID}:secret:{PATH-TO-SECRET}/*" | ||
- 'secretsmanager:GetSecretValue' | ||
Resource: 'arn:aws:secretsmanager:{REGION}:{ACCOUNT-ID}:secret:{PATH-TO-SECRET}/*' | ||
``` | ||
**JSON:** | ||
```javascript | ||
@@ -500,8 +508,4 @@ "Statement" : [ | ||
## Sponsors | ||
## Contributions | ||
[![New Relic](https://user-images.githubusercontent.com/2053544/96728664-55238700-1382-11eb-93cb-82fe7cb5e043.png)](https://ad.doubleclick.net/ddm/trackclk/N1116303.3950900PODSEARCH.COM/B24770737.285235234;dc_trk_aid=479074825;dc_trk_cid=139488579;dc_lat=;dc_rdid=;tag_for_child_directed_treatment=;tfua=;gdpr=${GDPR};gdpr_consent=${GDPR_CONSENT_755}) | ||
<IMG SRC="https://ad.doubleclick.net/ddm/trackimp/N1116303.3950900PODSEARCH.COM/B24770737.285235234;dc_trk_aid=479074825;dc_trk_cid=139488579;ord=[timestamp];dc_lat=;dc_rdid=;tag_for_child_directed_treatment=;tfua=;gdpr=${GDPR};gdpr_consent=${GDPR_CONSENT_755}?" BORDER="0" HEIGHT="1" WIDTH="1" ALT="Advertisement"> | ||
## Contributions | ||
Contributions, ideas and bug reports are welcome and greatly appreciated. Please add [issues](https://github.com/jeremydaly/data-api-client/issues) for suggestions and bug reports or create a pull request. You can also contact me on Twitter: [@jeremy_daly](https://twitter.com/jeremy_daly). |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
52049
490