RESTKit: Mapping inconsistency - core-data

Below is the log of mapping an object with RESTKit. startDate and modified both are NSDate in CoreData. The mapping is identical for both. startDate maps fine and the value is shown, but modified becomes as nil, 0 or 1. As you can see in the Log below that the value of modified fetched in the GET response is not nil, 0 or 1.
Invite.h
#property (nonatomic, retain) NSDate * modified;
#property (nonatomic, retain) NSDate * startDate;
Log
2014-06-22 09:34:15.343 App[86722:3703] T restkit.object_mapping:RKMappingOperation.m:550 Mapping attribute value keyPath 'startDate' to 'startDate'
2014-06-22 09:34:15.344 App[86722:3703] T restkit.object_mapping:RKMappingOperation.m:431 Found transformable value at keyPath 'startDate'. Transforming from type '__NSCFString' to 'NSDate'
2014-06-22 09:34:15.348 App[86722:3703] T restkit.object_mapping:RKMappingOperation.m:572 Mapped attribute value from keyPath 'startDate' to 'startDate'. Value: 2014-06-20 13:30:00 +0000
2014-06-22 09:34:15.350 App[86722:3703] T restkit.object_mapping:RKMappingOperation.m:550 Mapping attribute value keyPath 'comment' to 'comment'
2014-06-22 09:34:15.352 App[86722:3703] T restkit.object_mapping:RKMappingOperation.m:572 Mapped attribute value from keyPath 'comment' to 'comment'. Value: This is a comment
2014-06-22 09:34:15.358 App[86722:3703] T restkit.object_mapping:RKMappingOperation.m:550 Mapping attribute value keyPath 'modified' to 'modified'
2014-06-22 09:34:15.360 App[86722:3703] T restkit.object_mapping:RKMappingOperation.m:431 Found transformable value at keyPath 'modified'. Transforming from type '__NSCFString' to 'NSDate'
2014-06-22 09:34:15.363 App[86722:3703] T restkit.object_mapping:RKMappingOperation.m:572 Mapped attribute value from keyPath 'modified' to 'modified'. Value: 2014-06-19 06:16:55 +0000
2014-06-22 09:34:15.392 App[86722:3703] D restkit.object_mapping:RKMappingOperation.m:1021 Finished mapping operation successfully...
2014-06-22 09:34:15.393 App[86722:3703] D restkit.object_mapping:RKMapperOperation.m:231 Asked to map source object {
comment = "This is a comment";
modified = "2014-06-19T07:28:50.000Z";
startDate = "2014-06-21T07:15:37.000Z";
} with mapping <RKEntityMapping:0xd766f10 objectClass=Invite propertyMappings=(
"<RKAttributeMapping: 0xd951310 startDate => startDate>",
"<RKAttributeMapping: 0xd951610 comment => comment>",
"<RKAttributeMapping: 0xd951620 modified => modified>",
)>
2014-06-22 09:34:16.009 App[86722:3703] D restkit.object_mapping:RKMapperOperation.m:403 Finished performing object mapping. Results: {
meetings = (
"<Invite: 0xdb596a0> (entity: Invite; id: 0xdb34ee0 <x-coredata:///Invite/t8D98B490-5CEC-4EB6-9AD6-533674DA06663> ; data: {\n comment = \"This is a comment\";\n modified = 1;\n startDate = \"2014-06-20 13:30:00 +0000\";\n})",
I can't figure out why startDate returns the correct value, and modified does not.

modified appears to be a keyword for RESTKit. I changed the attribute in Core Data to modifiedDate and it seems to work fine.

Related

Json object with duplicate keys in node js

I have an array ["10","20"]
from this array i need to form an object as below. The elements in the array should point to "id" field in the json obejct. If there are more elements in the array that many times id field should come in the response.
input: ["10","20"]
expected response
{
id: "10",
id: "20:,
value1: true,// this key and value need to be hardcoded
value2: false //this key and value need to be hardcoded
}
I tried using the below option but it is not allowing duplicate keys in the response
var mapped = array.map(item => ({ "id": item.key }) );
console.log(mapped)
var newObj = Object.assign({}, ...mapped );
It will be a great help if anyone can help with this
You cannot have duplicate keys in Object
Every keys in object are unique while the values can be duplicate but not the keys
So if you have key id:'10' then the next time any value with obj["id"]=20 will overwrite the value 10

How can I allow an empty string value for optional enum with NestJs?

I am successfully validating my dto for valid enum types:
// time-unit.enum.ts
export enum TimeUnit {
SECONDS = 'SECONDS',
MINUTES = 'MINUTES',
HOURS = 'HOURS',
DAYS = 'DAYS',
}
// create-thing.dto.ts
#ApiPropertyOptional({
description: 'The lead time unit.',
example: 'DAYS',
})
#IsOptional()
#IsEnum(TimeUnit)
unit?: TimeUnit;
On the front-end, I am providing a <select> that is populated with an empty string for default value, then the corresponding enum values.
If I choose a value, everything works great! This is an optional field (a nullable column). So If I attempt to save without choosing something, I'll get a 400 error:
leadTime.unit must be a valid enum value
How can I allow an empty string as a valid enum option?
The best way is to transform the the value if its equal to empty string
// create-thing.dto.ts
#ApiPropertyOptional({
description: 'The lead time unit.',
example: 'DAYS',
})
#Transform((params) => (params.value === '' ? null : params.value))
#IsOptional()
#IsEnum(TimeUnit)
unit?: TimeUnit;

