def insert_unique(table, data, unique_fields=None, *, raise_if_found=False):
item = find_unique(table, data, unique_fields)<EOL>if item is not None:<EOL><INDENT>if raise_if_found:<EOL><INDENT>raise NotUniqueItemError('<STR_LIT>'<EOL>'<STR_LIT>'.format(unique_fields,<EOL>data,<EOL>table.get(eid=item),<EOL>item))<EOL><DEDENT>else:<EOL><INDENT>return item<EOL><DEDENT><DEDENT>return table.insert(data)<EOL>
Insert `data` into `table` ensuring that data has unique values
    in `table` for the fields listed in `unique_fields`.

    If `raise_if_found` is True, will raise an NotUniqueItemError if
    another item with the same `unique_fields` values are found
    previously in `table`.
    If False, will return the `eid` from the item found.

    Parameters
    ----------
    table: tinydb.Table

    data: dict

    unique_fields: list of str
        Name of fields (keys) from `data` which are going to be used to build
        a sample to look for exactly the same values in the database.
        If None, will use every key in `data`.

    raise_if_found: bool

    Returns
    -------
    eid: int
        Id of the object inserted or the one found with same `unique_fields`.

    Raises
    ------
    MoreThanOneItemError
        Raise even with `raise_with_found` == False if it finds more than one item
        with the same values as the sample.

    NotUniqueItemError
        If `raise_if_found` is True and an item with the same `unique_fields`
        values from `data` is found in `table`.