Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added PutItemQueryBuilder #13

Merged
merged 12 commits into from
Mar 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 42 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@ Type-friendly DynamoDB query builder! Inspired by [Kysely](https://github.com/ky

Usable with AWS SDK v3 `DynamoDBDocumentClient`.

> [!NOTE]
> Currently this is a POC and a WIP. Currently, `get-item` and `query` operations are
> supported, but I am planning to add support for the rest of the operations too.

![](https://github.com/woltsu/tsynamo/blob/main/assets/demo.gif)

# Installation
Expand Down Expand Up @@ -163,11 +159,48 @@ await tsynamoClient
> This would compile as the following FilterExpression:
> `NOT eventType = "LOG_IN"`, i.e. return all events whose types is not "LOG_IN"

## Delete item
## Put item

WIP
### Simple put item

## Put item
```ts
await tsynamoClient
.putItem("myTable")
.item({
userId: "123",
eventId: 313,
})
.execute();
```

### Put item with ConditionExpression

```ts
await tsynamoClient
.putItem("myTable")
.item({
userId: "123",
eventId: 313,
})
.conditionExpression("userId", "attribute_not_exists")
.execute();
```

### Put item with multiple ConditionExpressions

```ts
await tsynamoClient
.putItem("myTable")
.item({
userId: "123",
eventId: 313,
})
.conditionExpression("userId", "attribute_not_exists")
.orConditionExpression("eventType", "begins_with", "LOG_")
.execute();
```

## Delete item

WIP

Expand All @@ -180,8 +213,9 @@ WIP
WIP

# Contributors

<p>
<a href="https://github.com/woltsu/tsynamo/graphs/contributors">
<img src="https://contrib.rocks/image?repo=woltsu/tsynamo" />
</a>
</p>
</p>
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "tsynamo",
"author": "woltsu",
"version": "0.0.5",
"version": "0.0.6",
"description": "Typed query builder for DynamoDB",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
8 changes: 8 additions & 0 deletions src/nodes/expressionComparatorExpression.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { ExpressionConditionComparators } from "./operands";

export type ExpressionComparatorExpressions = {
readonly kind: "ExpressionComparatorExpressions";
readonly key: string;
readonly operation: ExpressionConditionComparators;
readonly value: unknown;
};
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,18 @@ import { AttributeNotExistsFunctionExpression } from "./attributeNotExistsFuncti
import { BeginsWithFunctionExpression } from "./beginsWithFunctionExpression";
import { BetweenConditionExpression } from "./betweenConditionExpression";
import { ContainsFunctionExpression } from "./containsFunctionExpression";
import { FilterExpressionComparatorExpressions } from "./filterExpressionComparatorExpression";
import { FilterExpressionNode } from "./filterExpressionNode";
import { FilterExpressionNotExpression } from "./filterExpressionNotExpression";
import { ExpressionComparatorExpressions } from "./expressionComparatorExpression";
import { ExpressionNode } from "./expressionNode";
import { ExpressionNotExpression } from "./expressionNotExpression";

export type JoinType = "AND" | "OR";

export type FilterExpressionJoinTypeNode = {
readonly kind: "FilterExpressionJoinTypeNode";
export type ExpressionJoinTypeNode = {
readonly kind: "ExpressionJoinTypeNode";
readonly expr:
| FilterExpressionNode
| FilterExpressionComparatorExpressions
| FilterExpressionNotExpression
| ExpressionNode
| ExpressionComparatorExpressions
| ExpressionNotExpression
| AttributeExistsFunctionExpression
| AttributeNotExistsFunctionExpression
| BetweenConditionExpression
Expand Down
6 changes: 6 additions & 0 deletions src/nodes/expressionNode.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { ExpressionJoinTypeNode } from "./expressionJoinTypeNode";

export type ExpressionNode = {
readonly kind: "ExpressionNode";
readonly expressions: ExpressionJoinTypeNode[];
};
6 changes: 6 additions & 0 deletions src/nodes/expressionNotExpression.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { ExpressionNode } from "./expressionNode";

export type ExpressionNotExpression = {
readonly kind: "ExpressionNotExpression";
readonly expr: ExpressionNode;
};
8 changes: 0 additions & 8 deletions src/nodes/filterExpressionComparatorExpression.ts

This file was deleted.

6 changes: 0 additions & 6 deletions src/nodes/filterExpressionNode.ts

This file was deleted.

6 changes: 0 additions & 6 deletions src/nodes/filterExpressionNotExpression.ts

This file was deleted.

4 changes: 4 additions & 0 deletions src/nodes/itemNode.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export type ItemNode = {
readonly kind: "ItemNode";
readonly item: Record<string, unknown>;
};
2 changes: 1 addition & 1 deletion src/nodes/operands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ export type FunctionExpression =
export type NotExpression = "NOT";

export type KeyConditionComparators = "=" | "<" | "<=" | ">" | ">=";
export type FilterConditionComparators = KeyConditionComparators | "<>";
export type ExpressionConditionComparators = KeyConditionComparators | "<>";
12 changes: 12 additions & 0 deletions src/nodes/putNode.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { ExpressionNode } from "./expressionNode";
import { ItemNode } from "./itemNode";
import { ReturnValuesNode } from "./returnValuesNode";
import { TableNode } from "./tableNode";

export type PutNode = {
readonly kind: "PutNode";
readonly table: TableNode;
readonly conditionExpression: ExpressionNode;
readonly item?: ItemNode;
readonly returnValues?: ReturnValuesNode;
};
4 changes: 2 additions & 2 deletions src/nodes/queryNode.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { AttributesNode } from "./attributesNode";
import { ConsistentReadNode } from "./consistentReadNode";
import { FilterExpressionNode } from "./filterExpressionNode";
import { ExpressionNode } from "./expressionNode";
import { KeyConditionNode } from "./keyConditionNode";
import { LimitNode } from "./limitNode";
import { ScanIndexForwardNode } from "./scanIndexForwardNode";
Expand All @@ -10,7 +10,7 @@ export type QueryNode = {
readonly kind: "QueryNode";
readonly table: TableNode;
readonly keyConditions: KeyConditionNode[];
readonly filterExpression: FilterExpressionNode;
readonly filterExpression: ExpressionNode;
readonly consistentRead?: ConsistentReadNode;
readonly scanIndexForward?: ScanIndexForwardNode;
readonly limit?: LimitNode;
Expand Down
11 changes: 11 additions & 0 deletions src/nodes/returnValuesNode.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export type ReturnValuesOptions =
| "NONE"
| "ALL_OLD"
| "UPDATED_OLD"
| "ALL_NEW"
| "UPDATED_NEW";

export type ReturnValuesNode = {
readonly kind: "ReturnValuesNode";
readonly option: ReturnValuesOptions;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`PutItemQueryBuilder > handles 'contains' ConditionExpression 1`] = `
{
"dataTimestamp": 212,
"tags": [
"cats",
],
"userId": "333",
}
`;
Loading
Loading