Time for action – Connecting to the database

In our main function, let's initiate the connection to our database and the SPOD system:

import Post;

class Main
{   
   public static function main()
   {
      //Parameters to connect to the MySQL database
      var cnx = neko.db.Mysql.connect({ 
            host : "localhost",
            port : 3306,
            database : "myBlog",
            user : "root",
            pass : "",
        });
      
      //Initialize the SPOD system
        neko.db.Manager.cnx = cnx;
        neko.db.Manager.initialize();


      //We've done our processing, let's clean things and disconnect
        neko.db.Manager.cleanup();
        cnx.close();
   }
}

When doing this, we can successfully connect to the database although it won't do anything at the moment.

Now, let's just retrieve our posts from the database by simply adding this:

var posts = Post.manager.all(); ...

Get haXe 2 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.