From 28d6ca2160fec5a8301dad68a7d9b4a1a1046f3d Mon Sep 17 00:00:00 2001 From: Arseny Yankovsky Date: Thu, 19 Sep 2019 13:26:14 +0200 Subject: [PATCH 01/23] Attempt to reproduce the issue --- .../basic/simple-queries.func.test.ts | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/test/functional/basic/simple-queries.func.test.ts b/test/functional/basic/simple-queries.func.test.ts index 0366681..3c81ee2 100644 --- a/test/functional/basic/simple-queries.func.test.ts +++ b/test/functional/basic/simple-queries.func.test.ts @@ -74,6 +74,46 @@ describe('aurora data api > simple queries', () => { await connection.close() }) + it('should be able to update', async () => { + const connection = await createConnection({ + type: 'aurora-data-api', + database: process.env.database!, + secretArn: process.env.secretArn!, + resourceArn: process.env.resourceArn!, + region: process.env.region!, + entities: [Post], + synchronize: true, + logging: true, + }) + + const postRepository = connection.getRepository(Post) + + const post = new Post() + + post.title = 'My First Post' + post.text = 'Post Text' + post.likesCount = 4 + post.publishedAt = new Date(2017, 1, 1) + + const insertResult = await postRepository.save(post) + + const postId = insertResult.id + + const dbPost = await postRepository.findOne(postId) + + dbPost.publishedAt = new Date() + + await postRepository.save(dbPost) + + const updatedPost = await postRepository.findOne(postId) + + expect(updatedPost.publishedAt > new Date(2017, 1, 1)).toBeTruthy() + + await connection.query('DROP TABLE aurora_data_api_test_post;') + + await connection.close() + }) + it('batch insert - with dates', async () => { const connection = await createConnection({ type: 'aurora-data-api', From 4b04c1a16a06e93a85cd318a6641471408f22e93 Mon Sep 17 00:00:00 2001 From: Arseny Yankovsky Date: Thu, 19 Sep 2019 13:36:50 +0200 Subject: [PATCH 02/23] Attempt to reproduce the issue --- .../basic/simple-queries.func.test.ts | 8 +- yarn.lock | 145 +----------------- 2 files changed, 11 insertions(+), 142 deletions(-) diff --git a/test/functional/basic/simple-queries.func.test.ts b/test/functional/basic/simple-queries.func.test.ts index 3c81ee2..e3c7852 100644 --- a/test/functional/basic/simple-queries.func.test.ts +++ b/test/functional/basic/simple-queries.func.test.ts @@ -99,15 +99,15 @@ describe('aurora data api > simple queries', () => { const postId = insertResult.id - const dbPost = await postRepository.findOne(postId) + const dbPost = await postRepository.findOne({ id: postId }) - dbPost.publishedAt = new Date() + dbPost!.publishedAt = new Date() - await postRepository.save(dbPost) + await postRepository.save(dbPost!) const updatedPost = await postRepository.findOne(postId) - expect(updatedPost.publishedAt > new Date(2017, 1, 1)).toBeTruthy() + expect(updatedPost!.publishedAt > new Date(2017, 1, 1)).toBeTruthy() await connection.query('DROP TABLE aurora_data_api_test_post;') diff --git a/yarn.lock b/yarn.lock index 0aeae8e..9395d2d 100644 --- a/yarn.lock +++ b/yarn.lock @@ -468,11 +468,6 @@ ansistyles@~0.1.3: version "0.1.3" resolved "https://registry.yarnpkg.com/ansistyles/-/ansistyles-0.1.3.tgz#5de60415bda071bb37127854c864f41b23254539" -any-promise@^1.0.0: - version "1.3.0" - resolved "https://registry.yarnpkg.com/any-promise/-/any-promise-1.3.0.tgz#abc6afeedcea52e809cdc0376aed3ce39635d17f" - integrity sha1-q8av7tzqUugJzcA3au0845Y10X8= - anymatch@^2.0.0: version "2.0.0" resolved "https://registry.yarnpkg.com/anymatch/-/anymatch-2.0.0.tgz#bcb24b4f37934d9aa7ac17b4adaf89e7c76ef2eb" @@ -480,11 +475,6 @@ anymatch@^2.0.0: micromatch "^3.1.4" normalize-path "^2.1.1" -app-root-path@^2.0.1: - version "2.2.1" - resolved "https://registry.yarnpkg.com/app-root-path/-/app-root-path-2.2.1.tgz#d0df4a682ee408273583d43f6f79e9892624bc9a" - integrity sha512-91IFKeKk7FjfmezPKkwtaRvSpnUc4gDwPAjA1YZ9Gn0q0PPeW+vbeUsZuyDwjI7+QTHhcLen2v25fi/AmhvbJA== - append-transform@^0.4.0: version "0.4.0" resolved "https://registry.yarnpkg.com/append-transform/-/append-transform-0.4.0.tgz#d76ebf8ca94d276e247a36bad44a4b74ab611991" @@ -937,14 +927,6 @@ buffer@4.9.1: ieee754 "^1.1.4" isarray "^1.0.0" -buffer@^5.1.0: - version "5.4.3" - resolved "https://registry.yarnpkg.com/buffer/-/buffer-5.4.3.tgz#3fbc9c69eb713d323e3fc1a895eee0710c072115" - integrity sha512-zvj65TkFeIt3i6aj5bIvJDzjjQQGs4o/sNoezg1F1kYap9Nu2jcUdpwzRSJTHMMzG0H7bZkn4rNQpImhuxWX2A== - dependencies: - base64-js "^1.0.2" - ieee754 "^1.1.4" - builtin-modules@^1.1.1: version "1.1.1" resolved "https://registry.yarnpkg.com/builtin-modules/-/builtin-modules-1.1.1.tgz#270f076c5a72c02f5b65a47df94c5fe3a278892f" @@ -1071,7 +1053,7 @@ chalk@2.3.1: escape-string-regexp "^1.0.5" supports-color "^5.2.0" -chalk@^1.1.1, chalk@^1.1.3: +chalk@^1.1.3: version "1.1.3" resolved "https://registry.yarnpkg.com/chalk/-/chalk-1.1.3.tgz#a8115c55e4a702fe4d150abd3872822a7e09fc98" dependencies: @@ -1141,17 +1123,6 @@ cli-cursor@^2.1.0: dependencies: restore-cursor "^2.0.0" -cli-highlight@^2.0.0: - version "2.1.1" - resolved "https://registry.yarnpkg.com/cli-highlight/-/cli-highlight-2.1.1.tgz#2180223d51618b112f4509cf96e4a6c750b07e97" - integrity sha512-0y0VlNmdD99GXZHYnvrQcmHxP8Bi6T00qucGgBgGv4kJ0RyDthNnnFPupHV7PYv/OXSVk+azFbOeaW6+vGmx9A== - dependencies: - chalk "^2.3.0" - highlight.js "^9.6.0" - mz "^2.4.0" - parse5 "^4.0.0" - yargs "^13.0.0" - cli-table3@^0.5.0, cli-table3@^0.5.1: version "0.5.1" resolved "https://registry.yarnpkg.com/cli-table3/-/cli-table3-0.5.1.tgz#0252372d94dfc40dbd8df06005f48f31f656f202" @@ -1544,7 +1515,7 @@ debug@^3.1.0, debug@^3.2.6: dependencies: ms "^2.1.1" -debug@^4.0.0, debug@^4.1.1: +debug@^4.0.0: version "4.1.1" resolved "https://registry.yarnpkg.com/debug/-/debug-4.1.1.tgz#3b72260255109c6b589cee050f1d516139664791" dependencies: @@ -1706,11 +1677,6 @@ dotenv@^5.0.1: version "5.0.1" resolved "https://registry.yarnpkg.com/dotenv/-/dotenv-5.0.1.tgz#a5317459bd3d79ab88cff6e44057a6a3fbb1fcef" -dotenv@^6.2.0: - version "6.2.0" - resolved "https://registry.yarnpkg.com/dotenv/-/dotenv-6.2.0.tgz#941c0410535d942c8becf28d3f357dbd9d476064" - integrity sha512-HygQCKUBSFl8wKQZBSemMywRWcEDNidvNbjGVyZu3nbZ8qq9ubiPoGLMdRDpfSrpkkm9BXYFkpKxxFX38o/76w== - duplexer2@~0.1.0: version "0.1.4" resolved "https://registry.yarnpkg.com/duplexer2/-/duplexer2-0.1.4.tgz#8b12dab878c0d69e3e7891051662a32fc6bddcc1" @@ -2026,11 +1992,6 @@ figgy-pudding@^3.4.1, figgy-pudding@^3.5.1: version "3.5.1" resolved "https://registry.yarnpkg.com/figgy-pudding/-/figgy-pudding-3.5.1.tgz#862470112901c727a0e495a80744bd5baa1d6790" -figlet@^1.1.1: - version "1.2.4" - resolved "https://registry.yarnpkg.com/figlet/-/figlet-1.2.4.tgz#2d2f48b61a77418ba6cbd9db2e25da27efee16d0" - integrity sha512-mv8YA9RruB4C5QawPaD29rEVx3N97ZTyNrE4DAfbhuo6tpcMdKnPVo8MlyT3RP5uPcg5M14bEJBq7kjFf4kAWg== - figures@^2.0.0: version "2.0.0" resolved "https://registry.yarnpkg.com/figures/-/figures-2.0.0.tgz#3ab1a2d2a62c8bfb431a0c94cb797a2fce27c962" @@ -2543,11 +2504,6 @@ highlight.js@^9.0.0: version "9.15.8" resolved "https://registry.yarnpkg.com/highlight.js/-/highlight.js-9.15.8.tgz#f344fda123f36f1a65490e932cf90569e4999971" -highlight.js@^9.6.0: - version "9.15.10" - resolved "https://registry.yarnpkg.com/highlight.js/-/highlight.js-9.15.10.tgz#7b18ed75c90348c045eef9ed08ca1319a2219ad2" - integrity sha512-RoV7OkQm0T3os3Dd2VHLNMoaoDVx77Wygln3n9l5YV172XonWG6rgQD3XnF/BuFFZw9A0TJgmMSO8FEWQgvcXw== - home-or-tmp@^2.0.0: version "2.0.0" resolved "https://registry.yarnpkg.com/home-or-tmp/-/home-or-tmp-2.0.0.tgz#e36c3f2d2cae7d746a857e38d18d5f32a7882db8" @@ -4235,15 +4191,6 @@ mute-stream@~0.0.4: version "0.0.8" resolved "https://registry.yarnpkg.com/mute-stream/-/mute-stream-0.0.8.tgz#1630c42b2251ff81e2a283de96a5497ea92e5e0d" -mz@^2.4.0: - version "2.7.0" - resolved "https://registry.yarnpkg.com/mz/-/mz-2.7.0.tgz#95008057a56cafadc2bc63dde7f9ff6955948e32" - integrity sha512-z81GNO7nnYMEhrGh9LeymoE4+Yr0Wn5McHIZMK5cfQCl+NDX08sCZgUc9/6MHni9IWuFLm1Z3HTCXu2z9fN62Q== - dependencies: - any-promise "^1.0.0" - object-assign "^4.0.1" - thenify-all "^1.0.0" - nan@^2.12.1: version "2.14.0" resolved "https://registry.yarnpkg.com/nan/-/nan-2.14.0.tgz#7818f722027b2459a86f0295d434d1fc2336c52c" @@ -4630,7 +4577,7 @@ oauth-sign@~0.9.0: version "0.9.0" resolved "https://registry.yarnpkg.com/oauth-sign/-/oauth-sign-0.9.0.tgz#47a7b016baa68b5fa0ecf3dee08a85c679ac6455" -object-assign@^4.0.1, object-assign@^4.1.0: +object-assign@^4.1.0: version "4.1.1" resolved "https://registry.yarnpkg.com/object-assign/-/object-assign-4.1.1.tgz#2109adc7965887cfc05cbbd442cac8bfbb360863" @@ -4874,11 +4821,6 @@ parent-module@^1.0.0: dependencies: callsites "^3.0.0" -parent-require@^1.0.0: - version "1.0.0" - resolved "https://registry.yarnpkg.com/parent-require/-/parent-require-1.0.0.tgz#746a167638083a860b0eef6732cb27ed46c32977" - integrity sha1-dGoWdjgIOoYLDu9nMssn7UbDKXc= - parse-github-url@^1.0.1: version "1.0.2" resolved "https://registry.yarnpkg.com/parse-github-url/-/parse-github-url-1.0.2.tgz#242d3b65cbcdda14bb50439e3242acf6971db395" @@ -4918,7 +4860,7 @@ parse-passwd@^1.0.0: version "1.0.0" resolved "https://registry.yarnpkg.com/parse-passwd/-/parse-passwd-1.0.0.tgz#6d5b934a456993b23d37f40a382d6f1666a8e5c6" -parse5@4.0.0, parse5@^4.0.0: +parse5@4.0.0: version "4.0.0" resolved "https://registry.yarnpkg.com/parse5/-/parse5-4.0.0.tgz#6d78656e3da8d78b4ec0b906f7c08ef1dfe3f608" @@ -5374,7 +5316,7 @@ redeyed@~2.1.0: dependencies: esprima "~4.0.0" -reflect-metadata@^0.1.12, reflect-metadata@^0.1.13: +reflect-metadata@^0.1.12: version "0.1.13" resolved "https://registry.yarnpkg.com/reflect-metadata/-/reflect-metadata-0.1.13.tgz#67ae3ca57c972a2aa1642b10fe363fe32d49dc08" @@ -6242,20 +6184,6 @@ text-table@~0.2.0: version "0.2.0" resolved "https://registry.yarnpkg.com/text-table/-/text-table-0.2.0.tgz#7f5ee823ae805207c00af2df4a84ec3fcfa570b4" -thenify-all@^1.0.0: - version "1.6.0" - resolved "https://registry.yarnpkg.com/thenify-all/-/thenify-all-1.6.0.tgz#1a1918d402d8fc3f98fbf234db0bcc8cc10e9726" - integrity sha1-GhkY1ALY/D+Y+/I02wvMjMEOlyY= - dependencies: - thenify ">= 3.1.0 < 4" - -"thenify@>= 3.1.0 < 4": - version "3.3.0" - resolved "https://registry.yarnpkg.com/thenify/-/thenify-3.3.0.tgz#e69e38a1babe969b0108207978b9f62b88604839" - integrity sha1-5p44obq+lpsBCCB5eLn2K4hgSDk= - dependencies: - any-promise "^1.0.0" - throat@^4.0.0: version "4.1.0" resolved "https://registry.yarnpkg.com/throat/-/throat-4.1.0.tgz#89037cbc92c56ab18926e6ba4cbb200e15672a6a" @@ -6522,26 +6450,6 @@ typedoc@^0.12.0: typedoc-default-themes "^0.5.0" typescript "3.0.x" -typeorm@^0.2.19: - version "0.2.19" - resolved "https://registry.yarnpkg.com/typeorm/-/typeorm-0.2.19.tgz#a0cff0714180e5720df157df02c5759a1a646dc3" - integrity sha512-xKVx/W41zckQ7v8WYcpRhSKpjXDKG/Jgjy0RWvYelR8ZnfyblNRL12jF4P8tIhwXv6l5t01s7HEc9lR+zb6Gtg== - dependencies: - app-root-path "^2.0.1" - buffer "^5.1.0" - chalk "^2.4.2" - cli-highlight "^2.0.0" - debug "^4.1.1" - dotenv "^6.2.0" - glob "^7.1.2" - js-yaml "^3.13.1" - mkdirp "^0.5.1" - reflect-metadata "^0.1.13" - tslib "^1.9.0" - xml2js "^0.4.17" - yargonaut "^1.1.2" - yargs "^13.2.1" - typescript@3.0.x: version "3.0.3" resolved "https://registry.yarnpkg.com/typescript/-/typescript-3.0.3.tgz#4853b3e275ecdaa27f78fda46dc273a7eb7fc1c8" @@ -6681,7 +6589,7 @@ util-promisify@^2.1.0: dependencies: object.getownpropertydescriptors "^2.0.3" -util.promisify@^1.0.0, util.promisify@~1.0.0: +util.promisify@^1.0.0: version "1.0.0" resolved "https://registry.yarnpkg.com/util.promisify/-/util.promisify-1.0.0.tgz#440f7165a459c9a16dc145eb8e72f35687097030" dependencies: @@ -6885,20 +6793,6 @@ xml2js@0.4.19: sax ">=0.6.0" xmlbuilder "~9.0.1" -xml2js@^0.4.17: - version "0.4.22" - resolved "https://registry.yarnpkg.com/xml2js/-/xml2js-0.4.22.tgz#4fa2d846ec803237de86f30aa9b5f70b6600de02" - integrity sha512-MWTbxAQqclRSTnehWWe5nMKzI3VmJ8ltiJEco8akcC6j3miOhjjfzKum5sId+CWhfxdOs/1xauYr8/ZDBtQiRw== - dependencies: - sax ">=0.6.0" - util.promisify "~1.0.0" - xmlbuilder "~11.0.0" - -xmlbuilder@~11.0.0: - version "11.0.1" - resolved "https://registry.yarnpkg.com/xmlbuilder/-/xmlbuilder-11.0.1.tgz#be9bae1c8a046e76b31127726347d0ad7002beb3" - integrity sha512-fDlsI/kFEx7gLvbecc0/ohLG50fugQp8ryHzMTuW9vSa1GJ0XYWKnhsUx7oie3G98+r56aTQIUB4kht42R3JvA== - xmlbuilder@~9.0.1: version "9.0.7" resolved "https://registry.yarnpkg.com/xmlbuilder/-/xmlbuilder-9.0.7.tgz#132ee63d2ec5565c557e20f4c22df9aca686b10d" @@ -6923,22 +6817,13 @@ yallist@^3.0.0, yallist@^3.0.2, yallist@^3.0.3: version "3.0.3" resolved "https://registry.yarnpkg.com/yallist/-/yallist-3.0.3.tgz#b4b049e314be545e3ce802236d6cd22cd91c3de9" -yargonaut@^1.1.2: - version "1.1.4" - resolved "https://registry.yarnpkg.com/yargonaut/-/yargonaut-1.1.4.tgz#c64f56432c7465271221f53f5cc517890c3d6e0c" - integrity sha512-rHgFmbgXAAzl+1nngqOcwEljqHGG9uUZoPjsdZEs1w5JW9RXYzrSvH/u70C1JE5qFi0qjsdhnUX/dJRpWqitSA== - dependencies: - chalk "^1.1.1" - figlet "^1.1.1" - parent-require "^1.0.0" - yargs-parser@10.x, yargs-parser@^10.0.0: version "10.1.0" resolved "https://registry.yarnpkg.com/yargs-parser/-/yargs-parser-10.1.0.tgz#7202265b89f7e9e9f2e5765e0fe735a905edbaa8" dependencies: camelcase "^4.1.0" -yargs-parser@^13.1.0, yargs-parser@^13.1.1: +yargs-parser@^13.1.0: version "13.1.1" resolved "https://registry.yarnpkg.com/yargs-parser/-/yargs-parser-13.1.1.tgz#d26058532aa06d365fe091f6a1fc06b2f7e5eca0" dependencies: @@ -6968,22 +6853,6 @@ yargs@^11.0.0: y18n "^3.2.1" yargs-parser "^9.0.2" -yargs@^13.0.0, yargs@^13.2.1: - version "13.3.0" - resolved "https://registry.yarnpkg.com/yargs/-/yargs-13.3.0.tgz#4c657a55e07e5f2cf947f8a366567c04a0dedc83" - integrity sha512-2eehun/8ALW8TLoIl7MVaRUrg+yCnenu8B4kBlRxj3GJGDKU1Og7sMXPNm1BYyM1DOJmTZ4YeN/Nwxv+8XJsUA== - dependencies: - cliui "^5.0.0" - find-up "^3.0.0" - get-caller-file "^2.0.1" - require-directory "^2.1.1" - require-main-filename "^2.0.0" - set-blocking "^2.0.0" - string-width "^3.0.0" - which-module "^2.0.0" - y18n "^4.0.0" - yargs-parser "^13.1.1" - yargs@^13.1.0, yargs@^13.2.2: version "13.2.4" resolved "https://registry.yarnpkg.com/yargs/-/yargs-13.2.4.tgz#0b562b794016eb9651b98bd37acf364aa5d6dc83" From 2cbc6847ce81807a552247fd7c06a98d296cdfed Mon Sep 17 00:00:00 2001 From: Arseny Yankovsky Date: Thu, 19 Sep 2019 13:44:29 +0200 Subject: [PATCH 03/23] Attempt to reproduce the issue --- src/typeorm-aurora-data-api-driver.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/typeorm-aurora-data-api-driver.ts b/src/typeorm-aurora-data-api-driver.ts index bb5d00a..9979238 100644 --- a/src/typeorm-aurora-data-api-driver.ts +++ b/src/typeorm-aurora-data-api-driver.ts @@ -102,6 +102,8 @@ export default class DataApiDriver { } public async query(query: string, parameters?: any[]): Promise { + console.log('query before transformation: ', query) + console.log(' parameters: ', JSON.stringify(parameters)) const transformedQueryData = DataApiDriver.transformQueryAndParameters(query, parameters) if (this.loggerFn) { From 72248c2049580519a18a0a32fee007c2ca6bde63 Mon Sep 17 00:00:00 2001 From: Arseny Yankovsky Date: Thu, 19 Sep 2019 13:49:00 +0200 Subject: [PATCH 04/23] Attempt to reproduce the issue --- test/functional/basic/simple-queries.func.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/functional/basic/simple-queries.func.test.ts b/test/functional/basic/simple-queries.func.test.ts index e3c7852..968ae38 100644 --- a/test/functional/basic/simple-queries.func.test.ts +++ b/test/functional/basic/simple-queries.func.test.ts @@ -5,7 +5,7 @@ import { Post } from './entity/Post' describe('aurora data api > simple queries', () => { jest.setTimeout(240000) - it('should do a simple select', async () => { + it.skip('should do a simple select', async () => { const connection = await createConnection({ type: 'aurora-data-api', database: process.env.database!, From 40580236f8ca277fdcc72cb5bdc38c653e0e4540 Mon Sep 17 00:00:00 2001 From: Arseny Yankovsky Date: Thu, 19 Sep 2019 14:01:10 +0200 Subject: [PATCH 05/23] Test with fixed typeorm --- .circleci/config.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index 2ce2afb..4de6b86 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -64,7 +64,7 @@ jobs: command: npm link - run: name: checkout typeorm - command: git clone --single-branch --branch feature/aurora-data-api https://github.com/ArsenyYankovsky/typeorm.git + command: git clone --single-branch --branch fix-update https://github.com/ArsenyYankovsky/typeorm.git - run: name: build typeorm with current package command: cd typeorm/ && npm i && npm run package && cd build/package && npm link && rm -rf node_modules/typeorm-aurora-data-api-driver && npm link typeorm-aurora-data-api-driver From 394d48f5ba7ae2176d879d78b647725fb5b058fd Mon Sep 17 00:00:00 2001 From: Arseny Yankovsky Date: Thu, 19 Sep 2019 14:44:19 +0200 Subject: [PATCH 06/23] Test with fixed typeorm --- test/functional/basic/simple-queries.func.test.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/test/functional/basic/simple-queries.func.test.ts b/test/functional/basic/simple-queries.func.test.ts index 968ae38..768fd3b 100644 --- a/test/functional/basic/simple-queries.func.test.ts +++ b/test/functional/basic/simple-queries.func.test.ts @@ -38,6 +38,8 @@ describe('aurora data api > simple queries', () => { }) it('should create a table and be able to query it', async () => { + console.log(process.env.database) + const connection = await createConnection({ type: 'aurora-data-api', database: process.env.database!, From cf215dba1db81b7c777d8086107da280fc6c6d5f Mon Sep 17 00:00:00 2001 From: Arseny Yankovsky Date: Thu, 19 Sep 2019 14:58:21 +0200 Subject: [PATCH 07/23] Test with fixed typeorm --- src/typeorm-aurora-data-api-driver.ts | 2 -- test/functional/basic/simple-queries.func.test.ts | 6 ++---- 2 files changed, 2 insertions(+), 6 deletions(-) diff --git a/src/typeorm-aurora-data-api-driver.ts b/src/typeorm-aurora-data-api-driver.ts index 9979238..bb5d00a 100644 --- a/src/typeorm-aurora-data-api-driver.ts +++ b/src/typeorm-aurora-data-api-driver.ts @@ -102,8 +102,6 @@ export default class DataApiDriver { } public async query(query: string, parameters?: any[]): Promise { - console.log('query before transformation: ', query) - console.log(' parameters: ', JSON.stringify(parameters)) const transformedQueryData = DataApiDriver.transformQueryAndParameters(query, parameters) if (this.loggerFn) { diff --git a/test/functional/basic/simple-queries.func.test.ts b/test/functional/basic/simple-queries.func.test.ts index 768fd3b..00c832d 100644 --- a/test/functional/basic/simple-queries.func.test.ts +++ b/test/functional/basic/simple-queries.func.test.ts @@ -5,7 +5,7 @@ import { Post } from './entity/Post' describe('aurora data api > simple queries', () => { jest.setTimeout(240000) - it.skip('should do a simple select', async () => { + it('should do a simple select', async () => { const connection = await createConnection({ type: 'aurora-data-api', database: process.env.database!, @@ -38,8 +38,6 @@ describe('aurora data api > simple queries', () => { }) it('should create a table and be able to query it', async () => { - console.log(process.env.database) - const connection = await createConnection({ type: 'aurora-data-api', database: process.env.database!, @@ -76,7 +74,7 @@ describe('aurora data api > simple queries', () => { await connection.close() }) - it('should be able to update', async () => { + it('should be able to update a post', async () => { const connection = await createConnection({ type: 'aurora-data-api', database: process.env.database!, From bb470058b8bdb9a17ca25800a2648e511dbfa675 Mon Sep 17 00:00:00 2001 From: ayankovsky Date: Sun, 13 Oct 2019 23:03:54 +0200 Subject: [PATCH 08/23] Merged master --- .../basic/simple-queries.func.test.ts | 124 +++++++--------- yarn.lock | 134 ++---------------- 2 files changed, 60 insertions(+), 198 deletions(-) diff --git a/test/functional/basic/simple-queries.func.test.ts b/test/functional/basic/simple-queries.func.test.ts index 285f0db..dee6d08 100644 --- a/test/functional/basic/simple-queries.func.test.ts +++ b/test/functional/basic/simple-queries.func.test.ts @@ -33,66 +33,38 @@ describe('aurora data api > simple queries', () => { const dbPost = await postRepository.findOne({ id: insertResult.id }) expect(dbPost).toBeTruthy() - expect(dbPost!.title).toBe('My First Post') - expect(dbPost!.text).toBe('Post Text') - expect(dbPost!.likesCount).toBe(4) - - await connection.query('DROP TABLE aurora_data_api_test_post;') - - await connection.close() + expect(dbPost!.title).toBe('My First Post') + expect(dbPost!.text).toBe('Post Text') + expect(dbPost!.likesCount).toBe(4) + }) }) it('should be able to update a post', async () => { - const connection = await createConnection({ - type: 'aurora-data-api', - database: process.env.database!, - secretArn: process.env.secretArn!, - resourceArn: process.env.resourceArn!, - region: process.env.region!, - entities: [Post], - synchronize: true, - logging: true, - }) - - const postRepository = connection.getRepository(Post) - - const post = new Post() + await useCleanDatabase({ entities: [Post, Category] }, async (connection) => { - post.title = 'My First Post' - post.text = 'Post Text' - post.likesCount = 4 - post.publishedAt = new Date(2017, 1, 1) + const postRepository = connection.getRepository(Post) - const insertResult = await postRepository.save(post) + const post = new Post() - const postId = insertResult.id + post.title = 'My First Post' + post.text = 'Post Text' + post.likesCount = 4 + post.publishedAt = new Date(2017, 1, 1) - const dbPost = await postRepository.findOne({ id: postId }) + const insertResult = await postRepository.save(post) - dbPost!.publishedAt = new Date() + const postId = insertResult.id - await postRepository.save(dbPost!) + const dbPost = await postRepository.findOne({ id: postId }) - const updatedPost = await postRepository.findOne(postId) + dbPost!.publishedAt = new Date() - expect(updatedPost!.publishedAt > new Date(2017, 1, 1)).toBeTruthy() + await postRepository.save(dbPost!) - await connection.query('DROP TABLE aurora_data_api_test_post;') + const updatedPost = await postRepository.findOne(postId) - await connection.close() - }) + expect(updatedPost!.publishedAt > new Date(2017, 1, 1)).toBeTruthy() - it('batch insert - with dates', async () => { - const connection = await createConnection({ - type: 'aurora-data-api', - database: process.env.database!, - secretArn: process.env.secretArn!, - resourceArn: process.env.resourceArn!, - region: process.env.region!, - entities: [Post], - synchronize: true, - logging: true, - }) expect(dbPost!.title).toBe('My First Post') expect(dbPost!.text).toBe('Post Text') expect(dbPost!.likesCount).toBe(4) @@ -133,22 +105,22 @@ describe('aurora data api > simple queries', () => { it('should be able to create and query a many-to-many relationship', async () => { await useCleanDatabase({ entities: [Post, Category] }, async (connection) => { - // Create categories + // Create categories const categoryRepository = connection.getRepository(Category) const firstCategory = await categoryRepository.save( - categoryRepository.create({ - name: 'first', - }), - ) + categoryRepository.create({ + name: 'first', + }), + ) const secondCategory = await categoryRepository.save( - categoryRepository.create({ - name: 'second', - }), - ) + categoryRepository.create({ + name: 'second', + }), + ) - // Create a post and associate with created categories + // Create a post and associate with created categories const postRepository = connection.getRepository(Post) const post = postRepository.create({ @@ -161,9 +133,9 @@ describe('aurora data api > simple queries', () => { const storedPost = await postRepository.save(post) - // Assert + // Assert const dbPost = await postRepository.findOne( - storedPost.id, { relations: ['categories'] }) + storedPost.id, { relations: ['categories'] }) expect(dbPost).toBeTruthy() expect(dbPost!.categories).toBeTruthy() @@ -173,19 +145,19 @@ describe('aurora data api > simple queries', () => { it('should be able to update a date field by primary key', async () => { await useCleanDatabase({ entities: [Post, Category] }, async (connection) => { - // Create a post and associate with created categories + // Create a post and associate with created categories const postRepository = connection.getRepository(Post) const storedPost = await postRepository.save( - postRepository.create({ - title: 'Post For Update', - text: 'Text', - likesCount: 6, - publishedAt: new Date(), - }), - ) - - // Retrieve the post and update the date + postRepository.create({ + title: 'Post For Update', + text: 'Text', + likesCount: 6, + publishedAt: new Date(), + }), + ) + + // Retrieve the post and update the date const getPost = await postRepository.findOne(storedPost.id) expect(getPost).toBeTruthy() @@ -193,7 +165,7 @@ describe('aurora data api > simple queries', () => { getPost!.updatedAt = updatedAt await postRepository.save(getPost!) - // Assert + // Assert const dbPost = await postRepository.findOne(storedPost.id) expect(dbPost).toBeTruthy() expect(Math.round(dbPost!.updatedAt!.getTime() / 1000)).toEqual(Math.round(updatedAt.getTime() / 1000)) @@ -206,17 +178,17 @@ describe('aurora data api > simple queries', () => { const newCategories = categoryNames.map(name => ({ name })) await connection.createQueryBuilder() - .insert() - .into(Category) - .values(newCategories) - .orIgnore() - .execute() + .insert() + .into(Category) + .values(newCategories) + .orIgnore() + .execute() - // Query back the inserted categories + // Query back the inserted categories const categoryRepository = connection.getRepository(Category) const categories = await categoryRepository.find() - // Assert + // Assert expect(categories.length).toBe(4) expect(categories[0].name = 'one') expect(categories[1].name = 'two') diff --git a/yarn.lock b/yarn.lock index f3e8af0..7f3fcdc 100644 --- a/yarn.lock +++ b/yarn.lock @@ -533,11 +533,6 @@ ansistyles@~0.1.3: resolved "https://registry.yarnpkg.com/ansistyles/-/ansistyles-0.1.3.tgz#5de60415bda071bb37127854c864f41b23254539" integrity sha1-XeYEFb2gcbs3EnhUyGT0GyMlRTk= -any-promise@^1.0.0: - version "1.3.0" - resolved "https://registry.yarnpkg.com/any-promise/-/any-promise-1.3.0.tgz#abc6afeedcea52e809cdc0376aed3ce39635d17f" - integrity sha1-q8av7tzqUugJzcA3au0845Y10X8= - anymatch@^2.0.0: version "2.0.0" resolved "https://registry.yarnpkg.com/anymatch/-/anymatch-2.0.0.tgz#bcb24b4f37934d9aa7ac17b4adaf89e7c76ef2eb" @@ -546,11 +541,6 @@ anymatch@^2.0.0: micromatch "^3.1.4" normalize-path "^2.1.1" -app-root-path@^2.0.1: - version "2.2.1" - resolved "https://registry.yarnpkg.com/app-root-path/-/app-root-path-2.2.1.tgz#d0df4a682ee408273583d43f6f79e9892624bc9a" - integrity sha512-91IFKeKk7FjfmezPKkwtaRvSpnUc4gDwPAjA1YZ9Gn0q0PPeW+vbeUsZuyDwjI7+QTHhcLen2v25fi/AmhvbJA== - append-transform@^0.4.0: version "0.4.0" resolved "https://registry.yarnpkg.com/append-transform/-/append-transform-0.4.0.tgz#d76ebf8ca94d276e247a36bad44a4b74ab611991" @@ -719,10 +709,10 @@ atob@^2.1.1: resolved "https://registry.yarnpkg.com/atob/-/atob-2.1.2.tgz#6d9517eb9e030d2436666651e86bd9f6f13533c9" integrity sha512-Wm6ukoaOGJi/73p/cl2GvLjTI5JM1k/O14isD73YML8StrH/7/lRFgmg8nICZgD3bZZvjwCGxtMOD3wWNAu8cg== -aws-sdk@2.533.0: - version "2.533.0" - resolved "https://registry.yarnpkg.com/aws-sdk/-/aws-sdk-2.533.0.tgz#69298d2e6c646ce5cb5ba2bf414c9e53ec3f7761" - integrity sha512-OSe7C0jnBdgfzedOtK+TgBEwtJaaZovm+Q2fbqIlfhUWg3rhhkt3oLyQ9bRD30/CFaunLhLr/8HAAN+/x+DWAA== +aws-sdk@^2.492.0: + version "2.548.0" + resolved "https://registry.yarnpkg.com/aws-sdk/-/aws-sdk-2.548.0.tgz#20a3c7a77e1c6fd694cdb5e0ecc0ccf0d0542674" + integrity sha512-dnG1MEc3uOonGDswHPiq7TYn6EOG3+IQhF2LSRambz9adaZxwRgFOII7UVPJjh33FtxKpMzOSmf62R+EX822Nw== dependencies: buffer "4.9.1" events "1.1.1" @@ -1068,14 +1058,6 @@ buffer@4.9.1: ieee754 "^1.1.4" isarray "^1.0.0" -buffer@^5.1.0: - version "5.4.3" - resolved "https://registry.yarnpkg.com/buffer/-/buffer-5.4.3.tgz#3fbc9c69eb713d323e3fc1a895eee0710c072115" - integrity sha512-zvj65TkFeIt3i6aj5bIvJDzjjQQGs4o/sNoezg1F1kYap9Nu2jcUdpwzRSJTHMMzG0H7bZkn4rNQpImhuxWX2A== - dependencies: - base64-js "^1.0.2" - ieee754 "^1.1.4" - builtin-modules@^1.1.1: version "1.1.1" resolved "https://registry.yarnpkg.com/builtin-modules/-/builtin-modules-1.1.1.tgz#270f076c5a72c02f5b65a47df94c5fe3a278892f" @@ -1224,7 +1206,7 @@ chalk@2.3.1: escape-string-regexp "^1.0.5" supports-color "^5.2.0" -chalk@^1.1.1, chalk@^1.1.3: +chalk@^1.1.3: version "1.1.3" resolved "https://registry.yarnpkg.com/chalk/-/chalk-1.1.3.tgz#a8115c55e4a702fe4d150abd3872822a7e09fc98" integrity sha1-qBFcVeSnAv5NFQq9OHKCKn4J/Jg= @@ -1306,17 +1288,6 @@ cli-cursor@^2.1.0: dependencies: restore-cursor "^2.0.0" -cli-highlight@^2.0.0: - version "2.1.1" - resolved "https://registry.yarnpkg.com/cli-highlight/-/cli-highlight-2.1.1.tgz#2180223d51618b112f4509cf96e4a6c750b07e97" - integrity sha512-0y0VlNmdD99GXZHYnvrQcmHxP8Bi6T00qucGgBgGv4kJ0RyDthNnnFPupHV7PYv/OXSVk+azFbOeaW6+vGmx9A== - dependencies: - chalk "^2.3.0" - highlight.js "^9.6.0" - mz "^2.4.0" - parse5 "^4.0.0" - yargs "^13.0.0" - cli-table3@^0.5.0, cli-table3@^0.5.1: version "0.5.1" resolved "https://registry.yarnpkg.com/cli-table3/-/cli-table3-0.5.1.tgz#0252372d94dfc40dbd8df06005f48f31f656f202" @@ -1773,7 +1744,7 @@ debug@^3.1.0, debug@^3.2.6: dependencies: ms "^2.1.1" -debug@^4.0.0, debug@^4.1.1: +debug@^4.0.0: version "4.1.1" resolved "https://registry.yarnpkg.com/debug/-/debug-4.1.1.tgz#3b72260255109c6b589cee050f1d516139664791" integrity sha512-pYAIzeRo8J6KPEaJ0VWOh5Pzkbw/RetuzehGM7QRRX5he4fPHx2rdKMB256ehJCkX+XRQm16eZLqLNS8RSZXZw== @@ -1967,11 +1938,6 @@ dotenv@^5.0.1: resolved "https://registry.yarnpkg.com/dotenv/-/dotenv-5.0.1.tgz#a5317459bd3d79ab88cff6e44057a6a3fbb1fcef" integrity sha512-4As8uPrjfwb7VXC+WnLCbXK7y+Ueb2B3zgNCePYfhxS1PYeaO1YTeplffTEcbfLhvFNGLAz90VvJs9yomG7bow== -dotenv@^6.2.0: - version "6.2.0" - resolved "https://registry.yarnpkg.com/dotenv/-/dotenv-6.2.0.tgz#941c0410535d942c8becf28d3f357dbd9d476064" - integrity sha512-HygQCKUBSFl8wKQZBSemMywRWcEDNidvNbjGVyZu3nbZ8qq9ubiPoGLMdRDpfSrpkkm9BXYFkpKxxFX38o/76w== - duplexer2@~0.1.0: version "0.1.4" resolved "https://registry.yarnpkg.com/duplexer2/-/duplexer2-0.1.4.tgz#8b12dab878c0d69e3e7891051662a32fc6bddcc1" @@ -2362,11 +2328,6 @@ figgy-pudding@^3.4.1, figgy-pudding@^3.5.1: resolved "https://registry.yarnpkg.com/figgy-pudding/-/figgy-pudding-3.5.1.tgz#862470112901c727a0e495a80744bd5baa1d6790" integrity sha512-vNKxJHTEKNThjfrdJwHc7brvM6eVevuO5nTj6ez8ZQ1qbXTvGthucRF7S4vf2cr71QVnT70V34v0S1DyQsti0w== -figlet@^1.1.1: - version "1.2.4" - resolved "https://registry.yarnpkg.com/figlet/-/figlet-1.2.4.tgz#2d2f48b61a77418ba6cbd9db2e25da27efee16d0" - integrity sha512-mv8YA9RruB4C5QawPaD29rEVx3N97ZTyNrE4DAfbhuo6tpcMdKnPVo8MlyT3RP5uPcg5M14bEJBq7kjFf4kAWg== - figures@^2.0.0: version "2.0.0" resolved "https://registry.yarnpkg.com/figures/-/figures-2.0.0.tgz#3ab1a2d2a62c8bfb431a0c94cb797a2fce27c962" @@ -2943,7 +2904,7 @@ has@^1.0.1, has@^1.0.3: dependencies: function-bind "^1.1.1" -highlight.js@^9.0.0, highlight.js@^9.6.0: +highlight.js@^9.0.0: version "9.15.10" resolved "https://registry.yarnpkg.com/highlight.js/-/highlight.js-9.15.10.tgz#7b18ed75c90348c045eef9ed08ca1319a2219ad2" integrity sha512-RoV7OkQm0T3os3Dd2VHLNMoaoDVx77Wygln3n9l5YV172XonWG6rgQD3XnF/BuFFZw9A0TJgmMSO8FEWQgvcXw== @@ -4892,15 +4853,6 @@ mute-stream@~0.0.4: resolved "https://registry.yarnpkg.com/mute-stream/-/mute-stream-0.0.8.tgz#1630c42b2251ff81e2a283de96a5497ea92e5e0d" integrity sha512-nnbWWOkoWyUsTjKrhgD0dcz22mdkSnpYqbEjIm2nhwhuxlSkpywJmBo8h0ZqJdkp73mb90SssHkN4rsRaBAfAA== -mz@^2.4.0: - version "2.7.0" - resolved "https://registry.yarnpkg.com/mz/-/mz-2.7.0.tgz#95008057a56cafadc2bc63dde7f9ff6955948e32" - integrity sha512-z81GNO7nnYMEhrGh9LeymoE4+Yr0Wn5McHIZMK5cfQCl+NDX08sCZgUc9/6MHni9IWuFLm1Z3HTCXu2z9fN62Q== - dependencies: - any-promise "^1.0.0" - object-assign "^4.0.1" - thenify-all "^1.0.0" - nan@^2.12.1: version "2.14.0" resolved "https://registry.yarnpkg.com/nan/-/nan-2.14.0.tgz#7818f722027b2459a86f0295d434d1fc2336c52c" @@ -5322,7 +5274,7 @@ oauth-sign@~0.9.0: resolved "https://registry.yarnpkg.com/oauth-sign/-/oauth-sign-0.9.0.tgz#47a7b016baa68b5fa0ecf3dee08a85c679ac6455" integrity sha512-fexhUFFPTGV8ybAtSIGbV6gOkSv8UtRbDBnAyLQw4QPKkgNlsH2ByPGtMUqdWkos6YCRmAqViwgZrJc/mRDzZQ== -object-assign@^4.0.1, object-assign@^4.1.0: +object-assign@^4.1.0: version "4.1.1" resolved "https://registry.yarnpkg.com/object-assign/-/object-assign-4.1.1.tgz#2109adc7965887cfc05cbbd442cac8bfbb360863" integrity sha1-IQmtx5ZYh8/AXLvUQsrIv7s2CGM= @@ -5608,11 +5560,6 @@ parent-module@^1.0.0: dependencies: callsites "^3.0.0" -parent-require@^1.0.0: - version "1.0.0" - resolved "https://registry.yarnpkg.com/parent-require/-/parent-require-1.0.0.tgz#746a167638083a860b0eef6732cb27ed46c32977" - integrity sha1-dGoWdjgIOoYLDu9nMssn7UbDKXc= - parse-github-url@^1.0.1: version "1.0.2" resolved "https://registry.yarnpkg.com/parse-github-url/-/parse-github-url-1.0.2.tgz#242d3b65cbcdda14bb50439e3242acf6971db395" @@ -5658,7 +5605,7 @@ parse-passwd@^1.0.0: resolved "https://registry.yarnpkg.com/parse-passwd/-/parse-passwd-1.0.0.tgz#6d5b934a456993b23d37f40a382d6f1666a8e5c6" integrity sha1-bVuTSkVpk7I9N/QKOC1vFmao5cY= -parse5@4.0.0, parse5@^4.0.0: +parse5@4.0.0: version "4.0.0" resolved "https://registry.yarnpkg.com/parse5/-/parse5-4.0.0.tgz#6d78656e3da8d78b4ec0b906f7c08ef1dfe3f608" integrity sha512-VrZ7eOd3T1Fk4XWNXMgiGBK/z0MG48BWG2uQNU4I72fkQuKUTZpl+u9k+CxEG0twMVzSmXEEz12z5Fnw1jIQFA== @@ -6197,7 +6144,7 @@ redeyed@~2.1.0: dependencies: esprima "~4.0.0" -reflect-metadata@^0.1.12, reflect-metadata@^0.1.13: +reflect-metadata@^0.1.12: version "0.1.13" resolved "https://registry.yarnpkg.com/reflect-metadata/-/reflect-metadata-0.1.13.tgz#67ae3ca57c972a2aa1642b10fe363fe32d49dc08" integrity sha512-Ts1Y/anZELhSsjMcU605fU9RE4Oi3p5ORujwbIKXfWa+0Zxs510Qrmrce5/Jowq3cHSZSJqBjypxmHarc+vEWg== @@ -7223,20 +7170,6 @@ text-table@~0.2.0: resolved "https://registry.yarnpkg.com/text-table/-/text-table-0.2.0.tgz#7f5ee823ae805207c00af2df4a84ec3fcfa570b4" integrity sha1-f17oI66AUgfACvLfSoTsP8+lcLQ= -thenify-all@^1.0.0: - version "1.6.0" - resolved "https://registry.yarnpkg.com/thenify-all/-/thenify-all-1.6.0.tgz#1a1918d402d8fc3f98fbf234db0bcc8cc10e9726" - integrity sha1-GhkY1ALY/D+Y+/I02wvMjMEOlyY= - dependencies: - thenify ">= 3.1.0 < 4" - -"thenify@>= 3.1.0 < 4": - version "3.3.0" - resolved "https://registry.yarnpkg.com/thenify/-/thenify-3.3.0.tgz#e69e38a1babe969b0108207978b9f62b88604839" - integrity sha1-5p44obq+lpsBCCB5eLn2K4hgSDk= - dependencies: - any-promise "^1.0.0" - throat@^4.0.0: version "4.1.0" resolved "https://registry.yarnpkg.com/throat/-/throat-4.1.0.tgz#89037cbc92c56ab18926e6ba4cbb200e15672a6a" @@ -7551,26 +7484,6 @@ typedoc@^0.12.0: typedoc-default-themes "^0.5.0" typescript "3.0.x" -typeorm@^0.2.19: - version "0.2.19" - resolved "https://registry.yarnpkg.com/typeorm/-/typeorm-0.2.19.tgz#a0cff0714180e5720df157df02c5759a1a646dc3" - integrity sha512-xKVx/W41zckQ7v8WYcpRhSKpjXDKG/Jgjy0RWvYelR8ZnfyblNRL12jF4P8tIhwXv6l5t01s7HEc9lR+zb6Gtg== - dependencies: - app-root-path "^2.0.1" - buffer "^5.1.0" - chalk "^2.4.2" - cli-highlight "^2.0.0" - debug "^4.1.1" - dotenv "^6.2.0" - glob "^7.1.2" - js-yaml "^3.13.1" - mkdirp "^0.5.1" - reflect-metadata "^0.1.13" - tslib "^1.9.0" - xml2js "^0.4.17" - yargonaut "^1.1.2" - yargs "^13.2.1" - typescript@3.0.x: version "3.0.3" resolved "https://registry.yarnpkg.com/typescript/-/typescript-3.0.3.tgz#4853b3e275ecdaa27f78fda46dc273a7eb7fc1c8" @@ -7730,7 +7643,7 @@ util-promisify@^2.1.0: dependencies: object.getownpropertydescriptors "^2.0.3" -util.promisify@^1.0.0, util.promisify@~1.0.0: +util.promisify@^1.0.0: version "1.0.0" resolved "https://registry.yarnpkg.com/util.promisify/-/util.promisify-1.0.0.tgz#440f7165a459c9a16dc145eb8e72f35687097030" integrity sha512-i+6qA2MPhvoKLuxnJNpXAGhg7HphQOSUq2LKMZD0m15EiskXUkMvKdF4Uui0WYeCUGea+o2cw/ZuwehtfsrNkA== @@ -7972,20 +7885,6 @@ xml2js@0.4.19: sax ">=0.6.0" xmlbuilder "~9.0.1" -xml2js@^0.4.17: - version "0.4.22" - resolved "https://registry.yarnpkg.com/xml2js/-/xml2js-0.4.22.tgz#4fa2d846ec803237de86f30aa9b5f70b6600de02" - integrity sha512-MWTbxAQqclRSTnehWWe5nMKzI3VmJ8ltiJEco8akcC6j3miOhjjfzKum5sId+CWhfxdOs/1xauYr8/ZDBtQiRw== - dependencies: - sax ">=0.6.0" - util.promisify "~1.0.0" - xmlbuilder "~11.0.0" - -xmlbuilder@~11.0.0: - version "11.0.1" - resolved "https://registry.yarnpkg.com/xmlbuilder/-/xmlbuilder-11.0.1.tgz#be9bae1c8a046e76b31127726347d0ad7002beb3" - integrity sha512-fDlsI/kFEx7gLvbecc0/ohLG50fugQp8ryHzMTuW9vSa1GJ0XYWKnhsUx7oie3G98+r56aTQIUB4kht42R3JvA== - xmlbuilder@~9.0.1: version "9.0.7" resolved "https://registry.yarnpkg.com/xmlbuilder/-/xmlbuilder-9.0.7.tgz#132ee63d2ec5565c557e20f4c22df9aca686b10d" @@ -8016,15 +7915,6 @@ yallist@^3.0.0, yallist@^3.0.2, yallist@^3.0.3: resolved "https://registry.yarnpkg.com/yallist/-/yallist-3.0.3.tgz#b4b049e314be545e3ce802236d6cd22cd91c3de9" integrity sha512-S+Zk8DEWE6oKpV+vI3qWkaK+jSbIK86pCwe2IF/xwIpQ8jEuxpw9NyaGjmp9+BoJv5FV2piqCDcoCtStppiq2A== -yargonaut@^1.1.2: - version "1.1.4" - resolved "https://registry.yarnpkg.com/yargonaut/-/yargonaut-1.1.4.tgz#c64f56432c7465271221f53f5cc517890c3d6e0c" - integrity sha512-rHgFmbgXAAzl+1nngqOcwEljqHGG9uUZoPjsdZEs1w5JW9RXYzrSvH/u70C1JE5qFi0qjsdhnUX/dJRpWqitSA== - dependencies: - chalk "^1.1.1" - figlet "^1.1.1" - parent-require "^1.0.0" - yargs-parser@10.x, yargs-parser@^10.0.0: version "10.1.0" resolved "https://registry.yarnpkg.com/yargs-parser/-/yargs-parser-10.1.0.tgz#7202265b89f7e9e9f2e5765e0fe735a905edbaa8" @@ -8065,7 +7955,7 @@ yargs@^11.0.0: y18n "^3.2.1" yargs-parser "^9.0.2" -yargs@^13.0.0, yargs@^13.2.1, yargs@^13.2.2: +yargs@^13.2.2: version "13.3.0" resolved "https://registry.yarnpkg.com/yargs/-/yargs-13.3.0.tgz#4c657a55e07e5f2cf947f8a366567c04a0dedc83" integrity sha512-2eehun/8ALW8TLoIl7MVaRUrg+yCnenu8B4kBlRxj3GJGDKU1Og7sMXPNm1BYyM1DOJmTZ4YeN/Nwxv+8XJsUA== From 5a939d084ea98dd05a5e185609bc98ef6523c12e Mon Sep 17 00:00:00 2001 From: ayankovsky Date: Sun, 13 Oct 2019 23:13:53 +0200 Subject: [PATCH 09/23] Checkout master of typeorm for the functional tests --- .circleci/config.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index 0fc718f..86fa111 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -64,7 +64,7 @@ jobs: command: npm link - run: name: checkout typeorm - command: git clone --single-branch --branch fix-update https://github.com/ArsenyYankovsky/typeorm.git + command: git clone --single-branch --branch master https://github.com/typeorm/typeorm.git - run: name: build typeorm with current package command: cd typeorm/ && npm i && npm run package && cd build/package && npm link && rm -rf node_modules/typeorm-aurora-data-api-driver && npm link typeorm-aurora-data-api-driver From 707e6d319834122c1a629f45c983c1695f643c2d Mon Sep 17 00:00:00 2001 From: ayankovsky Date: Sun, 13 Oct 2019 23:32:26 +0200 Subject: [PATCH 10/23] Checkout master of typeorm for the functional tests --- test/functional/basic/simple-queries.func.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/functional/basic/simple-queries.func.test.ts b/test/functional/basic/simple-queries.func.test.ts index dee6d08..d46b726 100644 --- a/test/functional/basic/simple-queries.func.test.ts +++ b/test/functional/basic/simple-queries.func.test.ts @@ -153,7 +153,7 @@ describe('aurora data api > simple queries', () => { title: 'Post For Update', text: 'Text', likesCount: 6, - publishedAt: new Date(), + publishedAt: null, }), ) From 3046bb89f87f8dd836a9869abc818a6a31e5fd67 Mon Sep 17 00:00:00 2001 From: ayankovsky Date: Sun, 13 Oct 2019 23:36:24 +0200 Subject: [PATCH 11/23] Checkout master of typeorm for the functional tests --- test/functional/basic/simple-queries.func.test.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/test/functional/basic/simple-queries.func.test.ts b/test/functional/basic/simple-queries.func.test.ts index d46b726..eb39c2f 100644 --- a/test/functional/basic/simple-queries.func.test.ts +++ b/test/functional/basic/simple-queries.func.test.ts @@ -153,7 +153,6 @@ describe('aurora data api > simple queries', () => { title: 'Post For Update', text: 'Text', likesCount: 6, - publishedAt: null, }), ) From 6f7f958f7ff03a2350e8f8014875f75bac9a0457 Mon Sep 17 00:00:00 2001 From: ayankovsky Date: Sun, 13 Oct 2019 23:43:01 +0200 Subject: [PATCH 12/23] Checkout master of typeorm for the functional tests --- test/functional/basic/simple-queries.func.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/functional/basic/simple-queries.func.test.ts b/test/functional/basic/simple-queries.func.test.ts index eb39c2f..0a73848 100644 --- a/test/functional/basic/simple-queries.func.test.ts +++ b/test/functional/basic/simple-queries.func.test.ts @@ -167,7 +167,7 @@ describe('aurora data api > simple queries', () => { // Assert const dbPost = await postRepository.findOne(storedPost.id) expect(dbPost).toBeTruthy() - expect(Math.round(dbPost!.updatedAt!.getTime() / 1000)).toEqual(Math.round(updatedAt.getTime() / 1000)) + expect(Math.trunc(dbPost!.updatedAt!.getTime() / 1000)).toEqual(Math.trunc(updatedAt.getTime() / 1000)) }) }) From 9bafd42e9c0449da2768501ac847e03664a9ea03 Mon Sep 17 00:00:00 2001 From: ayankovsky Date: Sun, 13 Oct 2019 23:47:29 +0200 Subject: [PATCH 13/23] Checkout master of typeorm for the functional tests --- test/functional/basic/simple-queries.func.test.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/test/functional/basic/simple-queries.func.test.ts b/test/functional/basic/simple-queries.func.test.ts index 0a73848..51fdba5 100644 --- a/test/functional/basic/simple-queries.func.test.ts +++ b/test/functional/basic/simple-queries.func.test.ts @@ -153,12 +153,14 @@ describe('aurora data api > simple queries', () => { title: 'Post For Update', text: 'Text', likesCount: 6, + publishedAt: new Date(), }), ) // Retrieve the post and update the date const getPost = await postRepository.findOne(storedPost.id) expect(getPost).toBeTruthy() + expect(getPost.updatedAt).toBeFalsy() const updatedAt = new Date() getPost!.updatedAt = updatedAt From ff49538c77ad41af83bc816646cff2a21164c97d Mon Sep 17 00:00:00 2001 From: ayankovsky Date: Sun, 13 Oct 2019 23:51:25 +0200 Subject: [PATCH 14/23] Checkout master of typeorm for the functional tests --- test/functional/basic/simple-queries.func.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/functional/basic/simple-queries.func.test.ts b/test/functional/basic/simple-queries.func.test.ts index 51fdba5..5d9330b 100644 --- a/test/functional/basic/simple-queries.func.test.ts +++ b/test/functional/basic/simple-queries.func.test.ts @@ -160,7 +160,7 @@ describe('aurora data api > simple queries', () => { // Retrieve the post and update the date const getPost = await postRepository.findOne(storedPost.id) expect(getPost).toBeTruthy() - expect(getPost.updatedAt).toBeFalsy() + expect(getPost!.updatedAt).toBeFalsy() const updatedAt = new Date() getPost!.updatedAt = updatedAt From e523558fd81ca56c93b78e52a6b0ed5e70accbb6 Mon Sep 17 00:00:00 2001 From: ayankovsky Date: Sun, 8 Mar 2020 18:02:36 +0100 Subject: [PATCH 15/23] Postgress support WIP --- package.json | 2 +- src/query-transformer/index.ts | 3 + .../mysql-query-transformer.ts | 70 +++++++++++++++++++ .../postgres-query-transformer.ts | 50 +++++++++++++ src/query-transformer/query-transformer.ts | 20 ++++++ src/typeorm-aurora-data-api-driver.ts | 20 +++++- .../{basic => mysql}/entity/Category.ts | 0 .../{basic => mysql}/entity/Post.ts | 0 test/transform-query.unit.test.ts | 20 +++--- 9 files changed, 172 insertions(+), 13 deletions(-) create mode 100644 src/query-transformer/index.ts create mode 100644 src/query-transformer/mysql-query-transformer.ts create mode 100644 src/query-transformer/postgres-query-transformer.ts create mode 100644 src/query-transformer/query-transformer.ts rename test/functional/{basic => mysql}/entity/Category.ts (100%) rename test/functional/{basic => mysql}/entity/Post.ts (100%) diff --git a/package.json b/package.json index f411ba3..9c86e9b 100644 --- a/package.json +++ b/package.json @@ -101,6 +101,6 @@ "typescript": "^3.0.3" }, "dependencies": { - "data-api-client": "ArsenyYankovsky/data-api-client#support-date" + "data-api-client": "ArsenyYankovsky/data-api-client#support-postgres" } } diff --git a/src/query-transformer/index.ts b/src/query-transformer/index.ts new file mode 100644 index 0000000..55af87a --- /dev/null +++ b/src/query-transformer/index.ts @@ -0,0 +1,3 @@ +export * from './mysql-query-transformer' +export * from './postgres-query-transformer' +export * from './query-transformer' diff --git a/src/query-transformer/mysql-query-transformer.ts b/src/query-transformer/mysql-query-transformer.ts new file mode 100644 index 0000000..d0fc4a7 --- /dev/null +++ b/src/query-transformer/mysql-query-transformer.ts @@ -0,0 +1,70 @@ +import { QueryTransformer } from './query-transformer' + +export class MysqlQueryTransformer extends QueryTransformer { + protected transformQuery(query: string, parameters: any[]): string { + const quoteCharacters = ["'", '"'] + let newQueryString = '' + let currentQuote = null + let srcIndex = 0 + let destIndex = 0 + + for (let i = 0; i < query.length; i += 1) { + const currentCharacter = query[i] + const currentCharacterEscaped = i !== 0 && query[i - 1] === '\\' + + if (currentCharacter === '?' && !currentQuote) { + const parameter = parameters![srcIndex] + + if (Array.isArray(parameter)) { + const additionalParameters = parameter.map((_, index) => + `:param_${destIndex + index}`) + + newQueryString += additionalParameters.join(', ') + destIndex += additionalParameters.length + } else { + newQueryString += `:param_${destIndex}` + destIndex += 1 + } + srcIndex += 1 + } else { + newQueryString += currentCharacter + + if (quoteCharacters.includes(currentCharacter) && !currentCharacterEscaped) { + if (!currentQuote) { + currentQuote = currentCharacter + } else if (currentQuote === currentCharacter) { + currentQuote = null + } + } + } + } + + return newQueryString + } + + protected expandArrayParameters(parameters: any[]): any[] { + return parameters.reduce( + (expandedParameters, parameter) => { + if (Array.isArray(parameter)) { + expandedParameters.push(...parameter) + } else { + expandedParameters.push(parameter) + } + return expandedParameters + }, []) + } + + protected transformParameters(parameters?: any[]) { + if (!parameters) { + return parameters + } + + const expandedParameters = this.expandArrayParameters(parameters) + + return [expandedParameters.reduce( + (params, parameter, index) => { + params[`param_${index}`] = parameter + return params + }, {})] + } +} diff --git a/src/query-transformer/postgres-query-transformer.ts b/src/query-transformer/postgres-query-transformer.ts new file mode 100644 index 0000000..e478cad --- /dev/null +++ b/src/query-transformer/postgres-query-transformer.ts @@ -0,0 +1,50 @@ +import { QueryTransformer } from './query-transformer' + +export class PostgresQueryTransformer extends QueryTransformer { + protected transformQuery(query: string) { + const quoteCharacters = ["'", '"'] + let newQueryString = '' + let currentQuote = null + + for (let i = 0; i < query.length; i += 1) { + const currentCharacter = query[i] + const currentCharacterEscaped = i !== 0 && query[i - 1] === '\\' + + if (currentCharacter === '$' && !currentQuote) { + newQueryString += ':param_' + i += 1 + + while (/^.*?[0-9]$/.test(query[i])) { + newQueryString += query[i] + i += 1 + } + i -= 1 + // TODO: think of a nicer fix for it + } else { + newQueryString += currentCharacter + + if (quoteCharacters.includes(currentCharacter) && !currentCharacterEscaped) { + if (!currentQuote) { + currentQuote = currentCharacter + } else if (currentQuote === currentCharacter) { + currentQuote = null + } + } + } + } + + return newQueryString + } + + protected transformParameters(parameters?: any[]) { + if (!parameters) { + return parameters + } + + return [parameters.reduce( + (params, parameter, index) => { + params[`param_${index + 1}`] = parameter + return params + }, {})] + } +} diff --git a/src/query-transformer/query-transformer.ts b/src/query-transformer/query-transformer.ts new file mode 100644 index 0000000..1e9d142 --- /dev/null +++ b/src/query-transformer/query-transformer.ts @@ -0,0 +1,20 @@ +export interface QueryTransformationResult { + queryString: string, + parameters: any[], +} + +export abstract class QueryTransformer { + public transformQueryAndParameters(query: string, srcParameters: any[] = []) { + if (!srcParameters.length) { + return { queryString: query, parameters: [] } + } + + const queryString = this.transformQuery(query, srcParameters) + const parameters = this.transformParameters(srcParameters) + return { queryString, parameters } + } + + protected abstract transformQuery(query: string, srcParameters: any[]): string + + protected abstract transformParameters(srcParameters?: any[]): any[] | undefined +} diff --git a/src/typeorm-aurora-data-api-driver.ts b/src/typeorm-aurora-data-api-driver.ts index d1c4139..914db70 100644 --- a/src/typeorm-aurora-data-api-driver.ts +++ b/src/typeorm-aurora-data-api-driver.ts @@ -1,8 +1,8 @@ // @ts-ignore import createDataApiClient from 'data-api-client' -import { transformQueryAndParameters } from './transform.utils' +import { MysqlQueryTransformer, PostgresQueryTransformer, QueryTransformer } from './query-transformer' -export default class DataApiDriver { +class DataApiDriver { private readonly client: any private transactionId?: string @@ -12,6 +12,7 @@ export default class DataApiDriver { private readonly resourceArn: string, private readonly database: string, private readonly loggerFn: (query: string, parameters?: any[]) => void = () => undefined, + private readonly queryTransformer: QueryTransformer, ) { this.region = region this.secretArn = secretArn @@ -26,10 +27,11 @@ export default class DataApiDriver { region, }, }) + this.queryTransformer = queryTransformer } public async query(query: string, parameters?: any[]): Promise { - const transformedQueryData = transformQueryAndParameters(query, parameters) + const transformedQueryData = this.queryTransformer.transformQueryAndParameters(query, parameters) this.loggerFn(transformedQueryData.queryString, transformedQueryData.parameters) @@ -57,3 +59,15 @@ export default class DataApiDriver { this.transactionId = undefined } } + +const createMysqlDriver = (region: string, secretArn: string, resourceArn: string, database: string, + loggerFn: (query: string, parameters?: any[]) => void = () => undefined) => { + return new DataApiDriver(region, secretArn, resourceArn, database, loggerFn, new MysqlQueryTransformer()) +} + +export default createMysqlDriver + +const createPostgresDriver = (region: string, secretArn: string, resourceArn: string, database: string, + loggerFn: (query: string, parameters?: any[]) => void = () => undefined) => { + return new DataApiDriver(region, secretArn, resourceArn, database, loggerFn, new PostgresQueryTransformer()) +} diff --git a/test/functional/basic/entity/Category.ts b/test/functional/mysql/entity/Category.ts similarity index 100% rename from test/functional/basic/entity/Category.ts rename to test/functional/mysql/entity/Category.ts diff --git a/test/functional/basic/entity/Post.ts b/test/functional/mysql/entity/Post.ts similarity index 100% rename from test/functional/basic/entity/Post.ts rename to test/functional/mysql/entity/Post.ts diff --git a/test/transform-query.unit.test.ts b/test/transform-query.unit.test.ts index a577c51..b763af6 100644 --- a/test/transform-query.unit.test.ts +++ b/test/transform-query.unit.test.ts @@ -1,9 +1,11 @@ -import { transformQueryAndParameters } from '../src/transform.utils' +import { MysqlQueryTransformer } from '../src/query-transformer' describe('aurora data api > query transformation', () => { + const transformer = new MysqlQueryTransformer() + it('should correctly transform a query with no parameters', async () => { const query = 'select 1' - const result = transformQueryAndParameters(query) + const result = transformer.transformQueryAndParameters(query) expect(result.queryString).toEqual('select 1') expect(result.parameters).toEqual([]) @@ -11,7 +13,7 @@ describe('aurora data api > query transformation', () => { it('should correctly transform a query with empty parameters', async () => { const query = 'select 1' - const result = transformQueryAndParameters(query, []) + const result = transformer.transformQueryAndParameters(query, []) expect(result.queryString).toEqual('select 1') expect(result.parameters).toEqual([]) @@ -21,7 +23,7 @@ describe('aurora data api > query transformation', () => { const query = 'select * from posts where id = ?' const parameters = [1] - const result = transformQueryAndParameters(query, parameters) + const result = transformer.transformQueryAndParameters(query, parameters) expect(result.queryString).toEqual('select * from posts where id = :param_0') expect(result.parameters).toEqual([{ param_0: 1 }]) @@ -31,7 +33,7 @@ describe('aurora data api > query transformation', () => { const query = 'select * from posts where id = ? and text = "?" and title = "\\"?\\""' const parameters = [1] - const result = transformQueryAndParameters(query, parameters) + const result = transformer.transformQueryAndParameters(query, parameters) expect(result.queryString).toEqual( 'select * from posts where id = :param_0 and text = "?" and title = "\\"?\\""', @@ -44,7 +46,7 @@ describe('aurora data api > query transformation', () => { "select * from posts where id = ? and text = '?' and title = '\\'?\\'' and description = \"'?'\"" const parameters = [1] - const result = transformQueryAndParameters(query, parameters) + const result = transformer.transformQueryAndParameters(query, parameters) expect(result.queryString).toEqual( "select * from posts where id = :param_0 and text = '?' and title = '\\'?\\'' and description = \"'?'\"", @@ -62,7 +64,7 @@ describe('aurora data api > query transformation', () => { group by DATE(CONVERT_TZ(dateTime, 'UTC', 'UTC')) order by dateTime asc ` - const result = transformQueryAndParameters(query, [1, 2, 3]) + const result = transformer.transformQueryAndParameters(query, [1, 2, 3]) expect(result.parameters).toEqual([{ param_0: 1, param_1: 2, param_2: 3 }]) }) @@ -72,7 +74,7 @@ describe('aurora data api > query transformation', () => { const id = 'dd32d900-3df6-45b9-a253-70a4516b88dc' const id2 = 'some-guid' - const result = transformQueryAndParameters(query, [id, [id, id2, id, id2]]) + const result = transformer.transformQueryAndParameters(query, [id, [id, id2, id, id2]]) expect(result.queryString).toEqual( 'select * from posts where id in (:param_0) and id in (:param_1, :param_2, :param_3, :param_4)', @@ -88,7 +90,7 @@ describe('aurora data api > query transformation', () => { const id = 'dd32d900-3df6-45b9-a253-70a4516b88dc' const id2 = 'some-guid' const id3 = 'some-other-guid' - const result = transformQueryAndParameters(query, [[id, id2, id, id2], [id3, id2, id, id3]]) + const result = transformer.transformQueryAndParameters(query, [[id, id2, id, id2], [id3, id2, id, id3]]) expect(result.queryString).toEqual( 'select * from posts where id in (:param_0, :param_1, :param_2, :param_3) and test in (:param_4, :param_5, :param_6, :param_7);', From 54611c73101556015766905ab4d1d19715fe36c6 Mon Sep 17 00:00:00 2001 From: ayankovsky Date: Sun, 8 Mar 2020 18:09:08 +0100 Subject: [PATCH 16/23] Postgres support WIP --- .circleci/config.yml | 2 +- src/transform.utils.ts | 69 ------ src/typeorm-aurora-data-api-driver.ts | 2 + .../simple-queries.func.test.ts | 18 +- test/functional/pg/basic/entity/Category.ts | 11 + test/functional/pg/basic/entity/Post.ts | 28 +++ .../pg/basic/pg-simple-queries.func.test.ts | 200 ++++++++++++++++++ test/functional/utils/create-connection.ts | 32 ++- yarn.lock | 10 +- 9 files changed, 277 insertions(+), 95 deletions(-) delete mode 100644 src/transform.utils.ts rename test/functional/{basic => mysql}/simple-queries.func.test.ts (88%) create mode 100644 test/functional/pg/basic/entity/Category.ts create mode 100644 test/functional/pg/basic/entity/Post.ts create mode 100644 test/functional/pg/basic/pg-simple-queries.func.test.ts diff --git a/.circleci/config.yml b/.circleci/config.yml index 86fa111..bd6b7fd 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -64,7 +64,7 @@ jobs: command: npm link - run: name: checkout typeorm - command: git clone --single-branch --branch master https://github.com/typeorm/typeorm.git + command: git clone --single-branch --branch data-api-pg https://github.com/ArsenyYankovsky/typeorm - run: name: build typeorm with current package command: cd typeorm/ && npm i && npm run package && cd build/package && npm link && rm -rf node_modules/typeorm-aurora-data-api-driver && npm link typeorm-aurora-data-api-driver diff --git a/src/transform.utils.ts b/src/transform.utils.ts deleted file mode 100644 index c8923bd..0000000 --- a/src/transform.utils.ts +++ /dev/null @@ -1,69 +0,0 @@ -const transformQuery = (query: string, parameters: any[]): string => { - const quoteCharacters = ["'", '"'] - let newQueryString = '' - let currentQuote = null - let srcIndex = 0 - let destIndex = 0 - - for (let i = 0; i < query.length; i += 1) { - const currentCharacter = query[i] - const currentCharacterEscaped = i !== 0 && query[i - 1] === '\\' - - if (currentCharacter === '?' && !currentQuote) { - const parameter = parameters![srcIndex] - - if (Array.isArray(parameter)) { - const additionalParameters = parameter.map((_, index) => - `:param_${destIndex + index}`) - - newQueryString += additionalParameters.join(', ') - destIndex += additionalParameters.length - } else { - newQueryString += `:param_${destIndex}` - destIndex += 1 - } - srcIndex += 1 - } else { - newQueryString += currentCharacter - - if (quoteCharacters.includes(currentCharacter) && !currentCharacterEscaped) { - if (!currentQuote) { - currentQuote = currentCharacter - } else if (currentQuote === currentCharacter) { - currentQuote = null - } - } - } - } - - return newQueryString -} - -const transformParameters = (parameters: any[]) => - parameters.reduce( - (params, parameter, index) => { - params[`param_${index}`] = parameter - return params - }, {}) - -const expandArrayParameters = (parameters: any[]) => - parameters.reduce( - (expandedParameters, parameter) => { - if (Array.isArray(parameter)) { - expandedParameters.push(...parameter) - } else { - expandedParameters.push(parameter) - } - return expandedParameters - }, []) - -export const transformQueryAndParameters = (query: string, srcParameters: any[] = []) => { - if (!srcParameters.length) { - return { queryString: query, parameters: [] } - } - - const queryString = transformQuery(query, srcParameters) - const expandedParameters = expandArrayParameters(srcParameters) - const parameters = [transformParameters(expandedParameters)] - return { queryString, parameters } -} diff --git a/src/typeorm-aurora-data-api-driver.ts b/src/typeorm-aurora-data-api-driver.ts index 914db70..70efb89 100644 --- a/src/typeorm-aurora-data-api-driver.ts +++ b/src/typeorm-aurora-data-api-driver.ts @@ -71,3 +71,5 @@ const createPostgresDriver = (region: string, secretArn: string, resourceArn: st loggerFn: (query: string, parameters?: any[]) => void = () => undefined) => { return new DataApiDriver(region, secretArn, resourceArn, database, loggerFn, new PostgresQueryTransformer()) } + +export const pg = createPostgresDriver diff --git a/test/functional/basic/simple-queries.func.test.ts b/test/functional/mysql/simple-queries.func.test.ts similarity index 88% rename from test/functional/basic/simple-queries.func.test.ts rename to test/functional/mysql/simple-queries.func.test.ts index 5d9330b..987cb50 100644 --- a/test/functional/basic/simple-queries.func.test.ts +++ b/test/functional/mysql/simple-queries.func.test.ts @@ -7,12 +7,12 @@ describe('aurora data api > simple queries', () => { jest.setTimeout(240000) it('should do a simple select', async () => { - await useCleanDatabase({ logger: 'simple-console' }, async (connection) => { + await useCleanDatabase('mysql', { logger: 'simple-console' }, async (connection) => { const logSpy = jest.spyOn(global.console, 'log') - const result = await connection.query('select 1') + const result = await connection.query('select 1 as \"1\"') - expect(logSpy).toHaveBeenCalledWith('query: select 1') + expect(logSpy).toHaveBeenCalledWith('query: select 1 as \"1\"') expect(logSpy).toBeCalledTimes(1) expect(result[0][1]).toBe(1) @@ -20,7 +20,7 @@ describe('aurora data api > simple queries', () => { }) it('should create a table and be able to query it', async () => { - await useCleanDatabase({ entities: [Post, Category] }, async (connection) => { + await useCleanDatabase('mysql', { entities: [Post, Category] }, async (connection) => { const postRepository = connection.getRepository(Post) const post = new Post() @@ -40,7 +40,7 @@ describe('aurora data api > simple queries', () => { }) it('should be able to update a post', async () => { - await useCleanDatabase({ entities: [Post, Category] }, async (connection) => { + await useCleanDatabase('mysql', { entities: [Post, Category] }, async (connection) => { const postRepository = connection.getRepository(Post) @@ -72,7 +72,7 @@ describe('aurora data api > simple queries', () => { }) it('should be able to handle dates and multiple inserts', async () => { - await useCleanDatabase({ entities: [Post, Category] }, async (connection) => { + await useCleanDatabase('mysql', { entities: [Post, Category] }, async (connection) => { const postRepository = connection.getRepository(Post) const post = new Post() @@ -104,7 +104,7 @@ describe('aurora data api > simple queries', () => { }) it('should be able to create and query a many-to-many relationship', async () => { - await useCleanDatabase({ entities: [Post, Category] }, async (connection) => { + await useCleanDatabase('mysql', { entities: [Post, Category] }, async (connection) => { // Create categories const categoryRepository = connection.getRepository(Category) @@ -144,7 +144,7 @@ describe('aurora data api > simple queries', () => { }) it('should be able to update a date field by primary key', async () => { - await useCleanDatabase({ entities: [Post, Category] }, async (connection) => { + await useCleanDatabase('mysql', { entities: [Post, Category] }, async (connection) => { // Create a post and associate with created categories const postRepository = connection.getRepository(Post) @@ -174,7 +174,7 @@ describe('aurora data api > simple queries', () => { }) it('should be able to correctly deal with bulk inserts', async () => { - await useCleanDatabase({ entities: [Category] }, async (connection) => { + await useCleanDatabase('mysql', { entities: [Category] }, async (connection) => { const categoryNames = ['one', 'two', 'three', 'four'] const newCategories = categoryNames.map(name => ({ name })) diff --git a/test/functional/pg/basic/entity/Category.ts b/test/functional/pg/basic/entity/Category.ts new file mode 100644 index 0000000..1038116 --- /dev/null +++ b/test/functional/pg/basic/entity/Category.ts @@ -0,0 +1,11 @@ +import { Column, Entity, Generated, PrimaryColumn } from 'typeorm' + +@Entity('category', { schema: 'test1' }) +export class Category { + @PrimaryColumn() + @Generated() + public id!: number + + @Column() + public name!: string +} diff --git a/test/functional/pg/basic/entity/Post.ts b/test/functional/pg/basic/entity/Post.ts new file mode 100644 index 0000000..2945520 --- /dev/null +++ b/test/functional/pg/basic/entity/Post.ts @@ -0,0 +1,28 @@ +import { Column, Entity, Generated, JoinTable, ManyToMany, PrimaryColumn } from 'typeorm' +import { Category } from './Category' + +@Entity('post', { schema: 'test1' }) +export class Post { + @PrimaryColumn() + @Generated() + public id!: number + + @Column() + public title!: string + + @Column() + public text!: string + + @Column({ nullable: false }) + public likesCount!: number + + @Column({ nullable: false, type: 'timestamp', default: () => 'now()' }) + public publishedAt!: Date + + @Column({ nullable: true, type: 'timestamp' }) + public updatedAt?: Date + + @ManyToMany(type => Category) + @JoinTable() + public categories!: Category[] +} diff --git a/test/functional/pg/basic/pg-simple-queries.func.test.ts b/test/functional/pg/basic/pg-simple-queries.func.test.ts new file mode 100644 index 0000000..c4c96cd --- /dev/null +++ b/test/functional/pg/basic/pg-simple-queries.func.test.ts @@ -0,0 +1,200 @@ +import 'reflect-metadata' +import { useCleanDatabase } from '../../utils/create-connection' +import { Category } from './entity/Category' +import { Post } from './entity/Post' + +describe('aurora data api pg > simple queries', () => { + jest.setTimeout(240000) + + it('should do a simple select', async () => { + await useCleanDatabase('postgres', { logger: 'simple-console' }, async (connection) => { + const logSpy = jest.spyOn(global.console, 'log') + + const result = await connection.query('select 1 as \"1\"') + + expect(logSpy).toHaveBeenCalledWith('query: select 1 as \"1\"') + expect(logSpy).toBeCalledTimes(1) + + expect(result[0][1]).toBe(1) + }) + }) + + it('should create a table and be able to query it', async () => { + await useCleanDatabase('postgres', { entities: [Post, Category] }, async (connection) => { + const postRepository = connection.getRepository(Post) + + const post = new Post() + post.title = 'My First Post' + post.text = 'Post Text' + post.likesCount = 4 + + const insertResult = await postRepository.save(post) + + const dbPost = await postRepository.findOne({ id: insertResult.id }) + expect(dbPost).toBeTruthy() + + expect(dbPost!.title).toBe('My First Post') + expect(dbPost!.text).toBe('Post Text') + expect(dbPost!.likesCount).toBe(4) + }) + }) + + it('should be able to update a post', async () => { + await useCleanDatabase('postgres', { entities: [Post, Category] }, async (connection) => { + + const postRepository = connection.getRepository(Post) + + const post = new Post() + + post.title = 'My First Post' + post.text = 'Post Text' + post.likesCount = 4 + post.publishedAt = new Date(2017, 1, 1) + + const insertResult = await postRepository.save(post) + + const postId = insertResult.id + + const dbPost = await postRepository.findOne({ id: postId }) + + dbPost!.publishedAt = new Date() + + await postRepository.save(dbPost!) + + const updatedPost = await postRepository.findOne(postId) + + expect(updatedPost!.publishedAt > new Date(2017, 1, 1)).toBeTruthy() + + expect(dbPost!.title).toBe('My First Post') + expect(dbPost!.text).toBe('Post Text') + expect(dbPost!.likesCount).toBe(4) + }) + }) + + it('should be able to handle dates and multiple inserts', async () => { + await useCleanDatabase('postgres', { entities: [Post, Category] }, async (connection) => { + const postRepository = connection.getRepository(Post) + + const post = new Post() + post.title = 'My First Post' + post.text = 'Post Text' + post.likesCount = 4 + post.publishedAt = new Date() + + const secondPost = new Post() + secondPost.title = 'My Second Post' + secondPost.text = 'Post Text' + secondPost.likesCount = 5 + secondPost.publishedAt = new Date() + + await postRepository.save([post, secondPost]) + + const dbPosts = await postRepository.find() + expect(dbPosts).toBeTruthy() + expect(dbPosts.length).toBe(2) + + for (const dbPost of dbPosts) { + expect(typeof dbPost!.title).toBe('string') + expect(typeof dbPost!.text).toBe('string') + expect(typeof dbPost!.likesCount).toBe('number') + + expect(dbPost!.publishedAt).toBeInstanceOf(Date) + } + }) + }) + + it('should be able to create and query a many-to-many relationship', async () => { + await useCleanDatabase('postgres', { entities: [Post, Category] }, async (connection) => { + // Create categories + const categoryRepository = connection.getRepository(Category) + + const firstCategory = await categoryRepository.save( + categoryRepository.create({ + name: 'first', + }), + ) + + const secondCategory = await categoryRepository.save( + categoryRepository.create({ + name: 'second', + }), + ) + + // Create a post and associate with created categories + const postRepository = connection.getRepository(Post) + + const post = postRepository.create({ + title: 'Post with categories', + text: 'Text', + likesCount: 6, + publishedAt: new Date(), + categories: [firstCategory, secondCategory], + }) + + const storedPost = await postRepository.save(post) + + // Assert + const dbPost = await postRepository.findOne( + storedPost.id, { relations: ['categories'] }) + + expect(dbPost).toBeTruthy() + expect(dbPost!.categories).toBeTruthy() + expect(dbPost!.categories.length).toBe(2) + }) + }) + + it('should be able to update a date field by primary key', async () => { + await useCleanDatabase('postgres', { entities: [Post, Category] }, async (connection) => { + // Create a post and associate with created categories + const postRepository = connection.getRepository(Post) + + const storedPost = await postRepository.save( + postRepository.create({ + title: 'Post For Update', + text: 'Text', + likesCount: 6, + publishedAt: new Date(), + }), + ) + + // Retrieve the post and update the date + const getPost = await postRepository.findOne(storedPost.id) + expect(getPost).toBeTruthy() + expect(getPost!.updatedAt).toBeFalsy() + + const updatedAt = new Date() + getPost!.updatedAt = updatedAt + await postRepository.save(getPost!) + + // Assert + const dbPost = await postRepository.findOne(storedPost.id) + expect(dbPost).toBeTruthy() + expect(Math.trunc(dbPost!.updatedAt!.getTime() / 1000)).toEqual(Math.trunc(updatedAt.getTime() / 1000)) + }) + }) + + it('should be able to correctly deal with bulk inserts', async () => { + await useCleanDatabase('postgres', { entities: [Category] }, async (connection) => { + const categoryNames = ['one', 'two', 'three', 'four'] + const newCategories = categoryNames.map(name => ({ name })) + + await connection.createQueryBuilder() + .insert() + .into(Category) + .values(newCategories) + .orIgnore() + .execute() + + // Query back the inserted categories + const categoryRepository = connection.getRepository(Category) + const categories = await categoryRepository.find() + + // Assert + expect(categories.length).toBe(4) + expect(categories[0].name = 'one') + expect(categories[1].name = 'two') + expect(categories[2].name = 'three') + expect(categories[3].name = 'four') + }) + }) +}) diff --git a/test/functional/utils/create-connection.ts b/test/functional/utils/create-connection.ts index 08207b9..a253748 100644 --- a/test/functional/utils/create-connection.ts +++ b/test/functional/utils/create-connection.ts @@ -4,14 +4,14 @@ import { createConnection as typeormCreateConnection, } from 'typeorm' -export const createConnection = async (partialOptions: Partial = {}) => { +export const createConnection = async (dbType: DbType, partialOptions: Partial = {}) => { return typeormCreateConnection({ ...partialOptions, - type: 'aurora-data-api', - database: process.env.database!, - secretArn: process.env.secretArn!, - resourceArn: process.env.resourceArn!, - region: process.env.region!, + type: dbType === 'mysql' ? 'aurora-data-api' : 'aurora-data-api-pg', + database: process.env[`${dbType}Database`]!, + secretArn: process.env[`${dbType}SecretArn`]!, + resourceArn: process.env[`${dbType}ResourceArn`]!, + region: 'eu-west-1', // process.env.region!, logging: true, logger: 'simple-console', extra: { @@ -28,21 +28,31 @@ export const createConnection = async (partialOptions: Partial = {}, ) => { - const connection = await createConnection({ ...partialOptions, synchronize: false }) - await connection.query(`DROP DATABASE IF EXISTS ${process.env.database};`) - await connection.query(`CREATE DATABASE ${process.env.database};`) - await connection.query(`USE ${process.env.database};`) + const connection = await createConnection(dbType, { ...partialOptions, synchronize: false }) + if(dbType === 'mysql') { + await connection.query(`DROP DATABASE IF EXISTS ${process.env.database};`) + await connection.query(`CREATE DATABASE ${process.env.database};`) + await connection.query(`USE ${process.env.database};`) + } else { + await connection.query(`DROP schema IF EXISTS ${process.env.database} CASCADE;`) + await connection.query(`CREATE schema ${process.env.database};`) + await connection.query(`SET search_path = ${process.env.database};`) + } await connection.synchronize(true) return connection } +export type DbType = 'mysql' | 'postgres' + export const useCleanDatabase = async ( + dbType: DbType, partialOptions: Partial = {}, invoke: (connection: Connection) => Promise, ) => { - const connection = await createConnectionAndResetData(partialOptions) + const connection = await createConnectionAndResetData(dbType, partialOptions) try { await invoke(connection) } finally { diff --git a/yarn.lock b/yarn.lock index 7f3fcdc..f3c49fa 100644 --- a/yarn.lock +++ b/yarn.lock @@ -710,9 +710,9 @@ atob@^2.1.1: integrity sha512-Wm6ukoaOGJi/73p/cl2GvLjTI5JM1k/O14isD73YML8StrH/7/lRFgmg8nICZgD3bZZvjwCGxtMOD3wWNAu8cg== aws-sdk@^2.492.0: - version "2.548.0" - resolved "https://registry.yarnpkg.com/aws-sdk/-/aws-sdk-2.548.0.tgz#20a3c7a77e1c6fd694cdb5e0ecc0ccf0d0542674" - integrity sha512-dnG1MEc3uOonGDswHPiq7TYn6EOG3+IQhF2LSRambz9adaZxwRgFOII7UVPJjh33FtxKpMzOSmf62R+EX822Nw== + version "2.634.0" + resolved "https://registry.yarnpkg.com/aws-sdk/-/aws-sdk-2.634.0.tgz#95077c191107b9cb696e922b06e79949dcd13434" + integrity sha512-cZfRD7bcKBHOLoHUJuqB9xaLs/z1/xsc9zfGLIzyuxKLJa7Z0pxy8Y/0GrhWO98yXLBvLLET7btj2iDI2oWWhQ== dependencies: buffer "4.9.1" events "1.1.1" @@ -1703,9 +1703,9 @@ dashdash@^1.12.0: dependencies: assert-plus "^1.0.0" -data-api-client@ArsenyYankovsky/data-api-client#support-date: +data-api-client@ArsenyYankovsky/data-api-client#support-postgres: version "1.0.0-beta" - resolved "https://codeload.github.com/ArsenyYankovsky/data-api-client/tar.gz/2880f8f7456fc126d356b26fb2ddbac5903ed23f" + resolved "https://codeload.github.com/ArsenyYankovsky/data-api-client/tar.gz/2507e36fc0c593e821b1c221d9c56ac95fa0aa6f" dependencies: sqlstring "^2.3.1" From 5a940d97447dd675b7c39693667cca3a33f0cd62 Mon Sep 17 00:00:00 2001 From: ayankovsky Date: Sun, 8 Mar 2020 18:14:18 +0100 Subject: [PATCH 17/23] Postgres support WIP --- test/functional/utils/create-connection.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/functional/utils/create-connection.ts b/test/functional/utils/create-connection.ts index a253748..e3ec158 100644 --- a/test/functional/utils/create-connection.ts +++ b/test/functional/utils/create-connection.ts @@ -32,7 +32,7 @@ export const createConnectionAndResetData = async ( partialOptions: Partial = {}, ) => { const connection = await createConnection(dbType, { ...partialOptions, synchronize: false }) - if(dbType === 'mysql') { + if (dbType === 'mysql') { await connection.query(`DROP DATABASE IF EXISTS ${process.env.database};`) await connection.query(`CREATE DATABASE ${process.env.database};`) await connection.query(`USE ${process.env.database};`) From 5f5904eb90287ddd1bb2362ab826e4edf24f6e55 Mon Sep 17 00:00:00 2001 From: ayankovsky Date: Sun, 8 Mar 2020 18:22:11 +0100 Subject: [PATCH 18/23] Postgres support WIP --- test/functional/utils/create-connection.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/functional/utils/create-connection.ts b/test/functional/utils/create-connection.ts index e3ec158..2fe77a9 100644 --- a/test/functional/utils/create-connection.ts +++ b/test/functional/utils/create-connection.ts @@ -37,9 +37,9 @@ export const createConnectionAndResetData = async ( await connection.query(`CREATE DATABASE ${process.env.database};`) await connection.query(`USE ${process.env.database};`) } else { - await connection.query(`DROP schema IF EXISTS ${process.env.database} CASCADE;`) - await connection.query(`CREATE schema ${process.env.database};`) - await connection.query(`SET search_path = ${process.env.database};`) + await connection.query('DROP schema IF EXISTS test1 CASCADE;') + await connection.query('CREATE schema test1;') + await connection.query('SET search_path = test1;') } await connection.synchronize(true) return connection From b680b910937f7b5beabd1433574b0d62851261b8 Mon Sep 17 00:00:00 2001 From: ayankovsky Date: Sun, 8 Mar 2020 18:59:44 +0100 Subject: [PATCH 19/23] Postgres support WIP --- test/functional/utils/create-connection.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/functional/utils/create-connection.ts b/test/functional/utils/create-connection.ts index 2fe77a9..edca628 100644 --- a/test/functional/utils/create-connection.ts +++ b/test/functional/utils/create-connection.ts @@ -7,11 +7,12 @@ import { export const createConnection = async (dbType: DbType, partialOptions: Partial = {}) => { return typeormCreateConnection({ ...partialOptions, + name: dbType, type: dbType === 'mysql' ? 'aurora-data-api' : 'aurora-data-api-pg', database: process.env[`${dbType}Database`]!, secretArn: process.env[`${dbType}SecretArn`]!, resourceArn: process.env[`${dbType}ResourceArn`]!, - region: 'eu-west-1', // process.env.region!, + region: process.env.region!, logging: true, logger: 'simple-console', extra: { From ce1f290d8d105652406e18ded3c7e0f221bd6586 Mon Sep 17 00:00:00 2001 From: ayankovsky Date: Sun, 22 Mar 2020 13:53:07 +0100 Subject: [PATCH 20/23] Postgres support refactor --- src/query-transformer/postgres-query-transformer.ts | 8 -------- 1 file changed, 8 deletions(-) diff --git a/src/query-transformer/postgres-query-transformer.ts b/src/query-transformer/postgres-query-transformer.ts index e478cad..01e943f 100644 --- a/src/query-transformer/postgres-query-transformer.ts +++ b/src/query-transformer/postgres-query-transformer.ts @@ -12,14 +12,6 @@ export class PostgresQueryTransformer extends QueryTransformer { if (currentCharacter === '$' && !currentQuote) { newQueryString += ':param_' - i += 1 - - while (/^.*?[0-9]$/.test(query[i])) { - newQueryString += query[i] - i += 1 - } - i -= 1 - // TODO: think of a nicer fix for it } else { newQueryString += currentCharacter From 6af0c28236f5fb848905c001e97df0342bccccc0 Mon Sep 17 00:00:00 2001 From: ayankovsky Date: Mon, 20 Apr 2020 19:07:05 +0200 Subject: [PATCH 21/23] Upgrade data-api-client --- yarn.lock | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/yarn.lock b/yarn.lock index f3c49fa..48cbbe7 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1705,7 +1705,7 @@ dashdash@^1.12.0: data-api-client@ArsenyYankovsky/data-api-client#support-postgres: version "1.0.0-beta" - resolved "https://codeload.github.com/ArsenyYankovsky/data-api-client/tar.gz/2507e36fc0c593e821b1c221d9c56ac95fa0aa6f" + resolved "https://codeload.github.com/ArsenyYankovsky/data-api-client/tar.gz/043f3320f8d665c21250d615101e7b3bc8f1d298" dependencies: sqlstring "^2.3.1" @@ -6887,9 +6887,9 @@ sprintf-js@~1.0.2: integrity sha1-BOaSb2YolTVPPdAVIDYzuFcpfiw= sqlstring@^2.3.1: - version "2.3.1" - resolved "https://registry.yarnpkg.com/sqlstring/-/sqlstring-2.3.1.tgz#475393ff9e91479aea62dcaf0ca3d14983a7fb40" - integrity sha1-R1OT/56RR5rqYtyvDKPRSYOn+0A= + version "2.3.2" + resolved "https://registry.yarnpkg.com/sqlstring/-/sqlstring-2.3.2.tgz#cdae7169389a1375b18e885f2e60b3e460809514" + integrity sha512-vF4ZbYdKS8OnoJAWBmMxCQDkiEBkGQYU7UZPtL8flbDRSNkhaXvRJ279ZtI6M+zDaQovVU4tuRgzK5fVhvFAhg== sshpk@^1.7.0: version "1.16.1" From 106a25e6425fca47770c71fc8b147a25fdba3441 Mon Sep 17 00:00:00 2001 From: ayankovsky Date: Sun, 17 May 2020 03:09:06 +0200 Subject: [PATCH 22/23] Merged in master --- src/typeorm-aurora-data-api-driver.ts | 29 ++++++++++++++++++++++----- 1 file changed, 24 insertions(+), 5 deletions(-) diff --git a/src/typeorm-aurora-data-api-driver.ts b/src/typeorm-aurora-data-api-driver.ts index 16bbdf6..6457add 100644 --- a/src/typeorm-aurora-data-api-driver.ts +++ b/src/typeorm-aurora-data-api-driver.ts @@ -12,8 +12,8 @@ class DataApiDriver { private readonly resourceArn: string, private readonly database: string, private readonly loggerFn: (query: string, parameters?: any[]) => void = () => undefined, - private readonly serviceConfigOptions?: any, private readonly queryTransformer: QueryTransformer, + private readonly serviceConfigOptions?: any, ) { this.region = region this.secretArn = secretArn @@ -62,15 +62,34 @@ class DataApiDriver { } const createMysqlDriver = (region: string, secretArn: string, resourceArn: string, database: string, - loggerFn: (query: string, parameters?: any[]) => void = () => undefined) => { - return new DataApiDriver(region, secretArn, resourceArn, database, loggerFn, new MysqlQueryTransformer()) + loggerFn: (query: string, parameters?: any[]) => void = () => undefined, + serviceConfigOptions?: any) => { + + return new DataApiDriver( + region, + secretArn, + resourceArn, + database, + loggerFn, + new MysqlQueryTransformer(), + serviceConfigOptions, + ) } export default createMysqlDriver const createPostgresDriver = (region: string, secretArn: string, resourceArn: string, database: string, - loggerFn: (query: string, parameters?: any[]) => void = () => undefined) => { - return new DataApiDriver(region, secretArn, resourceArn, database, loggerFn, new PostgresQueryTransformer()) + loggerFn: (query: string, parameters?: any[]) => void = () => undefined, + serviceConfigOptions?: any) => { + return new DataApiDriver( + region, + secretArn, + resourceArn, + database, + loggerFn, + new PostgresQueryTransformer(), + serviceConfigOptions, + ) } export const pg = createPostgresDriver From 906fb8628e327b9198cec587eb2dd681cf739a87 Mon Sep 17 00:00:00 2001 From: ayankovsky Date: Sun, 17 May 2020 20:49:44 +0200 Subject: [PATCH 23/23] Merged in master --- .circleci/config.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index bd6b7fd..86fa111 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -64,7 +64,7 @@ jobs: command: npm link - run: name: checkout typeorm - command: git clone --single-branch --branch data-api-pg https://github.com/ArsenyYankovsky/typeorm + command: git clone --single-branch --branch master https://github.com/typeorm/typeorm.git - run: name: build typeorm with current package command: cd typeorm/ && npm i && npm run package && cd build/package && npm link && rm -rf node_modules/typeorm-aurora-data-api-driver && npm link typeorm-aurora-data-api-driver