cognitom / momy

MongoDB to MySQL replication
https://www.npmjs.com/package/momy
MIT License
98 stars 37 forks source link

Error if datetime is type object #15

Open chemitaxis opened 7 years ago

chemitaxis commented 7 years ago

Hi, my MongoDB Collection is returning an object instead of a number or string, I have changed the code:

DATETIME: {
    type: 'DATETIME',
    convert: val => {
      if (typeof val === 'string') val = getValueOfDate(val);
      if (typeof val === 'object') {
        val = moment(val).format('YYYY-MM-DD HH:mm:ss');
        return `"${val}"`;
      }
      if (typeof val !== 'number') return 'NULL';
      val = moment(val).format('YYYY-MM-DD HH:mm:ss');
      return `"${val}"`;
    }
  },

I think this error could happen too in other date formats, but with DATETIME is fixed for me. I have created a PR, please check. Thanks and excellent work ;)

chemitaxis commented 7 years ago

This fix date like this:

ISODate("2016-05-11T15:06:37.638Z") // This is an object ;)

chemitaxis commented 7 years ago

I have fixed DATETIME and DATE objects, I think TIME has a different behaviour. Please tell me if I'm right.

cognitom commented 7 years ago

Hi, I'm trying to understand the spec of MongoDB.

In Mongo shell, we can create a Date object, and internally it is ISODate, according to its text. Here're my quick study and results:

replicaset:PRIMARY> var d = new Date()
replicaset:PRIMARY> d
ISODate("2017-07-07T13:15:22.921Z")
replicaset:PRIMARY> typeof d
object
replicaset:PRIMARY> d instanceof ISODate
false
replicaset:PRIMARY> d instanceof Date
true
replicaset:PRIMARY> var dd = new ISODate()
replicaset:PRIMARY> dd
ISODate("2017-07-07T13:18:34.296Z")
replicaset:PRIMARY> dd instanceof ISODate
false
replicaset:PRIMARY> dd instanceof Date
true

So, it seems that we can detect a date object by val instanceof Date.

@chemitaxis I've updated the code a bit for clarification and readability. a31e152df9322b875672721265654ab3ed0be90c Could you pls check that the updated version still work in your environment? Thanks.

chemitaxis commented 7 years ago

Hi @cognitom sorry for the delay... I can tell you that your approach if correct, here you have a screenshot after import my data users. Thanks. screen shot 2017-07-10 at 11 18 41

mateusrovedaa commented 3 years ago

Doesn't that work for me, any suggestions? @cognitom My DATETIME is '2018-07-01T12:34:00.000Z', but in import to MySQL, the result is NULL.