How to change gorm.Record type to array - go

currently I am using gorm to retrieve data from db (postgresql db to be specific) and scan it in an array, the data stored in db is also in form of array. So problem I am facing is after scanning data in empty int array it changes into gorm.Record type which can't be used for basic array operation like, appending, iterating, etc.
Here's a related part of my code:
var winner_selection []int64 // empty int64 array
db.Table("giveaways").Select("Participants").Where("Name = ?", name).Scan(&winner_selection) // scanning the value in array
How can I retrieve the data directly in form of array so the array remains array or is there anyway to change gorm.Record type to array?

Related

In flask_marshmallow Can you declare a schema to return an array of objects

So I have this schema that I want to return a list of objects, not as a field with a list of objects which I successfully can and do return. Just the array.
Is it possible?

Type error with Hasura array data type: "A string is expected for type : _int4"

I have a table I created in the Hasura console. A few of the columns are integer int types and I get the expected data type: Maybe<Scalars['Int']>.
However, a few needed to be an array of integers so I created those in the Hasura SQL tab:
ALTER TABLE my_table
add my_ids integer[];
If I populate those in GraphiQL with the following query variable everything works just fine:
{
"my_ids": "{1200, 1201, 1202}",
}
However, when I try to make the same request from my front-end client, I received the following error: A string is expected for type : _int4. Looking at the datatype, it is slightly different than the preset (in the data type dropdown) integer types: Maybe<Scalars['_int4']>.
Is there a way to get the array of integers to be Maybe<Scalars['Int']> like the preset integer types? Or if there isn't, how can resolve the issue with my request throwing errors for this _int4 type?
Hasura cannot process array but it can process array literals.
I've written a function to help you to transform your array into array literal before mutation:
const toArrayLiteral = (arr) => (JSON.stringify(arr).replace('[', '{').replace(']', '}'))
...
myObject.array = toArrayLiteral(myObject.array) // will make ['friend'] to {'friend'}
Good luck

Laravel Array to string conversion error while updating database

I want to update a totcosty field in the User table but it is throwing this error everytime and it is not updating the field
this is the function for execution:
public static function cost(){
$user = User::find($user_id);
$total = Helper::totcost();
// dd($tot_amt);
$user->totcosty = $total;
$user->save();
}
array to string means you are sending an array to the database but db will not accept it you have to explode() the array before sending it to db...
Hope it will help!
If you really want to store an array in some table field, then better declare it as a JSON field. For this, your DB should have support for JSON type columns.
See here how to do this.
Once this is done, you can save arrays in that column, you can assign an array value to the model property and laravel will convert it to JSON while saving and also it will be converted to array while retrieving.

How to update nested redux state

I have the following structure
lookups --> Object
lookups.CATEGORIES --> array of category objects
lookups.TIMEZONES --> array of timezone objects
I would like to add new object, which is a lookup object which has lookup_type property. It could be either 'CATEGORY' or 'TIMEZONE'.
Depending on lookup_type, the newly added object has to be added either to CATEGORIES or TIMEZONES object. How this could be achieved?
The structure of lookups object
lookups: {CATEGORIES:[{obj1}, {obj2}...], TIMEZONES:[{obj1}, {obj2}, {obj3}...]}
You can use spread on the nested object or array too:
return {
...state,
[action.lookupType]: [
...state[action.lookupType],
action.value,
]
};
That would add a new item to Categories or Timezone, if you want to replace a value or insert it at an index etc then you should construct the new array how you want it just above the return and pass that instead. Also note that array spread is ES7.
You probably want to pass your lookup object as the payload of an action, which your lookup reducer handles. In the lookup reducer check for the value action.payload.lookup_type and return the state with a new CATEGORIES or TIMEZONES array containing the old values of that array with the lookup object insterted. You should probably check out some redux examples first, if you are unsure how to work with it.

LevelDB key,value from csv

I've huge csv file database of ~5M rows having below fields
start_ip,end_ip,country,city,lat,long
I am storing these in LevelDB using start_ip as the key and rest as the value.
How can I retrieve records for keys where
( ip_key > start_ip and ip_key < end_ip )
Any alternative solution.
I assume that your keys are the hash values of the IP and the hashes are 64-bit `unsigned' integers, but if that's not the case then just modify the code below to account for the proper keys.
void MyClass::ReadRecordRange(const uint64 startRange, const uint64 endRange)
{
// Get the start slice and the end slice
leveldb::Slice startSlice(static_cast<const char*>(static_cast<const void*>(&startRange)), sizeof(startRange));
leveldb::Slice endSlice(static_cast<const char*>(static_cast<const void*>(&endRange)), sizeof(endRange));
// Get a database iterator
shared_ptr<leveldb::Iterator> dbIter(_database->NewIterator(leveldb::ReadOptions()));
// Possible optimization suggested by Google engineers
// for critical loops. Reduces memory thrash.
for(dbIter->Seek(startSlice); dbIter->Valid() && _options.comparator->Compare(dbIter->key(), endSlice)<=0); dbIter->Next())
{
// get the key
dbIter->key().data();
// get the value
dbIter->value().data();
// TODO do whatever you need to do with the key/value you read
}
}
Note that _options are the same leveldb::Options with which you opened the database instance. You want to use the comparator specified in the options so that the order in which you read the records is the same as the order in the database.
If you're not using boost or tr1, then you can either use something else similar to the shared_ptr or just delete the leveldb::Iterator by yourself. If you don't delete the iterator, then you'll leak memory and get asserts in debug mode.

Resources