Skip to content

Commit

Permalink
feat: bring dynamodb (#98)
Browse files Browse the repository at this point in the history
- Interface similar to the original DynamoDB SDK
- Includes streams support
- Uses one container for all tables (sim)

Closes winglang/wing#4949
  • Loading branch information
skyrpex authored Mar 19, 2024
1 parent 568595a commit 4647694
Show file tree
Hide file tree
Showing 21 changed files with 15,342 additions and 0 deletions.
29 changes: 29 additions & 0 deletions .github/workflows/dynamodb-pull.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: dynamodb-pull
on:
pull_request:
paths:
- dynamodb/**
jobs:
build-dynamodb:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
with:
sparse-checkout: dynamodb
- name: Setup Node.js
uses: actions/setup-node@v3
with:
node-version: 20.x
registry-url: https://registry.npmjs.org
- name: Install winglang
run: npm i -g winglang
- name: Install dependencies
run: npm install --include=dev
working-directory: dynamodb
- name: Test
run: wing test
working-directory: dynamodb
- name: Pack
run: wing pack
working-directory: dynamodb
56 changes: 56 additions & 0 deletions .github/workflows/dynamodb-release.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
name: dynamodb-release
on:
push:
branches:
- main
paths:
- dynamodb/**
- "!dynamodb/package-lock.json"
jobs:
build-dynamodb:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
with:
sparse-checkout: dynamodb
- name: Setup Node.js
uses: actions/setup-node@v3
with:
node-version: 20.x
registry-url: https://registry.npmjs.org
- name: Install winglang
run: npm i -g winglang
- name: Install dependencies
run: npm install --include=dev
working-directory: dynamodb
- name: Test
run: wing test
working-directory: dynamodb
- name: Pack
run: wing pack
working-directory: dynamodb
- name: Get package version
run:
echo WINGLIB_VERSION=$(node -p "require('./package.json').version") >>
"$GITHUB_ENV"
working-directory: dynamodb
- name: Publish
run:
npm publish --access=public --registry https://registry.npmjs.org --tag
latest *.tgz
working-directory: dynamodb
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
- name: Tag commit
uses: tvdias/[email protected]
with:
repo-token: ${{ secrets.PROJEN_GITHUB_TOKEN }}
tag: dynamodb-v${{ env.WINGLIB_VERSION }}
- name: GitHub release
uses: softprops/action-gh-release@v1
with:
name: dynamodb v${{ env.WINGLIB_VERSION }}
tag_name: dynamodb-v${{ env.WINGLIB_VERSION }}
files: "*.tgz"
token: ${{ secrets.PROJEN_GITHUB_TOKEN }}
2 changes: 2 additions & 0 deletions dynamodb/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
target/
node_modules/
21 changes: 21 additions & 0 deletions dynamodb/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.
61 changes: 61 additions & 0 deletions dynamodb/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# dynamodb

## Prerequisites

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

## Installation

```sh
npm i @winglibs/dynamodb
```

## Usage

```js
bring dynamodb;

let table = new dynamodb.Table(
attributes: [
{
name: "id",
type: "S",
},
],
hashKey: "id",
);

// Streams.
table.setStreamConsumer(inflight (record) => {
log("record processed = {Json.stringify(record)}");
});

// Put and query.
test "put and query" {
table.put(
item: {
id: "1",
body: "hello",
},
);
let response = table.query(
keyConditionExpression: "id = :id",
expressionAttributeValues: {":id": "1"},
);
assert(response.count == 1);
assert(response.items.at(0).get("id").asStr() == "1");
assert(response.items.at(0).get("body").asStr() == "hello");
}
```

In case you want to instantiate your own DynamoDB SDK, you can get the connection details like this:

```wing
let connection = table.connection();
connection.endpoint;
connection.tableName;
```

## License

This library is licensed under the [MIT License](./LICENSE).
116 changes: 116 additions & 0 deletions dynamodb/dynamodb-client.w
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
bring "./dynamodb-types.w" as dynamodb_types;

interface DocumentClient {
inflight batchGet(input: Json): Json;
inflight batchWrite(input: Json): Json;
inflight delete(input: Json): Json;
inflight get(input: Json): Json;
inflight put(input: Json): Json;
inflight query(input: Json): Json;
inflight scan(input: Json): Json;
inflight transactGet(input: Json): Json;
inflight transactWrite(input: Json): Json;
inflight update(input: Json): Json;
}

struct Credentials {
accessKeyId: str;
secretAccessKey: str;
}

struct CreateDocumentClientOptions {
endpoint: str?;
region: str?;
credentials: Credentials?;
}

class Util {
extern "./dynamodb.mjs" pub static inflight createDocumentClient(options: CreateDocumentClientOptions?): DocumentClient;
}

pub struct ClientProps {
tableName: str;
endpoint: str?;
region: str?;
credentials: Credentials?;
}

pub inflight class Client impl dynamodb_types.IClient {
inflight tableName: str;
inflight client: DocumentClient;

inflight new(props: ClientProps) {
this.tableName = props.tableName;
this.client = Util.createDocumentClient({
region: props.region,
credentials: props.credentials,
endpoint: props.endpoint,
});
}

pub inflight delete(options: dynamodb_types.DeleteOptions): dynamodb_types.DeleteOutput {
let input: MutJson = options;
input.set("TableName", this.tableName);
return unsafeCast(this.client.delete(input));
}

pub inflight get(options: dynamodb_types.GetOptions): dynamodb_types.GetOutput {
let input: MutJson = options;
input.set("TableName", this.tableName);
return unsafeCast(this.client.get(input));
}

pub inflight put(options: dynamodb_types.PutOptions): dynamodb_types.PutOutput {
let input: MutJson = options;
input.set("TableName", this.tableName);
return unsafeCast(this.client.put(input));
}

pub inflight transactWrite(options: dynamodb_types.TransactWriteOptions): dynamodb_types.TransactWriteOutput {
let transactItems = MutArray<Json> [];
for item in options.TransactItems {
if let operation = item.ConditionCheck {
let input: MutJson = operation;
input.set("TableName", this.tableName);
transactItems.push({
ConditionCheck: Json.deepCopy(input),
});
} elif let operation = item.Delete {
let input: MutJson = operation;
input.set("TableName", this.tableName);
transactItems.push({
Delete: Json.deepCopy(input),
});
} elif let operation = item.Put {
let input: MutJson = operation;
input.set("TableName", this.tableName);
transactItems.push({
Put: Json.deepCopy(input),
});
} elif let operation = item.Update {
let input: MutJson = operation;
input.set("TableName", this.tableName);
transactItems.push({
Update: Json.deepCopy(input),
});
} else {
throw "Invalid transact item";
}
}
return unsafeCast(this.client.transactWrite({
TransactItems: transactItems.copy(),
}));
}

pub inflight scan(options: dynamodb_types.ScanOptions?): dynamodb_types.ScanOutput {
let input: MutJson = options ?? {};
input.set("TableName", this.tableName);
return unsafeCast(this.client.scan(input));
}

pub inflight query(options: dynamodb_types.QueryOptions): dynamodb_types.QueryOutput {
let input: MutJson = options;
input.set("TableName", this.tableName);
return unsafeCast(this.client.query(input));
}
}
Loading

0 comments on commit 4647694

Please sign in to comment.