MarkLogic version 10.0-4 and higher supports attaching debuggers to requests. The Visual Studio Code (VS Code) editor offers a plug-in architecture to extend the functionality. A community-supported GitHub project offers a Visual Studio Debugger extension for Visual Studio Code. This project is available in the Visual Studio Code Marketplace as the MarkLogic developer tools extension.
Let’s learn about using Visual Studio Code with the MarkLogic Visual Studio Code Extension to debug a JavaScript Module in MarkLogic.
This tutorial is designed so you can follow along and get hands-on with debugging MarkLogic modules written in JavaScript. To do so you will need to setup a few (free) things on your machine:
Now that you’ve got your local environment setup, let’s begin the tutorial.
Once Visual Studio Code is installed on your computer, we’ll add the MarkLogic developer tools extension and configure it.
We’ll create a folder to use for our tutorial. Along with the new folder, we’ll also create a launch configuration file for Visual Studio Code. Visual Studio Code documentation has information about debugging and launch configurations.
MarkLogic Tutorial
.launch.json
file within a .vscode
folder inside your project folder. launch.json
file.launch.json
configuration file is displayed.{ // Use IntelliSense to learn about possible attributes. // Hover to view descriptions of existing attributes. // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 "version": "0.2.0", "configurations": [ { "type": "ml-jsdebugger", "request": "launch", "name": "Evaluate Current JavaScript Module" }, { "type": "ml-jsdebugger", "request": "attach", "name": "Attach to Remote JavaScript Request", "root": "${workspaceFolder}", "debugServerName": "App-Services" } ] }
launch.json
file.You should be able to log into MarkLogic’s Admin interface by going to http://localhost:8001
in your browser and using the MarkLogic username and password you supplied in the Visual Studio Code Settings. Now would be a great time to verify!
With our connection tested, we can do a quick JavaScript test.
MarkLogic Tutorial
project folder as mytest.sjs
(File -> Save As…).mytest.sjs
editor window.
/* This example is meant to demonstrate debugging JavaScript modules using the MarkLogic developer tools extension for Microsoft's Visual Studio Code. */ 'use strict'; const myvar = "this is my first test~"; myvar;
mytest.sjs
file (File -> Save).mytest.sjs
, set a breakpoint on the myvar;
line of code.myvar
and its value.JavaScript and XQuery modules in MarkLogic are stored in databases, just like your content. This protects your modules, permitting only those that have permissions to run them. Modules run on MarkLogic application servers, implementing Data Services and other functionality closest to the data.
When requests are made, MarkLogic Server 10.0-4 and greater can pause these requests, permitting a debugger to attach to the requests and examine them. To debug these requests with Visual Studio Code, you must have the code both in a MarkLogic database and on your local filesystem, opened in Visual Studio Code. Currently, streaming module code from MarkLogic to Visual Studio isn’t supported.
Let’s use our previous example. We’ll load the mytest.sjs
module into a MarkLogic database. Then we’ll attach to a MarkLogic application server and send a request to MarkLogic to run the mytest.sjs
module and debug it!
We’re going to use a MarkLogic application server that’s already listening for requests when MarkLogic is installed. In addition, you’ll need the following.
Currently, our launch.json
debugger configuration in Visual Studio contains a debugger type of ml-jsdebugger
and a request of attach
. Also, your configuration contains the following values for root and debugServerName:
The root is the local path to the JavaScript module code that is also stored in MarkLogic database. This is where our local copy of mytest.sjs
is found. The debugServerName is the name of the MarkLogic application server that calls the JavaScript module. In this example, we’ll use one of MarkLogic’s default application servers called App-Services
, listening on port 8000. These were copied into your launch.json
Visual Studio Code debugger configuration earlier in this tutorial, therefore no changes are needed to be made right now.
The following curl command loads our mytest.sjs
to the Modules database, a default database created in MarkLogic where the App-Services MarkLogic application server looks for code modules.
curl --anyauth --user admin:admin -X PUT -T ./mytest.sjs -H "Content-type: application/vnd.marklogic-javascript" "http://localhost:8000/LATEST/documents?uri=/mytest.sjs&database=Modules"
Note that MarkLogic has a preferred method of managing and deploying Data Hub projects and Data Services. We use ml-gradle tasks, added as a plugin to the Gradle build system. In real world project using the MarkLogic Data Hub and running on the MarkLogic Data Hub Service in the cloud you would use ml-gradle for complete deployment automation and configuration management.
But for the purposes of this tutorial, we’ll use MarkLogic’s REST API to load our code.
--user
argument from admin:admin
to your MarkLogic administrator user name and password. Make sure you separate the user and password with a colon (:).mytest.sjs
module has been loaded into a MarkLogic database by going to port 8000 in a browser on your MarkLogic server to access Query Console.In the output area of Query Console, see the mytest.sjs
module listed.
We need to let the MarkLogic App-Services application server know we are going to attach a debugger to any request it has queued. The App-Services application server, by default, enables debugging on this application server. We need to connect to the App-Services application server from Visual Studio Code, which will pause any request received after we have connected to that application server.
The following curl command sends a request to MarkLogic. The request executes our mytest.sjs
in the App-Services MarkLogic application server.
curl --anyauth --user admin:admin -X POST -i --data-urlencode module=/mytest.sjs -H "Content-type: application/x-www-form-urlencoded" -H "Accept: multipart/mixed" http://localhost:8000/v1/invoke
--user
argument from admin:admin
to your MarkLogic administrator user name and password. Make sure you separate the user and password with a colon (:).mytest.sjs
in a new editor window from the Visual Studio Code Explorer.mytest.sjs
module) so only one request should be listed.mytest.sjs
and stop at the first line.Remember to disconnect from a MarkLogic application server once you have finished debugging. Otherwise, additional requests will be paused unnecessarily in MarkLogic. If not attached to in the Visual Studio Code debugger, these paused requests will eventually continue. To disconnect from the App-Services application server, display the Command Palette window again (View -> Command Palette…) and select MarkLogic: Disconnect JavaScript Debug Server from the list of MarkLogic commands.
A message from the MarkLogic developer tools confirms you are disconnected from the App-Services application server.
The following curl command deletes the mytest.sjs
module from the Modules database.
curl --anyauth --user admin:admin -X DELETE "http://localhost:8000/LATEST/documents?uri=/mytest.sjs&database=Modules"
--user
argument from admin:admin
to your MarkLogic administrator user name and password. Make sure you separate the user and password with a colon (:).mytest.sjs
JavaScript module from the Modules MarkLogic database.By continuing to use this website you are giving consent to cookies being used in accordance with the MarkLogic Privacy Statement.