diff --git a/package-lock.json b/package-lock.json index 7105500..1667c86 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,20 @@ { "name": "@adileo/surrealdb-ts-client", +<<<<<<< HEAD "version": "1.0.10", +======= + "version": "1.0.6", +>>>>>>> c474bf760abc096c679e2f9ddb183a97f46718a9 "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@adileo/surrealdb-ts-client", +<<<<<<< HEAD "version": "1.0.10", +======= + "version": "1.0.6", +>>>>>>> c474bf760abc096c679e2f9ddb183a97f46718a9 "license": "Apache 2.0", "dependencies": { "fetch-h2": "^3.0.2" @@ -15,8 +23,11 @@ "@types/jest": "^29.2.2", "@types/node": "^18.11.9", "jest": "^29.3.1", +<<<<<<< HEAD "pino": "^8.7.0", "pino-pretty": "^9.1.1", +======= +>>>>>>> c474bf760abc096c679e2f9ddb183a97f46718a9 "ts-jest": "^29.0.3", "typescript": "^4.8.4" } @@ -1110,6 +1121,7 @@ "integrity": "sha512-iO9ZQHkZxHn4mSakYV0vFHAVDyEOIJQrV2uZ06HxEPcx+mt8swXoZHIbaaJ2crJYFfErySgktuTZ3BeLz+XmFA==", "dev": true }, +<<<<<<< HEAD "node_modules/abort-controller": { "version": "3.0.0", "resolved": "https://registry.npmjs.org/abort-controller/-/abort-controller-3.0.0.tgz", @@ -1122,6 +1134,8 @@ "node": ">=6.5" } }, +======= +>>>>>>> c474bf760abc096c679e2f9ddb183a97f46718a9 "node_modules/already": { "version": "2.2.1", "resolved": "https://registry.npmjs.org/already/-/already-2.2.1.tgz", @@ -1188,6 +1202,7 @@ "sprintf-js": "~1.0.2" } }, +<<<<<<< HEAD "node_modules/atomic-sleep": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/atomic-sleep/-/atomic-sleep-1.0.0.tgz", @@ -1197,6 +1212,8 @@ "node": ">=8.0.0" } }, +======= +>>>>>>> c474bf760abc096c679e2f9ddb183a97f46718a9 "node_modules/babel-jest": { "version": "29.3.1", "resolved": "https://registry.npmjs.org/babel-jest/-/babel-jest-29.3.1.tgz", @@ -1294,6 +1311,7 @@ "integrity": "sha512-3oSeUO0TMV67hN1AmbXsK4yaqU7tjiHlbxRDZOpH0KW9+CeX4bRAaX0Anxt0tx2MrpRpWwQaPwIlISEJhYU5Pw==", "dev": true }, +<<<<<<< HEAD "node_modules/base64-js": { "version": "1.5.1", "resolved": "https://registry.npmjs.org/base64-js/-/base64-js-1.5.1.tgz", @@ -1314,6 +1332,8 @@ } ] }, +======= +>>>>>>> c474bf760abc096c679e2f9ddb183a97f46718a9 "node_modules/brace-expansion": { "version": "1.1.11", "resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-1.1.11.tgz", @@ -1385,6 +1405,7 @@ "node-int64": "^0.4.0" } }, +<<<<<<< HEAD "node_modules/buffer": { "version": "6.0.3", "resolved": "https://registry.npmjs.org/buffer/-/buffer-6.0.3.tgz", @@ -1409,6 +1430,8 @@ "ieee754": "^1.2.1" } }, +======= +>>>>>>> c474bf760abc096c679e2f9ddb183a97f46718a9 "node_modules/buffer-from": { "version": "1.1.2", "resolved": "https://registry.npmjs.org/buffer-from/-/buffer-from-1.1.2.tgz", @@ -1539,12 +1562,15 @@ "integrity": "sha512-dOy+3AuW3a2wNbZHIuMZpTcgjGuLU/uBL/ubcZF9OXbDo8ff4O8yVp5Bf0efS8uEoYo5q4Fx7dY9OgQGXgAsQA==", "dev": true }, +<<<<<<< HEAD "node_modules/colorette": { "version": "2.0.19", "resolved": "https://registry.npmjs.org/colorette/-/colorette-2.0.19.tgz", "integrity": "sha512-3tlv/dIP7FWvj3BsbHrGLJ6l/oKh1O3TcgBqMn+yyCagOxc23fyzDS6HypQbgxWbkpDnf52p1LuR4eWDQ/K9WQ==", "dev": true }, +======= +>>>>>>> c474bf760abc096c679e2f9ddb183a97f46718a9 "node_modules/concat-map": { "version": "0.0.1", "resolved": "https://registry.npmjs.org/concat-map/-/concat-map-0.0.1.tgz", @@ -1571,6 +1597,7 @@ "node": ">= 8" } }, +<<<<<<< HEAD "node_modules/dateformat": { "version": "4.6.3", "resolved": "https://registry.npmjs.org/dateformat/-/dateformat-4.6.3.tgz", @@ -1580,6 +1607,8 @@ "node": "*" } }, +======= +>>>>>>> c474bf760abc096c679e2f9ddb183a97f46718a9 "node_modules/debug": { "version": "4.3.4", "resolved": "https://registry.npmjs.org/debug/-/debug-4.3.4.tgz", @@ -1654,6 +1683,7 @@ "integrity": "sha512-MSjYzcWNOA0ewAHpz0MxpYFvwg6yjy1NG3xteoqz644VCo/RPgnr1/GGt+ic3iJTzQ8Eu3TdM14SawnVUmGE6A==", "dev": true }, +<<<<<<< HEAD "node_modules/end-of-stream": { "version": "1.4.4", "resolved": "https://registry.npmjs.org/end-of-stream/-/end-of-stream-1.4.4.tgz", @@ -1663,6 +1693,8 @@ "once": "^1.4.0" } }, +======= +>>>>>>> c474bf760abc096c679e2f9ddb183a97f46718a9 "node_modules/error-ex": { "version": "1.3.2", "resolved": "https://registry.npmjs.org/error-ex/-/error-ex-1.3.2.tgz", @@ -1703,6 +1735,7 @@ "node": ">=4" } }, +<<<<<<< HEAD "node_modules/event-target-shim": { "version": "5.0.1", "resolved": "https://registry.npmjs.org/event-target-shim/-/event-target-shim-5.0.1.tgz", @@ -1721,6 +1754,8 @@ "node": ">=0.8.x" } }, +======= +>>>>>>> c474bf760abc096c679e2f9ddb183a97f46718a9 "node_modules/execa": { "version": "5.1.1", "resolved": "https://registry.npmjs.org/execa/-/execa-5.1.1.tgz", @@ -1769,18 +1804,22 @@ "node": "^14.15.0 || ^16.10.0 || >=18.0.0" } }, +<<<<<<< HEAD "node_modules/fast-copy": { "version": "3.0.0", "resolved": "https://registry.npmjs.org/fast-copy/-/fast-copy-3.0.0.tgz", "integrity": "sha512-4HzS+9pQ5Yxtv13Lhs1Z1unMXamBdn5nA4bEi1abYpDNSpSp7ODYQ1KPMF6nTatfEzgH6/zPvXKU1zvHiUjWlA==", "dev": true }, +======= +>>>>>>> c474bf760abc096c679e2f9ddb183a97f46718a9 "node_modules/fast-json-stable-stringify": { "version": "2.1.0", "resolved": "https://registry.npmjs.org/fast-json-stable-stringify/-/fast-json-stable-stringify-2.1.0.tgz", "integrity": "sha512-lhd/wF+Lk98HZoTCtlVraHtfh5XYijIjalXck7saUtuanSDyLMxnHhSXEDJqHxD7msR8D0uCmqlkwjCV8xvwHw==", "dev": true }, +<<<<<<< HEAD "node_modules/fast-redact": { "version": "3.1.2", "resolved": "https://registry.npmjs.org/fast-redact/-/fast-redact-3.1.2.tgz", @@ -1796,6 +1835,8 @@ "integrity": "sha512-W+KJc2dmILlPplD/H4K9l9LcAHAfPtP6BY84uVLXQ6Evcz9Lcg33Y2z1IVblT6xdY54PXYVHEv+0Wpq8Io6zkA==", "dev": true }, +======= +>>>>>>> c474bf760abc096c679e2f9ddb183a97f46718a9 "node_modules/fb-watchman": { "version": "2.0.2", "resolved": "https://registry.npmjs.org/fb-watchman/-/fb-watchman-2.0.2.tgz", @@ -1967,6 +2008,7 @@ "node": ">=8" } }, +<<<<<<< HEAD "node_modules/help-me": { "version": "4.1.0", "resolved": "https://registry.npmjs.org/help-me/-/help-me-4.1.0.tgz", @@ -2017,6 +2059,8 @@ "node": ">=10" } }, +======= +>>>>>>> c474bf760abc096c679e2f9ddb183a97f46718a9 "node_modules/html-escaper": { "version": "2.0.2", "resolved": "https://registry.npmjs.org/html-escaper/-/html-escaper-2.0.2.tgz", @@ -2032,6 +2076,7 @@ "node": ">=10.17.0" } }, +<<<<<<< HEAD "node_modules/ieee754": { "version": "1.2.1", "resolved": "https://registry.npmjs.org/ieee754/-/ieee754-1.2.1.tgz", @@ -2052,6 +2097,8 @@ } ] }, +======= +>>>>>>> c474bf760abc096c679e2f9ddb183a97f46718a9 "node_modules/import-local": { "version": "3.1.0", "resolved": "https://registry.npmjs.org/import-local/-/import-local-3.1.0.tgz", @@ -2836,6 +2883,7 @@ } } }, +<<<<<<< HEAD "node_modules/joycon": { "version": "3.1.1", "resolved": "https://registry.npmjs.org/joycon/-/joycon-3.1.1.tgz", @@ -2845,6 +2893,8 @@ "node": ">=10" } }, +======= +>>>>>>> c474bf760abc096c679e2f9ddb183a97f46718a9 "node_modules/js-tokens": { "version": "4.0.0", "resolved": "https://registry.npmjs.org/js-tokens/-/js-tokens-4.0.0.tgz", @@ -3027,6 +3077,7 @@ "node": "*" } }, +<<<<<<< HEAD "node_modules/minimist": { "version": "1.2.7", "resolved": "https://registry.npmjs.org/minimist/-/minimist-1.2.7.tgz", @@ -3036,6 +3087,8 @@ "url": "https://github.com/sponsors/ljharb" } }, +======= +>>>>>>> c474bf760abc096c679e2f9ddb183a97f46718a9 "node_modules/ms": { "version": "2.1.2", "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.2.tgz", @@ -3081,12 +3134,15 @@ "node": ">=8" } }, +<<<<<<< HEAD "node_modules/on-exit-leak-free": { "version": "2.1.0", "resolved": "https://registry.npmjs.org/on-exit-leak-free/-/on-exit-leak-free-2.1.0.tgz", "integrity": "sha512-VuCaZZAjReZ3vUwgOB8LxAosIurDiAW0s13rI1YwmaP++jvcxP77AWoQvenZebpCA2m8WC1/EosPYPMjnRAp/w==", "dev": true }, +======= +>>>>>>> c474bf760abc096c679e2f9ddb183a97f46718a9 "node_modules/once": { "version": "1.4.0", "resolved": "https://registry.npmjs.org/once/-/once-1.4.0.tgz", @@ -3216,6 +3272,7 @@ "url": "https://github.com/sponsors/jonschlinkert" } }, +<<<<<<< HEAD "node_modules/pino": { "version": "8.7.0", "resolved": "https://registry.npmjs.org/pino/-/pino-8.7.0.tgz", @@ -3309,6 +3366,8 @@ "integrity": "sha512-mMMOwSKrmyl+Y12Ri2xhH1lbzQxwwpuru9VjyJpgFIH4asSj88F2csdMwN6+M5g1Ll4rmsYghHLQJw81tgZ7LQ==", "dev": true }, +======= +>>>>>>> c474bf760abc096c679e2f9ddb183a97f46718a9 "node_modules/pirates": { "version": "4.0.5", "resolved": "https://registry.npmjs.org/pirates/-/pirates-4.0.5.tgz", @@ -3356,6 +3415,7 @@ "url": "https://github.com/chalk/ansi-styles?sponsor=1" } }, +<<<<<<< HEAD "node_modules/process": { "version": "0.11.10", "resolved": "https://registry.npmjs.org/process/-/process-0.11.10.tgz", @@ -3371,6 +3431,8 @@ "integrity": "sha512-+MmoAXoUX+VTHAlwns0h+kFUWFs/3FZy+ZuchkgjyOu3oioLAo2LB5aCfKPh2+P9O18i3m43tUEv3YqttSy0Ww==", "dev": true }, +======= +>>>>>>> c474bf760abc096c679e2f9ddb183a97f46718a9 "node_modules/prompts": { "version": "2.4.2", "resolved": "https://registry.npmjs.org/prompts/-/prompts-2.4.2.tgz", @@ -3389,6 +3451,7 @@ "resolved": "https://registry.npmjs.org/psl/-/psl-1.9.0.tgz", "integrity": "sha512-E/ZsdU4HLs/68gYzgGTkMicWTLPdAftJLfJFlLUAAKZGkStNU72sZjT66SnMDVOfOWY/YAoiD7Jxa9iHvngcag==" }, +<<<<<<< HEAD "node_modules/pump": { "version": "3.0.0", "resolved": "https://registry.npmjs.org/pump/-/pump-3.0.0.tgz", @@ -3399,6 +3462,8 @@ "once": "^1.3.1" } }, +======= +>>>>>>> c474bf760abc096c679e2f9ddb183a97f46718a9 "node_modules/punycode": { "version": "2.1.1", "resolved": "https://registry.npmjs.org/punycode/-/punycode-2.1.1.tgz", @@ -3412,12 +3477,15 @@ "resolved": "https://registry.npmjs.org/querystringify/-/querystringify-2.2.0.tgz", "integrity": "sha512-FIqgj2EUvTa7R50u0rGsyTftzjYmv/a3hO345bZNrqabNqjtgiDMgmo4mkUjd+nzU5oF3dClKqFIPUKybUyqoQ==" }, +<<<<<<< HEAD "node_modules/quick-format-unescaped": { "version": "4.0.4", "resolved": "https://registry.npmjs.org/quick-format-unescaped/-/quick-format-unescaped-4.0.4.tgz", "integrity": "sha512-tYC1Q1hgyRuHgloV/YXs2w15unPVh8qfu/qCTfhTYamaw7fyhumKa2yGpdSo87vY32rIclj+4fWYQXUMs9EHvg==", "dev": true }, +======= +>>>>>>> c474bf760abc096c679e2f9ddb183a97f46718a9 "node_modules/react-is": { "version": "18.2.0", "resolved": "https://registry.npmjs.org/react-is/-/react-is-18.2.0.tgz", @@ -3437,6 +3505,7 @@ "node": ">= 6" } }, +<<<<<<< HEAD "node_modules/real-require": { "version": "0.2.0", "resolved": "https://registry.npmjs.org/real-require/-/real-require-0.2.0.tgz", @@ -3446,6 +3515,8 @@ "node": ">= 12.13.0" } }, +======= +>>>>>>> c474bf760abc096c679e2f9ddb183a97f46718a9 "node_modules/require-directory": { "version": "2.1.1", "resolved": "https://registry.npmjs.org/require-directory/-/require-directory-2.1.1.tgz", @@ -3526,6 +3597,7 @@ } ] }, +<<<<<<< HEAD "node_modules/safe-stable-stringify": { "version": "2.4.1", "resolved": "https://registry.npmjs.org/safe-stable-stringify/-/safe-stable-stringify-2.4.1.tgz", @@ -3541,6 +3613,8 @@ "integrity": "sha512-ZQruFgZnIWH+WyO9t5rWt4ZEGqCKPwhiw+YbzTwpmT9elgLrLcfuyUiSnwwjUiVy9r4VM3urtbNF1xmEh9IL2w==", "dev": true }, +======= +>>>>>>> c474bf760abc096c679e2f9ddb183a97f46718a9 "node_modules/semver": { "version": "7.3.8", "resolved": "https://registry.npmjs.org/semver/-/semver-7.3.8.tgz", @@ -3598,6 +3672,7 @@ "node": ">=8" } }, +<<<<<<< HEAD "node_modules/sonic-boom": { "version": "3.2.0", "resolved": "https://registry.npmjs.org/sonic-boom/-/sonic-boom-3.2.0.tgz", @@ -3607,6 +3682,8 @@ "atomic-sleep": "^1.0.0" } }, +======= +>>>>>>> c474bf760abc096c679e2f9ddb183a97f46718a9 "node_modules/source-map": { "version": "0.6.1", "resolved": "https://registry.npmjs.org/source-map/-/source-map-0.6.1.tgz", @@ -3626,6 +3703,7 @@ "source-map": "^0.6.0" } }, +<<<<<<< HEAD "node_modules/split2": { "version": "4.1.0", "resolved": "https://registry.npmjs.org/split2/-/split2-4.1.0.tgz", @@ -3635,6 +3713,8 @@ "node": ">= 10.x" } }, +======= +>>>>>>> c474bf760abc096c679e2f9ddb183a97f46718a9 "node_modules/sprintf-js": { "version": "1.0.3", "resolved": "https://registry.npmjs.org/sprintf-js/-/sprintf-js-1.0.3.tgz", @@ -3777,6 +3857,7 @@ "node": ">=8" } }, +<<<<<<< HEAD "node_modules/thread-stream": { "version": "2.2.0", "resolved": "https://registry.npmjs.org/thread-stream/-/thread-stream-2.2.0.tgz", @@ -3786,6 +3867,8 @@ "real-require": "^0.2.0" } }, +======= +>>>>>>> c474bf760abc096c679e2f9ddb183a97f46718a9 "node_modules/through2": { "version": "4.0.2", "resolved": "https://registry.npmjs.org/through2/-/through2-4.0.2.tgz", @@ -4974,6 +5057,7 @@ "integrity": "sha512-iO9ZQHkZxHn4mSakYV0vFHAVDyEOIJQrV2uZ06HxEPcx+mt8swXoZHIbaaJ2crJYFfErySgktuTZ3BeLz+XmFA==", "dev": true }, +<<<<<<< HEAD "abort-controller": { "version": "3.0.0", "resolved": "https://registry.npmjs.org/abort-controller/-/abort-controller-3.0.0.tgz", @@ -4983,6 +5067,8 @@ "event-target-shim": "^5.0.0" } }, +======= +>>>>>>> c474bf760abc096c679e2f9ddb183a97f46718a9 "already": { "version": "2.2.1", "resolved": "https://registry.npmjs.org/already/-/already-2.2.1.tgz", @@ -6684,6 +6770,7 @@ "resolved": "https://registry.npmjs.org/psl/-/psl-1.9.0.tgz", "integrity": "sha512-E/ZsdU4HLs/68gYzgGTkMicWTLPdAftJLfJFlLUAAKZGkStNU72sZjT66SnMDVOfOWY/YAoiD7Jxa9iHvngcag==" }, +<<<<<<< HEAD "pump": { "version": "3.0.0", "resolved": "https://registry.npmjs.org/pump/-/pump-3.0.0.tgz", @@ -6694,6 +6781,8 @@ "once": "^1.3.1" } }, +======= +>>>>>>> c474bf760abc096c679e2f9ddb183a97f46718a9 "punycode": { "version": "2.1.1", "resolved": "https://registry.npmjs.org/punycode/-/punycode-2.1.1.tgz", @@ -6704,12 +6793,15 @@ "resolved": "https://registry.npmjs.org/querystringify/-/querystringify-2.2.0.tgz", "integrity": "sha512-FIqgj2EUvTa7R50u0rGsyTftzjYmv/a3hO345bZNrqabNqjtgiDMgmo4mkUjd+nzU5oF3dClKqFIPUKybUyqoQ==" }, +<<<<<<< HEAD "quick-format-unescaped": { "version": "4.0.4", "resolved": "https://registry.npmjs.org/quick-format-unescaped/-/quick-format-unescaped-4.0.4.tgz", "integrity": "sha512-tYC1Q1hgyRuHgloV/YXs2w15unPVh8qfu/qCTfhTYamaw7fyhumKa2yGpdSo87vY32rIclj+4fWYQXUMs9EHvg==", "dev": true }, +======= +>>>>>>> c474bf760abc096c679e2f9ddb183a97f46718a9 "react-is": { "version": "18.2.0", "resolved": "https://registry.npmjs.org/react-is/-/react-is-18.2.0.tgz", @@ -6726,12 +6818,15 @@ "util-deprecate": "^1.0.1" } }, +<<<<<<< HEAD "real-require": { "version": "0.2.0", "resolved": "https://registry.npmjs.org/real-require/-/real-require-0.2.0.tgz", "integrity": "sha512-57frrGM/OCTLqLOAh0mhVA9VBMHd+9U7Zb2THMGdBUoZVOtGbJzjxsYGDJ3A9AYYCP4hn6y1TVbaOfzWtm5GFg==", "dev": true }, +======= +>>>>>>> c474bf760abc096c679e2f9ddb183a97f46718a9 "require-directory": { "version": "2.1.1", "resolved": "https://registry.npmjs.org/require-directory/-/require-directory-2.1.1.tgz", @@ -6780,6 +6875,7 @@ "resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.2.1.tgz", "integrity": "sha512-rp3So07KcdmmKbGvgaNxQSJr7bGVSVk5S9Eq1F+ppbRo70+YeaDxkw5Dd8NPN+GD6bjnYm2VuPuCXmpuYvmCXQ==" }, +<<<<<<< HEAD "safe-stable-stringify": { "version": "2.4.1", "resolved": "https://registry.npmjs.org/safe-stable-stringify/-/safe-stable-stringify-2.4.1.tgz", @@ -6792,6 +6888,8 @@ "integrity": "sha512-ZQruFgZnIWH+WyO9t5rWt4ZEGqCKPwhiw+YbzTwpmT9elgLrLcfuyUiSnwwjUiVy9r4VM3urtbNF1xmEh9IL2w==", "dev": true }, +======= +>>>>>>> c474bf760abc096c679e2f9ddb183a97f46718a9 "semver": { "version": "7.3.8", "resolved": "https://registry.npmjs.org/semver/-/semver-7.3.8.tgz", @@ -6970,6 +7068,7 @@ "minimatch": "^3.0.4" } }, +<<<<<<< HEAD "thread-stream": { "version": "2.2.0", "resolved": "https://registry.npmjs.org/thread-stream/-/thread-stream-2.2.0.tgz", @@ -6979,6 +7078,8 @@ "real-require": "^0.2.0" } }, +======= +>>>>>>> c474bf760abc096c679e2f9ddb183a97f46718a9 "through2": { "version": "4.0.2", "resolved": "https://registry.npmjs.org/through2/-/through2-4.0.2.tgz", diff --git a/package.json b/package.json index aac228f..8d6eb35 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@adileo/surrealdb-ts-client", - "version": "2.0.1", + "version": "2.0.2", "description": "", "main": "lib/index.js", "types": "lib/index.d.ts", diff --git a/src/index.ts b/src/index.ts index ab678e2..0707da3 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,8 +1,213 @@ +<<<<<<< HEAD import SurrealRESTClient from "./restClient"; +======= +import { context, Response } from 'fetch-h2' +const ctx = context({ + userAgent: 'surrealdb-ts-client' +}); +export class SurrealType { + type: string + value: any + constructor(type: string, value: any) { + this.type = type + this.value = value + } +} +export class SDBType { + static Record(ref: string) { + return new SurrealType("record", ref) + } +} +export type StatementResponse = { + time: string + status: string + result: null | Array +} +export class SurrealObject { + static serialize(obj: any): string { + if (typeof obj !== 'object' || obj === null || obj instanceof Array) { + return SurrealObject.value(obj); + } +>>>>>>> c474bf760abc096c679e2f9ddb183a97f46718a9 export * from "./types" export * from "./collection" export * from "./model" export * from "./serializer" +<<<<<<< HEAD +======= +type ReturnType = "lastStatementSingle" | "lastStatementArray" | "raw" + +type SurrealRESTClientOptions = { + ns?: string + db?: string + + user?: string + pass?: string + token?: string // Access token + + logQuery?: boolean +} +type QueryResult = +RT extends "lastStatementSingle" ? T : +RT extends "raw" ? StatementResponse[] : +RT extends "lastStatementArray" ? T[] : never; + +type SurrealRESTClientConstructor = { + ns?: string + db?: string + user?: string + pass?: string + token?: string +} +export class SurrealRESTClient { + endpoint: string + ns?: string + db?: string + + user?: string + pass?: string + token?: string + + public constructor(endpoint: string, { ns, db, user, pass, token }: SurrealRESTClientConstructor) { + this.endpoint = endpoint + this.ns = ns + this.db = db + this.user = user + this.pass = pass + this.token = token + } + public async query(sql: string, variables: any, returnType: ReturnType, opts: SurrealRESTClientOptions = {}): Promise> { + const pre = Object.entries(variables).map(([k, v]) => { + return "LET $" + k + " = " + SurrealObject.serialize(v) + ";\n"; + }).join("") + const query = pre + sql + if(opts.logQuery) + console.log(query) + const response = await ctx.fetch(this.endpoint + "/sql", { + method: 'POST', + body: query, + headers: { + Accept: 'application/json', + Authorization: this.getAuthorization(opts), + NS: (opts.ns ? opts.ns : this.ns) || '', + DB: (opts.db ? opts.db : this.db) || '', + } + }) + return await this.parseResponse(response, returnType) as QueryResult + } + public async queryRaw(sql: string, variables: any, opts: SurrealRESTClientOptions = {}) { + return this.query(sql, variables, "raw", opts) + } + public async queryLast(sql: string, variables: any, opts: SurrealRESTClientOptions = {}) { + return this.query(sql, variables, "lastStatementSingle", opts) + } + public async queryLastArray(sql: string, variables: any, opts: SurrealRESTClientOptions = {}) { + return this.query(sql, variables, "lastStatementArray", opts) + } + + // Create or set (override all) record content + public async createOrSet(tableOrKey: string, data: any, opts: SurrealRESTClientOptions = {}): Promise { + const {thing, params} = this.tableOrThing(tableOrKey) + return this.queryLast(`CREATE ${thing} CONTENT $data;`, { + ...params, + data + }, {...opts}) + } + + // // Select table + public async findMany(table: string, opts: SurrealRESTClientOptions = {}): Promise { + const {thing, params, isThing} = this.tableOrThing(table) + const sql = `SELECT * FROM ${thing};` + if(isThing){ + throw new Error("Wrong ID") + }else{ + return this.queryLastArray(sql, {...params}, {...opts}) + } + } + + public async findById(id: string, opts: SurrealRESTClientOptions = {}): Promise { + const {thing, params, isThing} = this.tableOrThing(id) + const sql = `SELECT * FROM ${thing};` + if(isThing){ + return this.queryLast(sql, {...params}, {...opts}) + }else{ + throw new Error("Wrong ID") + } + } + + public async deleteById(id: string, opts: SurrealRESTClientOptions = {}): Promise { + const {thing, params, isThing} = this.tableOrThing(id) + const sql = `DELETE ${thing};` + if(isThing){ + return this.queryLast(sql, {...params}, {...opts}) + }else{ + throw new Error("Wrong ID") + } + } + + // Set field (MERGE Mode) into existing record/s + public async mergeSet(tableOrKey: string, data: any, opts: SurrealRESTClientOptions = {}) { + const {thing, params, isThing} = this.tableOrThing(tableOrKey) + const sql = `UPDATE ${thing} MERGE $data RETURN NONE;` + if(isThing){ + return this.queryLast(sql, {data, ...params}, {...opts}) + }else{ + return this.queryLastArray(sql, {data, ...params}, {...opts}) + } + } + + private tableOrThing(key: string){ + let split = key.split(":") + if(split.length == 1){ + return { + thing: "type::table($table)", + params: {"table": split[0]}, + isTable: true + } + }else if(split.length == 2){ + return { + thing: "type::thing($table, $itemId)", + params: {"table": split[0], "itemId": split[1]}, + isThing: true + } + } + throw new Error("Invalid key"); + } + private async parseResponse(response: Response, returnType: ReturnType): Promise[] | T | T[] | null> { + const resp: StatementResponse[] = await response.json() + if((resp as any).code){ + throw resp + } + // Return all JSON response + if (returnType === "raw") + return resp + if (resp.length > 0) { + if(returnType === "lastStatementSingle" && resp[resp.length-1].result !== null && resp[resp.length-1].result!.length > 0){ + // Pick last query first result: object + return resp[resp.length-1].result![0] + }else{ + // Pick last query all results: array + return resp[resp.length-1].result + } + } else { + return [] + } + } + + private getAuthorization(opts: SurrealRESTClientOptions) { + let username = opts.user ? opts.user : this.user + let password = opts.pass ? opts.pass : this.pass + let token = opts.token ? opts.token : this.token + if(token){ + return 'Bearer ' + token + }else{ + const authorization = Buffer.from(username + ":" + password) + return 'Basic ' + authorization.toString('base64') + } + } + +} +>>>>>>> c474bf760abc096c679e2f9ddb183a97f46718a9 export default SurrealRESTClient \ No newline at end of file