DataAccess constructor

DataAccess(
  1. DataAccessImplementation implementation, {
  2. List<DbCollection>? collections,
  3. Map<DbOperationType, bool>? defaultPermissionsByType,
  4. bool defaultPermission = true,
  5. bool streamSupport = false,
})

Implementation

factory DataAccess(
  DataAccessImplementation implementation, {
  List<DbCollection>? collections,
  Map<DbOperationType, bool>? defaultPermissionsByType,
  bool defaultPermission = true,
  bool streamSupport = false,
}) {
  /// set permission handler if
  /// or
  /// -- any collection have custom permission
  /// -- defaultPermissionsByType is not null
  /// -- defaultPermission is false
  var hasPermission = (collections
              ?.where((element) =>
                  element.permissionHandler != null || element.hasSchema)
              .isNotEmpty ??
          false) ||
      defaultPermissionsByType != null ||
      !defaultPermission;

  var hasTrigger = collections
          ?.where((element) =>
              element.triggers != null && element.triggers!.isNotEmpty)
          .isNotEmpty ??
      false;
  Map<String, String>? _identifierMapping;
  TriggerService? _triggerService;
  PermissionHandlerService? _permissionHandler;

  if (collections != null) {
    var hasIdentifier =
        collections.where((element) => element.identifier != null).isNotEmpty;

    if (hasIdentifier) {
      _identifierMapping = {};
      for (var db in collections) {
        if (db.identifier != null) {
          _identifierMapping[db.collectionName] = db.identifier!;
        }
      }
    }
  }

  if (hasPermission) {
    _permissionHandler = PermissionHandlerService.create(
        defaultPermission: defaultPermission,
        collections: collections,
        defaultRules: defaultPermissionsByType);
  }

  if (hasTrigger || streamSupport) {
    _triggerService = TriggerService.create(
        streamSupport: streamSupport, collections: collections);
  }

  DataAccess _acc;

  if (collections?.isEmpty ?? true) {
    _acc = _DataAccessEmpty(implementation, _identifierMapping);
  } else if (_triggerService != null && _permissionHandler == null) {
    _acc = _DataAccessWithOnlyTrigger(
        implementation, _triggerService, _identifierMapping);
  } else if (_triggerService == null && _permissionHandler != null) {
    _acc = _DataAccessWithPermission(
        implementation, _permissionHandler, _identifierMapping);
  } else {
    _acc = _DataAccessWithTriggerAndPermission(implementation,
        _triggerService!, _permissionHandler!, _identifierMapping);
  }
  return _acc;
}