Skip to content

Commit c79c0be

Browse files
For DateTime and Timestamp, support for input via Date objects in JavaScript. (#132)
Fix issue: 1. #122 Co-authored-by: Nick Josipovic <[email protected]>
1 parent 47ac8ba commit c79c0be

File tree

3 files changed

+36
-2
lines changed

3 files changed

+36
-2
lines changed

lib/change-log.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ const _formatCompositionContext = async function (changes, reqData) {
187187
const childNodeChanges = []
188188

189189
for (const change of changes) {
190-
if (typeof change.valueChangedTo === "object") {
190+
if (typeof change.valueChangedTo === "object" && change.valueChangedTo instanceof Date !== true) {
191191
if (!Array.isArray(change.valueChangedTo)) {
192192
change.valueChangedTo = [change.valueChangedTo]
193193
}

tests/bookshop/db/schema.cds

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ namespace sap.capire.bookshop;
1818
@title: '{i18n>RootEntity.objectTitle}'
1919
entity RootEntity @(cds.autoexpose) : managed, cuid {
2020
name : String;
21+
dateTime : DateTime;
22+
timestamp : Timestamp;
2123
lifecycleStatus : LifecycleStatusCode;
2224
child : Composition of many Level1Entity
2325
on child.parent = $self;

tests/integration/service-api.test.js

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,39 @@ describe("change log integration test", () => {
125125

126126
delete cds.services.AdminService.entities.Order.elements.netAmount["@changelog"];
127127
delete cds.services.AdminService.entities.Order.elements.isUsed["@changelog"];
128-
});
128+
});
129+
130+
it("1.9 For DateTime and Timestamp, support for input via Date objects.", async () => {
131+
cds.env.requires["change-tracking"].preserveDeletes = true;
132+
cds.services.AdminService.entities.RootEntity.elements.dateTime["@changelog"] = true;
133+
cds.services.AdminService.entities.RootEntity.elements.timestamp["@changelog"] = true;
134+
const rootEntityData = [
135+
{
136+
ID: "64625905-c234-4d0d-9bc1-283ee8940717",
137+
dateTime: new Date("2024-10-16T08:53:48Z"),
138+
timestamp: new Date("2024-10-23T08:53:54.000Z")
139+
}
140+
]
141+
await INSERT.into(adminService.entities.RootEntity).entries(rootEntityData);
142+
let changes = await adminService.run(SELECT.from(ChangeView).where({
143+
entity: "sap.capire.bookshop.RootEntity",
144+
attribute: "dateTime",
145+
}));
146+
expect(changes.length).to.equal(1);
147+
let change = changes[0];
148+
expect(change.entityKey).to.equal("64625905-c234-4d0d-9bc1-283ee8940717");
149+
expect(change.attribute).to.equal("dateTime");
150+
expect(change.modification).to.equal("Create");
151+
expect(change.valueChangedFrom).to.equal("");
152+
/**
153+
* REVISIT: Currently, when using '@cap-js/sqlite' or '@cap-js/hana' and inputting values of type Date in javascript,
154+
* there is an issue with inconsistent formats before and after, which requires a fix from cds-dbs (Issue-873).
155+
*/
156+
expect(change.valueChangedTo).to.equal(`${new Date("2024-10-16T08:53:48Z")}`);
157+
delete cds.services.AdminService.entities.RootEntity.elements.dateTime["@changelog"];
158+
delete cds.services.AdminService.entities.RootEntity.elements.timestamp["@changelog"];
159+
cds.env.requires["change-tracking"].preserveDeletes = false;
160+
});
129161

130162
it("2.5 Root entity deep creation by service API - should log changes on root entity (ERP4SMEPREPWORKAPPPLAT-32 ERP4SMEPREPWORKAPPPLAT-613)", async () => {
131163
const bookStoreData = {

0 commit comments

Comments
 (0)