Client
A client is an SgClient. RestClient speaks to the REST API with a token, ProxyClient speaks to
an app’s own endpoints behind createProxyHandler, and MockClient answers from a generated site.
The mock is a subpath of its own, so an app that ships without it ships none of its fixtures. Every
example below runs against the mock, so the writes land nowhere.
import { createQueryCache } from 'sg-widgets-core';import { MockClient } from 'sg-widgets-core/mock';
const client = createQueryCache(new MockClient());The cache remembers reads for ttlMs and drops what a write makes stale: a row read of the type the
write touched, and every note thread. A delete or a revive drops every row read, since it changes
rows of types it does not name.
One row
Section titled “One row”read answers one row by id in the search row shape, with the fields asked for. A row that is
not there rejects with a 404, and so does a retired one: pass retired to read it, and the same
id then tells the two apart (get_entity_type_id).
const shot = await client.read('Shot', 862, { fields: ['code', 'sg_sequence'] });const gone = await client.read('Shot', 862, { retired: true }).catch(() => null);A note thread
Section titled “A note thread”threadContents answers a Note, its Attachments and its Replies as one list in time order. The
author sits under created_by on a Note and an Attachment and under user on a Reply, and the
row exposes it as author whichever key it came from. A Reply’s author carries an avatar the other
two do not.
const thread = await client.threadContents(11030, { Note: ['subject', 'sg_status_list'] });for (const row of thread) { console.log(row.type, row.createdAt, row.author?.name, row.content ?? '');}The second argument widens a row type by extra fields. It works on Note and Attachment; the Reply
entry is accepted and changes nothing, so extra Reply fields come from a search on replies
(get_entity_notes_id_thread_contents).
The event log
Section titled “The event log”eventLog answers what changed, newest first. It is never cached. The cut is made on the project,
the entity, the event type, the attribute and a date window; meta holds the old and new values
and takes no filter, so the client lifts them out of each row.
const { data } = await client.eventLog({ entity: { type: 'Shot', id: 862 }, attributeName: 'sg_status_list', page: { size: 5 },});for (const event of data) console.log(event.createdAt, event.oldValue, '->', event.newValue);eventLogFilters builds the same cut as a filter group, for a search of your own.
import { eventLogFilters } from 'sg-widgets-core';
const filters = eventLogFilters({ projectId: 70, eventType: ['Shotgun_Shot_Change', 'Shotgun_Version_Change'] });Ids at the head of the log are sparse and fill in later, so a cursor on the highest id seen drops
events: re-scan a window behind the head or drive the feed from createdAt and deduplicate on
id (025_event_log).
What a person follows
Section titled “What a person follows”following answers every row one person follows, as a type and an id each, in one unpaged body.
It takes a type to keep and a project to cut on. The person is a HumanUser: a script cannot ask
what it follows (get_entity_human_users_id_following).
const followed = await client.following(20, { entity: 'Shot', projectId: 70 });const rows = await client.search('Shot', { filters: { logical_operator: 'and', conditions: [['id', 'in', followed.map((ref) => ref.id)]] }, fields: ['code', 'sg_status_list'],});Writes
Section titled “Writes”create posts one row and answers it. project is the whole contract on a project-scoped type; the
field the schema flags mandatory is optional, and the server fills the identity field when it is
left out. An entity link is a {type, id} hash, never a bare id (012_create_version).
const note = await client.create('Note', { project: { type: 'Project', id: 70 }, subject: 'Key light reads flat', content: 'Lift the fill on the left side.', note_links: [{ type: 'Shot', id: 862 }],});const reply = await client.create('Reply', { entity: { type: 'Note', id: note.id }, content: 'On it.' });The answer echoes the server’s defaults, so read them off it: a fresh Note is unread and
published (entity_types/Note). A create takes created_at although the schema flags it
read-only; a later update refuses it (070_authored_timestamps).
Delete and revive
Section titled “Delete and revive”delete retires a row: reads stop finding it and a second delete is a 404. revive brings it back
with the values it had and answers false when the row was already live (048_one_record_beyond_crud).
A delete changes other rows and says nothing: deleting a Task nulls the Task on its Versions and
PublishedFiles and cuts its dependency chain, and a revive restores all of it
(089_task_delete_side_effects).
await client.delete('Task', 5120);const revived = await client.revive('Task', 5120);A batch
Section titled “A batch”batch applies creates, updates and deletes in one call. It is atomic: one failing request rejects
the call and nothing lands. The answer has one row per request, in request order, so pair them by
position. A request cannot point at a row the same batch creates; send one batch per level
(recipes/002).
const results = await client.batch([ { request_type: 'create', entity: 'Version', data: { project: { type: 'Project', id: 70 }, code: 'v001' } }, { request_type: 'update', entity: 'Shot', record_id: 862, data: { description: 'Rebuilt' } }, { request_type: 'delete', entity: 'Version', record_id: 7011 },]);A create inside a batch skips the project check a single create makes, and answers the id of a row
no read reaches (reports/001). Check the payload before sending it. A timeout says nothing about
what landed, so keep a batch to about 200 requests.
A file
Section titled “A file”upload puts a file on a row in the three calls the API takes: a ticket, a PUT of the bytes to
storage, and a completing POST. The field in the path picks the kind: image is a Thumbnail,
another field an Attachment on that field, and no field a generic Attachment on the row
(recipes/001).
const bytes = new Uint8Array(await file.arrayBuffer());const result = await client.upload('Note', note.id, { filename: file.name, data: bytes, field: 'attachments' });console.log(result.uploadType, result.etag);The status of the PUT is the receipt; etag is present when the storage exposes one. A media field
is not readable straight after: it answers a placeholder under /images/status/transient/ on the
site root until the transcode lands (013_upload_media).
Schema
Section titled “Schema”A SchemaClient reads one field and writes the schema. It is a client of its own beside
SgClient: RestSchemaClient takes the options RestClient takes, ProxySchemaClient the options
ProxyClient takes, and MockSchemaClient answers from memory over a MockClient.
import { MockClient, MockSchemaClient } from 'sg-widgets-core/mock';
const schema = new MockSchemaClient({ client: new MockClient() });No call here verifies a write. A refusal rejects with a SchemaApiError that carries the server’s
status, body, title, detail and source. A success proves the request parsed, so read the field back
and compare (028_loud_and_silent).
One field
Section titled “One field”readField answers one of three results. found carries the normalised field and raw, the
wrapped record as it came, which is what to store as a snapshot. missing is the 404 that names the
field; a retired field and a name never used read the same (040_field_revive). undeclared is a
field the row answers and the schema leaves out (068_note_read_state). Any other failure rejects,
a type the site does not have included. A project id reads the field at that project’s scope.
const read = await schema.readField('Shot', 'sg_status_list', 70);if (read.status === 'found') console.log(read.field.dataType, read.raw.properties);readRawFields answers every wrapped record of a type.
Create a field
Section titled “Create a field”createField sends a data type, a display name and the other settings, and answers the
programmatic name the server assigned, read from the response’s self link. A display name already
in use creates a suffixed field with no warning, so compare the answered name with the wanted one
(019_create_fields). A checkbox needs default_value and a status list its valid_values; an
entity link needs valid_types, one type or several, and a multi-entity link exactly one
(116_field_settings_create_update).
const created = await schema.createField('Shot', { dataType: 'entity', displayName: 'Source Plate', properties: { valid_types: ['Version'] },});console.log(created.name);Update, retire, revive
Section titled “Update, retire, revive”updateField writes settings, at site scope or at a project’s, and answers the record the server
sent back. retireField retires a field and spends its programmatic name: the name does not create
again, and no call frees it (delete_schema_type_fields_field). reviveField brings a retired field
back at the data type it was created at, and answers revived: false with the server’s status and
message when the server declines.
await schema.updateField('Shot', created.name, { name: 'Plate' });await schema.retireField('Shot', created.name);const revive = await schema.reviveField('Shot', created.name);if (!revive.revived) console.log(revive.status, revive.message);Through the proxy
Section titled “Through the proxy”The schema methods are not carried unless the host opts in, since they change the site for
everyone. Hand createProxyHandler a schema client, and point a ProxySchemaClient at the same
endpoints.
import { createProxyHandler, ProxySchemaClient, RestClient, RestSchemaClient } from 'sg-widgets-core';
const handle = createProxyHandler(new RestClient({ siteUrl, token }), { schema: new RestSchemaClient({ siteUrl, token }),});const schema = new ProxySchemaClient({ basePath: '/sg' });