From b9105bb23a8ab3c3970ea29022c14c1a031e813d Mon Sep 17 00:00:00 2001 From: Chau Tran Date: Fri, 5 Mar 2021 22:02:52 -0600 Subject: [PATCH] feat(sequelize): add sequelize plugin --- .eslintrc.json | 3 +- README.md | 15 +-- jest.config.js | 1 + nx.json | 3 + package-lock.json | 141 +++++++++++++++++++++++- package.json | 2 + packages/sequelize/.eslintrc.json | 9 ++ packages/sequelize/README.md | 52 +++++++++ packages/sequelize/jest.config.js | 14 +++ packages/sequelize/package.json | 32 ++++++ packages/sequelize/src/index.ts | 1 + packages/sequelize/src/lib/sequelize.ts | 35 ++++++ packages/sequelize/tsconfig.json | 13 +++ packages/sequelize/tsconfig.lib.json | 11 ++ packages/sequelize/tsconfig.spec.json | 15 +++ tsconfig.base.json | 3 +- workspace.json | 43 +++++++- 17 files changed, 382 insertions(+), 11 deletions(-) create mode 100644 packages/sequelize/.eslintrc.json create mode 100644 packages/sequelize/README.md create mode 100644 packages/sequelize/jest.config.js create mode 100644 packages/sequelize/package.json create mode 100644 packages/sequelize/src/index.ts create mode 100644 packages/sequelize/src/lib/sequelize.ts create mode 100644 packages/sequelize/tsconfig.json create mode 100644 packages/sequelize/tsconfig.lib.json create mode 100644 packages/sequelize/tsconfig.spec.json diff --git a/.eslintrc.json b/.eslintrc.json index 144072f01..b07362bb7 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -20,7 +20,8 @@ "onlyDependOnLibsWithTags": [ "type:library", "type:type-defs", - "type:util" + "type:util", + "type:plugin" ] }, { diff --git a/README.md b/README.md index 8d658ed88..d006b1d6b 100644 --- a/README.md +++ b/README.md @@ -19,13 +19,14 @@ This is a monorepo of `@automapper/core` and official packages. ## Packages -| Project | Package | Version | Links | -| ----------- | ---------------------------------------------------------------------- | ----------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -| **core** | [`@automapper/core`](https://npmjs.com/package/@automapper/core) | ![npm (scoped)](https://img.shields.io/npm/v/@automapper/core) | [![README](https://img.shields.io/badge/README--green.svg)](/packages/core/README.md) ![npm bundle size (scoped)](https://img.shields.io/bundlephobia/minzip/@automapper/core) ![NPM](https://img.shields.io/npm/l/@automapper/core) | -| **classes** | [`@automapper/classes`](https://npmjs.com/package/@automapper/classes) | ![npm (scoped)](https://img.shields.io/npm/v/@automapper/classes) | [![README](https://img.shields.io/badge/README--green.svg)](/packages/classes/README.md) ![npm bundle size (scoped)](https://img.shields.io/bundlephobia/minzip/@automapper/classes) ![NPM](https://img.shields.io/npm/l/@automapper/classes) | -| **pojos** | [`@automapper/pojos`](https://npmjs.com/package/@automapper/pojos) | ![npm (scoped)](https://img.shields.io/npm/v/@automapper/pojos) | [![README](https://img.shields.io/badge/README--green.svg)](/packages/pojos/README.md) ![npm bundle size (scoped)](https://img.shields.io/bundlephobia/minzip/@automapper/pojos) ![NPM](https://img.shields.io/npm/l/@automapper/pojos) | -| **nestjs** | [`@automapper/nestjs`](https://npmjs.com/package/@automapper/nestjs) | ![npm (scoped)](https://img.shields.io/npm/v/@automapper/nestjs) | [![README](https://img.shields.io/badge/README--green.svg)](/packages/nestjs/README.md) ![npm bundle size (scoped)](https://img.shields.io/bundlephobia/minzip/@automapper/nestjs) ![NPM](https://img.shields.io/npm/l/@automapper/nestjs) | -| **types** | [`@automapper/types`](https://npmjs.com/package/@automapper/types) | ![npm (scoped)](https://img.shields.io/npm/v/@automapper/types) | [![README](https://img.shields.io/badge/README--green.svg)](/packages/types/README.md) ![npm bundle size (scoped)](https://img.shields.io/bundlephobia/minzip/@automapper/types) ![NPM](https://img.shields.io/npm/l/@automapper/types) | +| Project | Package | Version | Links | +| ------------- | -------------------------------------------------------------------------- | ------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| **core** | [`@automapper/core`](https://npmjs.com/package/@automapper/core) | ![npm (scoped)](https://img.shields.io/npm/v/@automapper/core) | [![README](https://img.shields.io/badge/README--green.svg)](/packages/core/README.md) ![npm bundle size (scoped)](https://img.shields.io/bundlephobia/minzip/@automapper/core) ![NPM](https://img.shields.io/npm/l/@automapper/core) | +| **classes** | [`@automapper/classes`](https://npmjs.com/package/@automapper/classes) | ![npm (scoped)](https://img.shields.io/npm/v/@automapper/classes) | [![README](https://img.shields.io/badge/README--green.svg)](/packages/classes/README.md) ![npm bundle size (scoped)](https://img.shields.io/bundlephobia/minzip/@automapper/classes) ![NPM](https://img.shields.io/npm/l/@automapper/classes) | +| **pojos** | [`@automapper/pojos`](https://npmjs.com/package/@automapper/pojos) | ![npm (scoped)](https://img.shields.io/npm/v/@automapper/pojos) | [![README](https://img.shields.io/badge/README--green.svg)](/packages/pojos/README.md) ![npm bundle size (scoped)](https://img.shields.io/bundlephobia/minzip/@automapper/pojos) ![NPM](https://img.shields.io/npm/l/@automapper/pojos) | +| **sequelize** | [`@automapper/sequelize`](https://npmjs.com/package/@automapper/sequelize) | ![npm (scoped)](https://img.shields.io/npm/v/@automapper/sequelize) | [![README](https://img.shields.io/badge/README--green.svg)](/packages/sequelize/README.md) ![npm bundle size (scoped)](https://img.shields.io/bundlephobia/minzip/@automapper/sequelize) ![NPM](https://img.shields.io/npm/l/@automapper/sequelize) | +| **nestjs** | [`@automapper/nestjs`](https://npmjs.com/package/@automapper/nestjs) | ![npm (scoped)](https://img.shields.io/npm/v/@automapper/nestjs) | [![README](https://img.shields.io/badge/README--green.svg)](/packages/nestjs/README.md) ![npm bundle size (scoped)](https://img.shields.io/bundlephobia/minzip/@automapper/nestjs) ![NPM](https://img.shields.io/npm/l/@automapper/nestjs) | +| **types** | [`@automapper/types`](https://npmjs.com/package/@automapper/types) | ![npm (scoped)](https://img.shields.io/npm/v/@automapper/types) | [![README](https://img.shields.io/badge/README--green.svg)](/packages/types/README.md) ![npm bundle size (scoped)](https://img.shields.io/bundlephobia/minzip/@automapper/types) ![NPM](https://img.shields.io/npm/l/@automapper/types) | ### Experimental diff --git a/jest.config.js b/jest.config.js index bfbc2769a..1e895597d 100644 --- a/jest.config.js +++ b/jest.config.js @@ -7,5 +7,6 @@ module.exports = { '/packages/nestjs', '/packages/nestjs-integration-test', '/packages/experimental/transformer-plugin', + '/packages/sequelize', ], }; diff --git a/nx.json b/nx.json index e582b0b1f..323638861 100644 --- a/nx.json +++ b/nx.json @@ -46,6 +46,9 @@ }, "classes-experimental-transformer-plugin": { "tags": ["type:transformer-plugin"] + }, + "sequelize": { + "tags": ["type:plugin"] } }, "workspaceLayout": { diff --git a/package-lock.json b/package-lock.json index b53595b45..7c30ea6b5 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1776,6 +1776,12 @@ "@babel/types": "^7.3.0" } }, + "@types/bluebird": { + "version": "3.5.33", + "resolved": "https://registry.npmjs.org/@types/bluebird/-/bluebird-3.5.33.tgz", + "integrity": "sha512-ndEo1xvnYeHxm7I/5sF6tBvnsA4Tdi3zj1keRKRs12SP+2ye2A27NDJ1B6PqkfMbGAcT+mqQVqbZRIrhfOp5PQ==", + "dev": true + }, "@types/cacheable-request": { "version": "6.0.1", "resolved": "https://registry.npmjs.org/@types/cacheable-request/-/cacheable-request-6.0.1.tgz", @@ -1788,6 +1794,15 @@ "@types/responselike": "*" } }, + "@types/continuation-local-storage": { + "version": "3.2.2", + "resolved": "https://registry.npmjs.org/@types/continuation-local-storage/-/continuation-local-storage-3.2.2.tgz", + "integrity": "sha512-aItm+aYPJ4rT1cHmAxO+OdWjSviQ9iB5UKb5f0Uvgln0N4hS2mcDodHtPiqicYBXViUYhqyBjhA5uyOcT+S34Q==", + "dev": true, + "requires": { + "@types/node": "*" + } + }, "@types/cookiejar": { "version": "2.1.2", "resolved": "https://registry.npmjs.org/@types/cookiejar/-/cookiejar-2.1.2.tgz", @@ -1883,6 +1898,12 @@ "@types/node": "*" } }, + "@types/lodash": { + "version": "4.14.168", + "resolved": "https://registry.npmjs.org/@types/lodash/-/lodash-4.14.168.tgz", + "integrity": "sha512-oVfRvqHV/V6D1yifJbVRU3TMp8OT6o6BG+U9MkwuJ3U8/CsDHvalRpsxBqivn71ztOFZBTfJMvETbqHiaNSj7Q==", + "dev": true + }, "@types/minimatch": { "version": "3.0.3", "resolved": "https://registry.npmjs.org/@types/minimatch/-/minimatch-3.0.3.tgz", @@ -1928,6 +1949,18 @@ "@types/node": "*" } }, + "@types/sequelize": { + "version": "4.28.9", + "resolved": "https://registry.npmjs.org/@types/sequelize/-/sequelize-4.28.9.tgz", + "integrity": "sha512-QqYgkw/2fEc0FyEQejnxM7cHKB8XBV3Y69k7GSFOToQBOXos0PJVqNpgROXZddXIkl2d6zicYssHuy75ws84sw==", + "dev": true, + "requires": { + "@types/bluebird": "*", + "@types/continuation-local-storage": "*", + "@types/lodash": "*", + "@types/validator": "*" + } + }, "@types/source-list-map": { "version": "0.1.2", "resolved": "https://registry.npmjs.org/@types/source-list-map/-/source-list-map-0.1.2.tgz", @@ -1959,6 +1992,12 @@ "@types/superagent": "*" } }, + "@types/validator": { + "version": "13.1.3", + "resolved": "https://registry.npmjs.org/@types/validator/-/validator-13.1.3.tgz", + "integrity": "sha512-DaOWN1zf7j+8nHhqXhIgNmS+ltAC53NXqGxYuBhWqWgqolRhddKzfZU814lkHQSTG0IUfQxU7Cg0gb8fFWo2mA==", + "dev": true + }, "@types/webpack-sources": { "version": "0.1.8", "resolved": "https://registry.npmjs.org/@types/webpack-sources/-/webpack-sources-0.1.8.tgz", @@ -2673,6 +2712,12 @@ "color-convert": "^2.0.1" } }, + "any-promise": { + "version": "1.3.0", + "resolved": "https://registry.npmjs.org/any-promise/-/any-promise-1.3.0.tgz", + "integrity": "sha1-q8av7tzqUugJzcA3au0845Y10X8=", + "dev": true + }, "anymatch": { "version": "3.1.1", "resolved": "https://registry.npmjs.org/anymatch/-/anymatch-3.1.1.tgz", @@ -5446,6 +5491,12 @@ "integrity": "sha512-8sJ78ElpbDJBHNeBzUbUVLsqKdccaa/BXF1uPTw3GrvQTBgrQrtObr2mUrE38vzYd8cEv+m/JBfDLioYcfXoaw==", "dev": true }, + "dottie": { + "version": "2.0.2", + "resolved": "https://registry.npmjs.org/dottie/-/dottie-2.0.2.tgz", + "integrity": "sha512-fmrwR04lsniq/uSr8yikThDTrM7epXHBAAjH9TbeH3rEA8tdCO7mRzB9hdmdGyJCxF8KERo9CITcm3kGuoyMhg==", + "dev": true + }, "dtrace-provider": { "version": "0.8.8", "resolved": "https://registry.npmjs.org/dtrace-provider/-/dtrace-provider-0.8.8.tgz", @@ -7594,6 +7645,12 @@ "integrity": "sha512-IClj+Xz94+d7irH5qRyfJonOdfTzuDaifE6ZPWfx0N0+/ATZCbuTPq2prFl526urkQd90WyUKIh1DfBQ2hMz9A==", "dev": true }, + "inflection": { + "version": "1.12.0", + "resolved": "https://registry.npmjs.org/inflection/-/inflection-1.12.0.tgz", + "integrity": "sha1-ogCTVlbW9fa8TcdQLhrstwMihBY=", + "dev": true + }, "inflight": { "version": "1.0.6", "resolved": "https://registry.npmjs.org/inflight/-/inflight-1.0.6.tgz", @@ -10518,8 +10575,16 @@ "version": "2.29.1", "resolved": "https://registry.npmjs.org/moment/-/moment-2.29.1.tgz", "integrity": "sha512-kHmoybcPV8Sqy59DwNDY3Jefr64lK/by/da0ViFcuA4DH0vQg5Q6Ze5VimxkfQNSC+Mls/Kx53s7TjP1RhFEDQ==", + "dev": true + }, + "moment-timezone": { + "version": "0.5.33", + "resolved": "https://registry.npmjs.org/moment-timezone/-/moment-timezone-0.5.33.tgz", + "integrity": "sha512-PTc2vcT8K9J5/9rDEPe5czSIKgLoGsH8UNpA4qZTVw0Vd/Uz19geE9abbIOQKaAQFcnQ3v5YEXrbSc5BpshH+w==", "dev": true, - "optional": true + "requires": { + "moment": ">= 2.9.0" + } }, "move-concurrently": { "version": "1.0.1", @@ -12682,6 +12747,15 @@ "integrity": "sha1-G0KmJmoh8HQh0bC1S33BZ7AcATs=", "dev": true }, + "retry-as-promised": { + "version": "3.2.0", + "resolved": "https://registry.npmjs.org/retry-as-promised/-/retry-as-promised-3.2.0.tgz", + "integrity": "sha512-CybGs60B7oYU/qSQ6kuaFmRd9sTZ6oXSc0toqePvV74Ac6/IFZSI1ReFQmtCN+uvW1Mtqdwpvt/LGOiCBAY2Mg==", + "dev": true, + "requires": { + "any-promise": "^1.3.0" + } + }, "reusify": { "version": "1.0.4", "resolved": "https://registry.npmjs.org/reusify/-/reusify-1.0.4.tgz", @@ -12925,6 +12999,50 @@ } } }, + "sequelize": { + "version": "6.5.0", + "resolved": "https://registry.npmjs.org/sequelize/-/sequelize-6.5.0.tgz", + "integrity": "sha512-owBt8fnzVy8E1OvyCyfCdVk7OOLyPVrBCMEf+CvRReC5oCyo+UqeXCtwaex9L6LM9ifZ1i3TG3sFeM5MgLK0CQ==", + "dev": true, + "requires": { + "debug": "^4.1.1", + "dottie": "^2.0.0", + "inflection": "1.12.0", + "lodash": "^4.17.20", + "moment": "^2.26.0", + "moment-timezone": "^0.5.31", + "retry-as-promised": "^3.2.0", + "semver": "^7.3.2", + "sequelize-pool": "^6.0.0", + "toposort-class": "^1.0.1", + "uuid": "^8.1.0", + "validator": "^10.11.0", + "wkx": "^0.5.0" + }, + "dependencies": { + "semver": { + "version": "7.3.4", + "resolved": "https://registry.npmjs.org/semver/-/semver-7.3.4.tgz", + "integrity": "sha512-tCfb2WLjqFAtXn4KEdxIhalnRtoKFN7nAwj0B3ZXCbQloV2tq5eDbcTmT68JJD3nRJq24/XgxtQKFIpQdtvmVw==", + "dev": true, + "requires": { + "lru-cache": "^6.0.0" + } + }, + "uuid": { + "version": "8.3.2", + "resolved": "https://registry.npmjs.org/uuid/-/uuid-8.3.2.tgz", + "integrity": "sha512-+NYs2QeMWy+GWFOEm9xnn6HCDp0l7QBD7ml8zLUmJ+93Q5NF0NocErnwkTkXVFNiX3/fpC6afS8Dhb/gz7R7eg==", + "dev": true + } + } + }, + "sequelize-pool": { + "version": "6.1.0", + "resolved": "https://registry.npmjs.org/sequelize-pool/-/sequelize-pool-6.1.0.tgz", + "integrity": "sha512-4YwEw3ZgK/tY/so+GfnSgXkdwIJJ1I32uZJztIEgZeAO6HMgj64OzySbWLgxj+tXhZCJnzRfkY9gINw8Ft8ZMg==", + "dev": true + }, "serialize-javascript": { "version": "4.0.0", "resolved": "https://registry.npmjs.org/serialize-javascript/-/serialize-javascript-4.0.0.tgz", @@ -14306,6 +14424,12 @@ "integrity": "sha512-yaOH/Pk/VEhBWWTlhI+qXxDFXlejDGcQipMlyxda9nthulaxLZUNcUqFxokp0vcYnvteJln5FNQDRrxj3YcbVw==", "dev": true }, + "toposort-class": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/toposort-class/-/toposort-class-1.0.1.tgz", + "integrity": "sha1-f/0feMi+KMO6Rc1OGj9e4ZO9mYg=", + "dev": true + }, "tough-cookie": { "version": "3.0.1", "resolved": "https://registry.npmjs.org/tough-cookie/-/tough-cookie-3.0.1.tgz", @@ -14850,6 +14974,12 @@ "spdx-expression-parse": "^3.0.0" } }, + "validator": { + "version": "10.11.0", + "resolved": "https://registry.npmjs.org/validator/-/validator-10.11.0.tgz", + "integrity": "sha512-X/p3UZerAIsbBfN/IwahhYaBbY68EN/UQBWHtsbXGT5bfrH/p4NQzUCG1kF/rtKaNpnJ7jAu6NGTdSNtyNIXMw==", + "dev": true + }, "vary": { "version": "1.1.2", "resolved": "https://registry.npmjs.org/vary/-/vary-1.1.2.tgz", @@ -15794,6 +15924,15 @@ } } }, + "wkx": { + "version": "0.5.0", + "resolved": "https://registry.npmjs.org/wkx/-/wkx-0.5.0.tgz", + "integrity": "sha512-Xng/d4Ichh8uN4l0FToV/258EjMGU9MGcA0HV2d9B/ZpZB3lqQm7nkOdZdm5GhKtLLhAE7PiVQwN4eN+2YJJUg==", + "dev": true, + "requires": { + "@types/node": "*" + } + }, "word-wrap": { "version": "1.2.3", "resolved": "https://registry.npmjs.org/word-wrap/-/word-wrap-1.2.3.tgz", diff --git a/package.json b/package.json index cee6bbf69..b24d9427c 100644 --- a/package.json +++ b/package.json @@ -75,6 +75,7 @@ "@types/jest": "26.0.20", "@types/jest-when": "2.7.2", "@types/node": "14.14.28", + "@types/sequelize": "4.28.9", "@types/supertest": "2.0.10", "@typescript-eslint/eslint-plugin": "4.15.1", "@typescript-eslint/parser": "4.15.1", @@ -93,6 +94,7 @@ "reflect-metadata": "0.1.13", "release-it": "14.4.0", "rxjs": "~6.6.3", + "sequelize": "6.5.0", "supertest": "6.1.3", "ts-jest": "26.5.1", "ts-node": "9.1.1", diff --git a/packages/sequelize/.eslintrc.json b/packages/sequelize/.eslintrc.json new file mode 100644 index 000000000..cda1e7a12 --- /dev/null +++ b/packages/sequelize/.eslintrc.json @@ -0,0 +1,9 @@ +{ + "extends": ["../../.eslintrc.json"], + "ignorePatterns": ["!**/*"], + "rules": { + "@typescript-eslint/no-non-null-assertion": 0, + "no-prototype-builtins": 0, + "@typescript-eslint/no-explicit-any": 0 + } +} diff --git a/packages/sequelize/README.md b/packages/sequelize/README.md new file mode 100644 index 000000000..d9d18f418 --- /dev/null +++ b/packages/sequelize/README.md @@ -0,0 +1,52 @@ +# @automapper/sequelize + +This is the official plugin from `@automapper` to work with `Sequelize` enabled projects + +## Installation + +```sh +npm i @automapper/sequelize +``` + +or with `yarn`: + +```sh +yarn add @automapper/sequelize +``` + +#### `peerDependencies` + +`@automapper/sequelize` depends on `sequelize`, `@automapper/classes`, and all of `@automapper/classes` `peerDependencies` + +```sh +npm i sequelize @automapper/{core,classes} reflect-metadata +npm i --save-dev @types/sequelize @automapper/types +``` + +or with `yarn`: + +```sh +yarn add sequelize @automapper/{core,classes} reflect-metadata +yarn add --dev @types/sequelize @automapper/types +``` + +## Usage + +- `@automapper/sequelize` provides `sequelize` as a `(valueGetter) => MapPluginInitializer`. Pass `sequelize()` to `createMapper` to create + a `Mapper` that uses `sequelize` plugin. + +- `valueGetter` is an optional getter that, if passed in, will be used to extract the value of the model after instantiation. By default, it will use `sequelize#Model.get()` if it's available. + +```ts +import { classes } from '@automapper/classes'; + +const mapper = createMapper({ + ..., + pluginInitializer: sequelize() // or sequelize(model => model.get(getterOptions)) +}) + +mapper.createMap(User, UserVm); +mapper.map(user, UserVm, User); +``` + +Read more about this plugin on [sequelize documentation](https://automapperts.netlify.app/docs/plugins-system/introduce-to-sequelize) diff --git a/packages/sequelize/jest.config.js b/packages/sequelize/jest.config.js new file mode 100644 index 000000000..ecf916200 --- /dev/null +++ b/packages/sequelize/jest.config.js @@ -0,0 +1,14 @@ +module.exports = { + displayName: 'sequelize', + preset: '../../jest.preset.js', + globals: { + 'ts-jest': { + tsConfig: '/tsconfig.spec.json', + }, + }, + transform: { + '^.+\\.[tj]sx?$': 'ts-jest', + }, + moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx'], + coverageDirectory: '../../coverage/packages/sequelize', +}; diff --git a/packages/sequelize/package.json b/packages/sequelize/package.json new file mode 100644 index 000000000..f75ab473f --- /dev/null +++ b/packages/sequelize/package.json @@ -0,0 +1,32 @@ +{ + "name": "@automapper/sequelize", + "version": "3.0.11", + "peerDependencies": { + "reflect-metadata": "~0.1.13", + "sequelize": "~6.5.0" + }, + "sideEffects": false, + "engines": { + "node": ">=10.0.0" + }, + "publishConfig": { + "access": "public" + }, + "repository": { + "type": "git", + "url": "https://github.com/nartc/mapper/tree/main/packages/classes" + }, + "author": { + "name": "Chau Tran", + "email": "nartc7789@gmail.com", + "url": "https://nartc.me" + }, + "description": "AutoMapper TypeScript Classes plugin", + "keywords": [ + "typescript", + "automapper", + "mapper", + "nx" + ], + "license": "MIT" +} diff --git a/packages/sequelize/src/index.ts b/packages/sequelize/src/index.ts new file mode 100644 index 000000000..9c50fdd02 --- /dev/null +++ b/packages/sequelize/src/index.ts @@ -0,0 +1 @@ +export * from './lib/sequelize'; diff --git a/packages/sequelize/src/lib/sequelize.ts b/packages/sequelize/src/lib/sequelize.ts new file mode 100644 index 000000000..23531fe3d --- /dev/null +++ b/packages/sequelize/src/lib/sequelize.ts @@ -0,0 +1,35 @@ +import type { Constructible } from '@automapper/classes'; +import { classes } from '@automapper/classes'; +import type { Dictionary, MapPluginInitializer } from '@automapper/types'; +import type { Model } from 'sequelize'; + +export const sequelize: < + TGetterResult extends Record = any, + TModel extends Model = Model +>( + valueGetter?: (model: TModel) => TGetterResult +) => MapPluginInitializer = < + TGetterResult extends Record = any, + TModel extends Model = Model +>( + valueGetter?: (model: TModel) => TGetterResult +) => (errorHandler) => { + const originalClasses = classes(errorHandler); + + return { + ...originalClasses, + instantiate = any>( + model: any, + obj?: TInstanceModel + ) { + const original = originalClasses.instantiate(model, obj); + const instance = (original[0] as unknown) as TModel; + original[0] = (valueGetter + ? valueGetter(instance) + : instance.get + ? instance.get() + : instance) as TInstanceModel; + return original; + }, + }; +}; diff --git a/packages/sequelize/tsconfig.json b/packages/sequelize/tsconfig.json new file mode 100644 index 000000000..62ebbd946 --- /dev/null +++ b/packages/sequelize/tsconfig.json @@ -0,0 +1,13 @@ +{ + "extends": "../../tsconfig.base.json", + "files": [], + "include": [], + "references": [ + { + "path": "./tsconfig.lib.json" + }, + { + "path": "./tsconfig.spec.json" + } + ] +} diff --git a/packages/sequelize/tsconfig.lib.json b/packages/sequelize/tsconfig.lib.json new file mode 100644 index 000000000..037d796f2 --- /dev/null +++ b/packages/sequelize/tsconfig.lib.json @@ -0,0 +1,11 @@ +{ + "extends": "./tsconfig.json", + "compilerOptions": { + "module": "commonjs", + "outDir": "../../dist/out-tsc", + "declaration": true, + "types": ["node"] + }, + "exclude": ["**/*.spec.ts"], + "include": ["**/*.ts"] +} diff --git a/packages/sequelize/tsconfig.spec.json b/packages/sequelize/tsconfig.spec.json new file mode 100644 index 000000000..559410b96 --- /dev/null +++ b/packages/sequelize/tsconfig.spec.json @@ -0,0 +1,15 @@ +{ + "extends": "./tsconfig.json", + "compilerOptions": { + "outDir": "../../dist/out-tsc", + "module": "commonjs", + "types": ["jest", "node"] + }, + "include": [ + "**/*.spec.ts", + "**/*.spec.tsx", + "**/*.spec.js", + "**/*.spec.jsx", + "**/*.d.ts" + ] +} diff --git a/tsconfig.base.json b/tsconfig.base.json index 61088ad05..95d2181f5 100644 --- a/tsconfig.base.json +++ b/tsconfig.base.json @@ -26,7 +26,8 @@ "@automapper/nestjs": ["packages/nestjs/src/index.ts"], "@automapper/classes/experimental/transformer-plugin": [ "packages/classes/experimental/transformer-plugin/src/index.ts" - ] + ], + "@automapper/sequelize": ["packages/sequelize/src/index.ts"] } }, "exclude": ["node_modules", "tmp"] diff --git a/workspace.json b/workspace.json index 37fd4869d..f0a820fe6 100644 --- a/workspace.json +++ b/workspace.json @@ -48,7 +48,8 @@ "npx nx run core:publish", "npx nx run classes:publish", "npx nx run pojos:publish", - "npx nx run nestjs:publish" + "npx nx run nestjs:publish", + "npx nx run sequelize:publish" ] } } @@ -311,6 +312,46 @@ } } } + }, + "sequelize": { + "root": "packages/sequelize", + "sourceRoot": "packages/sequelize/src", + "projectType": "library", + "architect": { + "lint": { + "builder": "@nrwl/linter:eslint", + "options": { + "lintFilePatterns": ["packages/sequelize/**/*.ts"] + } + }, + "test": { + "builder": "@nrwl/jest:jest", + "outputs": ["coverage/packages/sequelize"], + "options": { + "jestConfig": "packages/sequelize/jest.config.js", + "passWithNoTests": true + } + }, + "build": { + "builder": "@nrwl/node:package", + "outputs": ["{options.outputPath}"], + "options": { + "outputPath": "dist/packages/sequelize", + "tsConfig": "packages/sequelize/tsconfig.lib.json", + "packageJson": "packages/sequelize/package.json", + "main": "packages/sequelize/src/index.ts", + "assets": ["packages/sequelize/*.md"], + "buildableProjectDepsInPackageJsonType": "peerDependencies" + } + }, + "publish": { + "builder": "@nrwl/workspace:run-commands", + "options": { + "command": "npm publish", + "cwd": "dist/packages/sequelize" + } + } + } } }, "cli": {