Skip to content

Instantly share code, notes, and snippets.

View jmakeig's full-sized avatar

Justin Makeig jmakeig

View GitHub Profile
@jmakeig
jmakeig / jsearch-slice.sjs
Last active May 11, 2017 21:20
Monkey patch of MarkLogic’s JSearch API to change the default values for slice(). Proof-of-concept. Don’t actually do this.
'use strict';
const jsearch = require('/MarkLogic/jsearch');
// Monkey patch
const proto = Object.getPrototypeOf(
// Doesn’t actually run a query
jsearch.collections('.').documents().where(cts.trueQuery())
);
const _slice = proto.slice;
@jmakeig
jmakeig / insert-update-upsert-move.sjs
Last active March 26, 2019 14:52
True insert, update, upsert, and move functions for MarkLogic
'use strict';
/**
* @class
*/
class DocumentExistsError extends Error {
constructor(uri) {
super();
this.uri = uri;
this.message = `URI ${String(
@jmakeig
jmakeig / invoke-asuser.sjs
Created April 21, 2017 20:25
Invoke a MarkLogic JavaScript function as a particular user
'use strict';
/** Invokes any function as a specific user */
const asUser = (fn, user) =>
xdmp.invokeFunction(fn, { userId: xdmp.user(user) });
/** The actual function that does the work. */
function _doStuff(param) {
return `${param}: ${xdmp.getCurrentUser()}`;
}
@jmakeig
jmakeig / set-eqaulity.js
Created April 13, 2017 21:12
JavaScript ES2015 Set equality
/**
*
*
* @param {Set} aaa
* @param {Set} bbb
* @param {function} [comparator]
* @returns {boolean}
*/
function equalSets(aaa, bbb, comparator = (x, y) => x === y) {
if ('function' !== typeof comparator) {
@jmakeig
jmakeig / toggle-group.html
Last active July 13, 2017 06:22
Minimal HTML toggle group
<!DOCTYPE html>
<html>
<head>
<title>Toggle group</title>
<style type="text/css">
.toggleable:before {
width: 0.75rem;
/* The two arrows have different widths */
display: inline-block;
content: '▾';
const table = fn.head(xdmp.documentGet(CSV_FILE)).toObject(); // string
const rows = table.split('\r\n'); // Parse lines into Array<string>
const headers = rows.shift().split(','); // Array<string>
// Array of functions used to parse column values
// Only need to specify the left-most columns the others will be skipped
const types = [
function(columnValue) {
return; /* parsed representation or undefined to skip */
}
@jmakeig
jmakeig / export-workspaces.sh
Created March 24, 2017 18:17
cURL script to export MarkLogic Query Console workspaces
#!/usr/bin/env bash
# Downloads Query Console workspaces to the current working directory in the
# format `{host}_{workspace id}_{timestamp}.workspace`.
#
# Usage: ./query-console.sh localhost admin '******'
#
if command -v jq >/dev/null; then
:

TL;DR: For custom types, it’s always better to extend an type’s prototype than to overwrite it.

function Custom() {  // constructor
  this.constructor.apply(this, arguments); // call the super-type’s constructor
}
Object.assign(
 Custom.prototype, 
@jmakeig
jmakeig / about.md
Last active May 24, 2017 23:01
Search MarkLogic Query Console buffers

Searches all Query Console workspaces. Uses exact match, like you’d do in the browser’s own find functionality. Only matches current buffers, not histories.

Set-up

  1. Enable word search indexes in the App-Services database
  2. Paste qconsole-search.sjs above into a Query Console query buffer (how meta)
  3. Modify the first parameter of the findBuffers() call at bottom to change the actual search term

To-do

  1. Weight highest to lowest: current query buffer, active buffers, current workspace, other workspaces
  2. Figure out security: There don’t look to be document permissions (other than qconsole-internal) on any of the Query Console data. (Shouldn’t workspaces, queries, and histories be “owned” by a user?)
@jmakeig
jmakeig / database-xpath.sjs
Last active January 31, 2020 23:50
Database-wide XPath evaluation in JavaScript in MarkLogic
/**
* @param {string} path
* @param {object} [bindings]
* @param {string | Iterable<string>} [collection]
* @return {Sequence<Node>}
*/
const xpath = (function _memo() {
function isIterable(itr, ignoreStrings = false) {
if ('string' === typeof itr) return !ignoreStrings;
return Boolean(itr && itr[Symbol.iterator]);