-
Notifications
You must be signed in to change notification settings - Fork 0
feat(modifiers): add AutoDateModifier with CreationTime and UpdateTime decorators #11
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| import { MakePropertyDecorator } from "@antelopejs/interface-core/decorators"; | ||
| import { attachModifier, Modifier } from "./common"; | ||
|
|
||
| type AutoDateType = "created" | "updated"; | ||
|
|
||
| type Options = { | ||
| /** Timestamp behavior: `created` is only set on insert, `updated` is refreshed on every update */ | ||
| type: AutoDateType; | ||
| }; | ||
|
|
||
| /** | ||
| * Auto-date modifier. Enables the use of {@link CreationTime} and | ||
| * {@link UpdateTime} on table fields. | ||
| * | ||
| * This is an event-only modifier: it does not transform stored values and | ||
| * does not require the Table class to incorporate a mixin. | ||
| */ | ||
| export class AutoDateModifier extends Modifier<object, Options> { | ||
| public insert(object: Record<string, unknown>, field: string) { | ||
| object[field] = new Date(); | ||
| } | ||
|
|
||
| public update(object: Record<string, unknown>, field: string) { | ||
| if (this.options.type === "updated") { | ||
| object[field] = new Date(); | ||
| } else { | ||
| delete object[field]; | ||
| } | ||
|
Upd4ting marked this conversation as resolved.
|
||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Mark a Database Table class field as an automatic creation timestamp. | ||
| * | ||
| * The field is set to the current date on insert and removed from update | ||
| * payloads so the original creation date is preserved. | ||
| */ | ||
| export const CreationTime = MakePropertyDecorator((target, propertyKey) => { | ||
| attachModifier(target.constructor, AutoDateModifier, propertyKey, { | ||
| type: "created", | ||
| }); | ||
| }); | ||
|
|
||
| /** | ||
| * Mark a Database Table class field as an automatic update timestamp. | ||
| * | ||
| * The field is set to the current date on insert and refreshed on every | ||
| * update. | ||
| */ | ||
| export const UpdateTime = MakePropertyDecorator((target, propertyKey) => { | ||
| attachModifier(target.constructor, AutoDateModifier, propertyKey, { | ||
| type: "updated", | ||
| }); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| export * from "./autodate"; | ||
| export * from "./common"; | ||
| export * from "./encryption"; | ||
| export * from "./hash"; | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,128 @@ | ||
| import { | ||
| AutoDateModifier, | ||
| CreationTime, | ||
| UpdateTime, | ||
| } from "@antelopejs/interface-database-decorators/modifiers/autodate"; | ||
| import { | ||
| fromPlainData, | ||
| toDatabase, | ||
| triggerEvent, | ||
| } from "@antelopejs/interface-database-decorators/modifiers/common"; | ||
| import { Field, Table } from "@antelopejs/interface-database-decorators/table"; | ||
| import { expect } from "chai"; | ||
|
|
||
| describe("Modifiers - autodate", () => { | ||
| it("sets creation date on insert", async () => SetCreationDateOnInsertTest()); | ||
| it("sets update date on insert", async () => SetUpdateDateOnInsertTest()); | ||
| it("refreshes update date on update", async () => | ||
| RefreshUpdateDateOnUpdateTest()); | ||
| it("preserves creation date on update", async () => | ||
| PreserveCreationDateOnUpdateTest()); | ||
| it("handles decorators through events", async () => | ||
| HandleDecoratorsThroughEventsTest()); | ||
| it("omits creation date from update database payloads", async () => | ||
| OmitCreationDateFromUpdatePayloadTest()); | ||
| }); | ||
|
|
||
| interface AutoDateOptions { | ||
| type: "created" | "updated"; | ||
| } | ||
|
|
||
| class TestableAutoDateModifier extends AutoDateModifier { | ||
| public setOptions(options: AutoDateOptions) { | ||
| this.options = options; | ||
| } | ||
| } | ||
|
|
||
| async function SetCreationDateOnInsertTest() { | ||
| const modifier = new TestableAutoDateModifier(); | ||
| modifier.setOptions({ type: "created" }); | ||
|
|
||
| const object: Record<string, unknown> = {}; | ||
| modifier.insert(object, "createdAt"); | ||
|
|
||
| expect(object.createdAt).to.be.an.instanceOf(Date); | ||
| } | ||
|
|
||
| async function SetUpdateDateOnInsertTest() { | ||
| const modifier = new TestableAutoDateModifier(); | ||
| modifier.setOptions({ type: "updated" }); | ||
|
|
||
| const object: Record<string, unknown> = {}; | ||
| modifier.insert(object, "updatedAt"); | ||
|
|
||
| expect(object.updatedAt).to.be.an.instanceOf(Date); | ||
| } | ||
|
|
||
| async function RefreshUpdateDateOnUpdateTest() { | ||
| const modifier = new TestableAutoDateModifier(); | ||
| modifier.setOptions({ type: "updated" }); | ||
|
|
||
| const previousDate = new Date(0); | ||
| const object: Record<string, unknown> = { updatedAt: previousDate }; | ||
| modifier.update(object, "updatedAt"); | ||
|
|
||
| expect(object.updatedAt).to.be.an.instanceOf(Date); | ||
| expect(object.updatedAt).to.not.equal(previousDate); | ||
| } | ||
|
|
||
| async function PreserveCreationDateOnUpdateTest() { | ||
| const modifier = new TestableAutoDateModifier(); | ||
| modifier.setOptions({ type: "created" }); | ||
|
|
||
| const object: Record<string, unknown> = { createdAt: new Date(0) }; | ||
| modifier.update(object, "createdAt"); | ||
|
|
||
| expect(object).to.not.have.property("createdAt"); | ||
| } | ||
|
|
||
| async function HandleDecoratorsThroughEventsTest() { | ||
| class TestTable extends Table { | ||
| @CreationTime() | ||
| @Field("date") | ||
| declare createdAt: Date; | ||
|
|
||
| @UpdateTime() | ||
| @Field("date") | ||
| declare updatedAt: Date; | ||
| } | ||
|
|
||
| const instance = new TestTable(); | ||
| triggerEvent(instance, "insert"); | ||
|
|
||
| expect(instance.createdAt).to.be.an.instanceOf(Date); | ||
| expect(instance.updatedAt).to.be.an.instanceOf(Date); | ||
|
|
||
| const insertedUpdateDate = instance.updatedAt; | ||
| triggerEvent(instance, "update"); | ||
|
|
||
| expect(instance.createdAt).to.equal(undefined); | ||
| expect(instance.updatedAt).to.be.an.instanceOf(Date); | ||
| expect(instance.updatedAt).to.not.equal(insertedUpdateDate); | ||
| } | ||
|
Upd4ting marked this conversation as resolved.
|
||
|
|
||
| async function OmitCreationDateFromUpdatePayloadTest() { | ||
| class TestTable extends Table { | ||
| @CreationTime() | ||
| @Field("date") | ||
| declare createdAt: Date; | ||
|
|
||
| @UpdateTime() | ||
| @Field("date") | ||
| declare updatedAt: Date; | ||
| } | ||
|
|
||
| const inserted = fromPlainData({}, TestTable); | ||
| triggerEvent(inserted, "insert"); | ||
| const insertPayload = toDatabase(inserted); | ||
|
|
||
| expect(insertPayload.createdAt).to.be.an.instanceOf(Date); | ||
| expect(insertPayload.updatedAt).to.be.an.instanceOf(Date); | ||
|
|
||
| const updated = fromPlainData({ createdAt: new Date(0) }, TestTable); | ||
| triggerEvent(updated, "update"); | ||
| const updatePayload = toDatabase(updated); | ||
|
|
||
| expect(updatePayload).to.not.have.property("createdAt"); | ||
| expect(updatePayload.updatedAt).to.be.an.instanceOf(Date); | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.