How to do post filter with couchnode / couchbase server? - couchbase

I've been developing using Couchbase Server 4.0 and upgrading to 5.0 is down the road but not at the moment.
But for now I do need to search views (map/reduce) using text search, on Couchbase Lite .Net there is PostFilter that serve the purpose.
But I could not find the same settings on Couchnode, checking the Couchbase Lite .Net told me that a query option might help.
I tried couple of things like this:
query.options.filter = r => {
console.log('******', r)
return true
}
query.options.post_filter = r => {
console.log('******', r)
return true
}
query.options.postFilter = r => {
console.log('******', r)
return true
}
but nothing seems to work. Anyone experienced this before please help!!

On Couchbase server, map/reduce queries are created on the Server cluster itself, not created in the SDK like with Couchbase Lite. An example:
function(doc, meta)
{
emit(doc.name, [doc.city, doc.salary]);
}
When you create a view, you give it a name. You can call these view from the Node SDK (couchnode) by name like so:
var couchbase = require('couchbase');
var ViewQuery = couchbase.ViewQuery;
var query = ViewQuery.from('beer', 'by_name');
See the documentation: https://docs.couchbase.com/server/4.0/developer-guide/views-writing.html and https://docs.couchbase.com/nodejs-sdk/2.6/view-queries-with-sdk.html

Related

Cannot create node group via sdk

I am trying to create a nodegroup via the aws sdk for node.js I'd prefer to do this than via the eksctl.
This is a simple question but there are just no code examples of this in the aws docs and I can't figure out the correct name spaces so unfortunately I am asking here:
My code is as follows with node.js
const AWS = require('aws-sdk');
AWS.config.update({region: 'us-west-2'});
const eks = new AWS.EKS();
AWS.config.update({region: 'us-west-2'});
// Name must be cluster
eks.CreateNodegroup({name:'openstudio-server',
instanceTypes:["t2.xlarge"],
"nodegroupName":'openstudio-node',
"scalingConfig": {
"desiredSize": 3,
"maxSize": 10,
"minSize": 3
}
})
I am consistently getting the error
What am I doing wrong?
I'm guessing you've resolved this already but it looks like you just need to change eks.CreateNodegroup() to eks.createNodegroup() and it should work.
source: https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/EKS.html

How to pull down an schema-less JSON document from CosmosDB in Xamarin App

I am creating an app in Xamarin and am having issues querying a general JSON document from my CosmosDB. I am able to query my DB with a known structure (very similar to what we see in the Xamarin ToDo Example):
public async static Task<List<ToDoItem>> GetToDoItems()
{
var todos = new List<ToDoItem>();
if (!await Initialize())
return todos;
**var todoQuery = docClient.CreateDocumentQuery<ToDoItem>(
UriFactory.CreateDocumentCollectionUri(databaseName, collectionName),
new FeedOptions { MaxItemCount = -1, EnableCrossPartitionQuery = true })
.Where(todo => todo.Completed == false)
.AsDocumentQuery();**
while (todoQuery.HasMoreResults)
{
var queryResults = await todoQuery.ExecuteNextAsync<ToDoItem>();
todos.AddRange(queryResults);
}
return todos;
}
The problem I see with this "code fixed scheme" approach is that if the scheme of your JSON file changes throughout development, older versions of code will overwrite the newer scheme in CosmosDB since writes to the DB are on a document level and not a property level. Instead, it would be helpful for older versions of the code to be able to pull down the latest scheme and work with the properties that it knows about without having to force the user to update.
Does anyone know how to query a schema-less JSON document out of CosmosDB? Thanks!
Use JObject as the type to query a schema-less JSON document out of CosmosDB, and use following code to pull the raw JSON data into your object:
JsonConvert.DeserializeObject<Class>(JSONString);

Featherjs - Add custom field to hook context object

When using feathersjs on both client and server side, in the app hooks (in the client) we receive an object with several fields, like the service, the method, path, etc.
I would like, with socket io, to add a custom field to that object. Would that the possible? To be more precise, I would like to send to the client the current version of the frontend app, to be able to force or suggest a refresh when the frontend is outdated (using pwa).
Thanks!
For security reasons, only params.query and data (for create, update and patch) are passed between the client and the server. Query parameters can be pulled from the query into the context with a simple hook like this (where you can pass the version as the __v query parameter):
const setVersion = context => {
const { __v, ...query } = context.params.query || {};
context.version = __v;
// Update `query` with the data without the __v parameter
context.params.query = query;
return context;
}
Additionally you can also add additional parameters like the version number as extraHeaders which are then available as params.headers.
Going the other way around (sending the version information from the server) can be done by modifying context.result in an application hook:
const { version } = require('package.json');
app.hooks({
after: {
all (context) {
context.result = {
...context.result,
__v: version
}
}
}
});
It needs to be added to the returned data since websockets do not have any response headers.

