Skip to content

Instantly share code, notes, and snippets.

@dmcassel
Last active October 14, 2015 00:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dmcassel/dfad05e884e0cbc8fa0f to your computer and use it in GitHub Desktop.
Save dmcassel/dfad05e884e0cbc8fa0f to your computer and use it in GitHub Desktop.
MarkLogic 8 for Node.js Developers
{
"book": {
"title": "Grimms' Fairy Tales",
"authors": [
{
"fname": "Jacob",
"lname": "Grimm"
},
{
"fname": "Wilhelm",
"lname": "Grimm"
}
],
"chapters": [
{
"title": "The Golden Hand"
},
{
"title": "Hans in Luck"
},
{
"title": "Jorinda and Jorindel"
}
]
}
}
{
"com.marklogic.samplestack.domain.Contributor": {
"aboutMe": "Twitter: [@maryadmin](http://twitter.com/maryadmin) Disclaimer: This is not me. MaryAdmin _doesn't exist_!",
"displayName": "MaryAdmin",
"id": "9611450a-0663-45a5-8a08-f1c71320475e",
"location": "Barrow",
"originalId": null,
"reputation": 100,
"userName": "mary@example.com",
"voteCount": 5,
"websiteUrl": "http://website.com/grechaw"
}
}
{
"pipeline-name": "Normalize Date",
"pipeline-description": "Creates a new JSON property called 'releasedOn' based on 'releaseDate', using standard date format",
"success-action": {
"module": "/MarkLogic/cpf/actions/success-action.xqy"
},
"failure-action": {
"module": "/marklogic/cpf/actions/failure-action.xqy"
},
"state-transition": [
{
"annotation": "If the document is JSON, create releaseOn based on releaseDate.",
"state": "http://marklogic.com/states/initial",
"on-success": "http://marklogic.com/states/converted",
"on-failure": "http://marklogic.com/states/error",
"priority": 100,
"execute": [
{
"condition": {
"module": "/MarkLogic/cpf/actions/mimetype-condition.xqy",
"options": {
"mimetype": "application/json"
}
},
"action": {
"module": "/ext/cpf/transform-date.sjs"
}
}
]
}
]
}
$ curl --anyauth --user admin:admin -i -X PUT \
--data-binary @cpf/transform-date.sjs \
-H "Content-type:application/vnd.marklogic-javascript" \
'http://localhost:8000/v1/ext/cpf/transform-date.sjs'
$ curl -X POST --anyauth --user admin:admin \
--header "Content-Type:application/json" \
-d@cpf/normalize-date.json \
http://localhost:8002/manage/v2/databases/Triggers/pipelines?format=json
{
"domain-name": "All documents",
"description": "Default domain: covers everything in database",
"scope": "directory",
"uri": "/",
"depth": "infinity",
"eval-module": "Modules",
"eval-root": "/",
"pipeline": [
"Status Change Handling",
"Normalize Date"
]
}
$ mlcp.sh import -username admin -password admin -host localhost \
-port 8000 -input_file_path data
<content>
<person>David Cassel</person> started working for <company>MarkLogic</company>
in <date start="2009-01-01" end="2009-12-31">2009</date>. Before that, he
worked for <company>Lockheed Martin</company>.
</content>
{
"title": "The Princess Bride",
"releaseDate": "9/25/1987",
"releasedOn": "1987-09-25"
}
{
"title": "The Princess Bride",
"releaseDate": "9/25/1987"
}
{
"accepted": false,
"acceptedAnswerId": null,
"answerCount": 1,
"answers": [
{
"comments": [],
"creationDate": "2014-10-01T08:59:09.309Z",
"downvotingContributorIds": [],
"id": "6432cc02-2770-4b8d-b5f1-0b632875f86e",
"itemTally": 0,
"owner": {
"displayName": "JoeUser",
"id": "cf99542d-f024-4478-a6dc-7e723a51b040"
},
"text": "My second answer to a question",
"upvotingContributorIds": []
}
],
"comments": [
{
"creationDate": "2014-10-01T08:59:12.230Z",
"owner": {
"displayName": "JoeUser",
"id": "cf99542d-f024-4478-a6dc-7e723a51b041",
"userName": "joe@example.com"
},
"text": "This question is insightful."
}
],
"creationDate": "2014-10-01T08:59:08.866Z",
"downvotingContributorIds": [],
"id": "5dce8909-0972-4289-93cd-f2e8790a17fc",
"itemTally": 0,
"lastActivityDate": "2014-10-01T08:59:12.230Z",
"owner": {
"displayName": "MaryAdmin",
"id": "9611450a-0663-45a5-8a08-f1c71320475e",
"userName": "mary@example.com"
},
"tags": ["python", "seed-data"],
"text": "I, Mary, had a question about the word balsamic and the number 1",
"title": "Mary's Question Number 1",
"upvotingContributorIds": [],
"voteCount": 0
}
var marklogic = require('marklogic');
var db = marklogic.createDatabaseClient({
host: 'localhost',
port: 8000,
user: 'admin',
password: 'admin'
});
var bookUri = '/book/ml-4-node.json';
db.documents.write({
uri: bookUri,
content: {
title: "MarkLogic for Node.js Developers",
author: "David M. Cassel"
}
}).result().then(function(response) {
db.documents.read(bookUri).result().then(
function(documents) {
console.log('Doc descriptor: ' + JSON.stringify(documents[0]));
}
);
});
declareUpdate();
var cpf = require("/MarkLogic/cpf/cpf.xqy");
var uri;
var transition;
if (cpf.checkTransition(uri, transition)) {
try {
var doc = cts.doc(uri).toObject();
doc.releasedOn =
new Date(doc.releaseDate).toISOString().substr(0, 10);
xdmp.documentInsert(uri, doc);
cpf.success(uri, transition, null);
} catch (e) {
cpf.failure(uri, transition, e, null);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment