TypeScript Quick-Start

This page aims to give a quick start example on how to use Mind Lake via TypeScript SDK.

  • Demos on how to connect lake, create a table, encrypt data, and insert and query on encrypted data.

  • You can check out TypeScript API Reference to view more advanced functions.

Preparation

If you need to configure TypeScript local environment, please visit our step-by-step tutorial for TypeScript:

Source Code

// import module module
import { MindLake } from 'mind-lake-sdk';

(async () => {
 const mindLake = await MindLake.getInstance("YOU OWN APP KEY"); // replace to your own appKey

 // connect to MindLake."5" is example of Goerli Testnet chainId
 const chainId = "5"
 const res1 = await mindLake.connect(chainId);
 if(res1.code !== 0) {
   return
 }

 // create a table
 const dataLake = mindLake.dataLake;
 const res2 = await dataLake.createTable("table_encrypted", [{columnName: 'id', type: DataType.int4, encrypt: true}, {columnName: 'name', type: DataType.text, encrypt: true}], ["id"]);
 if(res2.code !==0) {
   return
 }

 // encrypt data
 const cryptor = mindLake.cryptor;
 const res3 = await cryptor.encrypt(1, 'table_encrypted.id');
 const res4 = await cryptor.encrypt("Alice", 'table_encrypted.name');
 if(res3.code !==0 || res4.code !== 0) {
   return
 }

 // insert encrypted data
 const sql = `insert into table (id, name) values ('${res3.result}', '${res4.result}')`;
 const res5 = await dataLake.query(sql);
 console.log(`${sql} >>>`, JSON.stringify(res5));
 if(res5.code !== 0){
   return
 }

 //query encrypted data;
 const selectSql = `select * from table_encrypted`;
 const res6 = await dataLake.query(selectSql);
 if(res6.code === 0) {
  const columnList = res6.result.columnList;
  for (const row of res6.result.data) {
    for (const index in row) {
      const encryptData = row[index];
      const column = columnList[index];
      const decryptRes = await cryptor.decrypt(encryptData);
      console.log(`${column} decrypt data >>> `, decryptRes.result);
    }
  }
}
})();

Execution Output:

insert into table_encrypt (id, name) values ('\xa00a0e01876d340832195bdf03ad5d18fe92355e62fdc823a650a3bbab064de2c55586be', '\x5c3a0f01fd8bfc4ef7ea3848f80e011d433b81e2e544975157556d75bad76c3b4865b0ea') >>> {"code":0,"result":null}
id decrypt data >>>  1
name decrypt data >>>  tom

Last updated