Prerequisites
- TerminusDB running locally (install guide)
- A connected client instance with a schema defined
What you'll achieve By the end of this guide, you will have inserted documents into your TerminusDB database using either the JavaScript client, the Python client, or the HTTP API directly.
After you have connected to a database and added a schema, you can insert documents that conform to the schema.
Insert a single document
Example: TypeScript
const doc = {
"@type" : "Player",
name : "George",
position: "Center Back",
}
const addDoc = async () => {
const result = await client.addDocument(doc)
console.log("Document added:", result)
}Example: Python
document = {"@type": "Player", "name": "George", "position": "Center Back"}
result = client.insert_document(document)curl -u admin:root -X POST \
"http://localhost:6363/api/document/admin/mydb/local/branch/main?author=admin&message=Add+document" \
-H "Content-Type: application/json" \
-d '{"@type":"Player","name":"George","position":"Center Back"}'Insert multiple documents
You can insert several documents in a single operation:
Example: TypeScript
const players = [
{
"@type" : "Player",
name : "George",
position: "Center Back",
},
{
"@type" : "Player",
name : "Doug",
position: "Full Back",
},
{
"@type" : "Player",
name : "Karen",
position: "Center Forward",
},
]
const addDocs = async () => {
const result = await client.addDocument(players)
console.log("Documents added:", result)
}Example: Python
documents = [
{"@type": "Player", "name": "George", "position": "Center Back"},
{"@type": "Player", "name": "Doug", "position": "Full Back"},
{"@type": "Player", "name": "Karen", "position": "Center Forward"},
]
results = client.insert_document(documents)curl -u admin:root -X POST \
"http://localhost:6363/api/document/admin/mydb/local/branch/main?author=admin&message=Add+documents" \
-H "Content-Type: application/json" \
-d '[
{"@type":"Player","name":"George","position":"Center Back"},
{"@type":"Player","name":"Doug","position":"Full Back"},
{"@type":"Player","name":"Karen","position":"Center Forward"}
]'Insert schema documents
You can also add schema documents programmatically:
Example: TypeScript
const schema = { "@type": "Class", "@id": "Player", name: "xsd:string", position: "xsd:string" }
const addSchema = async () => {
const result = await client.addDocument(schema, { graph_type: "schema" })
console.log("Schema added:", result)
}Example: Python
schema = {"@type": "Class", "@id": "Player", "name": "xsd:string", "position": "xsd:string"}
result = client.insert_document(schema, graph_type="schema")curl -u admin:root -X POST \
"http://localhost:6363/api/document/admin/mydb/local/branch/main?author=admin&message=Add+schema&graph_type=schema" \
-H "Content-Type: application/json" \
-d '{"@type":"Class","@id":"Player","name":"xsd:string","position":"xsd:string"}'Next steps
You've inserted documents into TerminusDB. From here you can:
- Get documents — retrieve and filter the documents you just added
- Edit documents — update existing documents
- Add a schema — add type validation and constraints to your data