Skip to content

Commit

Permalink
Merge branch 'master' into customMethods
Browse files Browse the repository at this point in the history
  • Loading branch information
fishcharlie committed Apr 1, 2020
2 parents 8c2dddb + cd839d6 commit f5b4f14
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 36 deletions.
5 changes: 5 additions & 0 deletions BREAKING_CHANGES.md
Expand Up @@ -9,9 +9,13 @@
- `$LISTAPPEND` has been removed, and `$ADD` now includes the behavior of `$LISTAPPEND`
- `$DELETE` now maps to the correct underlying DynamoDB method instead of the previous behavior of mapping to `$REMOVE`
- `$PUT` has been replaced with `$SET`
- `dynamoose.setDefaults` has been renamed to `dynamoose.Model.defaults`, and is now a property that you set, vs a method that you pass the object into
- `dynamoose.model` has been renamed to `dynamoose.Model`
- `dynamoose.local` has been renamed to `dynamoose.aws.ddb.local`
- `dynamoose.setDDB` has been renamed to `dynamoose.aws.ddb.set`
- `dynamoose.revertDDB` has been renamed to `dynamoose.aws.ddb.revert`
- `dynamoose.AWS.config.update` has been renamed to `dynamoose.aws.sdk.config.update`
- `dynamoose.ddb` has been renamed to `dynamoose.aws.ddb`
- `Model.getTableReq` has been renamed to `Model.table.create.request`
- `Model.table.create.request` (formerly `Model.getTableReq`) is now an async function
- `model.originalItem` has been renamed to `model.original` (or `Document.original`)
Expand All @@ -26,6 +30,7 @@
- `useNativeBooleans` & `useDocumentTypes` have been removed from the Model settings
- `Map` attribute type has been replaced with `Object`
- `List` attribute type has been replaced with `Array`
- The attribute types `Array` & `Object` in Dynamoose v1 don't work without a `schema` option in v2
- `Scan.null` & `Query.null` have been removed. In most cases this can be replaced with `.not().exists()`.
- DynamoDB set types are now returned as JavaScript Set's instead of Array's
- Trying to save a Document with a property set to `null` will now throw an error. If you would like to remove the property set it to `dynamoose.undefined` to set it to undefined without taking into account the `default` setting, or `undefined` to set it to undefined while taking into account the `default` setting.
Expand Down
4 changes: 1 addition & 3 deletions README.md
@@ -1,6 +1,4 @@
# Dynamoose

