Inserting openEHR Data with SQL via HTTP API
We use the fetch function in TypeScript to execute commands via HTTP API.
command function (used for executing SQL commands like INSERT):
const auth = Buffer.from('root:arcadedb').toString('base64')
async function command<T = unknown>(command: string, parameters: unknown[] = []): Promise<T[]> {
const response = await fetch('http://localhost:2480/api/v1/command/arcehr', {
method: 'POST',
headers: {'Content-Type': 'application/json', 'Authorization': `Basic ${auth}`},
body: JSON.stringify({language: 'sql', command, parameters}),
})
if (!response.ok) {
throw new Error(`Command failed with ${response.status} ${response.statusText}: ${await response.text()}`)
}
const data = await response.json() as CommandResponse<T>
return data.result ?? data.records ?? []
}
Inserting openEHR data with SQL INSERT statement:
// Insert a new composition
const content = {
archetype_node_id: 'openEHR-EHR-COMPOSITION.encounter.v1',
name: {value: 'Vital Signs'},
uid: {_type: 'OBJECT_VERSION_ID', value: '8849182c::openEHRSys.example.com::1'},
archetype_details: {
archetype_id: {value: 'openEHR-EHR-COMPOSITION.encounter.v1'},
template_id: {value: 'Example.v1::c7ec861c-c413-39ff-9965-a198ebf44747'},
rm_version: '1.0.2',
},
language: {terminology_id: { value: 'ISO_639-1' }, code_string: 'en'},
content: [{ _type: 'OBSERVATION' }]
}
const insert = `INSERT INTO create_composition (ehr_id, content) VALUES (:ehr_id, :content)`
const insertRes = await command(insert, ['ehr-1', content])
Result:
{
ehr_id: 'ehr-1',
content: '{"archetype_node_id":"openEHR-EHR-COMPOSITION.encounter.v1","name":{"value":"Vital Signs"},"uid":{"_type":"OBJECT_VERSION_ID","value":"8849182c::openEHRSys.example.com::1"},"archetype_details":{"archetype_id":{"value":"openEHR-EHR-COMPOSITION.encounter.v1"},"template_id":{"value":"Example.v1::c7ec861c-c413-39ff-9965-a198ebf44747"},"rm_version":"1.0.2"},"language":{"terminology_id":{"value":"ISO_639-1"},"code_string":"en"},"content":[{"_type":"COMPOSITION"}]}',
prefer: 'return=minimal',
content_type: null,
accept: null,
'@rid': '#72:1'
}