Added encodeEntity to encode an entity without a Lens - #181
Conversation
There was a problem hiding this comment.
Copilot review overview
🔵 Needs a closer look
Add tests covering both root exports and calling encode without options.
Review effort: Lite
Findings: None
What changed in this PR
Exports reusable schema expansion and RDF encoding APIs from the root module, with optional encoder options.
Changes:
- Exports
encodeandexpandSchema. - Adds JSDoc and explicit return types.
- Allows
encodeoptions to be omitted.
| File | Summary |
|---|---|
mod.ts |
Adds root-level exports for encode and expandSchema; public API coverage is still needed. |
library/schema/utils.ts |
Documents and types expandSchema. |
library/encoder.ts |
Documents encode and defaults options; the two-argument public contract needs coverage. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
|
Hi, thanks for the PR! The idea is good, I just need to think about what would be the best interface for this. I don't think that the |
|
Let me share the idea behind it, maybe it helps with the interface. Our backend takes entity writes as JSON-LD. At the moment every consumer of our SDK does the JSON-LD serialization manually, and that's where we saw an issue, someone forgetting to produce the typed literals for dates for example and sending those over as plain strings. LDkit's encoder is what we would use in order to stop doing it manually. The encoder walks the entity next to the schema and produces quads, with every literal carrying the datatype the schema declares: { $id: "ex:Campaign_1", label: "Summer stays", startTime: new Date("2026-09-22T23:00:00Z") }<ex:Campaign_1> rdf:type <ex:Campaign> .
<ex:Campaign_1> rdfs:label "Summer stays" .
<ex:Campaign_1> ex:startTime "2026-09-22T23:00:00Z"^^xsd:dateTime .As far as my understanding goes, the only public thing that runs the encoder is INSERT DATA {
<ex:Campaign_1> <rdf:type> <ex:Campaign> .
<ex:Campaign_1> <rdfs:label> "Summer stays" .
<ex:Campaign_1> <ex:startTime> "2026-09-22T23:00:00Z"^^<xsd:dateTime> .
}We don't write to a store at that point, so today we run {
"@id": "ex:Campaign_1",
"@type": "ex:Campaign",
"rdfs:label": "Summer stays",
"ex:startTime": { "@value": "2026-09-22T23:00:00Z", "@type": "xsd:dateTime" }
}So we go through Turtle and back only to end up with the quads the encoder had in the first place, because there is no way to reach it without a What we need is one call that takes a schema and an entity and returns the quads |
|
Thanks for the background. I understand the use case, it will be a good addition to the library. I agree that constructing the whole Let's go with the wrapper as you suggested - typed The wrapper would expand the schema and then call the internal I might decide to expose more settings for the encoder in the future to facilitate more advanced use cases, but in that case I will come up with a standelone constructor, i.e. |
encode and expandSchema from the root module|
Done, One thing to flag. The wrapper resolves options the way
|
|
Thanks for the updated PR! The change is merged and released in https://www.npmjs.com/package/ldkit/v/2.9.0 |
Adds
encodeEntity(schema, entity), a typed wrapper inencoder.tsthat expands the schema and runs the internal encoder, exported from the root module. It returns the quadsLens.insertwrites, with every literal carrying the datatype the schema declares, without needing a data source.Options resolve the way
createLensresolves them, so a globallanguageapplies.