Storing a Date Property for an Azure Table Storage Entity with Mobile Services and Node.js - azure-mobile-services

I'm working with Windows Azure Mobile Services Custom APIs and the Windows Azure SDK for Node.js.
I have a simple script that starts by importing the azure module.
var azure = require('azure');
var tableService = azure.createTableService();
In response to a post to my custom API, I would like to insert an entity into Azure Table Storage (not an Azure Sql Database) with a handful of properties, one of which is a timestamp (an instance of Date).
var entity = {
PartitionKey: partitionKey
RowKey: rowKey,
Time: new Date()
};
tableService.insertOrReplaceEntity(tableName, entity, callback);
The result is that the Time property is stored with the entity as a string instead of a date. For example, the Time property would be stored as the string Mon Aug 12 2013 20:32:51 GMT+0000 (Coordinated Universal Time). I've confirmed this by loading the table from the Server Explorer in Visual Studio and examining the details of the inserted entity.
I know that you can store dates in Azure Table Storage and I've done this from C#. However, the above does not work and I cannot think of a more canonical example to test with my custom API written in Javascript.
See also How to Use the Table Service from Node.js which shows a similar example.
So how do you store a date property with the expected data type using the Azure Node.js SDK?

This example does save the Time property using the WCF DateTime data type. Using this code:
var azure = require('azure');
var tableService = azure.createTableService(accountName, accountKey, host);
tableService.createTableIfNotExists('testtable', function(error){
if(!error){
var item = {PartitionKey: "Things"
, RowKey: "123"
, name: "Thing to add"
, category: "Test"
, time: new Date()};
tableService.insertOrReplaceEntity('testtable', item, function(error){
if(!error){
console.log('Item inserted');
tableService.queryEntity('testtable'
, 'Things'
, '123'
, function(error, entity){
if(!error){
console.log(entity);
}
});
}
});
}
});
If I run this code, I get:
Done
Item inserted
{ _:
{ 'xml:base': 'https://XXXXXX.table.core.windows.net/',
xmlns: 'http://www.w3.org/2005/Atom',
'xmlns:d': 'http://schemas.microsoft.com/ado/2007/08/dataservices',
'xmlns:m': 'http://schemas.microsoft.com/ado/2007/08/dataservices/metadata',
etag: 'W/"datetime\'2013-12-12T16%3A11%3A38.118877Z\'"',
ContentRootElement: 'm:properties',
id: 'https://XXXXXX.table.core.windows.net/testtable(PartitionKey=\'Things\',RowKey=\'123\')',
category: { '$': [Object] },
link: 'testtable(PartitionKey=\'Things\',RowKey=\'123\')',
title: '',
updated: '2013-12-12T16:11:38Z',
author: { name: '' } },
PartitionKey: 'Things',
RowKey: '123',
Timestamp: Thu Dec 12 2013 16:11:38 GMT+0000 (GMT Standard Time),
category: 'Test',
name: 'Thing to add',
time: Thu Dec 12 2013 16:11:38 GMT+0000 (GMT Standard Time) }
This is showing a DateTime value rather than a string for the time field. It also shows as a DateTime field in the Visual Studio table storage viewer/editor.
If I query this same table using C# code, I also get back a C# DateTime value for the time field.

Related

How can delete the specific data between two specific dates in mongodb

I have a DB table in MongoDB called users. I would like to delete the between two specific dates. like Oct 15 2019 to June 1st 2020 is there any way to delete the specific data created between the dates.
Data Structure like this:-
{
username: demo1,
createdate: "2019-10-15"
},
{
username: demo2,
createdate: "2020-01-15"
},
{
username: demo2,
createdate: "2020-05-01"
}

ExtJS: Date field writes the date one day back?

