CipherStash
Search…
Put, Get and Delete Records
In the previous section we learned how to create a collection with some indexes and briefly touched on how to add a record to a collection using put. In this section we'll discuss how to retrieve a record using get and then go into a little more detail on the put function.

Load a Collection

When you create a collection, a Collection object is returned on which you can call functions like put and get. But what should you do if a collection already exists? In this case, use the collection function.
TypeScript
JavaScript
1
const employees = await stash.loadCollection("employees")
Copied!
1
const employees = await stash.loadCollection("employees")
Copied!

Get a Record

To retrieve a source record from a collection, we can use the get function. Retrieval when using get is always done using the ID (primary key).
Let's get a record from our employees collection from the previous example:
TypeScript
JavaScript
1
const employee = await employees.get('d8e54fde-3925-429f-bb56-1788dfc59773')
Copied!
1
const employee = await employees.get('d8e54fde-3925-429f-bb56-1788dfc59773')
Copied!
The returned object is exactly the same as what was added to the collection in the previous section.

Insert a Record

To insert a record into a collection, you can pass a Plain Old JavaScript Object (POJO) to the put function. Any keys in the POJO that have matching indexes defined (when calling createCollection) will have their values processed using the configured analyzer for the index.
A field can have only one index defined (multiple indexes per field will be possible in the future)

Inserts are Upserts

All inserts into a collection are actually upserts. That means that if a source record with a given ID already exists in the collection, an additional insert will overwrite it (and all of the associated index terms). If you don't want this behaviour you can check for the existence of a source record first using get.
TypeScript
JavaScript
1
// Check if a source record exists before inserting
2
employees.get(id).catch((err) => {
3
employees.put({
4
id: id,
5
name: "Some Person",
6
//...other fields
7
})
8
})
Copied!
1
// Check if a source record exists before inserting
2
employees.get(id).catch((err) => {
3
employees.put({
4
id: id,
5
name: "Some Person",
6
//...other fields
7
})
8
})
Copied!
In this way, CipherStash collections behave more like a Lucence/ElasticSearch index than a Database table. You can't modify a single field - only the entire record. This is because CipherStash has no knowledge of the data it is storing.

Reindexing via Upsert

You can also use upserts to reindex a document:
TypeScript
JavaScript
1
// Reindex the source record with the given ID
2
let id = 'd8e54fde-3925-429f-bb56-1788dfc59773'
3
employees.get(id).then((empl) => {
4
employees.put({id: id, ...empl})
5
})
Copied!
1
// Reindex the source record with the given ID
2
let id = 'd8e54fde-3925-429f-bb56-1788dfc59773'
3
employees.get(id).then((empl) => {
4
employees.put({id: id, ...empl})
5
})
Copied!

Unindexed Fields

CipherStash does not require that all fields be indexed. In fact, a collection with no indexes at all is quite valid (searches would not be possible but source records could be retrieved by their ID). You may find that you don't need to perform queries on some fields in your collection. In that case, there is no need to add the field to the index array in createCollection and this will save storage space.

Bulk Inserts

CipherStash does not currently support bulk inserts. If you are adding an existing data set to a collection you can iterate and call put for each record.
TypeScript
JavaScript
1
// Inserting an array of records using put
2
existingRecords.forEach((rec) => {
3
await employees.put(rec)
4
})
Copied!
1
// Inserting an array of records using put
2
existingRecords.forEach((rec) => {
3
await employees.put(rec)
4
})
Copied!

Deleting Records

To delete a record from a collection, you can use delete.
TypeScript
JavaScript
1
await employees.delete('d8e54fde-3925-429f-bb56-1788dfc59773')
Copied!
1
await employees.delete('d8e54fde-3925-429f-bb56-1788dfc59773')
Copied!
Last modified 2mo ago