Skip to content

feat: _createdAt and _lastUpdatedAt #2426

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jun 5, 2025
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/twenty-rivers-behave.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"jazz-tools": patch
"cojson": patch
---

feat: `_createdAt` and `_lastUpdatedAt` getters for CoMaps
6 changes: 6 additions & 0 deletions packages/cojson/src/coValues/coMap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ export class RawCoMapView<
/** @internal */
latestTxMadeAt: number;
/** @internal */
earliestTxMadeAt: number;
/** @internal */
ops: {
[Key in keyof Shape & string]?: MapOp<Key, Shape[Key]>[];
};
Expand All @@ -75,6 +77,7 @@ export class RawCoMapView<
this.id = core.id as CoID<this>;
this.core = core;
this.latestTxMadeAt = 0;
this.earliestTxMadeAt = Number.MAX_SAFE_INTEGER;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can read the creation date from the header (core.verified.header.createdAt)

This way we can get the value even if the CoMap is empty, and fallback to the max integer only of the comap is empty and the createdAt in the header is not set (should never happen, because the CoValues that have no createdAt in the header have always some content)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks so much! So, you're saying we can assume createdAt will always be in the header, but if it isn't we can return Number.MAX_SAFE_INTEGER?

this.ignorePrivateTransactions =
options?.ignorePrivateTransactions ?? false;
this.ops = {};
Expand Down Expand Up @@ -121,6 +124,9 @@ export class RawCoMapView<
if (madeAt > this.latestTxMadeAt) {
this.latestTxMadeAt = madeAt;
}
if (madeAt < this.earliestTxMadeAt) {
this.earliestTxMadeAt = madeAt;
}

const entries = ops[change.key];
if (!entries) {
Expand Down
10 changes: 10 additions & 0 deletions packages/jazz-tools/src/coValues/coMap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,16 @@ export class CoMap extends CoValueBase implements CoValue {
} & { [ItemsSym]?: Schema };
}

/** @internal */
get _createdAt() {
return this._raw.earliestTxMadeAt;
}

/** @internal */
get _lastUpdatedAt() {
return this._raw.latestTxMadeAt;
}

/**
* If property `prop` is a `coField.ref(...)`, you can use `coMaps._refs.prop` to access
* the `Ref` instead of the potentially loaded/null value.
Expand Down
24 changes: 24 additions & 0 deletions packages/jazz-tools/src/tests/coMap.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1569,4 +1569,28 @@ describe("CoMap migration", () => {
expect(loaded?.friend?.name).toEqual("Charlie");
expect(loaded?.friend?.version).toEqual(2);
});
describe("Time", () => {
test("created time and last updated time", async () => {
const Person = co.map({
name: z.string(),
});

let currentTimestampInSeconds = Math.floor(Date.now() / 1000);
const person = Person.create({ name: "John" });

const createdAt = person._createdAt;
const createdAtInSeconds = Math.floor(createdAt / 1000);
expect(createdAtInSeconds).toEqual(currentTimestampInSeconds);
expect(person._lastUpdatedAt).toEqual(createdAt);

await new Promise((r) => setTimeout(r, 1000));
currentTimestampInSeconds = Math.floor(Date.now() / 1000);
person.name = "Jane";

const lastUpdatedAtInSeconds = Math.floor(person._lastUpdatedAt / 1000);
expect(lastUpdatedAtInSeconds).toEqual(currentTimestampInSeconds);
expect(person._createdAt).toEqual(createdAt);
expect(person._lastUpdatedAt).not.toEqual(createdAt);
});
});
});