Skip to content

Commit

Permalink
feat: cloudv2
Browse files Browse the repository at this point in the history
  • Loading branch information
Chriscbr committed Jul 30, 2024
1 parent 5df2901 commit a17615a
Show file tree
Hide file tree
Showing 21 changed files with 2,689 additions and 0 deletions.
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
2 changes: 2 additions & 0 deletions cloudv2/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
target/
node_modules/
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.
23 changes: 23 additions & 0 deletions cloudv2/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# cloudv2

## Prerequisites

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

## Installation

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

## Usage

```js
bring cloudv2;

let adder = new cloudv2.Adder();
```

## 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

0 comments on commit a17615a

Please sign in to comment.