public virtual Dictionary <string, AttributeValue> ToAttributeValues(IPocoDynamo db, object instance, DynamoMetadataType table)
        {
            var ret = ToAttributeValuesFn?.Invoke(instance, table);

            if (ret != null)
            {
                return(ret);
            }

            using (AwsClientUtils.GetJsScope())
            {
                var to = new Dictionary <string, AttributeValue>();

                foreach (var field in table.Fields)
                {
                    var value = field.GetValue(instance);

                    value = ApplyFieldBehavior(db, table, field, instance, value);

                    if (value == null)
                    {
                        if (DynamoConfig.ExcludeNullValues || field.ExcludeNullValue)
                        {
                            continue;
                        }
                    }

                    to[field.Name] = ToAttributeValue(db, field.Type, field.DbType, value);
                }

                return(to);
            }
        }
        private bool CacheSet <T>(string key, T value, DateTime?expiresAt)
        {
            var    now   = DateTime.UtcNow;
            string json  = AwsClientUtils.ToScopedJson(value);
            var    entry = new CacheEntry
            {
                Id           = key,
                Data         = json,
                CreatedDate  = now,
                ModifiedDate = now,
                ExpiryDate   = expiresAt,
            };

            Exception lastEx       = null;
            var       i            = 0;
            var       firstAttempt = DateTime.UtcNow;

            while (DateTime.UtcNow - firstAttempt < Dynamo.MaxRetryOnExceptionTimeout)
            {
                i++;
                try
                {
                    Dynamo.PutItem(entry);
                    return(true);
                }
                catch (ResourceNotFoundException ex)
                {
                    lastEx = ex;
                    i.SleepBackOffMultiplier(); //Table could temporarily not exist after a FlushAll()
                }
            }

            throw new TimeoutException($"Exceeded timeout of {Dynamo.MaxRetryOnExceptionTimeout}", lastEx);
        }
 public virtual Dictionary <string, AttributeValue> ToAttributeKeyValue(IPocoDynamo db, DynamoMetadataField field, object hash)
 {
     using (AwsClientUtils.GetJsScope())
     {
         return(new Dictionary <string, AttributeValue> {
             { field.Name, ToAttributeValue(db, field.Type, field.DbType, hash) },
         });
     }
 }
 public virtual Dictionary <string, AttributeValue> ToAttributeKeyValue(IPocoDynamo db, DynamoMetadataType table, DynamoId id)
 {
     using (AwsClientUtils.GetJsScope())
     {
         return(new Dictionary <string, AttributeValue> {
             { table.HashKey.Name, ToAttributeValue(db, table.HashKey.Type, table.HashKey.DbType, id.Hash) },
             { table.RangeKey.Name, ToAttributeValue(db, table.RangeKey.Type, table.RangeKey.DbType, id.Range) },
         });
     }
 }
        public virtual Dictionary <string, AttributeValue> ToAttributeKeyValue(IPocoDynamo db, DynamoMetadataType table, object hash, object range)
        {
            using (AwsClientUtils.GetJsScope())
            {
                var to = new Dictionary <string, AttributeValue> {
                    { table.HashKey.Name, ToAttributeValue(db, table.HashKey.Type, table.HashKey.DbType, hash) },
                };

                if (range != null)
                {
                    to[table.RangeKey.Name] = ToAttributeValue(db, table.RangeKey.Type, table.RangeKey.DbType, range);
                }

                return(to);
            }
        }
        public virtual Dictionary <string, AttributeValue> ToAttributeKey(IPocoDynamo db, DynamoMetadataType table, object instance)
        {
            using (AwsClientUtils.GetJsScope())
            {
                var field = table.HashKey;
                var to    = new Dictionary <string, AttributeValue> {
                    { field.Name, ToAttributeValue(db, field.Type, field.DbType, field.GetValue(instance)) },
                };

                if (table.RangeKey != null)
                {
                    field          = table.RangeKey;
                    to[field.Name] = ToAttributeValue(db, field.Type, field.DbType, field.GetValue(instance));
                }

                return(to);
            }
        }
        public virtual Dictionary <string, AttributeValueUpdate> ToNonDefaultAttributeValueUpdates(IPocoDynamo db, object instance, DynamoMetadataType table)
        {
            using (AwsClientUtils.GetJsScope())
            {
                var to = new Dictionary <string, AttributeValueUpdate>();
                foreach (var field in table.Fields)
                {
                    if (field.IsHashKey || field.IsRangeKey)
                    {
                        continue;
                    }

                    var value = field.GetValue(instance);

                    if (value == null)
                    {
                        continue;
                    }

                    to[field.Name] = new AttributeValueUpdate(ToAttributeValue(db, field.Type, field.DbType, value), DynamoAttributeAction.Put);
                }
                return(to);
            }
        }