7.8 Node.js compatible framework

The Limax integrates the native Java version's node.js compatible framework, uses the Nashorn engine provided by the JDK8, is implementted according to the description of the node.js manual and makes the appropriate expansion to integrate to the Limax framework and provide the convenience to connect the system outside of Limax.

The Nashorn supports the ES5.1 standard. For some js files matching ES6 standard, it should use the 6to5 tool to convert. The existing manual examples are all ES6 code and should be experimentted after conversion.

The below description conforms to the order of the node.js 6 documents, which is convenient to compare.


  • 7.8.2 Enhancement module

    For a long time, the most challenging issue of node.js is that it lacks of the ability of the persistence and management. The Java could easily solve these issues, so it expands two modules sql and edb, which seperately provides the access ability of the relational database and key-value database, and expands a simple JMX query module monitor.

    • Sql

      • sql.Connection class

        • 'close' event

        • connection.execute(statement [,parameter1[,parameter2…]], callback)

        • connection.destroy()

      • sql.createConnection(url)


      Sql.Connection class

      Provide the database connection, internally implement as the connection pool, and inactive connection keep-alive 1 mintue before fade out.


      var url = 'jdbc:mysql://192.168.1.3:3306/test?user=root&password=admin&autoReconnect=true'
      var sql = require("sql");
      var conn = sql.createConnection(url);
      conn.once('close', function() {
          console.log('closed')
      });
      conn.execute("select * from a where id > ?", 1, function(err) {
          if (err) {
              console.log(err);
              return;
          }
          var s = '!';
          for (var i = 1; i < arguments.length; i++)
              s += arguments[i] + ' ';
          console.log(s)
      });
      conn.destroy();
      

      'close' event

      Execute destroy to close the database connection. But there might be active connection existing when closing, this event is trigger after all the active connections finish, indicating that the database connection is completely closed.


      connection.execute(statement [,parameter1[,parameter2…]], callback)

      statement <String> the sql statement which matches the jdbc specification, uses ? as placeholder.

      parameter <Object> the number of these parameters must be equivalent to the number of placehoder(s) in the statement.

      callback <Function> callback while the result return

      The parameter is setted on the query request via PreparedStatement.setObject(). The acceptable type is determined by the JDBC connection pool of the application database. In general, the int and string type have no problem, the other type must be determined by the actual test, and it should take care the long because the long of js has only 53 effective bits. In the worst case, the sql statement could be redesigned to support string parameter, using the database own convert function to convert the string to the target type.

      The first parameter of callback is err, which is consistent with node.js. The parameter number of callback used by SELECT is determined by the number of the columns returned by the query statement itself. If the query statement returns N columns, the N+1 parameters are used to callback. The query statement might return empty set, so last callback passes 0 parameter in order to indicate that the query is done. The arguments.length = 0 could be used to check whether the query is done, so in the above example, the last line always outputs the single character "!". The type of the returned result is considerated to be consistent with the parameter. If the unrecongnized type is returned, modify the sql statement to use the database own convert function to convert to string. The UPDATE, DELETE and INSERT, this kind of operation uses two parameters to callback once, and the second parameter is the row number affected.


      connection.destroy()

      Call execute function following destroy, callback with exception of connection has been closed directly.


      sql.createConnection(url)

      url <String> datbase connection url

      A convenient way to create database connection.


    • Edb

      • edb.Instance class

        • 'close' event

        • instance.addTable(table1, [,table2 [,table3 …]], callback)

        • instance.removeTable(table1, [,table2 [,table3 …]], callback)

        • instance.insert(table, key, value, callback)

        • instance.replace(table, key, value, callback)

        • instance.remove(table, key, callback)

        • instance.find(table, key, callback)

        • instance.exist(table, key, callback)

        • instance.walk(table, [,from], callback)

        • instance.destroy()

      • edb.createEdb(path)


      edb.Instance class

      edb object instance, provides the access operation to the key-value database.

      First, the directory '/work/js.test/dbhome' must be created, The Edb does not create it automatically.


      var edb = require('edb').createEdb('/work/js.test/dbhome');
      edb.once('close', function() { console.log('close')});
      edb.addTable('a', function() {
          var n = 3;
          function insert3() {
              if (--n > 0)
                  return;
              edb.find('a', '1', console.log);
              edb.walk('a', '0', console.log);
              edb.destroy();
          }
          edb.insert('a', '0', 'AA', insert3);
          edb.insert('a', '1', 'b', insert3);
          edb.insert('a', '3','',insert3);
      });
      

      Be careful, the parallel feature of the code, 3 insert operations is parallel, followed by find and walk which is parallel, then close edb by destroy, forbid any operation succeed.


      'close' event

      Execute destroy, close Edb instance. However there might be operations in progress when closing, this event is triggered when all operations end, which indicates that the Edb instance is completely closed.


      instance.addTable(name1, [,name2 [,name3 …]] , callback)

      name <String> table name added into database

      callback <Function>

      Multiple tables can be added once, and callback is called after execution finishes. If there is error, the first parameter is Exception message. If there is table existed in the database, this operation is ignored.


      instance.removeTable(name1, [,name2 [,name3 …]] , callback)

      name <String> table name deleted from database

      callback <Function>

      Multiple tables can be deleted once, and callback is called after execution finishes. If there is error, the first parameter is Exception message. If the table does not exist in the database, this operation is ignored.


      instance.insert(table, key, value, callback)

      table <String> table name

      key <Buffer|String>

      value <Buffer|String>

      callback <Function>

      When inserting a pair of key-value, if key or value is String, code to Buffer with utf8 in the internal then excute.

      callback(err, succeed), if the key exists in the table, succeed == false, insert fails.


      instance.replace(table, key, value, callback)

      table <String> table name

      key <Buffer|String>

      value <Buffer|String>

      callback <Function>

      Replace a pair of key-value, if key or value is String, code to Buffer with utf8 in the internal then execute.

      callback(err), if the key does not exist in the table, execute inserting, or execute replacing.


      instance.remove(table, key, callback)

      table <String> table name

      key <Buffer|String>

      callback <Function>

      Delete a pair of key-value, if key is String, code to Buffer with utf8 in the internal then execute.

      callback(err), if the key does not exist in the table, the operation is ignored, not as wrong.


      instance.find(table, key, callback)

      table <String> table name

      key <Buffer|String>

      callback <Function>

      Execute finding, if key is String, code to Buffer with utf8 in the internal then execute.

      callback(err, value), if the key does not exist in the table, value == null, the value is the Buffer type.


      instance.exist(table, key, callback)

      table <String> table name

      key <Buffer|String>

      callback <Function>

      If key is String, code to Buffer with utf8 in the internal then execute.

      callback(err, value), if the key does not exist in the table, value == false.


      instance.walk(table, [,from], callback)

      table <String> table name

      from <Buffer|String>

      callback <Function>

      If from is String, code to Buffer with utf8 in the internal then execute.

      Traverse the table, if there is from parameter, begin the traverse from the next pair of key-value from from.

      callback(err, key, value), the key and value are Buffer type.


      instance.destroy()

      Close the connection of Edb instance, the operation executed after destroy. The callback with exception message is directly closed by instance.


      edb.createEdb(path)

      An easy way to create Edb instance. The application must ensure that the path directory exists, or throws exception and ends the execution of current code fragment.


Prev Next