Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Datetype for mysql #98

Closed
sarkistlt opened this issue Apr 19, 2017 · 2 comments
Closed

Datetype for mysql #98

sarkistlt opened this issue Apr 19, 2017 · 2 comments

Comments

@sarkistlt
Copy link

sarkistlt commented Apr 19, 2017

When you try to pass JS date like new Date().toString(), or new Date().toISOString() to datetime field it won't you need to create new compatible with mysql date string new Date().toISOString().slice(0, 19).replace('T', ' '), or you need to re-config your sql service what I don't want to do. It would be useful if this module will do it for you.

@daffl
Copy link
Member

daffl commented Apr 19, 2017

The database adapters do not do any automated type conversion since it can always be done in a small hook like this:

function convertDate(fieldName) {
  return function(hook) {
    const prop = hook.data && hook.data[fieldName];

    if(prop) {
      hook.data[fieldName] = Date().toISOString().slice(0, 19).replace('T', ' ');
    }
    
    return Promise.resolve(hook);
  }  
}

Which can then be used like

app.service('myservice').hooks({
  before: {
    create: [
      convertDate('birthdate')
    ]
  }
});

@sarkistlt
Copy link
Author

Thanks for quick answer. I already use hook, but thought that it can be useful to do it in the adapter.
But I got it what you are saying it makes sense.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants