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

feat: cloudv2 #301

Merged
merged 9 commits into from
Aug 1, 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
33 changes: 33 additions & 0 deletions .github/workflows/canary.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 29 additions & 0 deletions .github/workflows/cloudv2-pull.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

54 changes: 54 additions & 0 deletions .github/workflows/cloudv2-release.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions .github/workflows/pull-request-lint.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ jobs:
bedrock
budget
checks
cloudv2
cognito
containers
dynamodb
Expand Down
6 changes: 6 additions & 0 deletions .mergify.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ publishing them for you.
| [bedrock](./bedrock) | [@winglibs/bedrock](https://www.npmjs.com/package/@winglibs/bedrock) | sim, tf-aws |
| [budget](./budget) | [@winglibs/budget](https://www.npmjs.com/package/@winglibs/budget) | sim, tf-aws |
| [checks](./checks) | [@winglibs/checks](https://www.npmjs.com/package/@winglibs/checks) | * |
| [cloudv2](./cloudv2) | [@winglibs/cloudv2](https://www.npmjs.com/package/@winglibs/cloudv2) | sim, tf-aws |
| [cognito](./cognito) | [@winglibs/cognito](https://www.npmjs.com/package/@winglibs/cognito) | sim, tf-aws |
| [containers](./containers) | [@winglibs/containers](https://www.npmjs.com/package/@winglibs/containers) | sim, tf-aws |
| [dynamodb](./dynamodb) | [@winglibs/dynamodb](https://www.npmjs.com/package/@winglibs/dynamodb) | sim, tf-aws |
Expand Down
3 changes: 3 additions & 0 deletions cloudv2/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
target/
node_modules/
*.snap.md
21 changes: 21 additions & 0 deletions cloudv2/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2023 Wing

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
28 changes: 28 additions & 0 deletions cloudv2/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# cloudv2

This module contains a Wing-based implementation of the built-in `cloud` library. It's a work in progresss as part of a migration for our internal codebase.

Here are the resources available so far:
- Counter

## Prerequisites

* [winglang](https://winglang.io).

## Installation

```sh
npm i @winglibs/cloudv2
```

## Usage

```js
bring cloudv2 as cloud;

let counter = new cloud.Counter();
```

## License

This library is licensed under the [MIT License](./LICENSE).
6 changes: 6 additions & 0 deletions cloudv2/counter-aws.extern.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

86 changes: 86 additions & 0 deletions cloudv2/counter-aws.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import {
UpdateItemCommand,
GetItemCommand,
DynamoDBClient,
} from "@aws-sdk/client-dynamodb";

import types from "./counter-aws.extern";

const AMOUNT_TOKEN = "amount";
const INITIAL_VALUE_TOKEN = "initial";
const VALUE_ATTRIBUTE = "counter_value";
const SET_VALUE = "set_value";

const client = new DynamoDBClient();

export const _inc: types["_inc"] = async (
amount,
key,
tableName,
hashKey,
initial
) => {
const command = new UpdateItemCommand({
TableName: tableName,
Key: { [hashKey]: { S: key } },
UpdateExpression: `SET ${VALUE_ATTRIBUTE} = if_not_exists(${VALUE_ATTRIBUTE}, :${INITIAL_VALUE_TOKEN}) + :${AMOUNT_TOKEN}`,
ExpressionAttributeValues: {
[`:${AMOUNT_TOKEN}`]: { N: `${amount}` },
[`:${INITIAL_VALUE_TOKEN}`]: { N: `${initial}` },
},
ReturnValues: "UPDATED_NEW",
});

const result = await client.send(command);
let newValue = result.Attributes?.[VALUE_ATTRIBUTE].N;
if (!newValue) {
throw new Error(`${VALUE_ATTRIBUTE} attribute not found on table.`);
}

// return the old value
return parseInt(newValue) - amount;
};

export const _dec: types["_dec"] = async (
amount,
key,
tableName,
hashKey,
initial
) => {
return _inc(-1 * amount, key, tableName, hashKey, initial);
};

export const _peek: types["_peek"] = async (
key,
tableName,
hashKey,
initial
) => {
const command = new GetItemCommand({
TableName: tableName,
Key: { [hashKey]: { S: key } },
});

const result = await client.send(command);
let value = result.Item?.[VALUE_ATTRIBUTE].N;
if (!value) {
return initial;
}

return parseInt(value);
};

export const _set: types["_set"] = async (value, key, tableName, hashKey) => {
const command = new UpdateItemCommand({
TableName: tableName,
Key: { [hashKey]: { S: key } },
UpdateExpression: `SET ${VALUE_ATTRIBUTE} = :${SET_VALUE}`,
ExpressionAttributeValues: {
[`:${SET_VALUE}`]: { N: `${value}` },
},
ReturnValues: "UPDATED_NEW",
});

await client.send(command);
};
Loading
Loading