# MindLake.DataLake

## 1. query() Method

Execute a SQL query or command.

{% hint style="info" %}
When referencing a table in your SQL query statement, you can specify the table owner with their wallet address, like this: `SELECT * FROM someWalletAddress.someTable`. If you omit the wallet address in your query, it defaults to your own wallet address. For example, `SELECT * FROM myTable` is equivalent to `SELECT * FROM myWalletAddress.myTable`.
{% endhint %}

{% hint style="info" %}
During testing, SQL queries will timeout after 5 minutes. Requests that take longer than that to process will not receive a response.
{% endhint %}

{% hint style="info" %}
To specify an upper-case or mixed-case name of table or column, you need to double-quote the name; else it will be folded to lower case.
{% endhint %}

```typescript
const result = await mindLake.dataLake.query(statement);
```

### Parameters

1. `statement` - `string`: the SQL statement used to query Mind Lake, such as `SELECT, INSERT, UPDATE, and CREATE`.&#x20;

### Returns

An object of `Promise<`[`ResultType`](#user-content-fn-1)[^1]`>`. [For more information](https://docs.mindnetwork.xyz/mind-lake-sdk/mindlake#2.-mindlake.resulttype).

* `result` - `QueryResult`: The result of query execution.

  ```typescript
  type QueryResult = {
    data: Array<Array<any>>,
    columnList: Array<string>
  }
  ```

  * `data` - `Array<Array<any>>`: The query data result is presented as a 2D array, with rows and columns.
  * `columnList` - `Array<string>`: the column names in the specific order in which they appear.

### Example

```typescript
  const selectSql = `select * from wallet_balance`;
  const res = await dataLake.query(selectSql);
  if(res.code !== 0) {
    console.error(res.message);
    return
  }
  const columnList = res.result.columnList;
  for (const row of res.result.data) {
    for (const idx in row) {
     console.log(`${columnList[idx]} >>>`, row[idx])
    }
  }
```

## 2. createTable() Method

Create a table in the user's own account in Mind Lake.

```typescript
const result = await mindLake.dataLake.createTable(tableName, columns, primaryKey)
```

### Parameters

1. `tableName` - `string`: the name of the table to be created.
2. `columns` - `Array<`[`Column`](#column-type)`>`: defines the columns in the table. Each column should be defined as a [`MindLake.DataLake.Column`](#3.-mindlake.datalake.column) object.
3. `primaryKey` (Optional) - `Array<string>|undefined`: defines the table's primary key by specifying one or more columns. Each column in the primary key should be identified by its column name.\
   If the `primaryKey` parameter is omitted, no primary key will be defined for the table.

### Returns

An object of `Promise<`[`ResultType`](#user-content-fn-1)[^1]`>`. [For more information](https://docs.mindnetwork.xyz/mind-lake-sdk/mindlake#2.-resulttype-type).

### Example

```typescript
  const dataLake = mindLake.dataLake;
  const res = await dataLake.createTable("wallet_balance", [{columnName: 'WalletAddress', type: DataType.text, encrypt: false}, {columnName: 'Name', type: DataType.text, encrypt: true}, {columnName: 'Balance', type: DataType.float4, encrypt: true}], ["id"]);
  if(res.code !==0) {
    console.error(res.message);
    return
  }
```

## 3. MindLake.DataLake.Column

`Column` type is used to represent a single column in a table. Each `Column` instance encapsulates properties about the column, including its name, data type, and encryption status. By creating a `Column` object, you can define the properties of a table column and use it to construct a table schema.

### Fields

* `columnName` - `string`: The name of the column.
* `type` - [`MindLake.DataType`](https://docs.mindnetwork.xyz/mind-lake-sdk/mindlake#3.-datatype-enum): The data type of the column, which should be define as [`MindLake.DataType`](https://docs.mindnetwork.xyz/mind-lake-sdk/mindlake#3.-datatype-enum)
* `encrypt` - `boolean`: A flag that indicates whether the column should be encrypted or not.

### Example

```typescript
const column = {columnName: 'WalletAddress', type: MindLake.DataType.text, encrypt: false}
```

## 4. listTablesByCocoon() Method

Retrieve a list of names for the tables that belong to the user invoking the method, or in the specified cocoon.

```typescript
await dataLake.listTableByCocoon(cocoonName)
```

### Parameters

1. `cocoonName` (Optional) - `string`: specifies the name of the cocoon for which a list of tables should be returned.\
   If the `cocoonName` parameter is omitted, all the tables that belong to the method caller will be listed.

### Returns

An object of `Promise<`[`ResultType`](#user-content-fn-1)[^1]`>`. [For more information](https://docs.mindnetwork.xyz/mind-lake-sdk/mindlake#2.-resulttype-type).

* `result` - `Array<string>`: a list of names for the tables that belong to the user invoking the method, or in the specified cocoon.

### Example

```typescript
const result = await mindLake.dataLake.listTableByCocoon("cocoonName");
```

## 5. createCocoon() Method

Create a Cocoon in Mind Lake. Tables can be categorized into cocoons for classification purposes.

```typescript
await mindLake.dataLake.createCocoon(cocooName)
```

### Parameters

1. `cocoonName` - `string`: the name of the cocoon to be created.

### Returns

An object of `Promise<`[`ResultType`](#user-content-fn-1)[^1]`>`. [For more information](https://docs.mindnetwork.xyz/mind-lake-sdk/mindlake#2.-resulttype-type).

### Example

```typescript
const result = await mindLake.dataLake.createCocoon("cocoonName");
```

## 6. listCocoon() Method

Retrieve a list of names for the cocoons that belong to the user invoking the method.

```typescript
await mindLake.dataLake.listCocoon()
```

### Returns

An object of `Promise<`[`ResultType`](#user-content-fn-1)[^1]`>`. [For more information](https://docs.mindnetwork.xyz/mind-lake-sdk/mindlake#2.-resulttype-type).

* `result` - `Array<string>`: a list of names for the cocoons that belong to the user invoking the method.

### Example

```typescript
const result = await mindLake.dataLake.listCocoon();
```

## 7. linkTableToCocoon() Method

Assign the specified cocoon tag to the table.

```typescript
await mindLake.dataLake.linkTableToCocoon(tableName, cocoonName)
```

### Parameters

1. `tableName` - `string`: the name of the table to be tagged with.
2. `cocoonName` - `string`: specifies the name of the cocoon for which the table should be tagged with.

### Returns

An object of `Promise<`[`ResultType`](#user-content-fn-1)[^1]`>`. [For more information](https://docs.mindnetwork.xyz/mind-lake-sdk/mindlake#2.-resulttype-type).

### Example

```typescript
const result = await mindLake.dataLake.linkTableToCocoon("tableName", "cocoonName");
```

## 8. dropCocoon() Method

Delete a Cocoon in Mind Lake.

<pre class="language-typescript"><code class="lang-typescript"><strong>await mindLake.dataLake.dropCocoon(cocoonName)
</strong></code></pre>

### Parameters

1. `cocoonName` - `string`: the name of the cocoon to be dropped.

### Returns

An object of `Promise<`[`ResultType`](#user-content-fn-1)[^1]`>`. [For more information](https://docs.mindnetwork.xyz/mind-lake-sdk/mindlake#2.-resulttype-type).

### Example

```typescript
const result = await mindLake.dataLake.dropCocoon("cocoonName");
```

## 9. dropTable() Method

Drop a table in the user's own account in Mind Lake.

```typescript
await mindLake.dataLake.dropTable(tableName)
```

### Parameters

1. `tableName` - `string`: the name of the table to be dropped.

### Returns

An object of `Promise<`[`ResultType`](#user-content-fn-1)[^1]`>`. [For more information](https://docs.mindnetwork.xyz/mind-lake-sdk/mindlake#2.-resulttype-type).

### Example

```typescript
const result = await mindLake.dataLake.dropTable("tableName");
```

[^1]: ```typescript
    type ResultType = {
      code: number,
      message?: string,
      result?: any
    }
    ```
