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(api-form-builder): dynamodb storage ops #1955

Merged
merged 8 commits into from
Oct 13, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -248,9 +248,9 @@ export class FilesStorageOperations implements FileManagerFilesStorageOperations
* It will build the filters out of the where input and transform the values it is using.
*/
const filteredFiles = filterItems({
plugins: this.context.plugins,
items,
where,
context: this.context,
fields
});

Expand Down
7 changes: 1 addition & 6 deletions packages/api-form-builder-so-ddb-es/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,9 @@
[![code style: prettier](https://img.shields.io/badge/code_style-prettier-ff69b4.svg?style=flat-square)](https://github.com/prettier/prettier)
[![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat-square)](http://makeapullrequest.com)

The API for the [Webiny Forms (@webiny/app-form-builder-so-ddb-es)](../app-form-builder-so-ddb-es) app.
The DynamoDB + Elasticsearch Storage Operations for the [Webiny Forms (@webiny/api-form-builder)](../api-form-builder) API.

## Install
```
npm install --save @webiny/api-form-builder-so-ddb-es
```

Or if you prefer yarn:
```
yarn add @webiny/api-form-builder-so-ddb-es
```
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ export default () => [
field: "ownedBy",
path: "ownedBy.id"
}),
new FormElasticsearchFieldPlugin({
field: "createdBy",
path: "createdBy.id"
}),
/**
* Always add the ALL fields plugin because of the keyword/path build.
*/
Expand Down
10 changes: 2 additions & 8 deletions packages/api-form-builder-so-ddb-es/src/operations/form/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ export const createFormStorageOperations = (params: Params): FormBuilderFormStor
items.push(
entity.putBatch({
...form,
TYPE: createLatestSortKey(),
TYPE: createFormLatestType(),
...latestKeys
})
);
Expand Down Expand Up @@ -487,13 +487,7 @@ export const createFormStorageOperations = (params: Params): FormBuilderFormStor
formId: undefined
};
const filteredItems = filterItems({
/**
* At the moment we need to send the plugins like this because plugins are extracted from the context.plugins.
* When we implement sending only plugins that we require, we will change this as well.
*/
context: {
plugins
} as any,
plugins,
items,
where,
fields: formDynamoDbFields
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,10 @@ const createElasticsearchQuery = (params: CreateElasticsearchQueryParams) => {
*/
query.must.push({
term: {
"form.parent.keyword": where.parent
"form.parent.keyword": where.formId
}
});
delete where.parent;
delete where.formId;
/**
* We apply other conditions as they are passed via the where value.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,6 @@ export const createSubmissionStorageOperations = (
): FormBuilderSubmissionStorageOperations => {
const { entity, esEntity, table, elasticsearch, plugins } = params;

/**
* This is a form partition key.
* TODO: figure out how to use the one from the ~/operations/forms/index.ts file.
*/
const createSubmissionPartitionKey = (
params: FormBuilderSubmissionStorageOperationsCreatePartitionKeyParams
) => {
Expand Down Expand Up @@ -202,8 +198,7 @@ export const createSubmissionStorageOperations = (
const items = where.id_in.map(id => {
return entity.getBatch({
PK: createSubmissionPartitionKey({
...where,
formId: where.parent
...where
}),
SK: createSubmissionSortKey(id)
});
Expand Down Expand Up @@ -248,15 +243,6 @@ export const createSubmissionStorageOperations = (
const { where, sort, limit: initialLimit, after } = params;

if (where.id_in) {
if (!where.parent) {
throw new WebinyError(
`Cannot search for form submissions by IDs without the "parent".`,
"MALFORMED_WHERE_CONDITION",
{
where
}
);
}
const items = await listSubmissionsByIds(params);

return {
Expand Down
1 change: 1 addition & 0 deletions packages/api-form-builder-so-ddb/.babelrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require("../../.babel.node")({ path: __dirname });
4 changes: 4 additions & 0 deletions packages/api-form-builder-so-ddb/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Change Log

All notable changes to this project will be documented in this file.
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
21 changes: 21 additions & 0 deletions packages/api-form-builder-so-ddb/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) Webiny

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.
13 changes: 13 additions & 0 deletions packages/api-form-builder-so-ddb/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# @webiny/api-form-builder-so-ddb
[![](https://img.shields.io/npm/dw/@webiny/api-form-builder-so-ddb.svg)](https://www.npmjs.com/package/@webiny/api-form-builder-so-ddb)
[![](https://img.shields.io/npm/v/@webiny/api-form-builder-so-ddb.svg)](https://www.npmjs.com/package/@webiny/api-form-builder-so-ddb)
[![code style: prettier](https://img.shields.io/badge/code_style-prettier-ff69b4.svg?style=flat-square)](https://github.com/prettier/prettier)
[![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat-square)](http://makeapullrequest.com)

The DynamoDB Storage Operations for the [Webiny Forms (@webiny/api-form-builder)](../api-form-builder) API.

## Install
With yarn:
```
yarn add @webiny/api-form-builder-so-ddb
```
56 changes: 56 additions & 0 deletions packages/api-form-builder-so-ddb/__tests__/__api__/environment.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
const { DynamoDbDriver } = require("@webiny/db-dynamodb");
const { DocumentClient } = require("aws-sdk/clients/dynamodb");
const dbPlugins = require("@webiny/handler-db").default;
const NodeEnvironment = require("jest-environment-node");
const dynamoDbPlugins = require("@webiny/db-dynamodb/plugins").default;
/**
* For this to work it must load plugins that have already been built
*/
const { createFormBuilderStorageOperations } = require("../../dist/index");

if (typeof createFormBuilderStorageOperations !== "function") {
throw new Error(
`Loaded "createFormBuilderStorageOperations" must be a function that will return the storage operations.`
);
}

class FormBuilderTestEnvironment extends NodeEnvironment {
async setup() {
await super.setup();
const documentClient = new DocumentClient({
convertEmptyValues: true,
endpoint: process.env.MOCK_DYNAMODB_ENDPOINT || "http://localhost:8001",
sslEnabled: false,
region: "local",
accessKeyId: "test",
secretAccessKey: "test"
});
/**
* This is a global function that will be called inside the tests to get all relevant plugins, methods and objects.
*/
this.global.__getStorageOperations = () => {
return {
createStorageOperations: () => {
return createFormBuilderStorageOperations({
table: "DynamoDB",
documentClient,
plugins: [...dynamoDbPlugins()]
});
},
getGlobalPlugins: () => {
return [
...dbPlugins({
table: "DynamoDB",
driver: new DynamoDbDriver({
documentClient
})
}),
...dynamoDbPlugins()
];
}
};
};
}
}

module.exports = FormBuilderTestEnvironment;
10 changes: 10 additions & 0 deletions packages/api-form-builder-so-ddb/__tests__/__api__/presets.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const path = require("path");

const presets = [
{
testEnvironment: path.resolve(__dirname, "environment.js"),
setupFilesAfterEnv: [path.resolve(__dirname, "setupAfterEnv.js")]
}
];

module.exports = presets;
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const jestDynalite = require("jest-dynalite");
const path = require("path");
/**
* Must be a root of this package.
*/
jestDynalite.setup(path.resolve(__dirname, "../../"));
/**
* Assign all required dynalite lifecycle methods.
* And add custom lifecycle methods that are defined in the environment.js global
*/
beforeAll(async () => {
await jestDynalite.startDb();
});

beforeEach(async () => {
await jestDynalite.createTables();
});
afterEach(async () => {
await jestDynalite.deleteTables();
});

afterAll(async () => {
await jestDynalite.stopDb();
});
35 changes: 35 additions & 0 deletions packages/api-form-builder-so-ddb/jest-dynalite-config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
module.exports = {
tables: [
{
TableName: "DynamoDB",
KeySchema: [
{ AttributeName: "PK", KeyType: "HASH" },
{ AttributeName: "SK", KeyType: "RANGE" }
],
AttributeDefinitions: [
{ AttributeName: "PK", AttributeType: "S" },
{ AttributeName: "SK", AttributeType: "S" },
{ AttributeName: "GSI1_PK", AttributeType: "S" },
{ AttributeName: "GSI1_SK", AttributeType: "S" }
],
ProvisionedThroughput: { ReadCapacityUnits: 1, WriteCapacityUnits: 1 },
GlobalSecondaryIndexes: [
{
IndexName: "GSI1",
KeySchema: [
{ AttributeName: "GSI1_PK", KeyType: "HASH" },
{ AttributeName: "GSI1_SK", KeyType: "RANGE" }
],
Projection: {
ProjectionType: "ALL"
},
ProvisionedThroughput: {
ReadCapacityUnits: 1,
WriteCapacityUnits: 1
}
}
]
}
],
basePort: 8000
};
6 changes: 6 additions & 0 deletions packages/api-form-builder-so-ddb/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const dynamoDbPreset = require("jest-dynalite/jest-preset");
const base = require("../../jest.config.base");

module.exports = {
...base({ path: __dirname }, [dynamoDbPreset])
};
56 changes: 56 additions & 0 deletions packages/api-form-builder-so-ddb/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
{
"name": "@webiny/api-form-builder-so-ddb",
"version": "5.16.0",
"main": "index.js",
"keywords": [
"@webiny/api-form-builder",
"storage-operations",
"dynamodb",
"fb:ddb"
],
"repository": {
"type": "git",
"url": "https://github.com/webiny/webiny-js.git",
"directory": "packages/api-form-builder-so-ddb"
},
"contributors": [
"Pavel Denisjuk <pavel@webiny.com>",
"Sven Al Hamad <sven@webiny.com>",
"Adrian Smijulj <adrian@webiny.com>"
],
"license": "MIT",
"dependencies": {
"@babel/runtime": "^7.5.5",
"@webiny/api-form-builder": "^5.15.0",
"@webiny/db-dynamodb": "^5.15.0",
"@webiny/error": "^5.15.0",
"@webiny/plugins": "^5.15.0",
"@webiny/utils": "^5.15.0",
"dynamodb-toolbox": "^0.3.4"
},
"devDependencies": {
"@babel/cli": "^7.5.5",
"@babel/core": "^7.5.5",
"@babel/preset-env": "^7.5.5",
"@babel/preset-typescript": "^7.8.3",
"@webiny/cli": "^5.15.0",
"@webiny/handler-db": "^5.15.0",
"@webiny/project-utils": "^5.15.0",
"csvtojson": "^2.0.10",
"jest": "^26.6.3",
"jest-dynalite": "^3.2.0",
"jest-environment-node": "^27.2.4",
"rimraf": "^3.0.2",
"ttypescript": "^1.5.12",
"typescript": "^4.1.3"
},
"publishConfig": {
"access": "public",
"directory": "dist"
},
"scripts": {
"build": "yarn webiny run build",
"watch": "yarn webiny run watch"
},
"gitHead": "b8aec8a1be3f25c3b428b357fe1e352c7cbff9ae"
}