# overview

## HTML code

Only need reference two Javascript Libraries in HTML:

```markup
<script src="https://TML.ink/X.tml"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/2.1.1/socket.io.js"></script>
```

The first line reference the client function library. This's a dynamic library includes many dynamic components and CDN resources, so don't save to local. For new features, *strongly* recommend above address which comes with the latest version of X-Server, many new components, enhanced performance, and more. The second line is using third-party socket library from CDN.

## Javascript code

### Create a client instance

```java
tim = TML('wss://x.TML.ink'); //Create a client instance
```

If you connect multiple servers at the same time, you can create multiple client instances. Connect a server,Load authentication module, Provide offline login codes and method, logout codes and method, multi-server login, automatic login, user cache and other functions of authentication. If you don't need these features, you should not execute this line of code.

### Using eXtend feature

```javascript
tim.X('feature name').xxx()
```

Feature name can be defined by yourself, starting with lowercase letter and only letters without numbers & symbols.\
.xxx() should be Get(), Set(), New(), Del(). These are all asynchronous methods, so every asynchronous method needs to be followed by "then" or add "await" in front.

```javascript
console.log(await tim.X('feature name').Get());//add "await" in front
```

Above codes can also be written as follow:

```javascript
tim.X('feature name').Get().then(data => console.log(data)); //followed by "then"
```

### Sign Up a new user

```javascript
tim.X('user').New({
   email: "test email",
   password: "test"
     });
```

### Login code

```javascript
tim.Login({
     email: "test email",
     password: "test"
   });
```

Use local cache to automatically login can be written as follow:

```javascript
tim.Login();
```

### Logout code

```javascript
tim.Logout();
```

### Monitor user registration

```javascript
tim.X('user').on('add', function(msg){
   console.log(msg);
      });
```

Listen for real-time messages registered by users. This will be a mass message to update other users' interfaces without refreshing.

### Get example

```javascript
console.log(await tim.X('user').Get());//Get top 10 users

//Get top 50 users
 console.log(await tim.X('user').Get({
   $max: 50
     }))

//Get the user whose email is 'a'
 console.log(await tim.X('user').Get({
   email: 'a'
 }))
```

Above codes are equivalent to:

```javascript
tim.X('user').Get().then(data => console.log(data));//Get top 10 users

//Get top 50 users
  tim.X('user').Get({
      $max: 50
     }).then(data => console.log(data));

//Get the user whose email is 'a'
  tim.X('user').Get({
   email: 'a'
     }).then(data => console.log(data));
```

## Operators of Get Method

### Equals a Specified Value

Specifies equality condition. The $eq operator matches documents where the value of a field equals the specified value. The following example queries the user collection to select all documents where the value of the email field equals a:

```javascript
tim.X('user').Get({
email: 'a'
}).then(data => console.log(data));
```

### $max

The $max stage has the following prototype form:

```javascript
{ $max: <positive integer> }
```

$max takes a positive integer that specifies the maximum number of documents to pass along. Consider the following example:

```javascript
tim.X('user').Get({
 $max: 50
}).then(data => console.log(data));
```

This operation returns only the first 50 documents. $max has no effect on the content of the documents it passes. Set $max to 0 will get the counting faster.

### $sort

Specifies the order in which the returns matching documents. You can apply $sort to sort by and a value of 1 or -1 to specify an ascending or descending sort respectively before retrieving any documents from the database. The following sample document specifies a descending sort by the \_time field and will returns the 10 newest documents:

```javascript
tim.X('user').Get({
$max: 10,
$sort: {
      _time: -1
    }
}).then(data => console.log(data));
```

This operation use $max in conjunction with $sort to return the first (in terms of the sort order) documents.

### lessThan

$lt selects the documents where the value of the field is less than (i.e. <) the specified value.

```javascript
tim.X('user').Get({
price: {
  $lt: 9.9
  }
});
```

## Catch an exception

Use try...catch statement marks a block of statements to try, and specifies a response, should an exception be thrown.

### Asynchronous catch

For example: Login

```javascript
tim.Login({
    email: "test email",
    password: "test"
  }).then(function(data) {
    console.log(data);
  })
  .catch(function(err) {
    console.log(err);
    console.log(err.message);
  })
```

For example: Get

```javascript
tim.X('user').Get()
  .then(function(data) {
    console.log(data);
  })
  .catch(function(err) {
    console.log(err);
    console.log(err.message);
  })
```

### Synchronous catch

For example: Login

```javascript
try {
  await tim.Login({
    email: "test email",
    password: "test"
  });
} catch (err) {
  console.log(err);
  console.log(err.message);
}
```

For example: Get

```javascript
try {
  await tim.X('user').Get();
} catch (err) {
  console.log(err);
  console.log(err.message);
}
```

{% hint style="info" %}
To Be Continued

This page is just an unfinished draft. The author is writing now. Welcome to donate, cheer for the author.
{% endhint %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://book.tml.ink/app-development/backend-solution/overview.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
