@adonisjs/config
Advanced tools
Comparing version 1.0.17 to 1.1.0
@@ -22,2 +22,6 @@ declare module '@ioc:Adonis/Core/Config' { | ||
/** | ||
* Returns complete config | ||
*/ | ||
all(): any; | ||
/** | ||
* Get configuration from a config file and optionally access the object | ||
@@ -24,0 +28,0 @@ * properties using the `dot notation`. |
@@ -12,3 +12,2 @@ "use strict"; | ||
const utils_1 = require("@poppinss/utils"); | ||
const Config_1 = require("../src/Config"); | ||
class ConfigProvider { | ||
@@ -21,3 +20,4 @@ constructor(container) { | ||
const app = this.container.use('Adonis/Core/Application'); | ||
return new Config_1.Config(utils_1.requireAll(app.configPath())); | ||
const { Config } = require('../src/Config'); | ||
return new Config(utils_1.requireAll(app.configPath())); | ||
}); | ||
@@ -24,0 +24,0 @@ } |
@@ -33,2 +33,6 @@ /// <reference path="../adonis-typings/config.d.ts" /> | ||
/** | ||
* Returns complete config | ||
*/ | ||
all(): {}; | ||
/** | ||
* Read value from the pre-loaded config. Make use of the `dot notation` | ||
@@ -35,0 +39,0 @@ * syntax to read nested values. |
@@ -45,2 +45,8 @@ "use strict"; | ||
/** | ||
* Returns complete config | ||
*/ | ||
all() { | ||
return this.config; | ||
} | ||
/** | ||
* Read value from the pre-loaded config. Make use of the `dot notation` | ||
@@ -47,0 +53,0 @@ * syntax to read nested values. |
# The MIT License | ||
Copyright 2019 Harminder virk, contributors | ||
Copyright 2020 Harminder virk, contributors | ||
@@ -5,0 +5,0 @@ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: |
{ | ||
"name": "@adonisjs/config", | ||
"version": "1.0.17", | ||
"version": "1.1.0", | ||
"description": "Config management for AdonisJS framework", | ||
@@ -18,3 +18,2 @@ "main": "build/providers/ConfigProvider.js", | ||
"version": "npm run build", | ||
"lint": "eslint . --ext=.ts", | ||
"clean": "del build", | ||
@@ -25,3 +24,6 @@ "compile": "npm run lint && npm run clean && tsc", | ||
"release": "np", | ||
"format": "prettier --write ." | ||
"format": "prettier --write .", | ||
"prepublishOnly": "npm run build", | ||
"lint": "eslint . --ext=.ts", | ||
"sync-labels": "github-label-sync --labels ./node_modules/@adonisjs/mrm-preset/gh-labels.json adonisjs/config" | ||
}, | ||
@@ -35,21 +37,22 @@ "keywords": [ | ||
"devDependencies": { | ||
"@adonisjs/application": "^2.0.0", | ||
"@adonisjs/fold": "^6.3.5", | ||
"@adonisjs/mrm-preset": "^2.3.6", | ||
"@types/node": "^14.0.14", | ||
"@adonisjs/mrm-preset": "^2.4.0", | ||
"@types/node": "^14.0.23", | ||
"commitizen": "^4.1.2", | ||
"cz-conventional-changelog": "^3.2.0", | ||
"del-cli": "^3.0.1", | ||
"doctoc": "^1.4.0", | ||
"eslint": "^7.4.0", | ||
"eslint": "^7.5.0", | ||
"eslint-config-prettier": "^6.11.0", | ||
"eslint-plugin-adonis": "^1.0.14", | ||
"eslint-plugin-prettier": "^3.1.4", | ||
"github-label-sync": "^2.0.0", | ||
"husky": "^4.2.5", | ||
"japa": "^3.1.1", | ||
"mrm": "^2.3.3", | ||
"np": "^6.2.5", | ||
"np": "^6.3.2", | ||
"npm-audit-html": "^1.4.1", | ||
"prettier": "^2.0.5", | ||
"ts-node": "^8.10.2", | ||
"typescript": "^3.9.6" | ||
"typescript": "^3.9.7" | ||
}, | ||
@@ -66,3 +69,3 @@ "nyc": { | ||
"hooks": { | ||
"pre-commit": "doctoc README.md --title='## Table of contents' && git add README.md && npm audit --production --json | ./node_modules/.bin/npm-audit-html && git add npm-audit.html", | ||
"pre-commit": "npm audit --production --json | ./node_modules/.bin/npm-audit-html && git add npm-audit.html", | ||
"commit-msg": "node ./node_modules/@adonisjs/mrm-preset/validateCommit/conventional/validate.js" | ||
@@ -81,3 +84,3 @@ } | ||
"dependencies": { | ||
"@poppinss/utils": "^2.2.7" | ||
"@poppinss/utils": "^2.3.0" | ||
}, | ||
@@ -84,0 +87,0 @@ "directories": { |
186
README.md
<div align="center"> | ||
<img src="https://res.cloudinary.com/adonisjs/image/upload/q_100/v1564392111/adonis-banner_o9lunk.png" width="600px"> | ||
<img src="https://res.cloudinary.com/adonisjs/image/upload/q_100/v1558612869/adonis-readme_zscycu.jpg" width="600px"> | ||
</div> | ||
# Config | ||
[![circleci-image]][circleci-url] [![npm-image]][npm-url] ![][typescript-image] [![license-image]][license-url] | ||
<br /> | ||
Extremely simple module to **decouple application config** from the file system, which has handful of benefits. | ||
<div align="center"> | ||
<h3>AdonisJS Config Provider</h3> | ||
<p>Config provider to read AdonisJS application config values without directly relying on hard-coded paths</p> | ||
</div> | ||
1. Can rely on more sources to feed configuration. | ||
2. Easy to define fake values during testing. | ||
3. A much nicer API to read nested values. | ||
<br /> | ||
<!-- START doctoc generated TOC please keep comment here to allow auto update --> | ||
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE --> | ||
## Table of contents | ||
<div align="center"> | ||
- [Usage](#usage) | ||
- [Usage with AdonisJs](#usage-with-adonisjs) | ||
- [Why not simply create the config files?](#why-not-simply-create-the-config-files) | ||
- [Multiple config sources](#multiple-config-sources) | ||
- [Easy to fake during tests](#easy-to-fake-during-tests) | ||
- [Config file](#config-file) | ||
- [Config module](#config-module) | ||
- [Application code](#application-code) | ||
- [Test code](#test-code) | ||
- [Reading nested values](#reading-nested-values) | ||
- [API](#api) | ||
[![circleci-image]][circleci-url] [![npm-image]][npm-url] ![][typescript-image] [![license-image]][license-url] [![audit-report-image]][audit-report-url] | ||
<!-- END doctoc generated TOC please keep comment here to allow auto update --> | ||
</div> | ||
## Usage | ||
Install the package from npm as follows | ||
<div align="center"> | ||
<h3> | ||
<a href="https://preview.adonisjs.com"> | ||
Website | ||
</a> | ||
<span> | </span> | ||
<a href="https://preview.adonisjs.com/guides/config"> | ||
Guides | ||
</a> | ||
<span> | </span> | ||
<a href="CONTRIBUTING.md"> | ||
Contributing | ||
</a> | ||
</h3> | ||
</div> | ||
```sh | ||
npm i @adonisjs/config | ||
<div align="center"> | ||
<sub>Built with ❤︎ by <a href="https://twitter.com/AmanVirk1">Harminder Virk</a> | ||
</div> | ||
# yarn | ||
yarn add @adonisjs/config | ||
``` | ||
and then use the class as follows: | ||
```ts | ||
import { Config } from '@adonisjs/config/build/standalone' | ||
const initialConfiguration = { | ||
app: { | ||
name: 'adonis', | ||
}, | ||
database: { | ||
connection: 'mysql', | ||
}, | ||
logger: { | ||
level: 'debug', | ||
}, | ||
} | ||
const config = new Config(initialConfiguration) | ||
config.get('app.name') // adonis | ||
config.get('database.connection') // mysql | ||
config.get('database.user', 'root') // root | ||
``` | ||
## Usage with AdonisJs | ||
The `@adonisjs/core` includes this module and hence there is no need to install it seperately. However, here are the instructions to setup the provider. | ||
```ts | ||
export const providers = [ | ||
'@adonisjs/config', | ||
] | ||
``` | ||
After this, you have to register the typings file inside `files` array for Typescript to pick the ambient module. | ||
> All this hassle is required, since this module is never meant to be installed standalone. | ||
**tsconfig.json** | ||
```json | ||
{ | ||
"files": ["./node_modules/@adonisjs/config/build/adonis-typings/config.d.ts"] | ||
} | ||
``` | ||
## Why not simply create the config files? | ||
Majority of projects create config files next to the source files or inside a dedicated config directory and require those files wherever needed. | ||
However, with AdonisJs, we make the process of config management a little bit better over manually requiring config files and it has handful of benefits. | ||
AdonisJs recommends to save all configuration inside the `config` directory and then behind the scenes it read those files and feed it's content to the `Config` class and later the application developer can get rid of importing config files and rely on the `Config` class instance instead. | ||
## Multiple config sources | ||
We virtually decouple the config from the filesystem, which means your app can read the configuration from anywhere and pass it to the `Config` class. For example: | ||
```ts | ||
const { db } from 'some-db-module' | ||
import { Config } from '@adonisjs/config' | ||
const settings = await db.table('settings').select('*') | ||
const config = new Config({}) // start with empty store | ||
settings.forEach((row) => { | ||
config.set(row.key, JSON.parse(row.value)) | ||
}) | ||
``` | ||
## Easy to fake during tests | ||
Now since, you are not requiring the config files directly inside your application code, you can easily provide fake values during tests. | ||
#### Config file | ||
```ts | ||
export const db = { | ||
connection: 'pg' | ||
} | ||
``` | ||
#### Config module | ||
```ts | ||
import { Config } from '@adonisjs/config' | ||
import { db } from './config/database' | ||
export default new Config({ db }) | ||
``` | ||
#### Application code | ||
```ts | ||
import { Db } from 'some-db-module' | ||
import config from './config' | ||
const db = new Db(config.get('db')) | ||
class UserController { | ||
async store () { | ||
// perform insert | ||
} | ||
} | ||
``` | ||
#### Test code | ||
```ts | ||
import config from './config' | ||
config.set('db.connection', 'sqlite') | ||
// now run tests and connection will be sqlite over pg | ||
``` | ||
## Reading nested values | ||
Reading nested values in Javascript isn't fun. You have to ensure that top level object is actually an object before accessing it's child. | ||
However, with this module, you can pull nested values without worrying about the intermediate parents being `undefined` or `null`. | ||
```ts | ||
config.get('database.mysql.connection.host', '127.0.0.1') | ||
``` | ||
The `get` method will return `127.0.0.1` if any of the parents or the value of `host` itself is non-existent. | ||
## API | ||
Following are the autogenerated files via Typedoc | ||
* [API](docs/README.md) | ||
[circleci-image]: https://img.shields.io/circleci/project/github/adonisjs/config/master.svg?style=for-the-badge&logo=circleci | ||
@@ -178,1 +51,4 @@ [circleci-url]: https://circleci.com/gh/adonisjs/config "circleci" | ||
[license-url]: LICENSE.md "license" | ||
[audit-report-image]: https://img.shields.io/badge/-Audit%20Report-blueviolet?style=for-the-badge | ||
[audit-report-url]: https://htmlpreview.github.io/?https://github.com/adonisjs/config/blob/develop/npm-audit.html "audit-report" |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
310
14392
20
54
Updated@poppinss/utils@^2.3.0