Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(api-form-builder): storage operations (#1942)
- Loading branch information
1 parent
79027c6
commit a041a2c
Showing
145 changed files
with
6,862 additions
and
2,639 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import { parseWhereKey } from "~/where"; | ||
|
||
describe("where", () => { | ||
const whereKeys = [ | ||
[ | ||
"id", | ||
{ | ||
field: "id", | ||
operator: "eq" | ||
} | ||
], | ||
[ | ||
"id_in", | ||
{ | ||
field: "id", | ||
operator: "in" | ||
} | ||
], | ||
[ | ||
"id_not_in", | ||
{ | ||
field: "id", | ||
operator: "not_in" | ||
} | ||
] | ||
]; | ||
|
||
test.each(whereKeys)( | ||
"parse should result in field and operator values", | ||
(key: string, expected: any) => { | ||
const result = parseWhereKey(key); | ||
|
||
expect(result).toEqual(expected); | ||
} | ||
); | ||
|
||
const malformedWhereKeys = [["_a"], ["_"], ["__"], ["a_"]]; | ||
|
||
test.each(malformedWhereKeys)( | ||
`should throw error when malformed key is passed "%s"`, | ||
(key: string) => { | ||
expect(() => { | ||
parseWhereKey(key); | ||
}).toThrow(`It is not possible to search by key "${key}"`); | ||
} | ||
); | ||
|
||
test("should throw error when malformed field is parsed out", () => { | ||
const key = "a0_in"; | ||
expect(() => { | ||
parseWhereKey(key); | ||
}).toThrow(`Cannot filter by "a0".`); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { Client, ClientOptions } from "@elastic/elasticsearch"; | ||
import AWS from "aws-sdk"; | ||
import createAwsElasticsearchConnector from "aws-elasticsearch-connector"; | ||
|
||
export interface ElasticsearchClientOptions extends ClientOptions { | ||
endpoint?: string; | ||
} | ||
|
||
export const createElasticsearchClient = (options: ElasticsearchClientOptions) => { | ||
const { endpoint, node, ...rest } = options; | ||
|
||
const clientOptions: ClientOptions = { | ||
node: endpoint || node, | ||
...rest | ||
}; | ||
|
||
if (!clientOptions.auth) { | ||
/** | ||
* If no `auth` configuration is present, we setup AWS connector. | ||
*/ | ||
Object.assign(clientOptions, createAwsElasticsearchConnector(AWS.config)); | ||
} | ||
|
||
return new Client(clientOptions); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,68 +1,29 @@ | ||
import { Client, ClientOptions } from "@elastic/elasticsearch"; | ||
import AWS from "aws-sdk"; | ||
import createAwsElasticsearchConnector from "aws-elasticsearch-connector"; | ||
import { ElasticsearchContext } from "~/types"; | ||
import { ContextPlugin } from "@webiny/handler/plugins/ContextPlugin"; | ||
import { | ||
ElasticsearchQueryBuilderOperatorBetweenPlugin, | ||
ElasticsearchQueryBuilderOperatorNotBetweenPlugin, | ||
ElasticsearchQueryBuilderOperatorContainsPlugin, | ||
ElasticsearchQueryBuilderOperatorNotContainsPlugin, | ||
ElasticsearchQueryBuilderOperatorEqualPlugin, | ||
ElasticsearchQueryBuilderOperatorNotPlugin, | ||
ElasticsearchQueryBuilderOperatorGreaterThanPlugin, | ||
ElasticsearchQueryBuilderOperatorGreaterThanOrEqualToPlugin, | ||
ElasticsearchQueryBuilderOperatorLesserThanPlugin, | ||
ElasticsearchQueryBuilderOperatorLesserThanOrEqualToPlugin, | ||
ElasticsearchQueryBuilderOperatorInPlugin, | ||
ElasticsearchQueryBuilderOperatorAndInPlugin, | ||
ElasticsearchQueryBuilderOperatorNotInPlugin | ||
} from "~/plugins/operator"; | ||
import WebinyError from "@webiny/error"; | ||
import { createElasticsearchClient, ElasticsearchClientOptions } from "~/client"; | ||
import { getElasticsearchOperators } from "~/operators"; | ||
import { Client } from "@elastic/elasticsearch"; | ||
|
||
interface ElasticsearchClientOptions extends ClientOptions { | ||
endpoint?: string; | ||
} | ||
|
||
export default (options: ElasticsearchClientOptions): ContextPlugin<ElasticsearchContext> => { | ||
const { endpoint, node, ...rest } = options; | ||
/** | ||
* We must accept either Elasticsearch client or options that create the client. | ||
*/ | ||
export default ( | ||
params: ElasticsearchClientOptions | Client | ||
): ContextPlugin<ElasticsearchContext> => { | ||
return new ContextPlugin<ElasticsearchContext>(context => { | ||
if (context.elasticsearch) { | ||
throw new WebinyError( | ||
"Elasticsearch client is already initialized, no need to define it again. Check your code for duplicate initializations.", | ||
"ELASTICSEARCH_ALREADY_INITIALIZED" | ||
); | ||
} | ||
const clientOptions: ClientOptions = { | ||
node: endpoint || node, | ||
...rest | ||
}; | ||
|
||
if (!clientOptions.auth) { | ||
/** | ||
* If no `auth` configuration is present, we setup AWS connector. | ||
*/ | ||
Object.assign(clientOptions, createAwsElasticsearchConnector(AWS.config)); | ||
} | ||
/** | ||
* Initialize the Elasticsearch client. | ||
*/ | ||
context.elasticsearch = new Client(clientOptions); | ||
context.elasticsearch = | ||
params instanceof Client ? params : createElasticsearchClient(params); | ||
|
||
context.plugins.register([ | ||
new ElasticsearchQueryBuilderOperatorBetweenPlugin(), | ||
new ElasticsearchQueryBuilderOperatorNotBetweenPlugin(), | ||
new ElasticsearchQueryBuilderOperatorContainsPlugin(), | ||
new ElasticsearchQueryBuilderOperatorNotContainsPlugin(), | ||
new ElasticsearchQueryBuilderOperatorEqualPlugin(), | ||
new ElasticsearchQueryBuilderOperatorNotPlugin(), | ||
new ElasticsearchQueryBuilderOperatorGreaterThanPlugin(), | ||
new ElasticsearchQueryBuilderOperatorGreaterThanOrEqualToPlugin(), | ||
new ElasticsearchQueryBuilderOperatorLesserThanPlugin(), | ||
new ElasticsearchQueryBuilderOperatorLesserThanOrEqualToPlugin(), | ||
new ElasticsearchQueryBuilderOperatorInPlugin(), | ||
new ElasticsearchQueryBuilderOperatorAndInPlugin(), | ||
new ElasticsearchQueryBuilderOperatorNotInPlugin() | ||
]); | ||
context.plugins.register(getElasticsearchOperators()); | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { ElasticsearchQueryBuilderOperatorBetweenPlugin } from "~/plugins/operator/between"; | ||
import { ElasticsearchQueryBuilderOperatorNotBetweenPlugin } from "~/plugins/operator/notBetween"; | ||
import { ElasticsearchQueryBuilderOperatorContainsPlugin } from "~/plugins/operator/contains"; | ||
import { ElasticsearchQueryBuilderOperatorNotContainsPlugin } from "~/plugins/operator/notContains"; | ||
import { ElasticsearchQueryBuilderOperatorEqualPlugin } from "~/plugins/operator/equal"; | ||
import { ElasticsearchQueryBuilderOperatorNotPlugin } from "~/plugins/operator/not"; | ||
import { ElasticsearchQueryBuilderOperatorGreaterThanPlugin } from "~/plugins/operator/gt"; | ||
import { ElasticsearchQueryBuilderOperatorGreaterThanOrEqualToPlugin } from "~/plugins/operator/gte"; | ||
import { ElasticsearchQueryBuilderOperatorLesserThanPlugin } from "~/plugins/operator/lt"; | ||
import { ElasticsearchQueryBuilderOperatorLesserThanOrEqualToPlugin } from "~/plugins/operator/lte"; | ||
import { ElasticsearchQueryBuilderOperatorInPlugin } from "~/plugins/operator/in"; | ||
import { ElasticsearchQueryBuilderOperatorAndInPlugin } from "~/plugins/operator/andIn"; | ||
import { ElasticsearchQueryBuilderOperatorNotInPlugin } from "~/plugins/operator/notIn"; | ||
|
||
const operators = [ | ||
new ElasticsearchQueryBuilderOperatorBetweenPlugin(), | ||
new ElasticsearchQueryBuilderOperatorNotBetweenPlugin(), | ||
new ElasticsearchQueryBuilderOperatorContainsPlugin(), | ||
new ElasticsearchQueryBuilderOperatorNotContainsPlugin(), | ||
new ElasticsearchQueryBuilderOperatorEqualPlugin(), | ||
new ElasticsearchQueryBuilderOperatorNotPlugin(), | ||
new ElasticsearchQueryBuilderOperatorGreaterThanPlugin(), | ||
new ElasticsearchQueryBuilderOperatorGreaterThanOrEqualToPlugin(), | ||
new ElasticsearchQueryBuilderOperatorLesserThanPlugin(), | ||
new ElasticsearchQueryBuilderOperatorLesserThanOrEqualToPlugin(), | ||
new ElasticsearchQueryBuilderOperatorInPlugin(), | ||
new ElasticsearchQueryBuilderOperatorAndInPlugin(), | ||
new ElasticsearchQueryBuilderOperatorNotInPlugin() | ||
]; | ||
/** | ||
* We export as a function because there might be something to be sent to the operators at some point. | ||
* This way, we make it easier to upgrade. | ||
*/ | ||
export const getElasticsearchOperators = () => operators; |
18 changes: 7 additions & 11 deletions
18
packages/api-elasticsearch/src/plugins/definition/ElasticsearchBodyModifierPlugin.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 7 additions & 11 deletions
18
packages/api-elasticsearch/src/plugins/definition/ElasticsearchQueryModifierPlugin.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.