save method

Future<Resource> save(
  1. String? password,
  2. Resource? resource
)

Saves a Resource to the local Db, password is optional (but after set, it must always be used everytime), will update the meta fields of the Resource and adds an id if none is already given.

Implementation

Future<Resource> save(String? password, Resource? resource) async {
  if (resource != null) {
    if (resource.resourceType != null) {
      await _addResourceType(password, resource.resourceType!);

      _setStoreType(resource.resourceTypeString ?? 'resources');

      return resource.id == null
          ? await _insert(password, resource)
          : (await find(null,
                      resourceType: resource.resourceType, id: resource.id))
                  .isEmpty
              ? await _insert(password, resource)
              : await _update(password, resource);
    } else {
      throw const FormatException('ResourceType cannot be null');
    }
  } else {
    throw const FormatException('Resource to save cannot be null');
  }
}