From fac4f11909e3191c7092dd9df65185aa19a7106a Mon Sep 17 00:00:00 2001 From: Michael Wittwer Date: Wed, 28 Nov 2018 20:52:28 +0100 Subject: [PATCH 1/3] feat(reflect-metadata): remove import - reflect-metadata should only exist once in the whole code base of a project so the consumer of our library must import the reflect-metadata project, even if he might not use it in his code base. But if he uses the reflect-metadata in his code or uses another library like InversifyJS we must ensure the Reflect global variable only exists once, to prevent from possible hard to debug issues --- .lintstagedrc.yml | 3 ++ README.md | 39 ++++++++++++++++++----- package-lock.json | 9 ------ package.json | 2 +- src/dynamo-easy.ts | 7 ---- src/mapper/for-type/object.mapper.spec.ts | 1 + test/models/complex.model.ts | 2 ++ tsconfig.jest.json | 15 ++++++--- tsconfig.json | 5 ++- 9 files changed, 53 insertions(+), 30 deletions(-) diff --git a/.lintstagedrc.yml b/.lintstagedrc.yml index ea7ce9550..380070c91 100644 --- a/.lintstagedrc.yml +++ b/.lintstagedrc.yml @@ -3,6 +3,9 @@ linters: - prettier --write --config ./.prettierrc.yml - tslint --project ./tsconfig.json -t codeFrame --fix - git add + "(src/**/*.spec.ts,test/**/*.ts)": + - prettier --write --config ./.prettierrc.yml + - tslint --project ./tsconfig.jest.json -t codeFrame --fix "**/package.json": - sort-package-json - git add diff --git a/README.md b/README.md index ed1a62eb7..bac7f5084 100644 --- a/README.md +++ b/README.md @@ -8,12 +8,6 @@ [![styled with prettier](https://img.shields.io/badge/styled_with-prettier-ff69b4.svg)](https://github.com/prettier/prettier) [![All Contributors](https://img.shields.io/badge/all_contributors-3-orange.svg)](#contributors) -This is the documentation for the new version of dynamo-easy released as a scoped package using the namespace -@shiftcoders (@shiftcoders/dynamo-easy). If you still want to use the 0.0.x version you can -by installing the old version `npm install dynamo-easy` (https://github -.com/shiftcode/dynamo-easy/releases/tag/v0.10 -.1) - Abstracts away the complexity of the low level aws dynamosdk. Provides an easy to use fluent api to for requests and supports typescript decorators, to define some metadata for your models. You don't need to care about the mapping of javascript types to their dynamo types any more. We got you covered. @@ -22,6 +16,37 @@ Checkout the full technical api documentation [here](https://shiftcode.github.io Built with :heart: by [shiftcode](https://www.shiftcode.ch). # Get Started + +## Metadata Reflection API + +> ⚠ The reflect-metadata polyfill should be imported only once in your entire application because the Reflect object is +mean to be a global singleton. + +Required always. Use reflect-metadata as polyfill. + +``` +npm install reflect-metadata --save +``` + +The type definitions for reflect-metadata are included in the npm package. +You need to add the following reference to the types field in your tsconfig.json: + +``` +"types": ["reflect-metadata"] +``` + +Finally, import reflect-metadata in some entry file in your application. + +``` +import "reflect-metadata" +``` + +Also make sure to install the other peer dependencies of @shiftcoders/dynamo-easy. + +## First Sample + +When all the setup work is done, define your first model and create a dynamo store to execute actions on the dynamoDB. + ```typescript @Model() class Person{ @@ -57,8 +82,6 @@ dynamoStore.scan() ``` -Want to use this library with Angular (>4) [checkout our angular-service](TODO). - # Decorators Decorators are used to add some metadata to our model classes, relevant to our javascript-to-dynamo mapper. diff --git a/package-lock.json b/package-lock.json index 7d029d7f8..1163ec7d9 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1028,15 +1028,6 @@ "integrity": "sha512-Jnmm57+nHqvJUPwUzt1CLoLzFtF2B2vgG7cWFut+a4nqTp9/L6pL0N+o0Jt3V7AQnCKMsPEqQpLFZYleBCdq3w==", "dev": true }, - "@types/reflect-metadata": { - "version": "0.1.0", - "resolved": "https://registry.npmjs.org/@types/reflect-metadata/-/reflect-metadata-0.1.0.tgz", - "integrity": "sha512-bXltFLY3qhzCnVYP5iUpeSICagQ8rc9K2liS+8M0lBcz54BHs3O6W5UvqespVSuebo1BXLi+/y9ioELAW9SC2A==", - "dev": true, - "requires": { - "reflect-metadata": "*" - } - }, "@types/shelljs": { "version": "0.8.0", "resolved": "https://registry.npmjs.org/@types/shelljs/-/shelljs-0.8.0.tgz", diff --git a/package.json b/package.json index cc2445dae..7afcd371d 100644 --- a/package.json +++ b/package.json @@ -47,7 +47,6 @@ "@types/jest": "^23.3.1", "@types/lodash": "^4.14.116", "@types/node": "^8.0.26", - "@types/reflect-metadata": "^0.1.0", "@types/uuid": "^3.4.1", "aws-sdk": "^2.286.1", "colors": "^1.1.2", @@ -60,6 +59,7 @@ "lodash": "^4.17.11", "lodash.camelcase": "^4.3.0", "prettier": "^1.15.2", + "reflect-metadata": "^0.1.12", "rimraf": "^2.6.2", "rxjs": "^6.3.3", "semantic-release": "^15.12.0", diff --git a/src/dynamo-easy.ts b/src/dynamo-easy.ts index c69ec8101..5daf8d7d3 100644 --- a/src/dynamo-easy.ts +++ b/src/dynamo-easy.ts @@ -1,10 +1,3 @@ -// -// Reflect Metadata -// FIXME: Remove reflect-metadata as hard dep (lib using project needs to import it) -// - possible singleton override issues and it's a shim eg. runtime dependent in future --> es7 -// -import 'reflect-metadata' - // // Export public api of the library // diff --git a/src/mapper/for-type/object.mapper.spec.ts b/src/mapper/for-type/object.mapper.spec.ts index 0fe69621c..3daf5c5bb 100644 --- a/src/mapper/for-type/object.mapper.spec.ts +++ b/src/mapper/for-type/object.mapper.spec.ts @@ -1,5 +1,6 @@ import { ObjectMapper } from './object.mapper' +// TODO remove test comment describe('object mapper', () => { describe('to db', () => { it('should work', () => { diff --git a/test/models/complex.model.ts b/test/models/complex.model.ts index ff32021ad..46cbc8ca5 100644 --- a/test/models/complex.model.ts +++ b/test/models/complex.model.ts @@ -10,6 +10,8 @@ import { } from '../../src/dynamo-easy' import { NestedObject } from './nested-object.model' +// TODO remove test comment + @Model({ tableName: 'complex_model' }) export class ComplexModel { @PartitionKey() diff --git a/tsconfig.jest.json b/tsconfig.jest.json index 24d3817ec..1577af47b 100644 --- a/tsconfig.jest.json +++ b/tsconfig.jest.json @@ -1,8 +1,15 @@ { "extends": "./tsconfig.json", "compilerOptions": { - "module": "commonjs", - "declaration": false, - "allowJs": true // allow js to be compiled with ts for jest es2015 module import (lodash-es) - } + "declaration": false, /* Generates corresponding '.d.ts' file. */ + "types": [ /* Type declaration files to be included in compilation. */ + "node", + "reflect-metadata", + "jest" + ] + }, + "include": [ + "src/**/*.spec.ts", + "test/**/*" + ] } diff --git a/tsconfig.json b/tsconfig.json index 5fc9a4b6f..5bde973b6 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -47,7 +47,10 @@ // "paths": {}, /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */ // "rootDirs": [], /* List of root folders whose combined content represents the structure of the project at runtime. */ // "typeRoots": [], /* List of folders to include type definitions from. */ - // "types": [], /* Type declaration files to be included in compilation. */ + "types": [ /* Type declaration files to be included in compilation. */ + "node", + "reflect-metadata" + ], // "allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */ // "esModuleInterop": true /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */ // "preserveSymlinks": true, /* Do not resolve the real path of symlinks. */ From 0140a85d0f757c107fd0a49cfcf8f9b382cff37f Mon Sep 17 00:00:00 2001 From: Michael Wittwer Date: Thu, 29 Nov 2018 16:09:46 +0100 Subject: [PATCH 2/3] fix(lint-staged): remove tslint script in test glob somehow the tslint is not working when using with staged files matching the test glob --- .lintstagedrc.yml | 8 ++++++-- src/mapper/for-type/object.mapper.spec.ts | 1 - test/models/complex.model.ts | 2 -- 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/.lintstagedrc.yml b/.lintstagedrc.yml index 380070c91..0d3aec350 100644 --- a/.lintstagedrc.yml +++ b/.lintstagedrc.yml @@ -3,9 +3,13 @@ linters: - prettier --write --config ./.prettierrc.yml - tslint --project ./tsconfig.json -t codeFrame --fix - git add - "(src/**/*.spec.ts,test/**/*.ts)": + "(src/**/*.spec.ts|test/**/*.ts)": - prettier --write --config ./.prettierrc.yml - - tslint --project ./tsconfig.jest.json -t codeFrame --fix + # FIXME tslint will not work because of the following error + # ✖ tslint --project ./tsconfig.jest.json -t codeFrame --fix found some errors. Please fix them and try committing again. + #'/Users/michaelwittwer/dev/shiftcode/dynamo-easy/test/models/complex.model.ts' is not included in project. + # - tslint --project ./tsconfig.jest.json -t codeFrame --fix + - git add "**/package.json": - sort-package-json - git add diff --git a/src/mapper/for-type/object.mapper.spec.ts b/src/mapper/for-type/object.mapper.spec.ts index 3daf5c5bb..0fe69621c 100644 --- a/src/mapper/for-type/object.mapper.spec.ts +++ b/src/mapper/for-type/object.mapper.spec.ts @@ -1,6 +1,5 @@ import { ObjectMapper } from './object.mapper' -// TODO remove test comment describe('object mapper', () => { describe('to db', () => { it('should work', () => { diff --git a/test/models/complex.model.ts b/test/models/complex.model.ts index 46cbc8ca5..ff32021ad 100644 --- a/test/models/complex.model.ts +++ b/test/models/complex.model.ts @@ -10,8 +10,6 @@ import { } from '../../src/dynamo-easy' import { NestedObject } from './nested-object.model' -// TODO remove test comment - @Model({ tableName: 'complex_model' }) export class ComplexModel { @PartitionKey() From 44c34c14794e73ab7c54f886f73129b3d28466d3 Mon Sep 17 00:00:00 2001 From: Michael Wittwer Date: Fri, 30 Nov 2018 10:29:50 +0100 Subject: [PATCH 3/3] feat(reflect-metadata): remove import - update README to provide some more information about what is required to start to use dynamo-easy - adding comment with breaking change which I forgot to add previously BREAKING CHANGE all consumers of dynamo-easy must install and import reflect-metadata in their project. --- README.md | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index bac7f5084..c902bb629 100644 --- a/README.md +++ b/README.md @@ -17,19 +17,23 @@ Built with :heart: by [shiftcode](https://www.shiftcode.ch). # Get Started -## Metadata Reflection API +## Prerequisite + +### Typescript Metadata + +#### Reflection API > ⚠ The reflect-metadata polyfill should be imported only once in your entire application because the Reflect object is mean to be a global singleton. -Required always. Use reflect-metadata as polyfill. +Install the reflect-metadata polyfill. ``` npm install reflect-metadata --save ``` The type definitions for reflect-metadata are included in the npm package. -You need to add the following reference to the types field in your tsconfig.json: +You need to add the following reference to the types field in your [tsconfig.json](https://www.typescriptlang.org/docs/handbook/tsconfig-json.html#types-typeroots-and-types): ``` "types": ["reflect-metadata"] @@ -41,8 +45,20 @@ Finally, import reflect-metadata in some entry file in your application. import "reflect-metadata" ``` +#### Decorators + + +We need to enable the two experimental features to work with decorators, add this to your tsconfig.json: + +``` +"experimentalDecorators": true +"emitDecoratorMetadata": true +``` + +#### Other Also make sure to install the other peer dependencies of @shiftcoders/dynamo-easy. + ## First Sample When all the setup work is done, define your first model and create a dynamo store to execute actions on the dynamoDB. @@ -85,11 +101,6 @@ dynamoStore.scan() # Decorators Decorators are used to add some metadata to our model classes, relevant to our javascript-to-dynamo mapper. -This is an experimental feature and requires to set the following typescript compiler options: - -- "experimentalDecorators": true -- "emitDecoratorMetadata": true - Additionally we rely on the reflect-metadata (https://www.npmjs.com/package/reflect-metadata) library for reflection api. To get started with decorators just add a [@Model()](https://shiftcode.github.io/dynamo-easy/modules/_decorator_impl_model_model_decorator_.html) Decorator to any typescript class.