Sails.js: how to do updatedDate for records? - mysql

I am trying to figure out how to get an updatedDate record to save into entries on my basic MySQL-enabled CRUD application. I have this in my models/user.js for the createdDate:
createdData: {
type: 'string',
columnType: 'datetime'
}
I wanted something like this for the updatedDate:
createdData: {
type: 'string',
columnType: 'datetime',
value: date(),
}
According to the documentation, there is a function within Sails.js for updatedAt but this doesn't specify which field it sends that date(); calculation to. Is there something I am missing for creating an updatedAt column for updated records?

createdAt and updatedAt (along with ID) are base attributes that are included in all of your models by default. That means updatedAt is its own field created automatically by Sails.
updatedAt simply marks the most recent time any changes were made to that entry.
If you want to change the format this data is saved in you can do that in your config/models.js file.

Related

The user entered date changes when stored in mongodb, how to store it in original state?

I am making a practice project on a restaurant website that has an option of table reservation. The date-time entered in the form by the user changes when stored in MongoDB due to the UTC time-saving system of MongoDB. In India, the time is UTC+5:30 so it changes accordingly. How do I modify the time entered by the user so that it can be saved in its original state? I am new to MongoDB and mongoose so I am not well versed with the techniques which can be used.
Eg: Suppose the user entered the date-time as 26-5-22 8:27 pm
In mongodb, it saves as ISO(2022-05-26T14:57:00Z)
MY js code for MongoDB is below:
const mongoose = require('mongoose');
main().catch(err => console.log(err));
async function main(){
await mongoose.connect('mongodb://localhost:27017/HamdansEatery');
}
const reservationSchema= new mongoose.Schema({
fname: String,
lname: String,
tel: Number,
email: String,
no_of_persons: Number,
date_time: Date
})
const Reservation = mongoose.model("Reservations",reservationSchema);
//----------------------------------------->
app.post('/reserve_a_table', (req,res) => {
let yourData = new Reservation(req.body);
yourData.save().then(()=>{
res.send("Data has been saved successfully!");
}).catch(() => {
res.status(400).send("Data not saved due to some issues.");
})
})
The frontend is HTML form with (input type = datetime-local)
I saw some related questions but couldn't find one for a user-entered date and time.
How should I progress? How can I modify the Date SchemaType such that it can in a way add 5:30hrs while saving in MongoDB so I can get the result I want i.e. time in Indian Standard Time?

Enable only createdAt time stamp and ignore updatedAt timestamp in Sequelize

I am trying to create some records into table with only the createdAt column filled. I want the updatedAt column to not exist how ever when i create a model it automatically generates the createdAt and updatedAt timestamps.
I tried to use timestamps : false but the createdAt column contains empty values.
const MyTable = sequelize.define(
'my_table',
{
id: {
type: DataTypes.INTEGER(11),allowNull: false,
primaryKey: true,autoIncrement: true,
},
person_name: {type: DataTypes.STRING(255),allowNull: false},
created: {type: DataTypes.DATE,allowNull: true},
},
{
tableName: 'my_table',
timestamps: false,
createdAt: 'created',
},
);
Is it possible to fix this issue in the model itself without doing any change in the query ?
According to the documentation on Init, you'll wanna leave the timestamps feature turned on, but configure your model to not use updatedAt.
Here's an edited version of the options object in your sample code that should give you what you are looking for:
{
tableName: 'my_table',
updatedAt: false,
}
NB: The docs say "Timestamps must be true", which might indicate why it wasn't working the way it is in the sample you provided.
Cheers!

Not store updatedAt with sequelize model

When I store models in my MySQL DB, they are immutable. As a result I can see the need for the createdAt column in my tables, but I don't need the redundant updatedAt column. Can I configure sequelize not to store updatedAt time and then can I drop the column from my table?
Looking at the documentation regarding your situation
If you want sequelize to handle timestamps, but only want some of them, or want your timestamps to be called something else, you can override each column individually:
const Foo = sequelize.define('foo', { /* bla */ }, {
// don't forget to enable timestamps!
timestamps: true,
// I don't want createdAt
createdAt: false,
// I want updatedAt to actually be called updateTimestamp
updatedAt: 'updateTimestamp',
// And deletedAt to be called destroyTime (remember to enable paranoid for this to work)
deletedAt: 'destroyTime',
paranoid: true
})
So in the above example, just set timestamps to be true but then createdAt to be false
Found my own answer at http://docs.sequelizejs.com/manual/tutorial/models-definition.html#configuration, I should use updatedAt: false in my model definition.

Symfony 2 / Doctrine Not Saving Any Zeros in varchar field

UPDATE
As asked for,
$NewUser = new Users();
$Form = $this->createForm(new UserType(), $NewUser, [])
->add('save', 'submit', ['label' => 'Save',
'attr' => ['class' => 'SaveButton ftLeft'],
]);
$Form->handleRequest($request);
if ($Form->isValid()) {
/*
Sometimes add more data to the entity.....
*/
$em = $this->getDoctrine()->getManager();
$em->persist( $NewUser );
$em->flush();
}
$FormRender = $Form->createView();
return $this->render('xxxxBundle:Users/add.html.twig',
['AddUser' => $FormRender]);
Sometimes I will add extra to the entity, on fields being set within the php code, e.g. account sign up date, but in most cases just save the entity with the form data.
Very simple issue, I have a new (ish) system and have notice that when trying to save zeros, for anything, phone number inputs, it does not save any zeros?
I am using YMAL files to control my doctrine ORM, here is how I have set it up within my User table,
mobileNumber:
type: string
nullable: false
length: 50
options:
fixed: false
column: mobile_number
This does save/make the field has a varchar, and thought nothing about it until I did a random test and saw it was not saving any leading zeros?
I know you can not save leading zeros in a int field type, but this is a varchar. Also when I go into the database, and manually add a zero to that input, its fine, so I take its not the database. Something to do with how I get doctrine to save the data?
If so how do I change that? I have just been saving the data with a standard persist() and flush() commands? Not sure why it would not save the zeros? I was thinking that I had to change the YMAL type to something like phone? I have been over the docs, can not see it.
All help most welcome..
Thanks.
The reason is in your comment:
User form type has the mobile number set to 'number' input.
This value is being casted to int by the Form component. Change the field type to regular string.
If you want to validate value to be only digits, use Validation component

Grails: text instead of varchar

I need the MySQL column type for the String field in my Domain class to be TEXT or VARCHAR(3000), but nothing I try seems to work - it remains VARCHAR(255). I've tried
static mapping = {
longString type: 'text'
}
and
static mapping = {
longString sqlType: 'text'
}
and
static constraints = {
longString (blank: true, nullable: true, maxSize: 3000)
}
and
static constraints = {
longString (blank: true, nullable: true, size: 0..65535)
}
MySQL Server version 5.0.95, Grails 2.4.3.
I'm totally mystified and would appreciate any help..
You need to define the type of the column in the mapping block rather than constraints. Assuming the name of the property is longString, add this
static mapping = {
longString type: 'text'
}
This will create a column with a MySQL type of longtext.
To verify that this works, try dropping your database, create a new (empty) database, restart the app and check the type of the column that is created. See this example for reference.