Document History
Retrieve the history of changes for a document.
Definition
await zkdb.db(databaseName: string)
.collection(collectionName: string)
.findOne(filter: Record<string, any>)
.history(pagination?: TPagination): Promise<Result<TDocumentHistoryFindResponse>>;
Parameters
pagination(TPagination, optional):{ limit: number; offset: number }.
Returns
Promise<Result<TDocumentHistoryFindResponse>>: Returns a list of all historical snapshots of the document. Each entry represents the full state of the document at a specific point in time.
type TDocumentHistoryFindResponse = {
data: Array<{
docId: string;
createdAt: Date;
document: Record<string, any>;
}>;
total: number;
offset: number;
limit: number;
};
Example
import { ZkDatabase } from 'zkdb';
const zkdb = new ZkDatabase({
apiKey: 'zkdb_536aac02a1b7.c001b6b8f...da3aa4185d8d2ad07f0ae94aT',
// This URL is for test environment
url: "https://serverless.zkdatabase.org/graphql",
});
const dbTest = zkdb.db('zkdb_test');
const collection = dbTest.collection('Shirt');
const doc = (await collection.findOne({ name: 'Shirt 1' })).unwrap();
if (doc) {
const history = (await doc.history({ limit: 5, offset: 0 })).unwrap();
console.log('Document history:', history);
}
Result:
Document history: {
"total": 2,
"offset": 0,
"limit": 5,
"data": [
{
"docId": "6t7...",
"createdAt": "2025-01-02T00:00:00.000Z",
"document": { "name": "Shirt 1", "price": 25 }
},
{
"docId": "6t7...",
"createdAt": "2025-01-01T00:00:00.000Z",
"document": { "name": "Shirt 1", "price": 15 }
}
]
}