Collections Reference Guide

This guide is for versions Beta 37+


Source code


Class Collections

    An ordered sequence of Data Nodes.

    We define a "Collection" as an entity to which a variety of Data Nodes
    are attached, with positional attributes in their links.

    In terms of implementation, it's a hub, with a central data node (the "Collection"),
    to which any number of Data Nodes (or "Collection Items") are linked;
    the relationship name can be anything (user-passed) but it gets assigned a property
    named "pos", which is managed by this class, to maintain and alter the sequential order.

    Example of use case: "pages" of "Content Items" attached to a "Category"
    
nameargumentsreturns
set_databasecls, db :NeoAccessNone
        IMPORTANT: this method MUST be called before using this class!

        :param db:  Database-interface object, created with the NeoAccess library
        :return:    None
        
nameargumentsreturns
initialize_collectionscls(int, str)
        Create a new Schema Class node that represents a "Collection"

        :return:    An (int, str) pair of integers with the internal database ID
                        and the unique uri assigned to the node just created
        
nameargumentsreturns
is_collectioncls, collection_uri :strbool
        Return True if the Data Node with the given uri is a Collection,
        that is, if its schema is a Class that is an INSTANCE_OF the "Collections" Class

        :param collection_uri:  A string with the URI of a Data Node
        :return:                True if the given data node is a Collection, or False otherwise
        
nameargumentsreturns
collection_sizecls, collection_id :str, membership_rel_name: str, skip_check=Falseint
        Return the number of elements in the given Collection (i.e. Data Items linked to it thru the specified relationship)

        :param collection_id:       The uri of a data node whose schema is an instance of the Class "Collections"
        :param membership_rel_name: The name of the relationship from other Data Items to the given Collection node
        :param skip_check:          If True, no check is done to verify that the data node whose uri matches collection_id
                                    is indeed a Collection.
                                    Without a check, this function will return a zero if given a bad collection_id;
                                    with a check, it'll raise an Exception
        :return:                    The number of elements in the given Collection (possibly zero)
        
nameargumentsreturns
add_to_collection_at_beginningcls, collection_uri :str, membership_rel_name: str, item_class_name: str, item_properties: dict, new_uri=Nonestr
        Create a new data node, of the class specified in item_class_name, and with the given properties -
        and add it at the beginning of the specified Collection, linked by the specified relationship

        EXAMPLE:  new_uri = add_to_collection_at_beginning(collection_id=708, membership_rel_name="BA_in_category",
                                                        item_class_name="Headers", item_properties={"text": "New Caption, at the end"})
        

        :return:                    The auto-increment "uri" assigned to the newly-created data node
        
nameargumentsreturns
link_to_collection_at_endcls, item_uri :str, collection_uri :str, membership_rel_name :strNone
        Given an existing data node (meant to be a "Collection Item"),
        link it to the end of the specified Collection data node,
        using the requested relationship name.

        If a link already exists, an Exception is raised.

        :param item_uri:            The URI of an existing Data Node representing a "Collection Item"
        :param collection_uri:      The URI of an existing Collection Data Node
        :param membership_rel_name: The name to give to the relationship
                                        in the direction from the "Collection Item" to the Collection node
        :return:                    None
        
nameargumentsreturns
relocate_to_other_collection_at_endcls, item_uri :str, from_collection_uri :str, to_collection_uri :str, membership_rel_name :str
        Given an existing data node (representing a "Collection Item" of the specified "from" Collection),
        switch it to become a "Collection Item" of the "to" Collection, positioned at the end of it.

        The collection-membership relationship is severed from the "Collection Item" to the "from" Collection,
        and a new one is created from the "Collection Item" to the "to" Collection.

        In case no operation is performed, an Exception is raised.

        :param item_uri:            The URI of a Data Node representing a "Collection Item" of the "from" Collection below
        :param from_collection_uri: The URI of a Collection Data Node to which the above "Collection Item" is connected
        :param to_collection_uri:   The URI of a Collection Data Node to which the above "Collection Item" needs to be switched to
        :param membership_rel_name: The name to give to the relationship
                                        in the direction from the "Collection Item" to the Collection node
        :return:                    None
        
nameargumentsreturns
bulk_relocate_to_other_collection_at_endcls, items :Union[List[str], str], from_collection :str, to_collection :str, membership_rel_name :strint
        Given an existing list of data nodes (representing "Collection Items" of the specified "from" Collection),
        switch each of them to become a "Collection Item" of the "to" Collection, positioned at the end of it.

        The collection-membership relationship is severed from each of the "Collection Items" to the "from" Collection,
        and a new one is created from that "Collection Item" to the "to" Collection.

        Return the number of Collection Items successfully relocated.

        :param items:               URI, or list of URI's, of Data Node(s)
                                        representing a "Collection Items" of the "from" Collection below
        :param from_collection:     The URI of a Collection Data Node to which the above Collection Item(s) are connected
        :param to_collection:       The URI of a Collection Data Node to which the above Collection Item(s) needs to be switched to
        :param membership_rel_name: The name to give to the relationship
                                        in the direction from the "Collection Item" to the Collection node
        :return:                    The number of Collection Items successfully relocated
        
nameargumentsreturns
add_to_collection_at_endcls, collection_uri :str, membership_rel_name: str, item_class_name: str, item_properties: dict, new_uri=Nonestr
        Create a new data node, of the class specified in item_class_name, and with the given properties -
        and add it at the end of the specified Collection, linked by the specified relationship

        EXAMPLE:  new_uri = add_to_collection_at_end(collection_id=708, membership_rel_name="BA_in_category",
                                                     item_class_name="Headers", item_properties={"text": "New Caption, at the end"}

        :param collection_uri:      The uri of a data node whose schema is an instance of the Class "Collections"
        :param membership_rel_name:
        :param item_class_name:
        :param item_properties:
        :param new_uri:             Normally, the Item ID is auto-generated, but it can also be provided (Note: MUST be unique)

        :return:                    The auto-increment "uri" assigned to the newly-created data node
        
nameargumentsreturns
add_to_collection_after_elementcls, collection_uri :str, membership_rel_name: str, item_class_name: str, item_properties: dict, insert_after :str, new_uri=Nonestr
        Create a new data node, of the class specified in item_class_name, and with the given properties -
        and add to the specified Collection, linked by the specified relationship and inserted after the given collection Item
        (in the context of the positional order encoded in the relationship attribute "pos")

        :param collection_uri:      The uri of a data node whose schema is an instance of the Class "Collections"
        :param membership_rel_name: The name of the relationship to which the positions ("pos" attribute) apply
        :param item_class_name:     Name of the Class for the newly-created node
        :param item_properties:     Dictionary with the properties of the newly-created node
        :param insert_after:        The URI of the element after which we want to insert
        :param new_uri:             Normally, the Item ID is auto-generated, but it can also be provided (Note: MUST be unique)

        :return:                    The auto-increment "uri" assigned to the newly-created data node
        
nameargumentsreturns
shift_downcls, collection_id :str, membership_rel_name :str, first_to_move :intint
        Shift down the positions (values of the "pos" relationship attributes) by an arbitrary fixed amount,
        starting with nodes with the specified position value (and all greater values);
        this operation applies to nodes linked to the specified Collection thru a relationship with the given name.

        :param collection_id:       The uri of a data node whose schema is an instance of the Class "Collections"
        :param membership_rel_name: The name of the relationship to which the positions ("pos" attribute) apply
        :param first_to_move:       All position ("pos") values greater or equal to this one will get shifted down
        :return:                    The number of modified items