How many key is maximum in declare model schema in amplify? - amazon-dynamodb

i have model and declare #key but i don't know maximum declare index in model
#key(name: "byGuest", fields: ["guestId"], queryField: "listItemByGuest")
#key(name: "byName", fields: ["name", "createdAt"], queryField: "listItemByName")
#key(name: "byNickName", fields: ["guestId", "createdAt"], queryField: "listItemByNickName")
#key(name: "byGuestByPremium", fields: ["guestId", "premiumId"], queryField: "listItemByGuestByPremium")
#key(name: "byPremiumByState", fields: ["premiumId", "state"], queryField: "listItemByPremiumByState")

Related

Get Dynamically DynamoDB table name in request and response mapping template using a BatchGetItem

I am using microservice with folder structure.
Microservice =>
resolvers ->
Media/Image/get-images/request.vtl
Media/Image/get-images/response.vtl
templates -> services.yaml
Request mapping:
#set($imageIds=$ctx.source.imageIds)
#set($keys=[])
#foreach($imageId in $imageIds)
#set($key={})
$util.qr($key.put("id", $util.dynamodb.toString($imageId)))
$util.qr($keys.add($key))
#end
{
"version": "2018-05-29",
"operation": "BatchGetItem",
"tables" : {
"MediaImages": {
"keys": $util.toJson($keys)
}
}
}
Response mapping:
#set($result=$ctx.result.data.MediaImages)
$util.toJson($result)
Service.yaml
Resources:
MediaImagesTable:
Type: AWS::DynamoDB::Table
Properties:
TableName: MediaImages
AttributeDefinitions:
- AttributeName: id
AttributeType: S
- AttributeName: userId
KeySchema:
- AttributeName: id
KeyType: HASH
ProvisionedThroughput:
ReadCapacityUnits: 5
WriteCapacityUnits: 5
GlobalSecondaryIndexes:
- IndexName: UserImages
KeySchema:
- AttributeName: userId
KeyType: HASH
- AttributeName: id
KeyType: RANGE
Projection:
ProjectionType: ALL
ProvisionedThroughput:
ReadCapacityUnits: 5
WriteCapacityUnits: 2
ImageDetailsDataSource:
Type: AWS::AppSync::DataSource
Properties:
Name: ImageDetailsDataSource
Type: AMAZON_DYNAMODB
ServiceRoleArn:
Fn::ImportValue: !Sub "DynamoDB-Role"
ApiId:
Fn::ImportValue: !Sub "API-Id"
DynamoDBConfig:
TableName: !Ref MediaImagesTable
AwsRegion: !Ref AWS::Region
UseCallerCredentials: false
GetImagesPipelineFunction:
Type: AWS::AppSync::FunctionConfiguration
Properties:
ApiId:
Fn::ImportValue: !Sub "API-Id"
Name: GetImagesPipelineFunction
FunctionVersion: "2018-05-29"
Description: Function to get the images from dynamo db
DataSourceName: !GetAtt ImageDetailsDataSource.Name
RequestMappingTemplateS3Location: ../resolvers/get-images/request.vtl
ResponseMappingTemplateS3Location: ../resolvers/get-images/response.vtl
I have tried
#set($tableName=$util.dynamodb.getDataSourceTableName())
#set($imageIds=$ctx.source.imageIds)
#set($keys=[])
#foreach($imageId in $imageIds)
#set($key={})
$util.qr($key.put("id", $util.dynamodb.toString($imageId)))
$util.qr($keys.add($key))
#end
{
"version": "2018-05-29",
"operation": "BatchGetItem",
"tables" : {
"$tableName": {
"keys": $util.toJson($keys)
}
}
}
"error": {
"message": "1 validation error detected: Value '{$tableName= .
[com.amazonaws.dynamodb.v20120810.WriteRequest#1528275d]}' at
'requestItems' failed to satisfy constraint: Map keys must satisfy
constraint: [Member must have length less than or equal to 255, Member
must have length greater than or equal to 3, Member must satisfy regular
expression pattern: [a-zA-Z0-9_.-]+] (Service: AmazonDynamoDBv2; Status
Code: 400; Error Code: ValidationException; Request ID:
464H3LIEPOSA2S8OI34RJ31QLNVV4KQNSO5AEMVJF66Q9ASUAAJG)",
"type": "DynamoDB:AmazonDynamoDBException"
},
In my AppSync request mapping template. I am doing BatchGetItem and hardcoding the table name. I want to get the table name dynamically into my request and response mapping template. I tried Mapping Template Utility Reference $util.dynamodb.getDataSourceTableName($dataSourceName) but didn't work.
I do this way:
RequestMappingTemplate: !Sub
- |
#set($keys=[])
#foreach($imageId in $imageIds)
#set($key={})
$util.qr($key.put("id", $util.dynamodb.toString($imageId)))
$util.qr($keys.add($key))
#end
{
"version": "2018-05-29",
"operation": "BatchGetItem",
"tables" : {
"${TableName}": {
"keys": $util.toJson($keys)
}
}
}
- { TableName: INSERT YOUR TABLE NAME OR SOME REF HERE }
ResponseMappingTemplate: !Sub
- |
#set($result=$ctx.result.data["${TableName}"])
$util.toJson($result)
- { TableName: INSERT YOUR TABLE NAME OR SOME REF HERE }
I use !FindInMap [Environments, !Ref Environment, ProjectsTableName] but you could also use !Ref YourDynamoDBTable to replace INSERT YOUR TABLE NAME OR SOME REF HERE

doctrine create manyToMany over third entity does not work properly

I want to creta a manyToMany relation between two entites not directly but by using a third entity with double manyToOne relations:
AppBundle\Entity\AttributeKey:
type: entity
table: attribute_keys
repositoryClass: AppBundle\Repository\AttributeKeyRepository
id:
id:
type: integer
id: true
generator:
strategy: AUTO
fields:
label:
type: string
length: 255
unique: true
sort:
type: integer
AppBundle\Entity\AttributeValue:
type: entity
table: attribute_values
repositoryClass: AppBundle\Repository\AttributeValueRepository
id:
id:
type: integer
id: true
generator:
strategy: AUTO
fields:
label:
type: string
length: 255
unique: true
AppBundle\Entity\AttributeKeyValue:
type: entity
table: attribute_keys_values
repositoryClass: AppBundle\Repository\AttributeKeyValueRepository
manyToOne:
attributeKey:
targetEntity: AttributeKey
attributeValue:
targetEntity: AttributeValue
manyToMany:
documents:
targetEntity: Document
mappedBy: attributes
id:
id:
type: integer
id: true
generator:
strategy: AUTO
In the database everything looks fine, the foreign keys are properly built. But the AttributeKey Entity that is generated by doctrine does not contain a method getValues() and AttributeValue does not have the method getKeys() or addKey()
What did I do wrong?
You mapped only the party that owns the relationship. You need to set up a two-way relationship.
Everything is explained here :
http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/association-mapping.html#one-to-many-bidirectional
and
http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/association-mapping.html#many-to-many-bidirectional
That's all :)

Extending models with additional fields in Doctrine

I'm trying to figure out a way to extend a model in Doctrine. I have the following situation
Card Model (The base model):
Nos\CardBundle\Entity\Card:
type: entity
table: cards
inheritanceType: SINGLE_TABLE
discriminatorColumn:
name: card_type
type: string
discriminatorMap:
staff: Nos\StaffBundle\Entity\Staff
supplier: Nos\SupplierBundle\Entity\Supplier
id:
id:
id: true
type: integer
length: 11
generator:
strategy: AUTO
options:
unsigned: true
fields:
user_id:
type: integer
length: 11
nullable: true
options:
unsigned: true
card_display_name:
type: string
length: 255
nullable: false
I discriminate the model using the card_type property. When it's 'staff', it should return a Staff model, when it's 'supplier' it should return a Supplier Model
Staff Model (Extends Card model)
Nos\StaffBundle\Entity\Staff:
type: entity
inheritance:
extends: Nos\CardBundle\Entity\Card
type: simple
staff_initials:
type: string
length: 15
staff_baptismal_names:
type: string
length: 255
and the Supplier model
Nos\StaffBundle\Entity\Supplier:
type: entity
inheritance:
extends: Nos\CardBundle\Entity\Card
type: simple
supplier_name:
type: string
length: 255
When I generate the tables, the additional fields in Supplier and Staff models are not being created. Do I have to define those additional fields in the Card model? And if so, how can I exclude them from the models that they don't belong to?

How to specify field name

I had next entity with yaml format:
BW\UserBundle\Entity\User:
type: entity
table: users
repositoryClass: BW\UserBundle\Entity\UserRepository
id:
id:
type: integer
id: true
generator:
strategy: AUTO
fields:
username:
type: string
length: 25
unique: true
password:
type: string
length: 64
email:
type: string
length: 60
unique: true
isActive:
type: boolean
lifecycleCallbacks: { }
Now, when I update scheme, I get isActive field name in DB, same as property name.
How can I specify property name isActive like is_active field name in DB?
You need to specify a column name like this:
BW\UserBundle\Entity\User:
type: entity
table: users
repositoryClass: BW\UserBundle\Entity\UserRepository
id:
id:
type: integer
id: true
generator:
strategy: AUTO
fields:
username:
type: string
length: 25
unique: true
password:
type: string
length: 64
email:
type: string
length: 60
unique: true
isActive:
type: boolean
column: is_active
lifecycleCallbacks: { }
More on that - http://docs.doctrine-project.org/en/2.0.x/reference/basic-mapping.html#property-mapping
You can specify the column name for the database
fields:
username:
type: string
length: 25
unique: true
password:
type: string
length: 64
email:
type: string
length: 60
unique: true
isActive:
type: boolean
column: is_active
You dealing with two different kind of conventions here. It is common in Symfony to use camel case for naming variables. Within databases you often find this underscore written names. If you access your isActive property your entity model will map it to the is_active column in the database.

SonataDoctrineORMAdminBundle throws array_combine error

I have a many-to many relation which is throwing a warning:
Warning: array_combine(): Both parameters should have an equal number of elements in vendor/sonata-project/doctrine-orm-admin-bundle/Sonata/DoctrineORMAdminBundle/Model/ModelManager.php line 179
The admin class in question was working fine until updated SonataDoctrineORMAdminBundle to version 2.2.4 with composer.
I think that the problem may be on my models but not sure what it is.
bundle\Entity\EntityOne:
type: entity
table: entityOne
fields:
id:
type: integer
id: true
generator:
strategy: AUTO
title:
type: string
length: '100'
oneToMany:
entityRel:
targetEntity: EntityRel
mappedBy: entityOne
cascade: ["persist", "remove"]
lifecycleCallbacks: { }
bundle\Entity\EntityRel:
type: entity
table: entityRel
id:
entityOne:
associationKey: true
entityTwo:
associationKey: true
fields:
amount:
type: decimal
oneToOne:
entityOne:
targetEntity: EntityOne
entityTwo:
targetEntity: EntityTwo
lifecycleCallbacks: { }
bundle\Entity\EntityTwo:
type: entity
table: entityTwo
fields:
id:
type: integer
id: true
generator:
strategy: AUTO
name:
type: string
length: '100'
oneToMany:
entityRel:
targetEntity: entityRel
mappedBy: entityTwo
cascade: ["persist", "remove"]
lifecycleCallbacks: { }
The idea is that EntityRel connects EntityOne and EntityTwo with an amount field. Each connection between the two tables must be unique thus enforcing the key to be composite.
Any idea?
It seems like you miss the EntityTwo's table:
bundle\Entity\EntityTwo:
type: entity
table: entityTwo

Resources