Skip to content

Commit

Permalink
Add rows example
Browse files Browse the repository at this point in the history
  • Loading branch information
wooldridge committed Dec 2, 2017
1 parent 6f15a87 commit 1603709
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 0 deletions.
29 changes: 29 additions & 0 deletions examples/before-load.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ var marklogic = require('../');

var exutil = require('./example-util.js');

var testconfig = require('../etc/test-config.js');

var db = marklogic.createDatabaseClient(exutil.restWriterConnection);

var fsdir = 'examples/data/';
Expand Down Expand Up @@ -105,3 +107,30 @@ var ws = db.documents.write({
result(function(response) {
console.log('wrote '+imageFile);
});

// Write TDE template to Schemas database
console.log('copy TDE template');
const schemaDb = marklogic.createDatabaseClient({
database: 'Schemas',
host: testconfig.manageAdminConnection.host,
port: testconfig.manageAdminConnection.port,
user: testconfig.manageAdminConnection.user,
password: testconfig.manageAdminConnection.password,
authType: testconfig.manageAdminConnection.authType
});
schemaDb.documents.write({
uri:'/examples/data/countries.tdej',
contentType:'application/json',
collections:['http://marklogic.com/xdmp/tde'],
permissions: [
{'role-name':testconfig.restAdminConnection.user,
capabilities:['read', 'execute', 'update']}
],
content:fs.createReadStream(fsdir+'countries.tdej')
})
.result(function(response) {
console.log('template written');
})
.catch(function (error) {
console.log(error);
});
40 changes: 40 additions & 0 deletions examples/data/countries.tdej
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{
"template": {
"collections": [
"/countries"
],
"context": "/",
"rows": [
{
"schemaName": "facts",
"viewName": "countries",
"columns": [
{
"name": "name",
"scalarType": "string",
"val": "name"
},
{
"name": "code",
"scalarType": "string",
"val": "code",
"nullable": true
},
{
"name": "region",
"scalarType": "string",
"val": "region",
"nullable": true
},
{
"name": "area",
"scalarType": "int",
"val": "geo/Area/total/quantity",
"nullable": true,
"invalidValues": "ignore"
}
]
}
]
}
}
62 changes: 62 additions & 0 deletions examples/read-rows.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* Copyright 2014-2017 MarkLogic Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
const exutil = require('./example-util.js');

// a real application would require without the 'exutil.' namespace
const marklogic = exutil.require('marklogic');
const p = marklogic.planBuilder;

const db = marklogic.createDatabaseClient(exutil.restReaderConnection);

console.log('Read document content as rows from view');

// View defined here: examples/data/countries.tdej
const plan = p.fromView('facts', 'countries', '')
.select(['name', 'region', 'area'])
.where(p.eq(p.col('region'), 'North America'))
.orderBy(p.desc('area'))
.limit(4);

db.rows.query(plan, {
format: 'json',
structure: 'object',
columnTypes: 'header'
})
.then(function (response) {
console.log('Columns:');
console.log(
// Comma-separated list of column names
response.columns.map(function (col) {
return col.name;
}).join(', ')
);
console.log('Rows:');
response.rows.forEach(function (row) {
console.log(
// Comma-separated list of values in each row
Object.keys(row).map(function (key) {
return row[key];
}).join(', ')
);
})
console.log('done');

exutil.succeeded();
})
.catch(function(error) {
console.log(JSON.stringify(error));
exutil.failed();
});

0 comments on commit 1603709

Please sign in to comment.