Skip to main content

Step 3: Writing

In this step, we will write data to the database we created in Step 1

An item in the database consists of a key, a value, metadata, and a vector. The key uniquely identifies the item. Setting an item with the same key will overwrite the existing item. The value allows you to store arbitrary bytes but is not searchable. Metadata allows you to store additional information about the item to use with query filtering.

Writing data

To add data use the setItem method, this will upsert the data into the database. Let's insert some items representing a series of jobs:

await client.setItem({
databaseId,
key: `job#1`,
value: Buffer.from(`{"id": "1", "posted_by": "user#873240"}`), // Value is stored as bytes, use Buffer.from to convert a string to bytes
vector: [0,1,2,4],
metadata: {
'title': {string: 'Software Engineer'},
'tags': {stringArray: ['software', 'databases']},
'salary': {number: 10000}
}
})

await client.setItem({
databaseId,
key: `job#2`,
value: Buffer.from(`{"id": "2", "posted_by": "user#873241"}`),
vector: [0,1,2,5],
metadata: {
'title': {string: 'Product Manager'},
'tags': {stringArray: ['product', 'management']},
'salary': {number: 12000}
}
})

await client.setItem({
databaseId,
key: `job#3`,
value: Buffer.from(`{"id": "3", "posted_by": "user#873242"}`),
vector: [0,1,2,6],
metadata: {
'title': {string: 'Haskell Hacker'},
'tags': {stringArray: ['haskell', 'functional-programming']},
'salary': {number: 15000}
}
})

await client.setItem({
databaseId,
key: `job#4`,
value: Buffer.from(`{"id": "4", "posted_by": "user#873243"}`),
vector: [0,1,2,7],
metadata: {
'title': {string: 'Frontend Developer'},
'tags': {stringArray: ['frontend', 'javascript']},
'salary': {number: 20000}
}
})

await client.setItem({
databaseId,
key: `job#5`,
value: Buffer.from(`{"id": "5", "posted_by": "user#873244"}`),
vector: [0,1,2,8],
metadata: {
'title': {string: 'Backend Developer'},
'tags': {stringArray: ['backend', 'javascript']},
'salary': {number: 25000}
}
})

Using Metadata

Metadata is used to store data that can be used for filtering. In the example above, we stored the job title, tags, and salary as metadata. This data can be used to filter jobs based on those fields and vector similarity.

When adding metadata, you can use the following types:

  • string: A string value
  • stringArray: An array of strings
  • number: A number value, stored as a double
  • numberArray: An array of numbers, stored as a double array

Deleting data

To delete a document, use the deleteItem method:

await client.deleteItem({databaseId, key: 'job#1'});