[![Slack Chat](https://img.shields.io/badge/chat-on%20slack-informational.svg)](https://join.slack.com/t/dynamoose/shared_invite/enQtODM4OTI0MTc1NDc3LWI3MmNhMThmNmJmZDk5MmUxOTZmMGEwNGQzNTRkMjhjZGJlNGM5M2JmZjMzMzlkODRhMGY3MTQ5YjQ2Nzg3YTY) [![Twitter Follow](https://img.shields.io/twitter/follow/dynamoosejs?style=social)](https://twitter.com/DynamooseJS) [![CI](https://github.com/dynamoosejs/dynamoose/workflows/CI/badge.svg)](https://github.com/dynamoosejs/dynamoose/actions) [![Coverage Status](https://coveralls.io/repos/github/dynamoosejs/dynamoose/badge.svg?branch=master)](https://coveralls.io/github/dynamoosejs/dynamoose?branch=master)
# Dynamoose [![Slack Chat](https://img.shields.io/badge/chat-on%20slack-informational.svg)](https://join.slack.com/t/dynamoose/shared_invite/enQtODM4OTI0MTc1NDc3LWI3MmNhMThmNmJmZDk5MmUxOTZmMGEwNGQzNTRkMjhjZGJlNGM5M2JmZjMzMzlkODRhMGY3MTQ5YjQ2Nzg3YTY) [![Twitter Follow](https://img.shields.io/twitter/follow/dynamoosejs?style=social)](https://twitter.com/DynamooseJS) [![CI](https://github.com/dynamoosejs/dynamoose/workflows/CI/badge.svg)](https://github.com/dynamoosejs/dynamoose/actions) [![Coverage Status](https://coveralls.io/repos/github/dynamoosejs/dynamoose/badge.svg?branch=master)](https://coveralls.io/github/dynamoosejs/dynamoose?branch=master)

# Dynamoose Version 2.0 is still In Development. Please [contact me](https://charlie.fish/contact) with any feedback or questions.

Expand Down
66 changes: 34 additions & 32 deletions docs/api/Query.md
Expand Up @@ -7,7 +7,6 @@ Dynamoose provides the ability to query a model by using the `Model.query` funct
This is the basic entry point to construct a query request. The filter property is optional and can either be an object or a string representing the key you which to first filter on. In the event you don't pass in any parameters and don't call any other methods on the query object it will query with no filters or options.

```js
Cat.query().exec() // will query all items with no filters or options
Cat.query("breed").contains("Terrier").exec() // will query all items and filter all items where the key `breed` contains `Terrier`
Cat.query({"breed": {"contains": "Terrier"}}).exec() // will query all items and filter all items where the key `breed` contains `Terrier`
```
Expand All @@ -19,7 +18,7 @@ If you pass an object into `Model.query` the object for each key should contain
This will execute the query you constructed. If you pass in a callback the callback function will be executed upon completion passing in an error (if exists), and the results array. In the event you didn't pass in a callback parameter, a promise will be returned that will resolve to the results array upon completion.

```js
Cat.query().exec((error, results) => {
Cat.query("name").eq("Will").exec((error, results) => {
if (error) {
console.error(error);
} else {
Expand All @@ -29,6 +28,7 @@ Cat.query().exec((error, results) => {
// count: 1,
// queriedCount: 2,
// timesQueried: 1 ]
console.log(Array.isArray(results)); // true
}
});
```
Expand All @@ -38,7 +38,9 @@ const results = await Cat.query().exec();
// `results` is identical to what is listed in the callback version of this function.
```

The results array will have some special parameters attached to it to give you more information about the query operation:
The `results` array you receive back is a standard JavaScript array of objects. However, the array has some special properties with extra information about your query operation that you can access. This does not prevent the ability do running loops or accessing the objects within the array.

The extra properties attached to the array are:

- `lastKey` - In the event there are more items to query in DynamoDB this property will be equal to an object that you can pass into [`query.startAt(key)`](#querystartatkey) to retrieve more items. Normally DynamoDB returns this property as a DynamoDB object, but Dynamoose returns it and handles it as a standard JS object without the DynamoDB types.
- `count` - The count property from DynamoDB, which represents how many items were returned from DynamoDB. This should always equal the number of items in the array.
Expand All @@ -51,8 +53,8 @@ This function has no behavior and is only used to increase readability of your q

```js
// The two query objects below are identical
Cat.query().filter("id").eq(1).and().filter("name").eq("Bob");
Cat.query().filter("id").eq(1).filter("name").eq("Bob");
Animal.query("name").eq("Will").filter("age").eq(1).and().filter("type").eq("Dog");
Animal.query("name").eq("Will").filter("age").eq(1).filter("type").eq("Dog");
```

## query.not()
Expand All @@ -73,8 +75,8 @@ As well the following comparisons do not have an opposite comparison type, and w
- begins with (BEGINS_WITH)

```js
Cat.query().filter("id").not().eq(1); // Retrieve all objects where id does NOT equal 1
Cat.query().filter("id").not().between(1, 2); // Will throw error since between does not have an opposite comparison type
Cat.query("name").eq("Will").filter("age").not().eq(1); // Retrieve all objects where id does NOT equal 1
Cat.query("name").eq("Will").filter("age").not().between(1, 2); // Will throw error since between does not have an opposite comparison type
```

## query.filter(key)
Expand All @@ -84,8 +86,8 @@ This function prepares a new filter conditional to be used with the request. If
You will use this function with a comparison function which will complete the filter conditional and allow you to start another filter conditional if you wish.

```js
Cat.query().filter("id"); // Currently this query has no filter behavior and will query all items in the table
Cat.query().filter("id").eq(1); // Since this query has a comparison function (eq) after the filter it will complete the filter conditional and only query items where `id` = 1
Cat.query("name").eq("Will").filter("age"); // Currently this query has no filter behavior and will query all items in the table
Cat.query("name").eq("Will").filter("age").eq(1); // Since this query has a comparison function (eq) after the filter it will complete the filter conditional and only query items where `id` = 1
```

## query.where(key)
Expand All @@ -95,98 +97,98 @@ This function prepares a new range key conditional to query with the request. If
You will use this function with a comparison function which will complete the conditional and allow you to start another conditional if you wish.

```js
Cat.query().where("id"); // Currently this query has no behavior and will query all items in the table
Cat.query().where("id").eq(1); // Since this query has a comparison function (eq) after the conditional it will complete the conditional and only query items where `id` = 1
Cat.query("name").eq("Will"); // Currently this query has no behavior and will query all items in the table
Cat.query("name").eq("Will"); // Since this query has a comparison function (eq) after the conditional it will complete the conditional and only query items where `id` = 1
```

## query.eq(value)

This comparison function will check to see if the given filter key is equal to the value you pass in as a parameter.

```js
Cat.query().filter("name").eq("Tom"); // Return all items where `name` equals `Tom`
Cat.query("name").eq("Will").filter("name").eq("Tom"); // Return all items where `name` equals `Tom`
```

## query.exists()

This comparison function will check to see if the given filter key exists in the document.

```js
Cat.query().filter("phoneNumber").exists(); // Return all items where `phoneNumber` exists in the document
Cat.query("name").eq("Will").filter("phoneNumber").exists(); // Return all items where `phoneNumber` exists in the document

Cat.query().filter("phoneNumber").not().exists(); // Return all items where `phoneNumber` does not exist in the document
Cat.query("name").eq("Will").filter("phoneNumber").not().exists(); // Return all items where `phoneNumber` does not exist in the document
```

## query.lt(value)

This comparison function will check to see if the given filter key is less than the value you pass in as a parameter.

```js
Cat.query().filter("age").lt(5); // Return all items where `age` is less than 5
Cat.query("name").eq("Will").filter("age").lt(5); // Return all items where `age` is less than 5
```

## query.le(value)

This comparison function will check to see if the given filter key is less than or equal to the value you pass in as a parameter.

```js
Cat.query().filter("age").le(5); // Return all items where `age` is less than or equal to 5
Cat.query("name").eq("Will").filter("age").le(5); // Return all items where `age` is less than or equal to 5
```

## query.gt(value)

This comparison function will check to see if the given filter key is greater than the value you pass in as a parameter.

```js
Cat.query().filter("age").gt(5); // Return all items where `age` is greater than 5
Cat.query("name").eq("Will").filter("age").gt(5); // Return all items where `age` is greater than 5
```

## query.ge(value)

This comparison function will check to see if the given filter key is greater than or equal to the value you pass in as a parameter.

```js
Cat.query().filter("age").ge(5); // Return all items where `age` is greater than or equal to 5
Cat.query("name").eq("Will").filter("age").ge(5); // Return all items where `age` is greater than or equal to 5
```

## query.beginsWith(value)

This comparison function will check to see if the given filter key begins with the value you pass in as a parameter.

```js
Cat.query().filter("name").beginsWith("T"); // Return all items where `name` begins with `T`
Cat.query("name").eq("Will").filter("name").beginsWith("T"); // Return all items where `name` begins with `T`
```

## query.contains(value)

This comparison function will check to see if the given filter key contains the value you pass in as a parameter.

```js
Cat.query().filter("name").contains("om"); // Return all items where `name` contains `om`
Cat.query("name").eq("Will").filter("name").contains("om"); // Return all items where `name` contains `om`
```

## query.in(values)

This comparison function will check to see if the given filter key equals any of the items you pass in in the values array you pass in. The `values` parameter must be an array and will only return results where the value for the given key exists in the array you pass in.

```js
Cat.query("name").in(["Charlie", "Bob"]) // Return all items where `name` = `Charlie` OR `Bob`
Animal.query("name").eq("Will").filter("type").in(["Dog", "Cat"]) // Return all items where `name` = `Will` and filter where type = `Dog` OR `Cat`
```

## query.between(a, b)

This comparison function will check to see if the given filter key is between the two values you pass in as parameters.

```js
Cat.query().filter("age").between(5, 9); // Return all items where `age` is between 5 and 9
Cat.query("name").eq("Will").filter("age").between(5, 9); // Return all items where `age` is between 5 and 9
```

## query.limit(count)

This function will limit the number of items that DynamoDB will query in this request. Unlike most SQL databases this does not guarantee the response will contain 5 items. Instead DynamoDB will only query a maximum of 5 items to see if they match and should be returned. The `count` parameter passed in should be a number representing how many items you wish DynamoDB to query.

```js
Cat.query().limit(5); // Limit query request to 5 items
Cat.query("name").eq("Will").limit(5); // Limit query request to 5 items
```

## query.startAt(key)
Expand All @@ -196,24 +198,24 @@ In the event there are more items to query in a previous response, Dynamoose wil
Although the `.lastKey` property returns a standard (non DynamoDB) object, you can pass a standard object OR DynamoDB object into this function, and it will handle either case.

```js
const response = await Cat.query().exec();
const moreItems = Cat.query().startAt(response.lastKey);
const response = await Cat.query("name").eq("Will").exec();
const moreItems = Cat.query("name").eq("Will").startAt(response.lastKey);
```

## query.attributes(attributes)

This function will limit which attributes DynamoDB returns for each item in the table. This can limit the size of the DynamoDB response and helps you only retrieve the data you need. The `attributes` property passed into this function should be an array of strings representing the property names you wish DynamoDB to return.

```js
Cat.query().attributes(["id", "name"]); // Return all documents but only return the `id` & `name` properties for each item
Cat.query("name").eq("Will").attributes(["id", "name"]); // Return all documents but only return the `id` & `name` properties for each item
```

## query.count()

Instead of returning the items in the array this function will cause the query operation to return a special object with the count information for the query. The response you will receive from the query operation with this setting will be an object with the properties `count` & `queriedCount`, which have the same values as described in [`query.exec([callback])`](#queryexeccallback).

```js
const response = await Cat.query().count().exec();
const response = await Cat.query("name").eq("Will").count().exec();
console.log(response); // {"count": 1, "queriedCount": 1}
```

Expand All @@ -222,15 +224,15 @@ console.log(response); // {"count": 1, "queriedCount": 1}
This will cause the query to run in a consistent manner as opposed to the default eventually consistent manner.

```js
Cat.query().consistent(); // Run the query in a consistent manner
Cat.query("name").eq("Will").consistent(); // Run the query in a consistent manner
```

## query.using(index)

This causes the query to be run on a specific index as opposed to the default table wide query. The `index` parameter you pass in should represent the name of the index you wish to query on.

```js
Cat.query().using("name-index"); // Run the query on the `name-index` index
Cat.query("name").eq("Will").using("name-index"); // Run the query on the `name-index` index
```

## query.all([delay[, max]])
Expand All @@ -245,7 +247,7 @@ Two parameters can be specified on this setting:
The items for all of the requests will be merged into a single array with the `count` & `queriedCount` properties being summed in the response. If you set a maximum number of query requests and there is still a `lastKey` on the response that will be returned to you.

```js
Cat.query().all(); // Query table and so long as the `lastKey` property exists continuously query the table to retrieve all items
Cat.query().all(100); // Query table and so long as the `lastKey` property exists continuously query the table to retrieve all items with a 100 ms delay before the next query request
Cat.query().all(0, 5); // Query table and so long as the `lastKey` property exists continuously query the table to retrieve all items with a maximum of 5 requests total
Cat.query("name").eq("Will").all(); // Query table and so long as the `lastKey` property exists continuously query the table to retrieve all items
Cat.query("name").eq("Will").all(100); // Query table and so long as the `lastKey` property exists continuously query the table to retrieve all items with a 100 ms delay before the next query request
Cat.query("name").eq("Will").all(0, 5); // Query table and so long as the `lastKey` property exists continuously query the table to retrieve all items with a maximum of 5 requests total
```
5 changes: 4 additions & 1 deletion docs/api/Scan.md
Expand Up @@ -29,6 +29,7 @@ Cat.scan().exec((error, results) => {
// count: 1,
// scannedCount: 2,
// timesScanned: 1 ]
console.log(Array.isArray(results)); // true
}
});
```
Expand All @@ -38,7 +39,9 @@ const results = await Cat.scan().exec();
// `results` is identical to what is listed in the callback version of this function.
```

The results array will have some special parameters attached to it to give you more information about the scan operation:
The `results` array you receive back is a standard JavaScript array of objects. However, the array has some special properties with extra information about your scan operation that you can access. This does not prevent the ability do running loops or accessing the objects within the array.

The extra properties attached to the array are:

- `lastKey` - In the event there are more items to scan in DynamoDB this property will be equal to an object that you can pass into [`scan.startAt(key)`](#scanstartatkey) to retrieve more items. Normally DynamoDB returns this property as a DynamoDB object, but Dynamoose returns it and handles it as a standard JS object without the DynamoDB types.
- `count` - The count property from DynamoDB, which represents how many items were returned from DynamoDB. This should always equal the number of items in the array.
Expand Down

0 comments on commit f5b4f14

Please sign in to comment.