Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
12 changes: 10 additions & 2 deletions apps/obsidian/src/utils/__tests__/conceptConversion.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,15 +117,23 @@ describe("discourseNodeSchemaToLocalConcept format", () => {
).toMatchObject({ literal_content: { format: "CLM - {content}" } });
});

it("keeps the other metadata in source data without the format", () => {
it("publishes the color at the top level of literal content", () => {
expect(
discourseNodeSchemaToLocalConcept({
accountLocalId: "account-1",
node: NODE_TYPE,
}),
).toMatchObject({ literal_content: { color: "#7DA13E" } });
});

it("keeps the other metadata in source data without the format or the color", () => {
expect(
discourseNodeSchemaToLocalConcept({
accountLocalId: "account-1",
node: NODE_TYPE,
}),
).toHaveProperty("literal_content.source_data", {
shortcut: "C",
color: "#7DA13E",
tag: "clm-candidate",
keyImage: true,
folderPath: "Claims",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -613,8 +613,8 @@ describe("what Obsidian sync sends to upsert_concepts", () => {
format: "CLM - {content}",
template: "Claim template",
template_content: "## Evidence\n",
color: "#7DA13E",
source_data: {
color: "#7DA13E",
tag: "clm",
shortcut: "C",
keyImage: true,
Expand Down Expand Up @@ -671,7 +671,8 @@ describe("what Obsidian sync sends to upsert_concepts", () => {
roles: ["source", "destination"],
label: "cites",
complement: "is cited by",
source_data: { color: "blue" },
color: "blue",
source_data: {},
},
});
});
Expand All @@ -689,7 +690,8 @@ describe("what Obsidian sync sends to upsert_concepts", () => {
label: "opposes",
complement: "is opposed by",
importedFromRid: "orn:obsidian.schema:other-vault/opposes-type",
source_data: { color: "red", authorId: 9 },
color: "red",
source_data: { authorId: 9 },
},
});
});
Expand Down
6 changes: 4 additions & 2 deletions apps/obsidian/src/utils/conceptConversion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,15 @@ export const discourseNodeSchemaToLocalConcept = ({
id,
name,
format,
color,
created,
modified,
importedFromRid,
...otherData
} = node;
const concept = crossAppNodeSchemaToDbConcept(
nodeTypeToCrossApp({
nodeType: { id, name, format, template, created, modified },
nodeType: { id, name, format, color, template, created, modified },
templateContent,
accountLocalId,
}),
Expand Down Expand Up @@ -77,6 +78,7 @@ export const discourseRelationTypeToLocalConcept = ({
id,
label,
complement,
color,
created,
modified,
importedFromRid,
Expand All @@ -86,7 +88,7 @@ export const discourseRelationTypeToLocalConcept = ({
} = relationType;
const concept = crossAppRelationTypeSchemaToDbConcept(
relationTypeToCrossApp({
relationType: { id, label, complement, created, modified },
relationType: { id, label, complement, color, created, modified },
accountLocalId,
}),
);
Expand Down
6 changes: 4 additions & 2 deletions apps/obsidian/src/utils/obsidianToCrossAppConverters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,15 @@ export const nodeTypeToCrossApp = ({
}: {
nodeType: Pick<
DiscourseNode,
"id" | "name" | "format" | "template" | "created" | "modified"
"id" | "name" | "format" | "color" | "template" | "created" | "modified"
>;
templateContent?: string;
accountLocalId: string;
}): CrossAppNodeSchema => ({
localId: nodeType.id,
label: nodeType.name,
format: nodeType.format,
color: nodeType.color,
templateTitle: nodeType.template || undefined,
template: templateContent || undefined,
authorId: accountLocalId,
Expand All @@ -46,13 +47,14 @@ export const relationTypeToCrossApp = ({
}: {
relationType: Pick<
DiscourseRelationType,
"id" | "label" | "complement" | "created" | "modified"
"id" | "label" | "complement" | "color" | "created" | "modified"
>;
accountLocalId: string;
}): CrossAppRelationTypeSchema => ({
localId: relationType.id,
label: relationType.label,
complement: relationType.complement,
color: relationType.color,
authorId: accountLocalId,
createdAt: new Date(relationType.created),
modifiedAt: new Date(relationType.modified),
Expand Down
4 changes: 3 additions & 1 deletion packages/database/doc/concept_design.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ CrossAppRelation:

Note on metadata: It is read from the database, but currently not written. We will phase it out.

When some specific keys of `literal_content` are mapped to a CrossApp field, those key-value pairs are not included again in the `metadata`. Thus `metadata` is a grab-bag for residual data. (Eg color for now.)
When some specific keys of `literal_content` are mapped to a CrossApp field, those key-value pairs are not included again in the `metadata`. Thus `metadata` is a grab-bag for residual data. (Eg `source_data` for now.)

Residual (not otherwise accounted for) keys in Obsidian frontmatter are mapped to `literal_content->source_data`. (Not through CrossApp.)

Expand All @@ -119,6 +119,7 @@ Residual (not otherwise accounted for) keys in Obsidian frontmatter are mapped t
| `template` | `literal_content->template_content` | |
| `templateTitle` | `literal_content->template` | |
| `format` | `literal_content->format` | |
| `color` | `literal_content->color` | hex |
| - | `is_schema` | true |
| - | `schema_id` | null |
| - | `arity` | 0 |
Expand Down Expand Up @@ -169,6 +170,7 @@ Query filter: `.eq("is_relation", false).eq("is_schema", false)`
| - | `reference_content` | {} |
| `label` | `name`, `literal_content->label` | |
| `complement` | `literal_content->complement` | |
| `color` | `literal_content->color` | tldraw color name |
| - | `is_relation` | true |

Query filter: `.eq("is_relation", true).eq("is_schema", true).is("reference_content->source", "null")`
Expand Down
4 changes: 3 additions & 1 deletion packages/database/src/crossAppContracts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,16 @@ export type CrossAppNodeSchema = CrossAppSchemaBase & {
template?: string;
templateTitle?: string;
format?: string;
color?: string;
slotDefinitions?: Record<string, LocalId | undefined>;
};

// A relation type schema
export type CrossAppRelationTypeSchema = CrossAppSchemaBase & {
label: string;
complement: string;
// should we add colour? format?
color?: string;
// should we add format?
};

// A relation triple schema
Expand Down
42 changes: 42 additions & 0 deletions packages/database/src/lib/__tests__/crossAppConverters.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type { CrossAppNode, CrossAppNodeSchema } from "../../crossAppContracts";
import {
crossAppNodeSchemaToDbConcept,
crossAppNodeToDbConcept,
crossAppRelationTypeSchemaToDbConcept,
} from "../crossAppConverters";

const baseSchema: CrossAppNodeSchema = {
Expand Down Expand Up @@ -50,6 +51,17 @@ describe("crossAppNodeSchemaToDbConcept", () => {
});
});

it("maps color to literal_content.color", () => {
const concept = crossAppNodeSchemaToDbConcept({
...baseSchema,
color: "#7DA13E",
});
expect(concept.literal_content).toEqual({
label: "Some concept",
color: "#7DA13E",
});
});

it("writes the label into literal_content as well as the name", () => {
const concept = crossAppNodeSchemaToDbConcept(baseSchema);
expect(concept.name).toBe("Some concept");
Expand Down Expand Up @@ -83,6 +95,36 @@ describe("crossAppNodeSchemaToDbConcept", () => {
});
});

describe("crossAppRelationTypeSchemaToDbConcept", () => {
const relationType = {
...baseSchema,
label: "supports",
complement: "is supported by",
};

it("maps color to literal_content.color", () => {
expect(
crossAppRelationTypeSchemaToDbConcept({ ...relationType, color: "green" })
.literal_content,
).toStrictEqual({
roles: ["source", "destination"],
label: "supports",
complement: "is supported by",
color: "green",
});
});

it("leaves color out of literal_content when the relation type has none", () => {
expect(
crossAppRelationTypeSchemaToDbConcept(relationType).literal_content,
).toStrictEqual({
roles: ["source", "destination"],
label: "supports",
complement: "is supported by",
});
});
});

describe("crossAppNodeToDbConcept", () => {
it("stores node slots as local reference content", () => {
expect(
Expand Down
29 changes: 29 additions & 0 deletions packages/database/src/lib/__tests__/dbToCrossAppConverters.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,20 @@ describe("dbNodeSchemaToCrossApp", () => {
expect(result.metadata).toEqual({ extra: "kept" });
});

it("splits color out of metadata onto the result", () => {
const schema = baseConcept({
literal_content: { color: "#7DA13E", extra: "kept" },
});
const result = dbNodeSchemaToCrossApp({
schema,
spaceMap,
accountMap,
schemaMap: {},
});
expect(result.color).toBe("#7DA13E");
expect(result.metadata).toEqual({ extra: "kept" });
});

it("keeps format undefined when literal_content has no format", () => {
const schema = baseConcept({
literal_content: { extra: "kept" },
Expand Down Expand Up @@ -198,6 +212,21 @@ describe("dbRelationTypeSchemaToCrossApp", () => {
authorId: "account-local-1",
});
});

it("splits color out of metadata onto the result", () => {
const schema = baseConcept({
literal_content: {
roles: ["source", "destination"],
label: "supports",
complement: "is supported by",
color: "green",
extra: "kept",
},
});
const result = dbRelationTypeSchemaToCrossApp(schema, spaceMap, accountMap);
expect(result.color).toBe("green");
expect(result.metadata).toEqual({ extra: "kept" });
});
});

describe("dbRelationTripleSchemaToCrossApp", () => {
Expand Down
6 changes: 4 additions & 2 deletions packages/database/src/lib/crossAppConverters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ export const crossAppNodeSchemaToDbConcept = (
template: node.templateTitle,
template_content: node.template,
format: node.format,
color: node.color,
roles: slots.length > 0 ? slots : undefined,
});
const referenceContent = slots.length ? node.slotDefinitions : undefined;
Expand Down Expand Up @@ -141,11 +142,12 @@ export const crossAppRelationTypeSchemaToDbConcept = (
name: node.label,
author_local_id: node.authorId,
is_schema: true,
literal_content: {
literal_content: filterUndefined({
roles: ["source", "destination"],
label: node.label,
complement: node.complement,
},
color: node.color,
}),
created: node.createdAt?.toISOString(),
last_modified: node.modifiedAt?.toISOString(),
});
Expand Down
6 changes: 4 additions & 2 deletions packages/database/src/lib/dbToCrossAppConverters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ export const dbNodeSchemaToCrossApp = ({
string,
number
>;
const { template, template_content, roles, format, ...other } =
const { template, template_content, roles, format, color, ...other } =
schema.literal_content as Record<string, Json>;
const authorId = accountMap[schema.author_id || 0];
if (authorId === undefined) throw new Error("Missing author");
Expand All @@ -195,6 +195,7 @@ export const dbNodeSchemaToCrossApp = ({
template: template_content as string | undefined,
templateTitle: template as string | undefined,
format: format as string | undefined,
color: color as string | undefined,
authorId,
slotDefinitions,
};
Expand Down Expand Up @@ -409,7 +410,7 @@ export const dbRelationTypeSchemaToCrossApp = (
accountMap: Record<number, string>,
): CrossAppRelationTypeSchema => {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const { roles, label, complement, ...other } =
const { roles, label, complement, color, ...other } =
schema.literal_content as Record<string, Json>;
const authorId = accountMap[schema.author_id || 0];
if (authorId === undefined) throw new Error("Missing author");
Expand All @@ -428,6 +429,7 @@ export const dbRelationTypeSchemaToCrossApp = (
metadata: other,
label: label as string,
complement: complement as string,
color: color as string | undefined,
authorId,
};
};
Expand Down
Loading