I'm having issues with the timestamps on the Sequelize models. When creating a new record via the model, the createdAt timestamp contains milliseconds, but when I query the database using findOne(), it doesn't contain milliseconds. The createdAt column in the database is of type timestamp.
Value while creating model: 2018-12-04T07:18:14.075Z
Value while retrieving model via query: 2018-12-04T07:18:14.000Z
Anyone know what to do? Why is this even inconsistent?
I'm using Sequelize 4.41.2.
Related
The issue arises when I use bulkCreate or upsert in Sequelize to create a record if it not exist in database and otherwise update it. Basically, they serve my purpose successfully but there is one thing that makes me concerned is that for each record update bulkCreate and upsert will increase the value of id field (primary key) therefore if a new one record inserted, it will have an id value much greater than previous records. So how can I prevent the id field from increasing each time they update the records?
This is the records when I use bulkCreate and upsert.These are queries I use:
model.bulkCreate(data, { updateOnDuplicate: [] };
model.upsert(data);
This is the records I want them to be.
I use MariaDB. Thank you for your attention!
When I write a Date object to my Firestore, all works correctly and I can see a date in the DB stored with the type "timestamp". If I try to pass a Date object to a cloud function, I don't know why but it is an empty object when I store it from the cloud function to the firestore DB.
firebase.functions().httpsCallable("myFunction")({date: new Date()}) <---- // this doesn't work
So, I have decided to convert the Date object to a firestore timestamp in the client side before sending it to the cloud function. As follows:
date = firebase.firestore.Timestamp.fromDate(date);
firebase.functions().httpsCallable("myFunction")({date})
Then, if I get it in the cloud function and store it on my firestore, I see an object with the fields "seconds" and "nanoseconds", but not a timestamp (I mean it is not stored with the type Timestamp)... So when I get this date I cannot apply the method .toDate()
Is there any good way to store the date in this situation as a timestamp and not as an object?
The input and output of callable functions deal entirely with JSON, so they are limited to expressing only those types that are allowed in JSON: string, number, boolean, null, object, array. Date and Timestamp are not among those types. What the Firebase SDK will do is try to serialize custom objects as JSON, which is not working the way you want - the seconds and nanos fields of the Timestamp are being split up during serialization.
If you have a Date to send from the client, you should just send it as a number. Instead of passing a Date object, pass the single number inside it (milliseconds since unix epoch):
const number = date.getTime()
On the receiving side, in Cloud Functions, take the number and convert it back to a Date:
const date = new Date(number)
Then you can write the Date object to a Firestore document field.
I'm fairly new to Firebase/Firestore and I'm querying some stored data for analysis via Cloud Function.
I can't figure out how to compare timestamp within a query.
Each document has a field set using admin.firestore.FieldValue.serverTimestamp().
My initial query against the collection works, using ref.where("timestamp", ">=", startTime) where startTime is a Date object.
However, when I iterate through a snapshot - using doc.data().timestamp. Logging this value I get something like 2018-05-10T22:51:37.236Z which is not comparable.
Can I transform the doc.data().timestamp in a way that I can compare against a unix timestamp or Date object?
The database and node are set as -02:00 timezone.
When I save a register, using sequelize, it saves the register with the right date and time in its date fields. For example, if I save a register with the field moment set as '2017-01-15T23:59:59-0200' and look in the database via MySQL Workbench I will see 2017-01-16 00:00:00 in the respective column.
I can even correctly find registers and filter by date and time.
But the value returned by a find operation in the field is '2017-01-16T01:59:59.000Z', meaning it was added two hours to the answer.
How could I retrive the correct date and time from MySQL using Sequelize?
Solved by overhiding String.toJSON:
Date.prototype.toJSON = function(){ return this.toLocaleString(); }
I'm working with node.js and mongoose. I'm creating a REST API to expose my User model:
var userSchema = new Schema({
_id: {type:Number},
username: {type:String},
age: {type:Number},
genre:{type: Number,ref:'Genre'},
country: {type: Number,ref:'Country'}
});
As you can see I decided to include an _id field, so if I want to create a new user I'll need to generate the value for this field, for example:
exports.createUser = function(req,res){
var user = new User({
_id: //Generate and assing value here
//Other properties are retrieved from the request object
});
};
How could I "generate" or assign a value to my _id field properly? How does mongo deals with this?
I never used mongoose. but if _id is not included in insert query, mongodb driver will generate _ids for you as an ObjectId object. and if you wish to use your own _ids, it's up to you to decide about its type and length, and also you have to guarantee its uniqueness among the collection because any attempt to insert a document with a duplicated _id will fail.
accepted answer of this question may be useful, if you are looking for a method for creating custom _ids that provides a decent degree of guaranteed uniqueness.
mongoDB requires that _id, if supplied, be unique. If _id is not supplied, it is created by the client-side driver (i.e. NOT the mongod server!) as a 12-byte BSON ObjectId with the following structure:
4-byte value representing the seconds since the Unix epoch,
3-byte machine identifier,
2-byte process id, and
3-byte counter, starting with a random value.
more info available here: http://docs.mongodb.org/manual/reference/object-id