diff --git a/CHANGELOG.md b/CHANGELOG.md index d3ff651..0f93731 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,12 @@ # Changelog +## Unreleased + +### Added + +- `FieldMapping.id` names the stable task identity property. It defaults to `id`; mappings saved without it keep using `id`. `mapTaskFromFrontmatter` reads the ID from the mapped property, `toUserField`, `lookupMappingKey` and `validateFieldMapping` include it, and `DEFAULT_ID_PROPERTY` is exported from `@tasknotes/model/mapping`. +- `buildTaskNotesMdbaseResources` uses the mapped ID property for the type's `implements[].fields.id`, schema property, `on_create` UUID, uniqueness rule and the collection's `id_field`. `resolveTaskNotesModelConfigFromMdbaseType` reads a type's `fields.id` into the mapping, and with a remapped ID a plain `id` property resolves as a custom field. + ## 0.3.0 - 2026-11-15 Promotes the package to stable semver, retaining the tasknotes-spec 0.3.0 diff --git a/src/defaults.ts b/src/defaults.ts index aaa2a0e..392e477 100644 --- a/src/defaults.ts +++ b/src/defaults.ts @@ -6,6 +6,7 @@ import type { } from "./types"; export const DEFAULT_FIELD_MAPPING: FieldMapping = { + id: "id", title: "title", status: "status", priority: "priority", diff --git a/src/mapping.ts b/src/mapping.ts index d76624a..151cfcb 100644 --- a/src/mapping.ts +++ b/src/mapping.ts @@ -24,10 +24,14 @@ export const VALID_DEPENDENCY_RELTYPES: TaskDependencyRelType[] = [ ]; export const DEFAULT_RECURRENCE_ORIGINAL_DATE_PROPERTY = "recurrence_original_date"; +export const DEFAULT_ID_PROPERTY = "id"; -function withResolvedMapping(mapping: FieldMapping): FieldMapping & { recurrenceOriginalDate: string } { +type ResolvedFieldMapping = FieldMapping & { id: string; recurrenceOriginalDate: string }; + +function withResolvedMapping(mapping: FieldMapping): ResolvedFieldMapping { return { ...mapping, + id: toUserField(mapping, "id"), recurrenceOriginalDate: toUserField(mapping, "recurrenceOriginalDate"), }; } @@ -45,6 +49,11 @@ export function normalizeRecurrenceOriginalDate(value: unknown): string | undefi } export function toUserField(mapping: FieldMapping, internalName: FieldMappingKey): string { + if (internalName === "id") { + return typeof mapping.id === "string" && mapping.id.trim().length > 0 + ? mapping.id + : DEFAULT_ID_PROPERTY; + } if (internalName === "recurrenceOriginalDate") { return typeof mapping.recurrenceOriginalDate === "string" ? mapping.recurrenceOriginalDate @@ -120,8 +129,9 @@ export function mapTaskFromFrontmatter( const mapped: Partial = { path: filePath, }; - if (typeof frontmatter.id === "string" && frontmatter.id.trim().length > 0) { - mapped.id = frontmatter.id; + const id = frontmatter[toUserField(mapping, "id")]; + if (typeof id === "string" && id.trim().length > 0) { + mapped.id = id; } if (frontmatter[mapping.title] !== undefined) { diff --git a/src/mdbase.ts b/src/mdbase.ts index d122133..ccdb265 100644 --- a/src/mdbase.ts +++ b/src/mdbase.ts @@ -1,6 +1,7 @@ import YAML from "yaml"; import tasknotesTypePack from "./generated/tasknotes-type-pack.json"; import { resolveModelConfig } from "./config"; +import { toUserField } from "./mapping"; import { TASKNOTES_TASK_BINDING_SCHEMA, TASKNOTES_TASK_COMPLETED_EVENT_SCHEMA, @@ -550,6 +551,7 @@ export function buildTaskNotesMdbaseResources( options.capabilities ?? DEFAULT_TASKNOTES_MDBASE_CAPABILITIES ); const legacyCompatibility = options.legacyCompatibility === true; + const idField = toUserField(modelConfig.fieldMapping, "id"); const statusValues = uniqueStrings(modelConfig.statuses.map((status) => status.value)); const priorityValues = uniqueStrings(modelConfig.priorities.map((priority) => priority.value)); const completedValues = uniqueStrings( @@ -576,7 +578,7 @@ export function buildTaskNotesMdbaseResources( record_extensions: ["md"], validation: options.collection?.validation ?? "warn", explicit_type_keys: ["type", "types"], - id_field: "id", + id_field: idField, ...(excludes.length > 0 ? { exclude: excludes } : {}), }, }; @@ -640,7 +642,7 @@ export function buildTaskNotesMdbaseResources( }; if (options.stableId !== false) { - addField("id", "id", stringSchema({ minLength: 1 }, legacyCompatibility), { + addField("id", idField, stringSchema({ minLength: 1 }, legacyCompatibility), { createValue: { uuid: true }, }); } @@ -812,7 +814,7 @@ export function buildTaskNotesMdbaseResources( omittedCollectionPaths.add(titleField); } if (options.stableId !== false) { - collection.unique = [{ field: "id", scope: "type" }]; + collection.unique = [{ field: idField, scope: "type" }]; } const filenameFormat = @@ -1529,7 +1531,11 @@ function resolveUserFields( fieldMapping: FieldMapping, fallback: UserMappedField[] ): UserMappedField[] { - const reserved = new Set([...Object.values(fieldMapping), "id", "tags"]); + const reserved = new Set([ + ...Object.values(fieldMapping), + toUserField(fieldMapping, "id"), + "tags", + ]); const resolved: UserMappedField[] = []; for (const [key, schema] of Object.entries(schemaProperties)) { diff --git a/src/types.ts b/src/types.ts index 572a9bc..0b26ca6 100644 --- a/src/types.ts +++ b/src/types.ts @@ -109,6 +109,11 @@ export type TaskUpdateInput = Partial & { }; export interface FieldMapping { + /** + * The stable task identity property. Optional for mappings saved before + * this field existed; those use `id`. + */ + id?: string; title: string; status: string; priority: string; diff --git a/test/id-mapping.test.mjs b/test/id-mapping.test.mjs new file mode 100644 index 0000000..2fbeb7d --- /dev/null +++ b/test/id-mapping.test.mjs @@ -0,0 +1,64 @@ +import assert from "node:assert/strict"; +import test from "node:test"; +import { DEFAULT_FIELD_MAPPING } from "../dist/esm/defaults.js"; +import { + lookupMappingKey, + mapTaskFromFrontmatter, + toUserField, + validateFieldMapping, +} from "../dist/esm/mapping.js"; +import { + buildTaskNotesMdbaseResources, + resolveTaskNotesModelConfigFromMdbaseType, +} from "../dist/esm/mdbase.js"; + +const mapping = { ...DEFAULT_FIELD_MAPPING, id: "tasknote_id" }; + +test("the stable ID is read from its mapped property", () => { + assert.equal(DEFAULT_FIELD_MAPPING.id, "id"); + const task = mapTaskFromFrontmatter( + mapping, + { tasknote_id: "abc", id: "someone-elses", title: "Task" }, + "tasks/Task.md" + ); + assert.equal(task.id, "abc"); + assert.equal(lookupMappingKey(mapping, "tasknote_id"), "id"); + assert.equal(lookupMappingKey(mapping, "id"), null); + assert.equal(mapTaskFromFrontmatter(mapping, { id: "plain" }, "tasks/T.md").id, undefined); + assert.equal(validateFieldMapping({ ...mapping, title: "tasknote_id" }).valid, false); +}); + +test("mappings saved before the ID field existed keep using id", () => { + const legacy = { ...DEFAULT_FIELD_MAPPING }; + delete legacy.id; + assert.equal(toUserField(legacy, "id"), "id"); + assert.equal(mapTaskFromFrontmatter(legacy, { id: "abc" }, "tasks/T.md").id, "abc"); + assert.equal(validateFieldMapping(legacy).valid, true); +}); + +test("a remapped ID is generated into the mdbase type and read back from it", () => { + const resources = buildTaskNotesMdbaseResources({ modelConfig: { fieldMapping: mapping } }); + const { type, config } = resources; + const taskNotes = type.implements.find((entry) => entry.contract === "tasknotes.task"); + + assert.equal(taskNotes.fields.id, "tasknote_id"); + assert.ok(type.schema.value.properties.tasknote_id); + assert.equal(type.schema.value.properties.id, undefined); + assert.deepEqual(type.lifecycle.on_create.set.tasknote_id, { uuid: true }); + assert.equal(type.lifecycle.on_create.set.id, undefined); + assert.deepEqual(type.collection.unique, [{ field: "tasknote_id", scope: "type" }]); + assert.equal(config.settings.id_field, "tasknote_id"); + + const resolved = resolveTaskNotesModelConfigFromMdbaseType(type); + assert.equal(resolved.fieldMapping.id, "tasknote_id"); + assert.equal(resolveTaskNotesModelConfigFromMdbaseType( + buildTaskNotesMdbaseResources().type + ).fieldMapping.id, "id"); +}); + +test("with a remapped ID, a plain id property is the vault's own field", () => { + const { type } = buildTaskNotesMdbaseResources({ modelConfig: { fieldMapping: mapping } }); + type.schema.value.properties.id = { type: "string" }; + const resolved = resolveTaskNotesModelConfigFromMdbaseType(type); + assert.deepEqual(resolved.userFields.map((field) => field.key), ["id"]); +});