Query table data

For more information regarding database table query, please refer to kScript reference under coding reference. 

get

Get an item based on Id or primary key

      var table = k.database.getTable("tablename");
      var item = table.get(IdOrKey);

find

Find and return the first record basd on the search condition. 

find has two type of syntax, by condition or by field value. 

By field value takes two parameters, one is the field name, the other is the field value.

        <script engine="kscript">

        var table = k.database.getTable("tablename"); 
        // find equal value match.
        var item = table.find("fieldname", "matchvalue"); 

        </script>

By condition use the kooboo condition syntax. 

Syntax: fieldname operator value

Operators: ==,  >=,  >,  <,  <=, contains, startwith

        <script engine="kscript">

        var table = k.database.getTable("tablename");

        // Find  condition text can be one of the followings.
        // find item with field "name" value of "matchedvalue".
        var item = table.find("name=='matchedvalue'");

        // find item with field "number" bigger than or equal to 123.
        var item = table.find("number>=123");

        // find item with field "name" value contains "matchedvalue".
        var item = table.find("name contains 'matchedvalue'");

        // find item with field "name" value startwith "matchedvalue".
        var item = table.find("name startwith 'matchedvalue'");

        // && can be used to combine multiple query...
        var item = table.find("name contains na && number>=123"); 

        </script>

findAll

findAll has exactly the same format and condition as find method. It search till the last record and return all matched records an array instead of an object. 

all

return all records of that table

var list = k.database.getTable("tablename").all();

query, orderBy, skip, take

query can take the same search condition as find or blank to return all records. 

query is a fluent API  with support of where, orderBy, skip and take. 

<script engine="kscript">
        var table = k.database.getTable("mytablename");
        var obj = {name: "myname", number: 124};
        table.add(obj);
        var obj2 = {name: "myname", number: 125};
        table.add(obj2);

        // query table
        var values = table.query("number>=123").orderBy("number").skip(1).take(10);
        // or the same
        //var values = table.query().where("number>=123").orderBy("number").skip(1).take(10); 

        </script>


        <div k-foreach="item values">
        <p k-content="item.name">name value</p>
        <p k-content="item.number">number</p>
        </div>