Example #1
0
        public async Task Can_Create_Typed_GlobalIndex()
        {
            var db = CreatePocoDynamo();

            db.RegisterTable <OrderWithGlobalTypedIndex>();

            var customer = await CreateCustomerAsync(db);

            var table = DynamoMetadata.GetTable <OrderWithGlobalTypedIndex>();

            Assert.That(table.GlobalIndexes.Count, Is.EqualTo(1));
            Assert.That(table.GlobalIndexes[0].Name, Is.EqualTo(nameof(OrderGlobalCostIndex)));
            Assert.That(table.GlobalIndexes[0].HashKey.Name, Is.EqualTo("ProductId"));
            Assert.That(table.GlobalIndexes[0].RangeKey.Name, Is.EqualTo("Cost"));

            var orders = 10.Times(i => new OrderWithGlobalTypedIndex
            {
                ProductId = 1,
                Qty       = i + 2,
                Cost      = (i + 2) * 2,
                LineItem  = "Item " + (i + 1),
            });

            await db.PutRelatedItemsAsync(customer.Id, orders);

            var expensiveOrders = await db.QueryAsync(db.FromQueryIndex <OrderGlobalCostIndex>(x => x.ProductId == 1 && x.Cost > 10)).ToListAsync();

            Assert.That(expensiveOrders.Count, Is.EqualTo(orders.Count(x => x.ProductId == 1 && x.Cost > 10)));
            Assert.That(expensiveOrders.All(x => x.Cost > 10 && x.Id > 0 && x.Qty > 0));

            //Note: Local DynamoDb supports projecting attributes not in GlobalIndex, AWS DynamoDB Doesn't
            var dbOrders = (await db.QueryIntoAsync <OrderWithGlobalTypedIndex>(db.FromQueryIndex <OrderGlobalCostIndex>(x => x.ProductId == 1 && x.Cost > 10))).ToList();

            Assert.That(dbOrders.All(x => x.Cost > 10 && x.Id > 0 && x.Qty > 0 && x.LineItem != null));
            dbOrders = (await db.FromQueryIndex <OrderGlobalCostIndex>(x => x.ProductId == 1 && x.Cost > 10).ExecIntoAsync <OrderWithGlobalTypedIndex>()).ToList();
            Assert.That(dbOrders.All(x => x.Cost > 10 && x.Id > 0 && x.Qty > 0 && x.LineItem != null));

            expensiveOrders = await db.ScanAsync(db.FromScanIndex <OrderGlobalCostIndex>(x => x.Cost > 10)).ToListAsync();

            Assert.That(expensiveOrders.All(x => x.Cost > 10 && x.Id > 0 && x.Qty > 0));

            var indexOrders = (await db.ScanIntoAsync <OrderWithGlobalTypedIndex>(db.FromScanIndex <OrderGlobalCostIndex>(x => x.Cost > 10))).ToList();

            Assert.That(indexOrders.Count, Is.GreaterThan(0));
            Assert.That(indexOrders.All(x => x.Cost > 10 && x.Id > 0 && x.Qty > 0 && x.LineItem != null));
        }
        public void Does_ignore_fields()
        {
            var db = CreatePocoDynamo();

            db.RegisterTable <TableWithIgnoredFields>();
            db.InitSchema();

            db.PutItem(new TableWithIgnoredFields
            {
                Id        = 1,
                FirstName = "Foo",
                LastName  = "Bar",
                IsIgnored = 10,
            });

            var row = db.GetItem <TableWithIgnoredFields>(1);

            Assert.That(row.DisplayName, Is.EqualTo("Foo Bar"));
            Assert.That(row.IsIgnored, Is.EqualTo(0));

            var table   = DynamoMetadata.GetTable <TableWithIgnoredFields>();
            var request = new GetItemRequest
            {
                TableName = table.Name,
                Key       = db.Converters.ToAttributeKeyValue(db, table.HashKey, 1),
            };

            var raw = db.DynamoDb.GetItem(request);

            Assert.That(raw.Item.ContainsKey("Id"));
            Assert.That(raw.Item.ContainsKey("FirstName"));
            Assert.That(raw.Item.ContainsKey("LastName"));
            Assert.That(!raw.Item.ContainsKey("DisplayName"));
            Assert.That(!raw.Item.ContainsKey("IsIgnored"));

            var json = row.ToJson();

            Assert.That(json, Is.Not.Contains("LastName"));
        }