Skip to content
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { Entity, Table } from "dynamodb-toolbox";
import { Attributes } from "~/types";

interface Params {
table: Table;
entityName: string;
attributes: Attributes;
}

export const createBlockCategoryEntity = (params: Params): Entity<any> => {
const { entityName, attributes, table } = params;
return new Entity({
name: entityName,
table,
attributes: {
PK: {
partitionKey: true
},
SK: {
sortKey: true
},
TYPE: {
type: "string"
},
name: {
type: "string"
},
slug: {
type: "string"
},
createdOn: {
type: "string"
},
createdBy: {
type: "map"
},
tenant: {
type: "string"
},
locale: {
type: "string"
},
...(attributes || {})
}
});
};
50 changes: 37 additions & 13 deletions packages/api-page-builder-so-ddb-es/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,42 @@
import dynamoDbValueFilters from "@webiny/db-dynamodb/plugins/filters";
import { createSystemStorageOperations } from "~/operations/system";
import { PluginsContainer } from "@webiny/plugins";
import { getElasticsearchOperators } from "@webiny/api-elasticsearch/operators";

import { ENTITIES, StorageOperationsFactory } from "~/types";
import { createTable } from "~/definitions/table";
import { createElasticsearchTable } from "~/definitions/tableElasticsearch";
import { createSettingsEntity } from "~/definitions/settingsEntity";
import { createSystemEntity } from "./definitions/systemEntity";
import { createCategoryEntity } from "~/definitions/categoryEntity";
import { createMenuEntity } from "~/definitions/menuEntity";
import { createPageElementEntity } from "~/definitions/pageElementEntity";
import { createPageEntity } from "~/definitions/pageEntity";
import { createPageElasticsearchEntity } from "~/definitions/pageElasticsearchEntity";
import { PluginsContainer } from "@webiny/plugins";
import { getElasticsearchOperators } from "@webiny/api-elasticsearch/operators";
import { elasticsearchIndexPlugins } from "~/elasticsearch/indices";
import { createElasticsearchIndex } from "~/elasticsearch/createElasticsearchIndex";
import { createSettingsStorageOperations } from "~/operations/settings";

import { createCategoryEntity } from "~/definitions/categoryEntity";
import { createCategoryDynamoDbFields } from "~/operations/category/fields";
import { createCategoryStorageOperations } from "~/operations/category";

import { createMenuEntity } from "~/definitions/menuEntity";
import { createMenuDynamoDbFields } from "~/operations/menu/fields";
import { createMenuStorageOperations } from "~/operations/menu";

import { createPageElementEntity } from "~/definitions/pageElementEntity";
import { createPageElementDynamoDbFields } from "~/operations/pageElement/fields";
import { createPageElementStorageOperations } from "~/operations/pageElement";

import { createSettingsEntity } from "~/definitions/settingsEntity";
import { createSettingsStorageOperations } from "~/operations/settings";

import { createSystemEntity } from "~/definitions/systemEntity";
import { createSystemStorageOperations } from "~/operations/system";

import { createPageEntity } from "~/definitions/pageEntity";
import {
createPagesElasticsearchFields,
createPagesDynamoDbFields
} from "~/operations/pages/fields";
import { createPageStorageOperations } from "~/operations/pages";
import { elasticsearchIndexPlugins } from "~/elasticsearch/indices";
import { createPageElasticsearchEntity } from "~/definitions/pageElasticsearchEntity";

import { createBlockCategoryEntity } from "~/definitions/blockCategoryEntity";
import { createBlockCategoryDynamoDbFields } from "~/operations/blockCategory/fields";
import { createBlockCategoryStorageOperations } from "~/operations/blockCategory";

