API Reference
select_new

select_new(query, pre_snapshot, last_snapshot)

Identifies objects that were newly created within a specific time range defined by two snapshots. This method is essential for performing differential analysis between two points in time. It effectively returns objects that are present in last_snapshot but absent from pre_snapshot.


Parameters

NameTypeDescription
queryQueryA dbzero query object, typically from dbzero.find(), that filters which objects to consider.
pre_snapshotSnapshot / NoneThe snapshot representing the starting point for the comparison.
last_snapshotSnapshotThe snapshot representing the ending point for the comparison.
💡

If pre_snapshot is None or doesn't contain the object prefix defined in the query, all objects matching the query in last_snapshot will be returned as new.


Returns

A query result set containing the objects that match the query and were created between pre_snapshot and last_snapshot. The result is stable and is not affected by subsequent commits made after last_snapshot was taken.


Examples

Find newly created objects

Here, we create a District object between two snapshots and then use select_new to find it.

# Create an initial state and snapshot
db0.commit()
snap_1 = db0.snapshot()
 
# Create a new District object and commit the change
new_district = District("some_District")
db0.commit()
snap_2 = db0.snapshot()
 
# Query for new District objects created between snap_1 and snap_2
new_objects = db0.select_new(db0.find(District), snap_1, snap_2)
 
assert len(new_objects) == 1
assert next(iter(new_objects)) == new_district

Handle a newly introduced object type

select_new correctly identifies all instances of a new object type if its corresponding prefix didn't exist in the pre_snapshot.

@db0.memo(prefix="new/prefix")
class ValueWrapper:
    def __init__(self, value):
        self.value = value
 
# Take a snapshot before the new type is used
snap_1 = db0.snapshot()
db0.commit()
 
# Create an instance of the new type
value_1 = ValueWrapper("some value")
db0.commit()
snap_2 = db0.snapshot()
 
# select_new finds the new object because its prefix "new/prefix"
# did not exist in snap_1.
results = db0.select_new(db0.find(ValueWrapper), snap_1, snap_2)
 
assert len(results) == 1