Skip to content

Rocketseat/adonis-timezone

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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();
  }
}

About

Timezone provider for the Adonis framework

Resources

Code of conduct

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 3

  •  
  •  
  •