public virtual AttributeValue ToMapAttributeValue(IPocoDynamo db, object oMap)
        {
            var map = oMap as IDictionary
                      ?? oMap.ToObjectDictionary();

            var meta = DynamoMetadata.GetType(oMap.GetType());

            var to = new Dictionary <string, AttributeValue>();

            foreach (var key in map.Keys)
            {
                var value = map[key];
                if (value != null)
                {
                    value = ApplyFieldBehavior(db,
                                               meta,
                                               meta?.GetField((string)key),
                                               oMap,
                                               value);
                }

                to[key.ToString()] = value != null
                    ? ToAttributeValue(db, value.GetType(), GetFieldType(value.GetType()), value)
                    : new AttributeValue
                {
                    NULL = true
                };
            }
            return(new AttributeValue {
                M = to, IsMSet = true
            });
        }
Example #2
0
        public long Increment <T>(object hash, string fieldName, long amount = 1)
        {
            var type    = DynamoMetadata.GetType <T>();
            var request = new UpdateItemRequest
            {
                TableName        = type.Name,
                Key              = Converters.ToAttributeKeyValue(this, type.HashKey, hash),
                AttributeUpdates = new Dictionary <string, AttributeValueUpdate> {
                    {
                        fieldName,
                        new AttributeValueUpdate {
                            Action = AttributeAction.ADD,
                            Value  = new AttributeValue {
                                N = amount.ToString()
                            }
                        }
                    }
                },
                ReturnValues = ReturnValue.ALL_NEW,
            };

            var response = DynamoDb.UpdateItem(request);

            return(response.Attributes.Count > 0
                ? Convert.ToInt64(response.Attributes[fieldName].N)
                : 0);
        }
 public PocoDynamoExpression(Type type)
 {
     Type             = type;
     Table            = DynamoMetadata.GetType(type);
     ParamPrefix      = "p";
     Params           = new Dictionary <string, object>();
     ReferencedFields = new List <string>();
     Aliases          = new Dictionary <string, string>();
 }
Example #4
0
        public IEnumerable <T> ScanAll <T>()
        {
            var type    = DynamoMetadata.GetType <T>();
            var request = new ScanRequest
            {
                Limit     = PagingLimit,
                TableName = type.Name,
            };

            return(Scan(request, r => r.ConvertAll <T>()));
        }
        public virtual object FromMapAttributeValue(Dictionary <string, AttributeValue> map, Type type)
        {
            var from = new Dictionary <string, object>();

            var metaType = DynamoMetadata.GetType(type);

            if (metaType == null)
            {
                var toMap       = (IDictionary)type.CreateInstance();
                var genericDict = type.GetTypeWithGenericTypeDefinitionOf(typeof(IDictionary <,>));
                if (genericDict != null)
                {
                    var genericArgs = genericDict.GetGenericArguments();
                    var keyType     = genericArgs[0];
                    var valueType   = genericArgs[1];

                    foreach (var entry in map)
                    {
                        var key = ConvertValue(entry.Key, keyType);
                        toMap[key] = FromAttributeValue(entry.Value, valueType);
                    }

                    return(toMap);
                }

                throw new ArgumentException("Unknown Map Type " + type.Name);
            }

            foreach (var field in metaType.Fields)
            {
                AttributeValue attrValue;
                if (!map.TryGetValue(field.Name, out attrValue))
                {
                    continue;
                }

                from[field.Name] = FromAttributeValue(attrValue, field.Type);
            }

            var to = from.FromObjectDictionary(type);

            return(to);
        }
        public virtual AttributeValue ToListAttributeValue(IPocoDynamo db, object oList)
        {
            var list = ((IEnumerable)oList).Map(x => x);

            if (list.Count <= 0)
            {
                return new AttributeValue {
                           L = new List <AttributeValue>(), IsLSet = true
                }
            }
            ;

            var elType = list[0].GetType();
            var elMeta = DynamoMetadata.GetType(elType);

            if (elMeta != null)
            {
                var autoIncrFields = elMeta.Fields.Where(x => x.IsAutoIncrement).ToList();

                foreach (var field in autoIncrFields)
                {
                    //Avoid N+1 by fetching a batch of ids
                    var autoIds = db.Sequences.GetNextSequences(elMeta, list.Count);
                    for (var i = 0; i < list.Count; i++)
                    {
                        var instance = list[i];
                        var value    = field.GetValue(instance);
                        if (IsNumberDefault(value))
                        {
                            field.SetValue(instance, autoIds[i]);
                        }
                    }
                }
            }

            var values = list.Map(x => ToAttributeValue(db, x.GetType(), GetFieldType(x.GetType()), x));

            return(new AttributeValue {
                L = values
            });
        }
 public static List <T> ConvertAll <T>(this QueryResponse response)
 {
     return(response.Items
            .Select(values => DynamoMetadata.GetType <T>().ConvertTo <T>(values))
            .ToList());
 }
Example #8
0
 public static async Task ResetAsync <T>(this ISequenceSourceAsync seq, int startingAt = 0, CancellationToken token = default)
 {
     var tableName = DynamoMetadata.GetType <T>().Name;
     await seq.ResetAsync(tableName, startingAt, token).ConfigAwait();
 }
Example #9
0
        public static async Task <long> IncrementAsync <T>(this ISequenceSourceAsync seq, int amount = 1, CancellationToken token = default)
        {
            var tableName = DynamoMetadata.GetType <T>().Name;

            return(await seq.IncrementAsync(tableName, amount, token).ConfigAwait());
        }
Example #10
0
        public static void Reset <T>(this ISequenceSource seq, int startingAt = 0)
        {
            var tableName = DynamoMetadata.GetType <T>().Name;

            seq.Reset(tableName, startingAt);
        }
Example #11
0
        public static long Increment <T>(this ISequenceSource seq, int amount = 1)
        {
            var tableName = DynamoMetadata.GetType <T>().Name;

            return(seq.Increment(tableName, amount));
        }
Example #12
0
 public static async Task <long[]> GetNextSequencesAsync <T>(this ISequenceSourceAsync seq, int noOfSequences, CancellationToken token = default)
 {
     return(await GetNextSequencesAsync(seq, DynamoMetadata.GetType <T>(), noOfSequences).ConfigAwait());
 }
Example #13
0
 public static long[] GetNextSequences <T>(this ISequenceSource seq, int noOfSequences)
 {
     return(GetNextSequences(seq, DynamoMetadata.GetType <T>(), noOfSequences));
 }
Example #14
0
        public static async Task <long> CurrentAsync <T>(this ISequenceSourceAsync seq, CancellationToken token = default)
        {
            var tableName = DynamoMetadata.GetType <T>().Name;

            return(await seq.IncrementAsync(tableName, 0, token));
        }
Example #15
0
        public static long Current <T>(this ISequenceSource seq)
        {
            var tableName = DynamoMetadata.GetType <T>().Name;

            return(seq.Increment(tableName, 0));
        }