public static async Task ImportAsync(this ISession session, IImportSettings setting, ILogger log, IEnumerable <DynamicData> datas) { var contents = session.Contents(setting.Schema); var totalWritten = 0; using (var logLine = log.WriteSameLine()) { foreach (var data in datas) { await contents.CreateAsync(data, !setting.Unpublished); totalWritten++; logLine.WriteLine("> Imported: {0}.", totalWritten); } } log.WriteLine("> Imported: {0}. Completed.", totalWritten); }
public static async Task ImportAsync(this ISession session, IImportSettings setting, ILogger log, IEnumerable <DynamicData> datas) { var contents = session.Contents(setting.Schema); var totalWritten = 0; using (var logLine = log.WriteSameLine()) { var keyField = setting.KeyField; var update = new BulkUpdate { Jobs = new List <BulkUpdateJob>(), DoNotScript = false, DoNotValidate = false, Publish = !setting.Unpublished }; const string op = "eq"; foreach (var batch in datas.Batch(50)) { update.Jobs.Clear(); foreach (var data in batch) { var job = new BulkUpdateJob { Data = data, }; if (!string.IsNullOrWhiteSpace(keyField)) { if (!data.TryGetValue(keyField, out var temp) || temp is not JObject obj || !obj.TryGetValue("iv", StringComparison.Ordinal, out var value)) { throw new InvalidOperationException($"Cannot find key '{keyField}' in data."); } job.Query = new { filter = new { path = $"data.{keyField}.iv", op, value, } }; job.Type = BulkUpdateType.Upsert; } else { job.Type = BulkUpdateType.Create; } update.Jobs.Add(job); } var result = await contents.BulkUpdateAsync(update); var error = result.Find(x => x.Error != null)?.Error; if (error != null) { throw new SquidexManagementException <ErrorDto>(error.Message, error.StatusCode, null, null, error, null); } totalWritten += update.Jobs.Count; logLine.WriteLine("> Imported: {0}.", totalWritten); } } log.WriteLine("> Imported: {0}. Completed.", totalWritten); }