mongodb query referenced objects inside array

I'm using mongoose in nodejs. I'm trying to query some fields that contains a list made of referenced objects. I created two schemas
PILOT
var PilotSchema = new mongoose.Schema({
races: [{type: mongoose.Schema.Types.ObjectId, ref: 'Race'}],
});
RACE
var RaceSchema = new mongoose.Schema({
start_ms: Number,
});
Here I wrote only the relevant fields.
In the race schema I have a key start_ms that indicates the date of the start of the race in millisecons, and in the PilotSchema races field I save the references to many races.
What I want to do is:
Take a date in the future ( my_date ) in milliseconds
Retrive all the pilots that have not a race in the interval (my_date - 2_hours, my_date + 2_hours)
I try to do an example for explain better.
My date: Feb 23 2020 - 12:00:00 ( in milliseconds ),
In the pilot field races I have many refences to the RaceSchema and in each refenced object ther is the time of the start of a race.
Now querying the Pilots collection I want to retrieve all the pilots that in they races field have not a race that start during (Feb 23 2020 - 10:00:00 - Feb 23 2020 - 14:00:00)
I tried using this query but didn't work
races: {$not:
{$elemMatch:
{start_ms:
{ "$gt" : min_time, "$lt" : max_time }
}
}
}
Where
min_time, max_time = my_date - 2_hours, my_date + 2_hours
Actually I don't know how to do it, I saw that for retrive a number in an intervall I can use $gt and $lt but I don't understand how to query an array of referenced objects that respect this specific conditions.
You'd have to write an aggregate query for this, finding your reference of races from pilot & filter them based on your condition
Pilot.aggregate([
{
$lookup:{
from:'races',
localField:'races',
foreignField:'_id',
as:'races'
}
},{
$unwind:'$races'
},
{
$match:{
'races.start_ms':{
"$gt" : min_time, "$lt" : max_time
}
}
},{
$group:{
_id:'$_id',
name:{$first:'$name'}
}
}
]);
Assuming that you have name in your pilot schema, you can get list of all such pilots by writing this query

Upsert into Mongo based on date?

obj = {
date: 137097408891,
id: '1234',
value: 'value'
}
What I want to do it apply the value field to the object with the id, only if the date is newer then the current one saved. If no document with that ID is saved, just save this one.
I'm using node-mongodb-native. Is there a way to do this without first getting the document checking the date and saving it again?
Thanks
Given object o
db.collection.update( { id:o.id, date: { $lt:o.date } }, {$set : { o }}, {upsert:true} );
This is assuming there is a unique index on id.

Data modelling in Cassandra

I am trying out Cassandra and looking at ways to model our data in it. I have described our data store requirements along with my thoughts on how to model in Cassandra. Please let me know whether this makes sense and suggest changes.
Did quite some search on the web, but didn't get clear idea regarding how to model multi-valued column requirements and index it, which is quite a common requirement.
Any help would be greatly appreciated.
Our current data for each record:
{
‘id’ : <some uuid>,
‘title’ : text,
‘description’ text,
‘images’ : [{id : id1, ‘caption’: cap1}, {id : id2, ‘caption’: cap2}, ... ],
‘videos’ : [‘video id1’, video id2’, …],
‘keywords’ [‘keyword1’, ‘keyword2’,...]
updated_at: <timestamp>
}
Queries we would need
Lookup by id
Lookup by images.id
Lookup by keyword
All records where updated_at >
Our current model
Column Family: Article
id: uuid
title: varchar
description: varchar
images:
videos:
keywords:
updated_at:
updated_date: [eg: ‘2013-05-06:02’]
Column Family: Image-Article Index
{
‘id’ : <image id>,
‘article1 uuid’ : null,
‘article2 uuid’ : null,
...
}
Column Family: Keyword-Article Index
{
‘id’ : <keyword>,
‘article1 uuid’ : null,
‘article2 uuid’ : null,
...
}
Sample queries:
Lookup by id => straight forward
Lookup by images.id =>
ids = select * from ‘Image-Article Index’ where id=<image id>
select * from Article where id in (ids)
Lookup by keyword =>
ids = select * from ‘Keyword-Article Index’ where id=<image id>
select * from Article where id in (ids)
All records where updated_at > <some timestamp>
Cassandra doesn’t support range queries unless there is one equality condition on one of the indexed columns.
extract date and hour from given timestamp;
for each date:hour in start to current time
ids = select * from Article where update_date=date:hour and timestamp > <some timestamp>
select * from Article where id in (ids)

Resources