Skip to content

Commit

Permalink
feat(Packaging): Use es6 module packaging
Browse files Browse the repository at this point in the history
BREAKING CHANGE: The default export has been removed. You need to do a deconstruction import to load the library now.

import JsonDB from 'node-json-db'
becomes
import {JsonDB} from 'node-json-db'
  • Loading branch information
Antoine Aflalo committed Jun 24, 2019
1 parent 045d977 commit 4487c4b
Show file tree
Hide file tree
Showing 9 changed files with 2,991 additions and 1,607 deletions.
16 changes: 0 additions & 16 deletions .babelrc

This file was deleted.

25 changes: 14 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,14 @@ See [test](https://github.com/Belphemur/node-json-db/tree/master/test) for more


```javascript
import JsonDB from 'node-json-db';
import { JsonDB } from 'node-json-db';
import { Config } from 'node-json-db/lib/JsonDBConfig';

// The second argument is used to tell the DB to save after each push
// If you put false, you'll have to call the save() method.
// The third argument is to ask JsonDB to save the database in an human readable format. (default false)
// The last argument is the separator. By default it's slash (/)
var db = new JsonDB(new Config("myDataBase", true, false, '/');
var db = new JsonDB(new Config("myDataBase", true, false, '/'));

// Pushing the data into the database
// With the wanted DataPath
Expand Down Expand Up @@ -119,21 +119,24 @@ db.reload();
```

### TypeScript Support
As of v0.8.0, [TypeScript](https://www.typescriptlang.org) types are

#### v0.8.0
As of v0.8.0, [TypeScript](https://www.typescriptlang.org) types are
included in this package, so using `@types/node-json-db` is no longer required.


#### v1.0.0

JsonDB isn't exported as default any more. You'll need to change how you load the library.

This change is done to follow the right way to import module.
```javascript
import JsonDB from 'node-json-db';
import { JsonDB } from 'node-json-db';
import { Config } from 'node-json-db/lib/JsonDBConfig';

const db = new JsonDB(new Config("myDataBase", true, false, '/');
const db = new JsonDB(new Config("myDataBase", true, false, '/'));
```
**IMPORTANT NOTE:** Ensure that you have the `esModuleInterop`
[compiler flag](https://www.typescriptlang.org/docs/handbook/compiler-options.html)
set to `true` in your `tsconfig.json`. This enables your TypeScript project to
correctly load ES6 modules that have a default export.
See [here](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-7.html#support-for-import-d-from-cjs-form-commonjs-modules-with---esmoduleinterop)
for more background on this TypeScript option.


### Array Support
You can also access the information stored into arrays and manipulate them.
Expand Down
32 changes: 14 additions & 18 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
"version": "0.11.0",
"description": "Database using JSON file as storage for Node.JS",
"main": "dist/JsonDB.js",
"types": "dist/types/JsonDB.d.ts",
"types": "dist/JsonDB.d.ts",
"scripts": {
"test": "jest --coverage",
"build": "babel src --out-dir dist --extensions \".ts,.tsx\" && tsc --emitDeclarationOnly",
"build": "tsc",
"build:doc": "typedoc --out docs --target es6 --theme minimal --mode file src",
"commitmsg": "validate-commit-msg",
"semantic-release": "semantic-release",
Expand Down Expand Up @@ -59,29 +59,25 @@
]
},
"devDependencies": {
"@babel/cli": "^7.2.3",
"@babel/core": "^7.3.4",
"@babel/plugin-proposal-class-properties": "^7.3.4",
"@babel/plugin-proposal-object-rest-spread": "^7.3.4",
"@babel/preset-env": "^7.3.4",
"@babel/preset-typescript": "^7.3.3",
"@semantic-release/changelog": "^3.0.2",
"@semantic-release/git": "^7.0.8",
"@types/jest": "^24.0.11",
"@semantic-release/changelog": "^3.0.4",
"@semantic-release/git": "^7.0.12",
"@types/jest": "^24.0.15",
"@types/mkdirp": "^0.5.2",
"@types/node": "^11.11.3",
"@types/node": "^12.0.10",
"@types/safe-regex": "^1.1.2",
"babel-plugin-add-module-exports": "^1.0.0",
"babel-plugin-add-module-exports": "^1.0.2",
"bili": "^4.8.0",
"cz-conventional-changelog": "^2.0.0",
"husky": "^1.3.1",
"jest": "^24.5.0",
"husky": "^2.5.0",
"jest": "^24.8.0",
"last-release-git": "^0.0.3",
"rollup-plugin-typescript2": "^0.21.2",
"safe-regex": "~2.0.2",
"semantic-release": "^15.13.3",
"semantic-release": "^15.13.16",
"travis-deploy-once": "^5.0.11",
"ts-jest": "^24.0.0",
"ts-jest": "^24.0.2",
"typedoc": "^0.14.2",
"typescript": "^3.3.3333",
"typescript": "^3.5.2",
"validate-commit-msg": "^2.14.0"
}
}
2 changes: 1 addition & 1 deletion src/JsonDB.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ type DataPath = Array<string>

export type FindCallback = (entry: any, index: number | string) => boolean

export default class JsonDB {
export class JsonDB {
private loaded: boolean = false
private data: KeyValue = {}
private readonly config : JsonDBConfig
Expand Down
2 changes: 1 addition & 1 deletion src/lib/DBParentData.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {ArrayInfo} from "./ArrayInfo"
import {DataError} from "./Errors"
import {KeyValue} from "./Utils"
import JsonDB from "../JsonDB"
import {JsonDB} from "../JsonDB"

export class DBParentData {
readonly parent?: string
Expand Down
2 changes: 1 addition & 1 deletion test/02-jsondb.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {DatabaseError, DataError} from "../src/lib/Errors"
import JsonDB from "../src/JsonDB"
import {JsonDB} from "../src/JsonDB"
import * as fs from 'fs'
import { Config } from "../src/lib/JsonDBConfig"

Expand Down
2 changes: 1 addition & 1 deletion test/03-existing-db.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import JsonDB from '../src/JsonDB'
import { JsonDB } from '../src/JsonDB'

describe('JsonDB', () => {
const db = new JsonDB('test/game_file', true, true)
Expand Down
7 changes: 2 additions & 5 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,15 @@
"compilerOptions": {
"moduleResolution": "node",
"target": "esnext",
"module":"es2015",
"module":"commonjs",
"lib": ["es2015", "es2016", "es2017", "dom", "es6"],
"strict": true,
"sourceMap": true,
"declaration": true,
"allowSyntheticDefaultImports": true,
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"declarationDir": "dist/types",
// Import non-ES modules as default imports.
"esModuleInterop": true,
"outDir": "dist/lib",
"outDir": "dist",
"typeRoots": [
"node_modules/@types"
]
Expand Down
Loading

0 comments on commit 4487c4b

Please sign in to comment.