Executing UDF ( User Defined function ) containing a Node module in Cosmos Db - azure

I have created a UDF function to process some content based on dates in Azure cosmos database. The function looks like below
function userDefinedFunction(array,dateString){
var moment = require('moment');
const startDate = moment(dateString);
const endDate = moment(dateString).add(1,'days');
// filter the array by the dates and return a value
}
When the above UDF is used inside a query the following error message is thrown .
Encountered exception while executing Javascript. Exception = ReferenceError: 'require' is not defined
This error is seemed to be an error orginating from incorrect import of the moment node module.
i have already tried checking on the microsoft offcial docs about the UDFs and using of node module inside UDF.
I tried surfing through the internet about this issue, but both the methods did not provide me with satisfactory answer.
so i would like to know how to import a node module and use it inside an UDF function . Thanks a lot in advance .

Importing modules is not supported for any of the service-side features including stored procedures, triggers and user-defined functions.
This is not currently documented. Will ask for this to be updated.

Related

Unable to use Code data type in Mongoose Schema

I wanted to store a data type of Code in my MongoDB database per the docs.
The issue is that when I use Mongoose to specify this type, I get an error:
ReferenceError: Code is not defined
And yes, Mongoose's doc's don't have a type of Code.
Say I want to save Javascript code in my MongoDB database, how could I do so?
For more insight into why I am doing this: I want to save Puppeteer scripts in the database, and using the FS module add them to a file, execute them and send the results as a response object back to the client.
Right now, the code is saved as String, which seems to cause execution errors.
For more information, please don't hesitate to ask.
Thank you,

nodejs gremlin update if exist or else create

I am using nodejs gremlin against AWS neptune, the requirement is to update properties if vertice exist, or else, create a new vertice, i tried below
g.V().has('event','id','1').
fold().
coalesce(unfold(),
addV('event').property('id','1'))
but I got 'unfold is not defined' error, how do I resolve this?
You probably just need to import unfold() properly. Some common imports for working with Gremlin can be found here but in your case I think you just need to do:
const __ = gremlin.process.statics
and then refer to unfold() as __.unfold() - or just import unfold() as a function explicitly to use it as you were using it.

Cloud Firestore node.js invalid use of type "object" as a Firestore argument

My question is about why I can't seem to pass data into a Firestore database. I'm running code on node.js trying to parse data coming from my Firebase Firestore database and then return information back to the database. Retrieval of the information works fine, but an error message that I cannot escape for the life of me keeps appearing when trying to pass data back to the database.
if(message.type = "0") {
console.log(0)
db.collection("outbox").doc("messageID").set({message: "test message"})
}
On trying to process the last line of code, the following error message appears:
Error: Argument "data" is not a valid Firestore document. Invalid use of type "object" as a Firestore argument.
My database is structured as outbox/messageID/message. Message holds a string value and I'm using the admin SDK so write issues aren't a problem. I'm doing something wrong with passing the object along to Firebase but I just can't figure it out. This is my first time working with Cloud Firestore and node.js but I've done small-scale projects in HTML/JS before.
It looks like an internal error with the way firestore checks if a value is an array.
I have the same issue trying a simple
db.collection('Test').add({ foo: 'bar' })
Which leads to
Error: Value for argument "data" is not a valid Firestore document. Invalid use of type "object" as a Firestore argument
After digging into firebase source code i found they check if an object is an object, based on this test :
Object.getPrototypeOf(input) === Object.prototype
Which is false in some environments for some unknwon reasons (In my case: Node v10.15.3 CLI)
As a workaround if you temporary needs to have this works, you may edit your node module source code in this file : node_modules/#google-cloud/firestore/build/src/serializer.js
Change the isPlainObject function to :
function isPlainObject(input) {
return util_1.isObject(input);
}

Array to function parameters

I'm using the node.js Redis library and I'm attempting to bulk-subscribe to many keys. I've got an array which is dynamic i.e
var keys {'key1','key2',...,'keyN'}
and I want to feed each index in as parameters to subscribe in the Redis library which takes one or more string(s). I've tried the apply function in JS using..
redisClient.subscribe.apply(this,keys);
but it doesn't cause a subscription. Any suggestions on how I can get over this issue?
Your example data is totally invalid JS, but I'm assuming you have it correct in your code.
You need to set the proper function context:
redisClient.subscribe.apply(redisClient, keys);

node-postgres: how to prepare a statement without executing the query?

I want to create a "prepared statement" in postgres using the node-postgres module. I want to create it without binding it to parameters because the binding will take place in a loop.
In the documentation i read :
query(object config, optional function callback) : Query
If _text_ and _name_ are provided within the config, the query will result in the creation of a prepared statement.
I tried
client.query({"name":"mystatement", "text":"select id from mytable where id=$1"});
but when I try passing only the text & name keys in the config object, I get an exception :
(translated) message is binding 0 parameters but the prepared statement expects 1
Is there something I am missing ? How do you create/prepare a statement without binding it to specific value in order to avoid re-preparing the statement in every step of a loop ?
I just found an answer on this issue by the author of node-postgres.
With node-postgres the first time you issue a named query it is
parsed, bound, and executed all at once. Every subsequent query issued
on the same connection with the same name will automatically skip the
"parse" step and only rebind and execute the already planned query.
Currently node-postgres does not support a way to create a named,
prepared query and not execute the query. This feature is supported
within libpq and the client/server protocol (used by the pure
javascript bindings), but I've not directly exposed it in the API. I
thought it would add complexity to the API without any real benefit.
Since named statements are bound to the client in which they are
created, if the client is disconnected and reconnected or a different
client is returned from the client pool, the named statement will no
longer work (it requires a re-parsing).
You can use pg-prepared for that:
var prep = require('pg-prepared')
// First prepare statement without binding parameters
var item = prep('select id from mytable where id=${id}')
// Then execute the query and bind parameters in loop
for (i in [1,2,3]) {
client.query(item({id: i}), function(err, result) {...})
}
Update: Reading your question again, here's what I believe you need to do. You need to pass a "value" array as well.
Just to clarify; where you would normally "prepare" your query, just prepare the object you pass to it, without the value array. Then where you would normally "execute" your query, set the value array in the object and pass it to the query. If it's the first time, the driver will do the actual prepare for you the first time around, and simple do binding and execution for the rest of the iteration.

Resources