Jaxer

How do I use Jaxer.Dir.grep and Jaxer.Dir.map

A recent addition to the Jaxer IO capabilities are the grep/map methods, lets look at some use cases to explain the powerful functionality these provide to the developer.

The signature of the grep method looks like the following

The path parameter is a directory path to the folder you want to search.

The options parameter is a Javascript Object that contains some required properties.

The supported properties are pattern, flags and recursive.
pattern is a string value representing a javascript regular expression,
flags contains any regexp flags you want to provide to govern the behavour of the regular expression provided by the pattern parameter,
recursive is used to indicate whether the search should also scan sub folders.

Let's look at some examples of this in practice.

consider the following folder structure

c:\rootFolder
----foo.txt
----bar.txt
----subFolder1
--------foo1.txt
--------foo2.txt
--------foo3.html (1)
--------ignore.html
----subFolder2
--------a.js
--------b.js
--------c.js
----subFolder3
-------subFolder3a
------------deepFOO1.txt (2)
------------deepFOO2.txt

example paths

(1) c:\rootFolder\subFolder1\foo3.html

(2) c:\rootFolder\subFolder3\subFolder3a\deepFOO1.txt

Jaxer.Dir.grep

So given the example folder structure we can use the simplest form of the grep method specifying only the folder to search in.

which returns an array of file objects representing these files :-

c:\rootFolder\bar.txt
c:\rootFolder\foo.txt

Note that is only files in the specified folder that are returned and no subfolder searching is attempted

The next example provides a pattern parameter that matches a single file in the top level folder.

this returns an array containing a single file object representing :-

c:\rootFolder\foo.txt

Now we add the recursive property to the invocation

and this returns an array of file objects that includes matches from the scanned subfolders :-

c:\rootFolder\foo.txt
c:\rootFolder\subFolder1\foo1.txt,
c:\rootFolder\subFolder1\foo2.txt
c:\rootFolder\subFolder1\foo3.html

Next we add a flag property to the options object to include case insensitive results.

which returns an array of file objects including some containing 'FOO' :-

c:\rootFolder\foo.txt,
c:\rootFolder\subFolder1\foo1.txt
c:\rootFolder\subFolder1\foo2.txt
c:\rootFolder\subFolder1\foo3.html
c:\rootFolder\subFolder3\subFolder3a\deepFOO1.txt
c:\rootFolder\subFolder3\subFolder3a\deepFOO2.txt

You can also provide a string containing the folder path and use an object literal for your options parameter

which returns an array of file objects :-

c:\rootFolder\subFolder1\foo1.txt
c:\rootFolder\subFolder1\foo2.txt
c:\rootFolder\subFolder3\subFolder3a\deepFOO1.txt
c:\rootFolder\subFolder3\subFolder3a\deepFOO2.txt

OK, so you get the idea, you can provide an fairly complex query and get a result set of matched values in an array, and you can do it without a whole lot of scaffolding code to navigate the folder tree.

Jaxer.Dir.map

Taking Jaxer.Dir.grep as a given we can now look at the Jaxer.Dir.Map functionality, this has a signature that is extended from the grep method and adds a third parameter of a function to invoke on each matched file object.

The function can be declared inline or externally and if omitted the map method will behave exactly like the grep method and return an array of Jaxer.File objects.

Let's look at some examples.

will write a log entry out for each of the matched files and return an array of matched Jaxer.File objects

13:55:19 02/20/2008 [  2356] [INFO] [JS Framework] [framework.] c:\rootFolder\subFolder1\foo1.txt
13:55:19 02/20/2008 [  2356] [INFO] [JS Framework] [framework.] c:\rootFolder\subFolder1\foo2.txt
13:55:19 02/20/2008 [  2356] [INFO] [JS Framework] [framework.] c:\rootFolder\subFolder3\subFolder3a\deepFOO1.txt
13:55:19 02/20/2008 [  2356] [INFO] [JS Framework] [framework.] c:\rootFolder\subFolder3\subFolder3a\deepFOO2.txt

The important point to note here is that the function is defined to expect a parameter containing the matching file object being handled

this code could be rewritten as

A more complex example using a decorator function to format the results of the matches

which outputs the following to the log.
15:43:05 02/20/2008 [framework.] foo1.txt|0|txt|Tue Feb 19 2008 19:15:04 GMT-0800 (Pacific Standard Time)|c:\rootFolder\subFolder1
15:43:05 02/20/2008 [framework.] foo2.txt|0|txt|Tue Feb 19 2008 19:15:04 GMT-0800 (Pacific Standard Time)|c:\rootFolder\subFolder1
15:43:05 02/20/2008 [framework.] deepFOO1.txt|0|txt|Tue Feb 19 2008 19:15:04 GMT-0800 (Pacific Standard Time)|c:\rootFolder\subFolder3\subFolder3a
15:43:05 02/20/2008 [framework.] deepFOO2.txt|0|txt|Tue Feb 19 2008 19:15:04 GMT-0800 (Pacific Standard Time)|c:\rootFolder\subFolder3\subFolder3a

So as you can see the grep and map methods of the Jaxer.Dir namespace provide you with an excellent utility knife to deal with accessing objects on the file system

One final note, as these kind of searches can take some time depending on the depth of the nested searching, care needs to be taken when invoking through a callback to ensure that a timeout doesn't occur.

TwitJax - Jaxer, Twitter, Javascript 1.8 and E4X

This sample application demonstrates using some of the advanced Javascript language features available out of the box with Jaxer.

We show how to use the ECMASCRIPT for XML additions to Javascript to pull an XML feed of the latest public twit list remotely and reprocess it inside Jaxer using server-side Javascript.

fileGrep - Jaxer Filesystem Access Sample

This sample application demonstrates some of the filesystem access capabilities of the Jaxer Javascript framework.

You can download a zip of the sample here.

Running Jaxer with MySQL

Changing the Database Configuration Settings

To run Jaxer with the mysql database it is just a matter of making a very simple change, to a single file
<INSTALL FOLDER>/local_jaxer/config.js

As shipped this file contains a set of common configuration overrides, but they are all commented out. The file is a simple JS function that updates a set of property values on the Config object. The source for this file is shown below

uncomment lines 33-43 if you only want to use MYSQL for your application data

uncomment lines 44-54 if you only want to use MYSQL for the jaxer server metadata

uncomment lines 33-54 if you want to use MYSQL for both application data and server metadata

Once you have uncommented the lines you need to provide the connection parameters for your MYSQL instance.

The Config.DB_CONNECTION_PARAMS / Config.DB_FRAMEWORK_CONNECTION_PARAMS objects have properties for

HOST : the ip address or uri for the server
PORT : the port the server is running on (typically 3306 for MYSQL)
NAME : the name of the database to use
USER : user name
PASS : password

Set these value to match your database. Then restart Jaxer to load the changes, your Jaxer instance will now be running with MYSQL

After the changes the config file will look something like this.

Creating a Connection to a MySQL db.

For many applications you wont want to use the built-in jaxer DB instance definitions and will require a dedicated connection to another instance or perhaps a separate database on the same instance.

The code below is an example of how to create and use a connection to a database.

Syndicate content