Sequelize/ MySQL table:
id: integer,
timestamp: Date
anotherField: string
I am upserting with a new document:
{ id: alreadyInTheDatabase, timestamp: reallyOldValue, anotherField: "newValue" }
I need to update timestamp but only if it is older (smaller) then the one upserting the database, anotherField needs to be always updated.
What is the best approach for it?
Thank you! :-)
Related
I am trying to insert a date\time value in the format YYYY-MM-DD HH:mm:ss in MySQL DB table using Sequlize as ORM in a nodejs (Expressjs middleware). For some odd reason the time element gets stored with an incorrect time. For example, I try to insert 05:50 PM but the DB column post insert shows 06:12 pm. Any idea how to fix this?
My model has below definition for the datetime column
time_stamp: {
type: DataTypes.DATE,
allowNull: false,
},
My DB table has below definition for the field
time_stamp DATETIME NOT NULL,
This is one of the models from my schema:
model playlist {
id Int #id #default(autoincrement())
list String #db.VarChar(34)
duration DateTime? #db.Time(0)
}
And I would like to add a time value when I am creating a new record. But it seems that I can only add value with the DateTime type.
I tried just passing a String but that doesnt work and I get and error.
This is the error:
Argument duration: Got invalid value '00:01:29' on prisma.createOneplaylist. Provided String, expected DateTime or Null.
Is there a way to only add the time or am I forced to use DateTime format?
You would always need to send a Date object in this case. For e.g. this is how I would set the time using date-fns.
import set from 'date-fns/set'
await prisma.playlist.create({
data: {
list: 'list',
duration: set(new Date(), { hours: 1, minutes: 10 }),
},
})
A similar parsing mechanism can be used when you retrieve the time.
There is a column in a table as
table: articles
rows: id type: int(11)
json_data type: json
created_at type: datetime
updated_at type: datetime
Now the json_data is
{
"version":"1",
"title":"A good product",
"body":"Very good",
"published_at":null
}
Want to update its data to new json schema format based on the current meta data:
{
"version":"2",
"items":[
{
"title":"A good product",
"body":"Very good",
"published_at":null
}
]
}
How to do it if not use a programming language. Is it possible to do it by MySQL's procedure?
UPDATE articles
SET json_data = JSON_OBJECT('version', 2,
'items', JSON_ARRAY(JSON_REMOVE(json_data, '$.version')));
fiddle
If run the update command twice, the data will become nested and break the format - fiddle. Do you know how to avoid it even run many times? – iooi
Add proper WHERE, check "$.version", update only those rows where this value is 1:
UPDATE articles
SET json_data = JSON_OBJECT('version', 2, 'items', JSON_ARRAY(JSON_REMOVE(json_data, '$.version')))
WHERE json_data->>"$.version" = 1;
fiddle
My goal is to get the statistics count of data through timestamp, some what like google analytics. I'm using node.js & sequelize The timestamp field in model looks
request_timestamp: {
type: DataTypes.STRING,
allowNull: false
},
For now, I want to get data grouped by day of every timestamp. I tries date_truc but it's not working for some reason.
I have a model called Encryption which generates an AES encrypted string and saves that to a database table called encryptions. My encryption table schema is as follows:
id : bigint unsigned
user_id : bigint unsigned
encryption : VARCHAR(128) (utf8_unicode_ci)
created_at : datetime
updated_at : datetime
However, when I call .save on my encryption model instance, the value for encryption.encryption (in the database) is blank ('').
Here's the contents of the model:
ruby-1.9.2-head :005 > encryption
=> #<Encryption id: nil, user_id: 1, encryption: "\xD6\xD6\x95\x15\x0F\x92\xC6\x01\x86\x1E\x88\xD1\xB0\x1D\xE0\xEC", created_at: nil, updated_at: nil>
As you can see, it does have a value in the model. Any ideas on why the field is saving as blank in the database?
I'm using the strongbox gem to do something similar, and it requested a column type of :binary, not text, maybe you could try that...
You appear to be saving a string into a bigint column, which won't work. Switch it to a text column type.