I'm using a 'Ext.form.field.Date' and on CRUD process it writes given date as one day back. I mean if I select 05 June 2018, it writes as 04 June 2018.
I've checked related model and widget itself but nothing seems weird! Why this could be?
Here is model statement and field;
Ext.define('MyApp.FooModel', {
extend: 'Ext.data.Model',
fields: [
{name: 'mydatefld', type: 'date', dateReadFormat: 'c', dateWriteFormat: 'Y-m-d'},
//and implementation
Ext.define('MyApp.BaseDateFld', {
extend: 'Ext.form.field.Date',
xtype: 'basedatefld',
format: 'd.m.Y',
flex: 1,
labelAlign: 'right',
name: 'MyDate Fld',
fieldLabel: 'MyDate Fld',
bind: '{currRec.mydatefld}'
});
Each time saves date as on XHR request payload;
2018-06-05T21:00:00.000Z //But should be 2018-06-06T06:05:00.000Z
Update:
I've tried change dateWriteForm to 'Y-m-d H:i:s' and 'Y-m-d H:i' but still noting changes on payload and keep decrease one day and sets time to 21:00:00
As well tried to change compouter TS to some another as #Alexander adviced but nothing changed.
Update 2:
I've over come the current issue but really a very DRY solution, so not safe!
Below there is insertion method (update method is almost same as this) and formating the related date value on here, thusly became success.
Server accepting the date format for this field as Y-m-d, so I've stated dateWriteFormat on model and submitFormat on datefield as 'Y-m-d' but it keeps write the date value with timestamp.
When I check rec param on method it is as 2018-06-06T21:00:00.000Z(The TZ part shouldn't be here!). And store param changes the givin date one day off as 2018-06-05T21:00:00.000Z.
Still not sure why I can't convert/format through model or field.
Thanks for advices.
recInsertion: function (rec, store) {
store.getProxy().url = store.getProxy().api.create;
rec.data.givindate = Ext.Date.format(rec.data.mydate, 'Y-m-d'); //This is current solution to format date! Which is really not safe and will cause DRY.
Ext.Ajax.request({
url: store.proxy.url,
method: 'POST',
jsonData: JSON.stringify(rec.data),
callback: function (options, success, response) {
Ext.GlobalEvents.fireEvent('showspinner');
if (success) {
Ext.GlobalEvents.fireEvent('refreshList');
}
else
Ext.GlobalEvents.fireEvent('savefailed');
}
});
},

Mongoose or MongoDB removing 24 hours from date?

I have to be doing something wrong but I cannot figure out what it is. I currently have a date field in my application that has a value format of MMMM, D YYYY.
For this example, I am going to be using the date October 1, 2018.
In the application, the user selects October 1, 2018 which logs as
October 1, 2018
Later I convert this date into a savable date by using var docDate = new Date($('#insuranceExpiration').val()).toISOString(); which logs as
2018-10-01T04:00:00.000Z
I then add this to an object stringify it and send it to the server where I log the date and shows as
2018-10-01T04:00:00.000Z
I then use Mongoose to save this date to MongoDB but when I go to see the saved date I show
2018-09-30T04:00:00.000Z
I cannot figure out for the life of me why I am seeing a date 24 hours in the past from the date I choose. Below is the mongoose code to see if I am doing something wrong on that end.
app.post('/api/myDocuments/insuranceGeneral', function(req, res ) {
console.log(req.body.expires);
// prints 2018-10-01T04:00:00.000Z
InsuranceGeneral.findOneAndUpdate({
_id: req.rsaConPortal.id
}, {
$set: {
"documents.insurance.name": req.body.name,
"documents.insurance.expires": req.body.expires,
"documents.insurance.url": req.body.url,
"documents.insurance.uploadDate": req.body.uploadDate
}
}, function(err, doc) {
if (err) throw err;
res.send('Success');
});
});
Even if I use a standard javascript date object I can print this on the browswer log
Mon Oct 01 2018 00:00:00 GMT-0400 (Eastern Daylight Time)
This prints on the sever just before mongoose
2018-10-01T04:00:00.000Z
But this is the end result in mongodb
2018-09-30T04:00:00.000Z
I think you and your server in different timezone. And mongodb uses GMT. If you are not declaring time in your iso date, it is setting it's time as 00:00.000Z. So for GMT this is one day earlier. Simply you should convert your date to GMT, you can use moment or you can fix problem by subtraction offset from date like this:
var offset = new Date().getTimezoneOffset() * 60000;
var date = new Date(new Date($('#insuranceExpiration').val())) - offset).toISOString();
or with moment.js
var date = moment($('#insuranceExpiration').val()).tz('GMT').format('YYYY-MM-DD');

Meteor new Date() invalid with mongodb 3.0.1 and autoform/simple schema

I've trouble dealing with a type: Date into SimpleSchema having a defaultValue.
In mongodb (3.0.1), the date entry have the time the meteor thread have been launch. The expected behaviour is to have "date" the insertion date of the object on the server.
lib/schema.js
Schema.DateCol = new SimpleSchema({
date: {
type: Date,
defaultValue: new Date()
},
dateModified: {
type: Date,
autoValue: function () { return new Date(); }
}
});
client/home.html
{{> quickForm id="test" schema="Schema.DateCol" collection="DateCol" type="insert" }}
Into the mongo, after inserting two objects:
meteor thread launch at "Wed May 20 2015 12:28:42 GMT+0200 (CEST)"
both objects have been inserted at one minute of interval: the first at
"Wed May 20 2015 12:30:50 GMT+0200 (CEST)" and the second at "Wed May 20 2015 12:31:30 GMT+0200 (CEST)"
the date field (defaultValue) for both object is: "Wed May 20 2015 12:28:42 GMT+0200 (CEST)"
Object 1
{
"_id": "PuME9jWwJJiw9diSC",
"date": new Date(1432117722634),
"dateModified": new Date(1432117850366)
}
Object 2:
{
"_id": "qqHqkN4YapWDsFhxx",
"date": new Date(1432117722634),
"dateModified": new Date(1432117890380)
}
You'll fin enclose a repository Github with the error, using MongoDB 3.0.1 (I haven't this error on MongoDB 2.4):
https://github.com/JVercout/meteor-defaultValue-date-errored
Any ideas?
The problem is that the new Date() expression is evaluated once when the code creating the schema is run. That value is then used as the defaultValue. There is no difference between:
var x = new SimpleSchema({
date: {defaultValue: new Date(), ...}
});
and
var defaultDate = new Date();
var x = new SimpleSchema({
date: {defaultValue: defaultDate, ...}
});
It looks like you'll need an autoValue, since it doesn't look like you can use a function for defaultValue. The Collection2 documentation actually has an example of using autoValue for a "created at" field. It depends on fields added by Collection2 but I saw in your git repo that you're using that.
// Force value to be current date (on server) upon insert
// and prevent updates thereafter.
createdAt: {
type: Date,
autoValue: function() {
if (this.isInsert) {
return new Date();
} else if (this.isUpsert) {
return {$setOnInsert: new Date()};
} else {
this.unset();
}
}
}

sailsJS .exec() within forloop doesn't work

i have a model something like this:
\**
* user.petIds = ["1","2","3","4"];
*\
getPets: function(users){
var response = [];
users.forEach(function(user){
Pet.find(users[i].petIds).exec(function(err, pets){
if(err) return null;
console.log("index: "+i);
response.push({ name: users[i].name, pets: pets});
});
});
return response;
}
I want to get every pets from an array of pet ids for an array of users. However, an exec only executes after a callback which in this case causes a return prematurely. Is there anything in Sails which solve this problem? Otherwise it's impossible to use a for loop to query. I'm still very new to the way how JS works. Any help is appreciated. Thanks (:
This isn't a Sails issue, it's a general issue with Node which you'll encounter as you learn more about asynchronous programming. If you search StackOverflow for "node.js asynchronous loop" you'll find dozens of questions and answers. The problem is that you're calling an asynchronous function (.exec()) inside a synchronous for loop, so all of the iterations of the loop (and the return response) occur before any of the actual callbacks (the function(err, pets) functions) are called.
The easiest way to solve this is to use a library like async to run the loop asynchronously. It's for this reason that async is included and globalized by Sails by default, so you can do:
// Note the new second argument to "getPets", since this is
// an asynchronous function it requires a callback
getPets: function(users, getPetsCb){
// Asynchronous "map" call to transform users
async.map(users, function iterator (user, mapCb){
// Get the user's pets
Pet.find(user.petIds).exec(function(err, pets) {
// In case of error pass it to the callback, which
// will abort the loop
if(err) return mapCb(err);
// Use "null" as the first arg to indicate no error has
// occurred, and pass the transformed user as the second
// to add it to the array
return mapCb(null, { name: user.name, pets: pets});
});
},
// Pass the outer callback as the 3rd argument to async.map
// to pass the final error state and array of transformed
// users to whoever called "getPets"
getPetsCb);
}
Read the full docs for async.map here, as well as all the other fun async methods.
Also note that in your particular case you could use Sails associations to model the relationship between User and Pet and get all this information with one query: User.find(...).populate('pets').exec(...)!
If you use Sails .10 or greater you can use model associations to handle this by approaching the problem a bit differently. In fact they detail a very similar use case in the documentation.
If you setup your models in this way:
myApp/api/models/pet.js
module.exports = {
attributes: {
name:'STRING',
// include whatever other attributes
owner:{
model:'user'
}
}
}
myApp/api/models/user.js
module.exports = {
attributes: {
name:'STRING',
// other attributes you may want included here
pets:{
collection: 'pet',
via: 'owner'
}
}
}
Then in your controller you can use the following to find all the pets for given users:
getPets: function(req, res) {
User.find()
.where({id: users})
.populate('pets')
.exec(function(err, users) {
if (err) throw err; // do error handling here
return res.json({
users: users
});
});
}
By doing this the JSON that is sent back to you has each user, and all of that user's pets as shown here.
{
users:
[{
pets:
[ { name: 'Spot',
id: 2,
createdAt: Tue Feb 11 2014 17:58:04 GMT-0600 (CST),
updatedAt: Tue Feb 11 2014 17:58:04 GMT-0600 (CST),
owner: 1 },
{ name: 'Sparky',
id: 4,
createdAt: Tue Feb 11 2014 18:02:58 GMT-0600 (CST),
updatedAt: Tue Feb 11 2014 18:02:58 GMT-0600 (CST),
owner: 1 } ],
name: 'Mike',
createdAt: Tue Feb 11 2014 17:49:04 GMT-0600 (CST),
updatedAt: Tue Feb 11 2014 17:49:04 GMT-0600 (CST),
id: 1
}]
}
You can then access an individual users pets using the returned JSON.