Skip to content

Commit

Permalink
populate checked data types when schema is passed in
Browse files Browse the repository at this point in the history
  • Loading branch information
dwwoelfel committed Nov 21, 2024
1 parent 6bed51a commit 0fd0706
Show file tree
Hide file tree
Showing 2 changed files with 148 additions and 0 deletions.
141 changes: 141 additions & 0 deletions client/packages/core/__tests__/src/instaml.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -754,6 +754,7 @@ test("Schema: uses info in `attrs` and `links`", () => {
cardinality: "one",
"unique?": true,
"index?": true,
"checked-data-type": "string",
isUnsynced: true,
},
],
Expand Down Expand Up @@ -893,6 +894,7 @@ test("Schema: lookup creates unique attrs for custom lookups", () => {
"index?": true,
isUnsynced: true,
"unique?": true,
"checked-data-type": "string",
"value-type": "blob",
},
],
Expand Down Expand Up @@ -961,6 +963,7 @@ test("Schema: lookup creates unique attrs for lookups in link values", () => {
cardinality: "one",
"unique?": true,
"index?": true,
"checked-data-type": "string",
isUnsynced: true,
},
],
Expand Down Expand Up @@ -1043,6 +1046,7 @@ test("Schema: lookup creates unique attrs for lookups in link values with arrays
cardinality: "one",
"unique?": true,
"index?": true,
"checked-data-type": "string",
isUnsynced: true,
},
],
Expand Down Expand Up @@ -1233,3 +1237,140 @@ test("Schema: lookup creates unique ref attrs for ref lookup in link value", ()
expect(result).toContainEqual(item);
}
});

test("Schema: populates checked-data-type", () => {
const schema = i.graph(
{
comments: i.entity({
s: i.string(),
n: i.number(),
d: i.date(),
b: i.boolean(),
a: i.any(),
j: i.json(),
}),
},
{},
);

const commentId = uuid();
const ops = instatx.tx.comments[commentId].update({
s: "str",
n: "num",
d: "date",
b: "bool",
a: "any",
j: "json",
});

const result = instaml.transform(
{
attrs: zenecaAttrs,
schema: schema,
},
ops,
);

const expected = [
[
"add-attr",
{
id: expect.any(String),
"forward-identity": [expect.any(String), "comments", "s"],
"value-type": "blob",
cardinality: "one",
"unique?": false,
"index?": false,
isUnsynced: true,
"checked-data-type": "string",
},
],
[
"add-attr",
{
id: expect.any(String),
"forward-identity": [expect.any(String), "comments", "n"],
"value-type": "blob",
cardinality: "one",
"unique?": false,
"index?": false,
isUnsynced: true,
"checked-data-type": "number",
},
],
[
"add-attr",
{
id: expect.any(String),
"forward-identity": [expect.any(String), "comments", "d"],
"value-type": "blob",
cardinality: "one",
"unique?": false,
"index?": false,
isUnsynced: true,
"checked-data-type": "date",
},
],
[
"add-attr",
{
id: expect.any(String),
"forward-identity": [expect.any(String), "comments", "b"],
"value-type": "blob",
cardinality: "one",
"unique?": false,
"index?": false,
isUnsynced: true,
"checked-data-type": "boolean",
},
],
[
"add-attr",
{
id: expect.any(String),
"forward-identity": [expect.any(String), "comments", "a"],
"value-type": "blob",
cardinality: "one",
"unique?": false,
"index?": false,
isUnsynced: true,
},
],
[
"add-attr",
{
id: expect.any(String),
"forward-identity": [expect.any(String), "comments", "j"],
"value-type": "blob",
cardinality: "one",
"unique?": false,
"index?": false,
isUnsynced: true,
},
],
[
"add-attr",
{
id: expect.any(String),
"forward-identity": [expect.any(String), "comments", "id"],
"value-type": "blob",
cardinality: "one",
"unique?": true,
"index?": false,
isUnsynced: true,
},
],
["add-triple", commentId, expect.any(String), commentId],
["add-triple", commentId, expect.any(String), "str"],
["add-triple", commentId, expect.any(String), "num"],
["add-triple", commentId, expect.any(String), "date"],
["add-triple", commentId, expect.any(String), "bool"],
["add-triple", commentId, expect.any(String), "any"],
["add-triple", commentId, expect.any(String), "json"],
];

expect(result).toHaveLength(expected.length);
for (const item of expected) {
expect(result).toContainEqual(item);
}
});
7 changes: 7 additions & 0 deletions client/packages/core/src/instaml.js
Original file line number Diff line number Diff line change
Expand Up @@ -238,16 +238,23 @@ function toTxSteps(attrs, step) {
// ---------
// transform

const CHECKED_DATA_TYPES = ["string", "date", "boolean", "number"];

function objectPropsFromSchema(schema, etype, label) {
const attr = schema.entities[etype]?.attrs?.[label];
if (label === "id") return null;
if (!attr) {
throw new Error(`${etype}.${label} does not exist in your schema`);
}
const { unique, indexed } = attr?.config;
const checkedDataType = CHECKED_DATA_TYPES.includes(attr?.valueType)
? attr.valueType
: undefined;

return {
"index?": indexed,
"unique?": unique,
"checked-data-type": checkedDataType,
};
}

Expand Down

0 comments on commit 0fd0706

Please sign in to comment.