Path: redb/redb.Core/Providers/Base/ObjectStorageProviderBase.AddNewObjects.cs
Snapshot: redb, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages.
License: Apache-2.0. Repository: https://github.com/redbase-app/redb
Page: https://redbase.app/src/dev-2026-12-06/redb/redb.Core/Providers/Base/ObjectStorageProviderBase.AddNewObjects.cs
----
using redb.Core.Providers;
using redb.Core.Data;
using redb.Core.Utils;
using redb.Core.Models.Contracts;
using redb.Core.Models.Entities;
using redb.Core.Models.Configuration;
using redb.Core.Models.Security;
using redb.Core.Query;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System;
namespace redb.Core.Providers.Base
{
///
/// BULK INSERT - high-performance creation of multiple objects
///
public abstract partial class ObjectStorageProviderBase
{
///
/// BULK INSERT: Create multiple new objects in one operation (WITHOUT permission checks)
/// The batch save's preparation steps + BulkInsert for maximum performance
///
public async Task> AddNewObjectsAsync(IEnumerable> objects, CancellationToken cancellationToken = default) where TProps : class, new()
{
return await AddNewObjectsAsync(objects, _securityContext.CurrentUser, cancellationToken);
}
///
/// BULK INSERT with explicit user: Create multiple new objects (WITHOUT permission checks)
///
public async Task> AddNewObjectsAsync(IEnumerable> objects, IRedbUser user, CancellationToken cancellationToken = default) where TProps : class, new()
{
// Materialize IEnumerable to list for multiple iterations
var objectsList = objects?.ToList() ?? new List>();
if (objectsList.Count == 0)
{
return new List();
}
// Validation
foreach (var obj in objectsList)
{
if (obj.Props == null)
{
throw new ArgumentException($"Object properties '{obj.Name}' cannot be null", nameof(objects));
}
}
// Initial processing of hashes and schemes for main objects
foreach (var obj in objectsList)
{
// Recalculate hash
var currentHash = RedbHash.ComputeFor(obj);
if (currentHash.HasValue)
{
obj.Hash = currentHash.Value;
}
// Auto-determination of scheme (WITHOUT checks for existing objects)
if (obj.SchemeId == 0 && _configuration.AutoSyncSchemesOnSave)
{
var existingScheme = await _schemeSync.GetSchemeByTypeAsync();
if (existingScheme != null)
{
obj.SchemeId = existingScheme.Id;
}
else if (_configuration.AutoSyncSchemesOnSave)
{
try
{
var scheme = await _schemeSync.SyncSchemeAsync();
obj.SchemeId = scheme.Id;
}
catch (Exception ex)
{
throw;
}
}
}
}
// === THE BATCH SAVE'S PREPARATION STEPS ===
var objectsToSave = new List();
var valuesToSave = new List();
var processedObjectIds = new HashSet();
var seenInstances = new HashSet