Skip to content

Commit

Permalink
Merge pull request #328 from AmirHJabari/Fix-IsNew-dynamodb-bug
Browse files Browse the repository at this point in the history
Update DynamoDB provider.
  • Loading branch information
m-sadegh-sh authored Mar 8, 2022
2 parents 0ae3e43 + e892f8d commit f9e0219
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 7 deletions.
39 changes: 32 additions & 7 deletions Olive.Entities.Data.DynamoDB/DynamoDBDataProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,23 @@ public Task<object> Aggregate(IDatabaseQuery query, AggregateFunction function,
throw new NotImplementedException();
}

public Task BulkInsert(IEntity[] entities, int batchSize)
public async Task BulkInsert(IEntity[] entities, int batchSize)
{
var writer = Dynamo.Db.CreateBatchWrite<T>();
writer.AddPutItems(entities.Cast<T>());
return writer.ExecuteAsync();
await writer.ExecuteAsync();

OnEntityLoaded(entities);
}

public Task BulkUpdate(IEntity[] entities, int batchSize)
public async Task BulkUpdate(IEntity[] entities, int batchSize)
{
var writer = Dynamo.Db.CreateBatchWrite<T>();
entities.Do(x => writer.AddDeleteKey(x.GetId()));
writer.AddPutItems(entities.Cast<T>());
return writer.ExecuteAsync();
await writer.ExecuteAsync();

OnEntityLoaded(entities);
}

public Task<int> Count(IDatabaseQuery query) => GetList(query).Count();
Expand All @@ -62,7 +66,8 @@ public string GenerateSelectCommand(IDatabaseQuery iquery, string fields)

public async Task<IEntity> Get(object objectID)
{
return (IEntity)await Dynamo.Table<T>().Get(objectID);
var result = (IEntity)await Dynamo.Table<T>().Get(objectID);
return OnEntityLoaded(result);
}

public DirectDatabaseCriterion GetAssociationInclusionCriteria(IDatabaseQuery masterQuery, PropertyInfo association)
Expand Down Expand Up @@ -96,6 +101,8 @@ async Task<IEnumerable<IEntity>> FindByHashKey(IDatabaseQuery query)
if (hashKeyInfo.Value is null) return Enumerable.Empty<IEntity>();
var item = await Get(hashKeyInfo.Value);
if (item is null) return Enumerable.Empty<IEntity>();

OnEntityLoaded(item);
return new[] { item };
}

Expand All @@ -121,7 +128,9 @@ async Task<IEnumerable<IEntity>> FindByIndex(IDatabaseQuery query)
if (indexInfo.IsIndex)
{
if (indexInfo.Value is null) return Enumerable.Empty<IEntity>();
return (await Dynamo.Index<T>(indexInfo.Name).All(indexInfo.Value)).Cast<IEntity>();

var result = (await Dynamo.Index<T>(indexInfo.Name).All(indexInfo.Value)).Cast<IEntity>();
return OnEntityLoaded(result);
}

return null;
Expand Down Expand Up @@ -177,7 +186,8 @@ ScanOperator GetOperator()
return new ScanCondition(criterion.PropertyName, @operator, values);
}

return (await Dynamo.Table<T>().All(query.Criteria.Select(ToCondition).ToArray())).Cast<IEntity>();
var result = (await Dynamo.Table<T>().All(query.Criteria.Select(ToCondition).ToArray())).Cast<IEntity>();
return OnEntityLoaded(result);
}

public IDictionary<string, Tuple<string, string>> GetUpdatedValues(IEntity original, IEntity updated)
Expand Down Expand Up @@ -221,8 +231,23 @@ public async Task Save(IEntity record)
await Dynamo.Client.UpdateItemAsync(
new UpdateItemRequest(tableName, key, updates)
);

OnEntityLoaded(record);
}

public bool SupportValidationBypassing() => throw new NotImplementedException();

IEntity OnEntityLoaded(IEntity entity)
{
Entity.Services.SetSaved(entity);
Entity.Services.SetOriginalId(entity);
return entity;
}

IEnumerable<IEntity> OnEntityLoaded(IEnumerable<IEntity> entities)
{
entities.Do(i => OnEntityLoaded(i));
return entities;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,7 @@
<ItemGroup>
<ProjectReference Include="..\Olive.Entities.Data\Olive.Entities.Data.csproj" />
</ItemGroup>
<Target Name="PostBuild" AfterTargets="PostBuildEvent">
<Exec Command="update-local-nuget-cache &quot;$(TargetPath)&quot;" />
</Target>
</Project>

0 comments on commit f9e0219

Please sign in to comment.