export const createStorageOperations: StorageOperationsFactory = params => {
const {
Expand Down Expand Up @@ -82,7 +93,11 @@ export const createStorageOperations: StorageOperationsFactory = params => {
/**
* Built-in Elasticsearch index templates
*/
elasticsearchIndexPlugins()
elasticsearchIndexPlugins(),
/**
* Block Category fields required for filtering/sorting.
*/
createBlockCategoryDynamoDbFields()
]);

const entities = {
Expand Down Expand Up @@ -120,6 +135,11 @@ export const createStorageOperations: StorageOperationsFactory = params => {
entityName: ENTITIES.PAGES_ES,
table: tableElasticsearchInstance,
attributes: attributes ? attributes[ENTITIES.PAGES_ES] : {}
}),
blockCategories: createBlockCategoryEntity({
entityName: ENTITIES.BLOCK_CATEGORIES,
table: tableInstance,
attributes: attributes ? attributes[ENTITIES.BLOCK_CATEGORIES] : {}
})
};

Expand Down Expand Up @@ -160,6 +180,10 @@ export const createStorageOperations: StorageOperationsFactory = params => {
esEntity: entities.pagesEs,
elasticsearch,
plugins
}),
blockCategories: createBlockCategoryStorageOperations({
entity: entities.blockCategories,
plugins
})
};
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import DataLoader from "dataloader";
import { batchReadAll } from "@webiny/db-dynamodb/utils/batchRead";
import { BlockCategory } from "@webiny/api-page-builder/types";
import { cleanupItem } from "@webiny/db-dynamodb/utils/cleanup";
import { Entity } from "dynamodb-toolbox";
import { createPartitionKey, createSortKey } from "./keys";

interface Params {
entity: Entity<any>;
}

type DataLoaderGetItem = Pick<BlockCategory, "slug" | "tenant" | "locale">;

export class BlockCategoryDataLoader {
private _getDataLoader: DataLoader<any, any> | undefined = undefined;

private readonly entity: Entity<any>;

constructor(params: Params) {
this.entity = params.entity;
}

public async getOne(item: DataLoaderGetItem): Promise<BlockCategory> {
return await this.getDataLoader().load(item);
}

public async getAll(items: DataLoaderGetItem[]): Promise<BlockCategory[]> {
return await this.getDataLoader().loadMany(items);
}

public clear(): void {
this.getDataLoader().clearAll();
}

private getDataLoader(): DataLoader<any, any> {
if (!this._getDataLoader) {
const cacheKeyFn = (key: DataLoaderGetItem) => {
return `T#${key.tenant}#L#${key.locale}#${key.slug}`;
};
this._getDataLoader = new DataLoader(
async items => {
const batched = items.map(item => {
return this.entity.getBatch({
PK: createPartitionKey(item),
SK: createSortKey(item)
});
});

const records = await batchReadAll<BlockCategory>({
table: this.entity.table,
items: batched
});

const results = records.reduce((collection, result) => {
if (!result) {
return collection;
}
const key = cacheKeyFn(result);
collection[key] = cleanupItem(this.entity, result) as BlockCategory;
return collection;
}, {} as Record<string, BlockCategory>);
return items.map(item => {
const key = cacheKeyFn(item);
return results[key] || null;
});
},
{
cacheKeyFn
}
);
}
return this._getDataLoader;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { BlockCategoryDynamoDbElasticFieldPlugin } from "~/plugins/definitions/BlockCategoryDynamoDbElasticFieldPlugin";

export const createBlockCategoryDynamoDbFields = (): BlockCategoryDynamoDbElasticFieldPlugin[] => {
return [
new BlockCategoryDynamoDbElasticFieldPlugin({
field: "id"
}),
new BlockCategoryDynamoDbElasticFieldPlugin({
field: "createdOn",
type: "date"
}),
new BlockCategoryDynamoDbElasticFieldPlugin({
field: "savedOn",
type: "date"
}),
new BlockCategoryDynamoDbElasticFieldPlugin({
field: "publishedOn",
type: "date"
}),
new BlockCategoryDynamoDbElasticFieldPlugin({
field: "createdBy",
path: "createdBy.id"
})
];
};
Loading