-
Notifications
You must be signed in to change notification settings - Fork 622
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Block Categories to Page Builder ddb-es project
- Loading branch information
1 parent
090f78b
commit 91f400f
Showing
10 changed files
with
423 additions
and
17 deletions.
There are no files selected for viewing
46 changes: 46 additions & 0 deletions
46
packages/api-page-builder-so-ddb-es/src/definitions/blockCategoryEntity.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
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 || {}) | ||
} | ||
}); | ||
}; |
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
74 changes: 74 additions & 0 deletions
74
packages/api-page-builder-so-ddb-es/src/operations/blockCategory/dataLoader.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
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; | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
packages/api-page-builder-so-ddb-es/src/operations/blockCategory/fields.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
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" | ||
}) | ||
]; | ||
}; |
Oops, something went wrong.