using mongodb for caching json responses

Disclaimer: I'm new to NoSQL databases, if something is not clear, appreciate comments and questions to clear things up.
I'm calling some 3rd party web services to get JSON response but need a way to cache the response to avoid repeated calling since each response is constant across remote entities.
Question.
I've selected mongodb, is it the right tool for the job, the response entities have lengthy schemas and mongoose is forcing me to define one, is there a way to avoid having to define schema and just save the response by some id and read it later. if someone can kindly help with condition for checking the A. cache and B. saving.
var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/test');
if() { // A. condition to check if cached response is available
} else { // call web service
http.request({}, function (response) {
var buffers = [];
response.on('data', function (chunk) {
buffers.push(chunk)
});
response.on('end', function () {
var data = Buffer.concat(buffers).toString();
// B. how to save data to a mongoose db
});
})
}
If approach is not right, and I should be using something else then please also enlighten.
If you don't have a schema available then don't use mongoose. Just store the json as mongo documents directly.
https://docs.mongodb.com/getting-started/node/client/
https://docs.mongodb.com/getting-started/node/update/

dojo - data from sql server for datagrid and charts

I have just started getting familiar with dojo and creating widgets and have a web UI which I would like to now populate with data. My question is merely to get some references or ideas on how to do this. My databases are all sql server 2008 and I usually work with microsoft.net. I thought that I would probably have to create a service that calls the sql queries and converts the results into json and feed that into the widgets whether it be the datagrid or charts. Just not sure how to do this and if it is indeed possible to do that. Any ideas appreciated.
EDIT:
store = new dojo.data.ItemFileWriteStore({
url: "hof-batting.json"
});
ngrid = new dojox.grid.DataGrid({
store: store,
id: 'ngrid',
structure: [
{ name: "Search Term", field: "searchterm", width: "10%" },
{ name: "Import Date", field: "importDate", width: "10%" }
]
}, "grid");
ngrid.startup();
I want to add data returned from my web service to this datagrid and use the same principle to add data to a chart.
Your describe exactly what you need to do.
We use C# to query our database to get the data and then convert it to json. We use multiple techniques right now for json serialization. I would recommend using JSON.NET. It is what the .NET MVC team is going to use. I would not use the DataContractSerialization that is currently part of .NET.
http://json.codeplex.com/
We sometimes put JSON right on the page and the javascript accesses it as a page variable. Other times we call services in .NET. We use WCF and we have also used an .ashx file to give the web client json data.
The structure of the json will be the contract between your dojo widgets and web server. I would use what the chart widgets or store will need to start the process of defining the contract.
EDIT
WCF Interface
[OperationContract]
[WebInvoke(Method="POST", UriTemplate = "/data/{service}/",
BodyStyle = WebMessageBodyStyle.WrappedRequest)]
String RetrieveData(string service, Stream streamdata);
The implementation returns a string that is the json. This gets sent to the browser as json, but it's wrapped by .NET by an xml node. I have a utility function that cleans it.
MyUtil._xmlPrefix =
'<string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">';
MyUtil._xmlPostfix = '</string>';
MyUtil.CleanJsonResponse = function(data) {
// summary:
// a method that cleans a .NET response and converts it
// to a javascript object with the JSON.
// The .NET framework, doesn't easily allow for custom serialization,
// so the results are shipped as a string and we need to remove the
// crap that Microsoft adds to the response.
var d = data;
if (d.startsWith(MyUtil._xmlPrefix)) {
d = d.substring(MyUtil._xmlPrefix.length);
}
if (d.endsWith(MyUtil._xmlPostfix)) {
d = d.substring(0, d.length - MyUtil._xmlPostfix.length);
}
return dojo.fromJson(d);
};
// utility methods I have added to string
String.prototype.startsWith = function(str) {
return this.slice(0, str.length) == str;
};
String.prototype.endsWith = function(str) {
return this.slice(-str.length) == str;
};