private static CreateQueryReturn ToCreateNode(NeoContext context, object entity, string queryKey, IDictionary <object, string> entities) { entities.Add(entity, queryKey); var meta = context.GetMetaData(entity.GetType()); meta.GetCreatedAtProperty()?.SetValue(entity, DateTime.UtcNow); meta.GetUpdatedAtProperty()?.SetValue(entity, DateTime.UtcNow); var properties = BuildPropertyClause(meta, entity); var r = new CreateQueryReturn(); r.FlattedEntities.Add(queryKey, entity); r.Query.AppendLine($"CREATE ({queryKey}:{meta.Label} {properties})"); var rIndex = 0; foreach (var p in meta.RelationshipProperties) { var rMeta = context.GetMetaData(p.EntityType); var rEntity = p.GetValue(entity); if (rEntity == null) { continue; } var rCollection = p.IsCollection ? (ICollection)rEntity : new[] { rEntity }; foreach (var item in rCollection) { if (entities.TryGetValue(item, out var existKey)) { r.Query.AppendLine(ToRelationshipCreate(p, queryKey, existKey)); continue; } var rNodeKey = NodeKey.Create(rMeta.Key, rIndex++, queryKey); r.Append(ToCreateNode(context, item, rNodeKey, entities)); r.Query.AppendLine(ToRelationshipCreate(p, queryKey, rNodeKey)); } } return(r); }
internal static CreateQueryReturn ToCreate(NeoContext context, object entity) { var meta = context.GetMetaData(entity.GetType()); var createQuery = ToCreateNode(context, entity, NodeKey.Create(meta.Key), new Dictionary <object, string>()); createQuery.Query.Append("RETURN "); foreach (var key in createQuery.FlattedEntities.Keys) { createQuery.Query.Append($"{key},"); } createQuery.Query.TryRemoveLastCharacter(','); return(createQuery); }
public T Create <T>(T entity) { var create = CypherTranslator.ToCreate(_context, entity); using var session = NewSession(); return(session.WriteTransaction(tx => { var result = tx.Run(create.Query.ToString()).Single(); foreach (var(key, value) in result.Values) { var iNode = value.As <INode>(); var e = create.FlattedEntities[key]; var eMeta = _context.GetMetaData(e.GetType()); eMeta.SetId(e, iNode.Id); } return entity; })); }