graphiti-api / spraypaint.js

Graphiti Client / Javascript ORM / JSONAPI
MIT License
107 stars 69 forks source link

Finder methods return "Persisted" type of a record #91

Closed wadetandy closed 3 years ago

wadetandy commented 3 years ago

This adds a new PersistedSpraypaintRecord type which extends any record type, definining the id as string instead of the initial string | undefined. This will allow any record retrieved from finders to be correctly typed with a defined id. This should avoid the need for a bunch of boilerplate type guards when dealing with records which are always coming from from finders. You can also use this to manually cast a record after you save it:

function createPerson(attrs): PersistedSpraypaintRecord<Person>
  let person = new Person(attrs)

  let success = await person.save()

  if (success) {
    person = person as PersistedSpraypaintRecord<Person>
  } else {
    // handle error
  }

  return person
}
richmolj commented 3 years ago

LGTM 👍