Inserting an array of json to hasura - javascript

I am trying to insert an array of objects to my hasura table. I have defined my columns like the image below
But I am receiving malformed array literal: \"[]\" error. I am using JSON.stringify from my client side code to stringify my array before calling the mutation. What am I doing wrong?

I'd highly recommend that you just make the column a jsonb and then store the array directly. jsonb querying capabilities are significantly improved over json and Hasura doesn't have the greatest support for array columns (of any type).
When submitting data to an array column with Hasura it expects to receive it as a string using the PG array literal syntax, eg '{1, 2, 3}' for an int[] column.

Related

MongoDB array turned into string on database

I have problem with Mongo/express array parsing. A array from body seems to be correct when I console.log response in node console. But the problem is when I try to save the body to mongo.db using insert.one method, the post is saved but the array turns into string, which is bad for me.
This is what I'm sending to mongo (the data is ok, i console.log it, array is not string here)
{
createdBy: this.userName,
postContent: this.post,
tags: this.tags,
addedAt: new Date()
};
And this is what is stored in database
Update:
When I hardcode array into payload it's showed correctly as an array in mongo.
But of course problem still exist for dynamic data
You should be able to use JSON parse along with a replace to force it to accept the string as an array, like this:
const array = JSON.parse(this.tags.replace(/'/g, '"'));
Then set tags to the value of array:
tags: array
Use data type as array -- 4 and use .pretty().
Generally if you use find() method returns data in a dense format.
By using cursor.pretty() you can set the cursor to return data in a format that is easier for humans to parse

Passing Javascript Object Array to MVC Controller w/ POST

So I have an array of javascript objects that is constructed using JQuery across multiple table rows. It has the following form:
obj_rows = [
{"param1":value11, "param2":value12, "param3":value3},
{"param1":value21, "param2":value22, "param3":value23},
//more objects with the same parameters
]
What I would like is a way for an AJAX POST request to a Controller method SomeController/SomeAction that takes in this array and can iterate over all objects. I was trying to figure out a way to do this using JSON, but to no success.
NOTE: Even though I am using MVC, I do not have a model class corresponding to these objects. Ideally, I would like to end up with some structure like a List of 3 Tuples.
actually the solution is to take the object and stringify it into a string and then send it over the ajax request.
just do:
JSON.stringify(obj_rows)
and you will get on the other endpoint an array (stringified of course), you have to parse it back and you will have your array.
You can use javascript method JSON.stringify() to convert you object into a string and assign its result to a hidden field in your view and once you recieve this in your controller you can then deserialize it to an object or parse it yourself.

Search an array using massivejs

I'm trying to construct a query on an array using massivejs, but it keeps telling me the operator is unsupported.
This query works:
SELECT * FROM my_table WHERE data->'items' #> '[{"foo": "bar"}]';
where data is a jsonb field and items is an array of objects. My massivejs query is:
{ 'data #>> {items} #>': '[{ \"foo\": \"bar\" }]' }
but massive tells me the #> operator doesn't exist.
I realize I can execute raw SQL, but I'm building up a query with paging, sorting, and other query conditions, so I'd rather not rebuild all that if I can avoid it.
Is there a mistake in my query? Is this something massivejs even supports?
I don't think massivejs supports jsonb operators.
Your query is SQL-correct. And I guess that you'll have to do raw SQL on this one.
I just read the API docs and it goes in my direction but I might be overlooking something.

Is List<string> guaranteed to be in correct order when serialized to JSON?

Is there any condition where the serialized JSON array is not in the same order as the .NET List that was originally serialized?
Assuming you are using JSON.NET and default JSON Serializer settings, your list will be serialized to a JavaScript array of strings in the same order as per the JSONConvert.SerializeObject() method code found here.
https://github.com/JamesNK/Newtonsoft.Json/blob/master/Src/Newtonsoft.Json/JsonConvert.cs

How to use typed arrays to simulate objects?

Objects in JavaScript contain key-value pairs. The cost of a typical pair (using DevTools Profiler) is a reference to the key name, 8 bytes, and the cost of the object: 4 bytes for small ints, 8 bytes for numbers, references, etc.
The cost of the keys add up, especially in arrays with millions of objects.
Is there a asm.js sort of way to use typed arrays for arrays of identical objects?
Yes, I know this seems like a pain, but for a particular project, this may be required.
The sort of approach I'm thinking of is using a template JS object who's keys describes the offset into a typed array for each key's value, along with its type. For arrays of these objects, there'd be multiple of these object spans.
Thus two questions:
1 - Are my assumptions correct .. and there is no optimization in chrome/modern browsers that optimize the key costs? Possibly with constraints used here: http://www.2ality.com/2013/08/protecting-objects.html
2 - If so, is there a library for handling typed arrays as objects? Or any articles or gists etc?
If you have millions of objects, all with the same set of known keys and you have so many of them that memory is an issue, then you probably don't want to store your data as javascript objects at all.
You probably want to think about this like a database problem. You want:
A semi-compact storage format
A way to find the right record in the storage format (this depends upon what the data is and how you need to access it.
A way to read the semi-compact storage format and turn it into a live javascript object only when you want to actually use that object in your code.
A way to write changes back to the semi-compact storage format.
For example, if you had seven keys (e.g. fields) and three were numbers and four were strings and one of the numbers was your lookup key, then you could do this:
Create three typed arrays (one for each numeric value)
Create one regular array. This will hold all the string values concatenated together with a unique separator character.
Create a master key lookup object
As you read your data in, presumably from multiple ajax calls, you do the following:
Note the length of one of your arrays (they are all the same length) as this will be your new record number.
Add the lookup key to the master key object and set the value of the key to be the record number.
Add each numeric value to each typed array (each will be the record number index in the array)
Concat all the string values together with a separator between them in order by key and then put the contactenated string value into the string array.
Now you have a semi-compact storage format and a key lookup means. When you want to lookup a value, you use the master key lookup object. The value for the key will be the record number which is an index into the other arrays. You can create two functions that will find a record and return a javascript object form of the record (all data in key/value pairs on the object) and another function that will write an object (that might have changed = but the master key can't change) back to your storage format.
This makes a few assumptions about your data that you have one master key that won't change that you use for lookup and that you can find a separator to bind all the string values together and then separate them apart later and that you can know when you go to store all this and that you know what the keys are and that the objects generally all have the same keys.
If any of those assumptions are not true, then the design would have to be adapted to deal with that, but hopefully you get the idea of using something other than a giant array of objects to store your data and then constituting a given object only when you need to work with that record's data.

Categories

Resources