Skip to content

Towards a very developer friendly API - get properties by shortname, typed #665

@joepio

Description

@joepio

One of the long-term goals I had with Atomic Data was to be able to generate typed class models / interfaces in JS / TS from externally defined models. We can now generate Typescript interfaces from Classes: atomicdata-dev/atomic-data-browser#118... But this only works with plain JS objects, so not with JSON-AD serialized resources. Using the plain JSON object comes with the advantage of shorter keys, so devs can do this:

resource.description // "hi, I'm a description"

but at the cost of:

  • No way to find semantic definition of the Property
  • Not able to determine datatype (except for basic JSON types), which makes it impossible to render forms effectively.

So when users edit data, they will probably need the JSON-AD representation. And for these cases, I have this very verbose API:

resource.get("https://atomicdata.dev/properties/description") // "hi, I'm a description"

I can quite easily make something like this:

resource.get('description) // "hi, I'm a description"

But what I want is this:

resource.description // "hi, I'm a description"

where we still have all the methods and options that we have in a full JSON-AD resource, such as the option to .save().

And even better, fetch these definitions from the server using modules.

Tables + typescript

  • Server converts classes to TS definitions
  • Table-scoped queries can be passed a generic, which tells the runtime something about the type of the responses.
// A class that extends `Resource`
import Agent from "http://atomicdata.dev/classes/Agent?ts=true";

// Fetch the table collection (paginated)
const agents = store.getTable(Agent);

let agent = agents[0]

// Is a string
agent.name as string;

// Creates a Commit
agent.name = "newname";
// Or maybe make it explicit
agent.setName("newname");

Add generics to useResource and similar functions

When calling store.getResource in @tomic/lib or useResource in @tomic/react, we might be able to pass a generic that helps keep type safety.

let AgentProps = enum {
   name: "https://atomicdata.dev/properties/name",
}

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions