3️Basic Usage
Glacier
primarily has the following core parts:
Let's see how to use them.
👨💼 GlacierClient
As we mentioned in the previous chapter, GlacierClient
is the carrier we use to access Glacier
. If you want to create a read-only client
, you don't need to specify privateKey
when instantiating or provider
. You only need to specify the endpoint
of the service, such as:
You can query Glacier
for data through the above client
.
But suppose you need to write to Glacier.
In that case, you need to specify the privateKey
or provider
required for the signature. Usually, privateKey
is only used for the Node.js
server or script code. However, since it needs to be signed by the browser wallet at runtime in the browser, so provider
is passed in, as in the following example:
src/server.ts
src/browser.ts
📇 Namespace
Namespace
is the first-level unit in Glacier
. There are usually only two operations: creation and enumeration. The code for creation is as follows:
src/createNamespace.ts
Output result:
The codes to enumerate Namespace
is as follows:
src/namespaces.ts
Output result:
🗄️ Dataset
Dataset
is a secondary unit in Glacier
, similar to the MongoDB database, but each Dateset
must belong to a Namespace
.
The codes for creating Dataset
is as follows, you need to get Namespace
to which it belongs through client.namespace(name)
firstly:
src/createDataset.ts
Output result:
If you want to query the details of a Dataset
, you can use the queryDataset
method provided by Namespace
, the example is as follows:
src/queryDataset.ts
Output result:
collections
from the above codes is Collection
contained in this Dataset
.
🗃️ Collection
Collection
is a third-level unit in Glacier
, it corresponds to Collection
in MongoDB
, and it needs to belong to a Dataset
.
To create a Collection
, you can use the createCollection
method provided by Dataset
, and you need to specify the data verification model required to create a Collection
, that is, JSON Schema
. The sample code is as follows:
src/createCollection.ts
Output result:
Collection
is used to store each specific data record. You can get the Collection
example to be operated through dataset.collection(name)
. The operations are addition, deletion, modification, and query. The sample is as follows:
Insert data
src/insertOne.ts
Query data
Collection
provides a find
method, which is analogous to MongoDB
and supports most of MongoDB
query conditions. Examples are as follows:
src/find.ts
The advanced query syntax currently supported by Glacier SDK
is as follows:
$eq
$gt
$gte
$in
$lt
$lte
$ne
$nin
$not
$exists
$regexp
$and
$nor
$or
CAUTION
In particular, find
must call toArray()
at the end to get the final query result.
Update data
Collection
provides a updateOne
method, which is used in a similar way to a combination of find
and insertOne
. Examples are as follows:
src/updateOne.ts
CAUTION
The data update in Glacier
is different from MongoDB
. Here updateOne
is always a partial update rather than replacing the entire record when updating data, which is equivalent to using $set
in MongoDB
to update the data.
Delete data
Collection
provides a deleteOne
method, which is similar to the find
method. The example is as follows:
src/deleteOne.ts
Last updated