/adonis-timezone

Timezone provider for the Adonis framework

Primary LanguageJavaScript

Adonis Timezone Provider

This library provides an easy way to start using timezones with AdonisJS.

Coverage Status

Install

adonis install @rocketseat/adonis-timezone

Include adonis-timezone provider into providers list

Insert provider reference: @rocketseat/adonis-timezone/providers/TimezoneProvider into providers list.

file path: start/app.js

const providers = [   
  ...    
  '@rocketseat/adonis-timezone/providers/TimezoneProvider',  
  
];

Use

To use this provider you need to add a trait inside your Model, like this:

class User extends Model {
  static boot() {
    super.boot();

    this.addTrait("@provider:Timezone/Trait");
  }
}
OBS: Unfortunately cannot overwrite castDates

And you need to create a middleware and inside it, use the timezone context variable.

On the timezone variable, you can choose the timezone, that your model will provide the timestamps and custom time & date.

class Timezone {
  async handle({ timezone }, next) {
    timezone.activate("America/Sao_Paulo");
    await next();
  }
}

If you'd like to add user's timezone, then you should add a named middleware to the timezone and the user must have a timezone field in their Model.

class Timezone {
  async handle({ timezone, auth }, next) {
    timezone.activate(auth.user.timezone);
    await next();
  }
}