Skip to main content

create database

The SDK provides a function to create a database. The following example shows how to create a library database

Python

db_name = "library"
dataset_desc = DatasetDesc(
description="The eBookLibrary database is designed to store and manage a collection of electronic books, facilitating easy access, organization, and retrieval of eBook information for users.",
cover_img="https://ipfs.glitterprotocol.dev/ipfs/QmZHD8P65mghh818W4hb4ReEZ1G5XxFzqGgV5hkQmHMtWJ",
cid="",
website=Website(),
app=[App(
img="https://ipfs.glitterprotocol.dev/ipfs/QmZHD8P65mghh818W4hb4ReEZ1G5XxFzqGgV5hkQmHMtWJ",
name="lib3",
website="https://liber3.eth.limo/"
)],
storage="98G",
row_cnt=1430000,
)
r = glitter_cli.create_dataset(db_name, "https://library.glitterprotocol.xyz", dataset_desc)

JavaScript

const client = new LCDClient({
chainID: chainId,
URL: url,
gasPrices: gasPrices,
gasAdjustment: gasAdjustment,
});

const key = new MnemonicKey({
mnemonic: mnemonicKey,
account: 0,
index: 0,
});
const dbClient = client.db(key);

const params = {
fromAddress: key.accAddress,
datasetName: "testcreatedataset",
workStatus: 2,
hosts: "https://xxx.com",
manageAddresses: "glitter1rjj69vzkuqc7jqtlggj8dqd30ar2myemur2p6j",
meta: "datasetmeta",
description: '{"github":"xxx3.com"}',
duration: Long.fromNumber(86400),
};

const r = await dbClient.createDatabase(params);

Golang

cli := New()
ctx := context.Background()
datasetName := "library_test"
workStatus := chaindepindextype.ServiceStatus(2)
hosts := "https://anybt.glitterprotocol.xyz"
managageAddr := "glitter178uwquz93vwkc292s5n56kwq4cc2hnxfvlujmt"
description := "{}"
duration := int64(10000)
resp, err := cli.CreateDataset(ctx, datasetName, workStatus, hosts, managageAddr, description, duration)
t.Log(utils.ConvToJSON(resp))
t.Log(err)

edit database

We can edit the database we have created. The SDK provides a function to edit the database. The following example shows how to edit the library database we created earlier.

Python

r = glitter_cli.edit_dataset(db_name, "https://library2.glitterprotocol.xyz", dataset_desc)

JavaScript

const client = new LCDClient({
chainID: chainId,
URL: url,
gasPrices: gasPrices,
gasAdjustment: gasAdjustment,
});

const key = new MnemonicKey({
mnemonic: mnemonicKey,
account: 0,
index: 0,
});
const dbClient = client.db(key);

const editParams = {
fromAddress: key.accAddress,
datasetName: "testcreatedataset",
workStatus: 2,
hosts: "http://sg5.testnet.glitter.link:50051",
manageAddresses: "glitter1rjj69vzkuqc7jqtlggj8dqd30ar2myemur2p6j",
meta: "datasetmeta",
description: '{"github":"xxx3.com"}',
};
const editRst = await dbClient.editDatabase(editParams);

Golang

cli := New()
ctx := context.Background()
datasetName := "library_test"
workStatus := chaindepindextype.ServiceStatus(2)
hosts := "https://anybt.glitterprotocol.xyz"
managageAddr := "glitter178uwquz93vwkc292s5n56kwq4cc2hnxfvlujmt"
description := "{\"description\":\"Dataset containing magnet links\"}"
resp, err := cli.EditDataset(ctx, datasetName, workStatus, hosts, managageAddr, description)
t.Log(utils.ConvToJSON(resp))
t.Log(err)

renewal database

We can renew the database. The SDK provides a function to renew the database. The following example shows how to renew the library database we created earlier.

Python

r = glitter_cli.renewal_dataset(db_name, 2 * 24 * 3600)

JavaScript

const client = new LCDClient({
chainID: chainId,
URL: url,
gasPrices: gasPrices,
gasAdjustment: gasAdjustment,
});

const key = new MnemonicKey({
mnemonic: mnemonicKey,
account: 0,
index: 0,
});
const dbClient = client.db(key);

const renewalParams: RenewalDatasetRequest = {
fromAddress: key.accAddress,
datasetName: "testcreatedataset",
duration: Long.fromNumber(86400),
};
const renewalRst = await dbClient.RenewalDataset(renewalParams);

Golang

cli := New()
ctx := context.Background()
datasetName := "library_test"
duration := int64(10000)
resp, err := cli.RenewalDataset(ctx, datasetName, duration)
t.Log(utils.ConvToJSON(resp))
t.Log(err)

create or edit table

After we create the database, we need to create a table on the database. The following example shows how to create a table called book on the database library we have created. We can also use this method to update an existing table.

Python

book_tb_name = "book"
tb_desc = TableDesc(
table_name=book_tb_name,
engine="bleve",
comment="This table stores the primary information about each eBook in the library.",
columns=[
ColumnMeta(name="_id", type="text", comment="Unique identifier for the dataset"),
ColumnMeta(name="author", type="text", comment="The author of the dataset"),
ColumnMeta(name="coverurl", type="text", comment="URL for the cover image of the dataset"),
ColumnMeta(name="descr", type="text", comment="Description of the dataset"),
ColumnMeta(name="extension", type="text", comment="File extension of the dataset"),
ColumnMeta(name="filesize", type="number", comment="Size of the dataset file in bytes"),
ColumnMeta(name="ipfs_cid", type="text", comment="IPFS CID for the dataset"),
ColumnMeta(name="issn", type="text", comment="International Standard Serial Number for the dataset"),
ColumnMeta(name="language", type="text", comment="Language of the dataset"),
ColumnMeta(name="publisher", type="text", comment="Publisher of the dataset"),
ColumnMeta(name="series", type="text", comment="Series name for the dataset"),
ColumnMeta(name="tags", type="text", comment="Tags associated with the dataset"),
ColumnMeta(name="title", type="text", comment="Title of the dataset"),
ColumnMeta(name="year", type="text", comment="Publication year of the dataset")
]
)
rst = glitter_cli.edit_table(db_name, book_tb_name, tb_desc)

JavaScript

const client = new LCDClient({
chainID: chainId,
URL: url,
gasPrices: gasPrices,
gasAdjustment: gasAdjustment,
});

const key = new MnemonicKey({
mnemonic: mnemonicKey,
account: 0,
index: 0,
});
const dbClient = client.db(key);

const meta = `"CREATE TABLE testcreatedataset.testTable0811 ( id INT(11) PRIMARY KEY COMMENT 'id', name VARCHAR(255) COMMENT 'name of book', category VARCHAR(255) COMMENT 'book category (eg:history,cartoon..)', author VARCHAR(255) COMMENT 'author', public_at INT(11) COMMENT 'public time', stock INT(11) COMMENT 'stock of book', price INT(11) COMMENT 'price', _tx_id VARCHAR(255), KEY 'name_ind' (name), KEY 'name_author_ind' (name, author)) ENGINE=standard;"`;
const editTableParams: EditTableRequest = {
fromAddress: key.accAddress,
datasetName: "testcreatedataset",
tableName: "testTable0811",
meta,
};

const editTableRst = await dbClient.editTable(editTableParams);

Golang

cli := New()
ctx := context.Background()
datasetName := "library_test"
tableName := "table_test"
description := "{\"tableName\": \"ebook_v3\", \"engine\": \"bleve\", \"comment\": \"ebook entries\", \"columns\": [{\"name\": \"_id\", \"type\": \"text\", \"comment\": \"Unique identifier for the dataset\"}, {\"name\": \"author\", \"type\": \"text\", \"comment\": \"Author of the document\"}, {\"name\": \"extension\", \"type\": \"text\", \"comment\": \"File extension of the document\"}, {\"name\": \"filesize\", \"type\": \"number\", \"comment\": \"Size of the file\"}, {\"name\": \"ipfs_cid\", \"type\": \"text\", \"comment\": \"IPFS CID for the document\"}, {\"name\": \"issn\", \"type\": \"text\", \"comment\": \"International Standard Serial Number\"}, {\"name\": \"language\", \"type\": \"text\", \"comment\": \"Language of the document\"}, {\"name\": \"publisher\", \"type\": \"text\", \"comment\": \"Publisher of the document\"}, {\"name\": \"series\", \"type\": \"text\", \"comment\": \"Series name of the document\"}, {\"name\": \"tags\", \"type\": \"text\", \"comment\": \"Tags associated with the document\"}, {\"name\": \"title\", \"type\": \"text\", \"comment\": \"Title of the document\"}, {\"name\": \"descr\", \"type\": \"text\", \"comment\": \"Description of the document\"}, {\"name\": \"coverurl\", \"type\": \"text\", \"comment\": \"URL of the cover image\"}, {\"name\": \"year\", \"type\": \"text\", \"comment\": \"Year of publication\"}]}"
resp, err := cli.EditTable(ctx, datasetName, tableName, description)
t.Log(utils.ConvToJSON(resp))
t.Log(err)