// import module moduleimport { MindLake } from'mind-lake-sdk';(async () => {constmindLake=awaitMindLake.getInstance("YOU OWN APP KEY"); // replace to your own appKey// connect to MindLake."5" is example of Goerli Testnet chainIdconstchainId="5"constres1=awaitmindLake.connect(chainId);if(res1.code !==0) {return }// create a tableconstdataLake=mindLake.dataLake;constres2=awaitdataLake.createTable("table_encrypted", [{columnName:'id', type:DataType.int4, encrypt:true}, {columnName:'name', type:DataType.text, encrypt:true}], ["id"]);if(res2.code !==0) {return }// encrypt dataconstcryptor=mindLake.cryptor;constres3=awaitcryptor.encrypt(1,'table_encrypted.id');constres4=awaitcryptor.encrypt("Alice",'table_encrypted.name');if(res3.code !==0||res4.code !==0) {return }// insert encrypted dataconstsql=`insert into table (id, name) values ('${res3.result}', '${res4.result}')`;constres5=awaitdataLake.query(sql);console.log(`${sql} >>>`,JSON.stringify(res5));if(res5.code !==0){return }//query encrypted data;constselectSql=`select * from table_encrypted`;constres6=awaitdataLake.query(selectSql);if(res6.code ===0) {constcolumnList=res6.result.columnList;for (constrowofres6.result.data) {for (constindexin row) {constencryptData= row[index];constcolumn= columnList[index];constdecryptRes=awaitcryptor.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