From c923785a90c0708fe40dfb56a04c1813b85d8ea1 Mon Sep 17 00:00:00 2001 From: jusax23 Date: Mon, 23 Jan 2023 21:19:18 +0100 Subject: [PATCH] init --- .gitignore | 3 + .npmignore | 0 package-lock.json | 506 ++++++++++++++++++++++++++++++++++++++++++ package.json | 33 +++ src/db.ts | 75 +++++++ src/dbStructure.ts | 39 ++++ src/defaultHandler.ts | 76 +++++++ src/funcs.ts | 22 ++ src/query.ts | 47 ++++ src/types.ts | 19 ++ tsconfig.json | 21 ++ 11 files changed, 841 insertions(+) create mode 100644 .gitignore create mode 100644 .npmignore create mode 100644 package-lock.json create mode 100644 package.json create mode 100644 src/db.ts create mode 100644 src/dbStructure.ts create mode 100644 src/defaultHandler.ts create mode 100644 src/funcs.ts create mode 100644 src/query.ts create mode 100644 src/types.ts create mode 100644 tsconfig.json diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..795255a --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +dist +node_modules +test.ts \ No newline at end of file diff --git a/.npmignore b/.npmignore new file mode 100644 index 0000000..e69de29 diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 0000000..312e5d8 --- /dev/null +++ b/package-lock.json @@ -0,0 +1,506 @@ +{ + "name": "dblang", + "version": "1.0.0", + "lockfileVersion": 3, + "requires": true, + "packages": { + "": { + "name": "dblang", + "version": "1.0.0", + "license": "UNLICENSED", + "dependencies": { + "mariadb": "^3.0.2" + }, + "devDependencies": { + "@types/node": "^18.11.18", + "esbuild": "^0.17.4", + "typescript": "^4.9.4" + } + }, + "node_modules/@esbuild/android-arm": { + "version": "0.17.4", + "resolved": "https://registry.npmjs.org/@esbuild/android-arm/-/android-arm-0.17.4.tgz", + "integrity": "sha512-R9GCe2xl2XDSc2XbQB63mFiFXHIVkOP+ltIxICKXqUPrFX97z6Z7vONCLQM1pSOLGqfLrGi3B7nbhxmFY/fomg==", + "cpu": [ + "arm" + ], + "dev": true, + "optional": true, + "os": [ + "android" + ], + "engines": { + "node": ">=12" + } + }, + "node_modules/@esbuild/android-arm64": { + "version": "0.17.4", + "resolved": "https://registry.npmjs.org/@esbuild/android-arm64/-/android-arm64-0.17.4.tgz", + "integrity": "sha512-91VwDrl4EpxBCiG6h2LZZEkuNvVZYJkv2T9gyLG/mhGG1qrM7i5SwUcg/hlSPnL/4hDT0TFcF35/XMGSn0bemg==", + "cpu": [ + "arm64" + ], + "dev": true, + "optional": true, + "os": [ + "android" + ], + "engines": { + "node": ">=12" + } + }, + "node_modules/@esbuild/android-x64": { + "version": "0.17.4", + "resolved": "https://registry.npmjs.org/@esbuild/android-x64/-/android-x64-0.17.4.tgz", + "integrity": "sha512-mGSqhEPL7029XL7QHNPxPs15JVa02hvZvysUcyMP9UXdGFwncl2WU0bqx+Ysgzd+WAbv8rfNa73QveOxAnAM2w==", + "cpu": [ + "x64" + ], + "dev": true, + "optional": true, + "os": [ + "android" + ], + "engines": { + "node": ">=12" + } + }, + "node_modules/@esbuild/darwin-arm64": { + "version": "0.17.4", + "resolved": "https://registry.npmjs.org/@esbuild/darwin-arm64/-/darwin-arm64-0.17.4.tgz", + "integrity": "sha512-tTyJRM9dHvlMPt1KrBFVB5OW1kXOsRNvAPtbzoKazd5RhD5/wKlXk1qR2MpaZRYwf4WDMadt0Pv0GwxB41CVow==", + "cpu": [ + "arm64" + ], + "dev": true, + "optional": true, + "os": [ + "darwin" + ], + "engines": { + "node": ">=12" + } + }, + "node_modules/@esbuild/darwin-x64": { + "version": "0.17.4", + "resolved": "https://registry.npmjs.org/@esbuild/darwin-x64/-/darwin-x64-0.17.4.tgz", + "integrity": "sha512-phQuC2Imrb3TjOJwLN8EO50nb2FHe8Ew0OwgZDH1SV6asIPGudnwTQtighDF2EAYlXChLoMJwqjAp4vAaACq6w==", + "cpu": [ + "x64" + ], + "dev": true, + "optional": true, + "os": [ + "darwin" + ], + "engines": { + "node": ">=12" + } + }, + "node_modules/@esbuild/freebsd-arm64": { + "version": "0.17.4", + "resolved": "https://registry.npmjs.org/@esbuild/freebsd-arm64/-/freebsd-arm64-0.17.4.tgz", + "integrity": "sha512-oH6JUZkocgmjzzYaP5juERLpJQSwazdjZrTPgLRmAU2bzJ688x0vfMB/WTv4r58RiecdHvXOPC46VtsMy/mepg==", + "cpu": [ + "arm64" + ], + "dev": true, + "optional": true, + "os": [ + "freebsd" + ], + "engines": { + "node": ">=12" + } + }, + "node_modules/@esbuild/freebsd-x64": { + "version": "0.17.4", + "resolved": "https://registry.npmjs.org/@esbuild/freebsd-x64/-/freebsd-x64-0.17.4.tgz", + "integrity": "sha512-U4iWGn/9TrAfpAdfd56eO0pRxIgb0a8Wj9jClrhT8hvZnOnS4dfMPW7o4fn15D/KqoiVYHRm43jjBaTt3g/2KA==", + "cpu": [ + "x64" + ], + "dev": true, + "optional": true, + "os": [ + "freebsd" + ], + "engines": { + "node": ">=12" + } + }, + "node_modules/@esbuild/linux-arm": { + "version": "0.17.4", + "resolved": "https://registry.npmjs.org/@esbuild/linux-arm/-/linux-arm-0.17.4.tgz", + "integrity": "sha512-S2s9xWTGMTa/fG5EyMGDeL0wrWVgOSQcNddJWgu6rG1NCSXJHs76ZP9AsxjB3f2nZow9fWOyApklIgiTGZKhiw==", + "cpu": [ + "arm" + ], + "dev": true, + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=12" + } + }, + "node_modules/@esbuild/linux-arm64": { + "version": "0.17.4", + "resolved": "https://registry.npmjs.org/@esbuild/linux-arm64/-/linux-arm64-0.17.4.tgz", + "integrity": "sha512-UkGfQvYlwOaeYJzZG4cLV0hCASzQZnKNktRXUo3/BMZvdau40AOz9GzmGA063n1piq6VrFFh43apRDQx8hMP2w==", + "cpu": [ + "arm64" + ], + "dev": true, + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=12" + } + }, + "node_modules/@esbuild/linux-ia32": { + "version": "0.17.4", + "resolved": "https://registry.npmjs.org/@esbuild/linux-ia32/-/linux-ia32-0.17.4.tgz", + "integrity": "sha512-3lqFi4VFo/Vwvn77FZXeLd0ctolIJH/uXkH3yNgEk89Eh6D3XXAC9/iTPEzeEpsNE5IqGIsFa5Z0iPeOh25IyA==", + "cpu": [ + "ia32" + ], + "dev": true, + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=12" + } + }, + "node_modules/@esbuild/linux-loong64": { + "version": "0.17.4", + "resolved": "https://registry.npmjs.org/@esbuild/linux-loong64/-/linux-loong64-0.17.4.tgz", + "integrity": "sha512-HqpWZkVslDHIwdQ9D+gk7NuAulgQvRxF9no54ut/M55KEb3mi7sQS3GwpPJzSyzzP0UkjQVN7/tbk88/CaX4EQ==", + "cpu": [ + "loong64" + ], + "dev": true, + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=12" + } + }, + "node_modules/@esbuild/linux-mips64el": { + "version": "0.17.4", + "resolved": "https://registry.npmjs.org/@esbuild/linux-mips64el/-/linux-mips64el-0.17.4.tgz", + "integrity": "sha512-d/nMCKKh/SVDbqR9ju+b78vOr0tNXtfBjcp5vfHONCCOAL9ad8gN9dC/u+UnH939pz7wO+0u/x9y1MaZcb/lKA==", + "cpu": [ + "mips64el" + ], + "dev": true, + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=12" + } + }, + "node_modules/@esbuild/linux-ppc64": { + "version": "0.17.4", + "resolved": "https://registry.npmjs.org/@esbuild/linux-ppc64/-/linux-ppc64-0.17.4.tgz", + "integrity": "sha512-lOD9p2dmjZcNiTU+sGe9Nn6G3aYw3k0HBJies1PU0j5IGfp6tdKOQ6mzfACRFCqXjnBuTqK7eTYpwx09O5LLfg==", + "cpu": [ + "ppc64" + ], + "dev": true, + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=12" + } + }, + "node_modules/@esbuild/linux-riscv64": { + "version": "0.17.4", + "resolved": "https://registry.npmjs.org/@esbuild/linux-riscv64/-/linux-riscv64-0.17.4.tgz", + "integrity": "sha512-mTGnwWwVshAjGsd8rP+K6583cPDgxOunsqqldEYij7T5/ysluMHKqUIT4TJHfrDFadUwrghAL6QjER4FeqQXoA==", + "cpu": [ + "riscv64" + ], + "dev": true, + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=12" + } + }, + "node_modules/@esbuild/linux-s390x": { + "version": "0.17.4", + "resolved": "https://registry.npmjs.org/@esbuild/linux-s390x/-/linux-s390x-0.17.4.tgz", + "integrity": "sha512-AQYuUGp50XM29/N/dehADxvc2bUqDcoqrVuijop1Wv72SyxT6dDB9wjUxuPZm2HwIM876UoNNBMVd+iX/UTKVQ==", + "cpu": [ + "s390x" + ], + "dev": true, + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=12" + } + }, + "node_modules/@esbuild/linux-x64": { + "version": "0.17.4", + "resolved": "https://registry.npmjs.org/@esbuild/linux-x64/-/linux-x64-0.17.4.tgz", + "integrity": "sha512-+AsFBwKgQuhV2shfGgA9YloxLDVjXgUEWZum7glR5lLmV94IThu/u2JZGxTgjYby6kyXEx8lKOqP5rTEVBR0Rw==", + "cpu": [ + "x64" + ], + "dev": true, + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=12" + } + }, + "node_modules/@esbuild/netbsd-x64": { + "version": "0.17.4", + "resolved": "https://registry.npmjs.org/@esbuild/netbsd-x64/-/netbsd-x64-0.17.4.tgz", + "integrity": "sha512-zD1TKYX9553OiLS/qkXPMlWoELYkH/VkzRYNKEU+GwFiqkq0SuxsKnsCg5UCdxN3cqd+1KZ8SS3R+WG/Hxy2jQ==", + "cpu": [ + "x64" + ], + "dev": true, + "optional": true, + "os": [ + "netbsd" + ], + "engines": { + "node": ">=12" + } + }, + "node_modules/@esbuild/openbsd-x64": { + "version": "0.17.4", + "resolved": "https://registry.npmjs.org/@esbuild/openbsd-x64/-/openbsd-x64-0.17.4.tgz", + "integrity": "sha512-PY1NjEsLRhPEFFg1AV0/4Or/gR+q2dOb9s5rXcPuCjyHRzbt8vnHJl3vYj+641TgWZzTFmSUnZbzs1zwTzjeqw==", + "cpu": [ + "x64" + ], + "dev": true, + "optional": true, + "os": [ + "openbsd" + ], + "engines": { + "node": ">=12" + } + }, + "node_modules/@esbuild/sunos-x64": { + "version": "0.17.4", + "resolved": "https://registry.npmjs.org/@esbuild/sunos-x64/-/sunos-x64-0.17.4.tgz", + "integrity": "sha512-B3Z7s8QZQW9tKGleMRXvVmwwLPAUoDCHs4WZ2ElVMWiortLJFowU1NjAhXOKjDgC7o9ByeVcwyOlJ+F2r6ZgmQ==", + "cpu": [ + "x64" + ], + "dev": true, + "optional": true, + "os": [ + "sunos" + ], + "engines": { + "node": ">=12" + } + }, + "node_modules/@esbuild/win32-arm64": { + "version": "0.17.4", + "resolved": "https://registry.npmjs.org/@esbuild/win32-arm64/-/win32-arm64-0.17.4.tgz", + "integrity": "sha512-0HCu8R3mY/H5V7N6kdlsJkvrT591bO/oRZy8ztF1dhgNU5xD5tAh5bKByT1UjTGjp/VVBsl1PDQ3L18SfvtnBQ==", + "cpu": [ + "arm64" + ], + "dev": true, + "optional": true, + "os": [ + "win32" + ], + "engines": { + "node": ">=12" + } + }, + "node_modules/@esbuild/win32-ia32": { + "version": "0.17.4", + "resolved": "https://registry.npmjs.org/@esbuild/win32-ia32/-/win32-ia32-0.17.4.tgz", + "integrity": "sha512-VUjhVDQycse1gLbe06pC/uaA0M+piQXJpdpNdhg8sPmeIZZqu5xPoGWVCmcsOO2gaM2cywuTYTHkXRozo3/Nkg==", + "cpu": [ + "ia32" + ], + "dev": true, + "optional": true, + "os": [ + "win32" + ], + "engines": { + "node": ">=12" + } + }, + "node_modules/@esbuild/win32-x64": { + "version": "0.17.4", + "resolved": "https://registry.npmjs.org/@esbuild/win32-x64/-/win32-x64-0.17.4.tgz", + "integrity": "sha512-0kLAjs+xN5OjhTt/aUA6t48SfENSCKgGPfExADYTOo/UCn0ivxos9/anUVeSfg+L+2O9xkFxvJXIJfG+Q4sYSg==", + "cpu": [ + "x64" + ], + "dev": true, + "optional": true, + "os": [ + "win32" + ], + "engines": { + "node": ">=12" + } + }, + "node_modules/@types/geojson": { + "version": "7946.0.10", + "resolved": "https://registry.npmjs.org/@types/geojson/-/geojson-7946.0.10.tgz", + "integrity": "sha512-Nmh0K3iWQJzniTuPRcJn5hxXkfB1T1pgB89SBig5PlJQU5yocazeu4jATJlaA0GYFKWMqDdvYemoSnF2pXgLVA==" + }, + "node_modules/@types/node": { + "version": "18.11.18", + "resolved": "https://registry.npmjs.org/@types/node/-/node-18.11.18.tgz", + "integrity": "sha512-DHQpWGjyQKSHj3ebjFI/wRKcqQcdR+MoFBygntYOZytCqNfkd2ZC4ARDJ2DQqhjH5p85Nnd3jhUJIXrszFX/JA==", + "dev": true + }, + "node_modules/denque": { + "version": "2.1.0", + "resolved": "https://registry.npmjs.org/denque/-/denque-2.1.0.tgz", + "integrity": "sha512-HVQE3AAb/pxF8fQAoiqpvg9i3evqug3hoiwakOyZAwJm+6vZehbkYXZ0l4JxS+I3QxM97v5aaRNhj8v5oBhekw==", + "engines": { + "node": ">=0.10" + } + }, + "node_modules/esbuild": { + "version": "0.17.4", + "resolved": "https://registry.npmjs.org/esbuild/-/esbuild-0.17.4.tgz", + "integrity": "sha512-zBn9MeCwT7W5F1a3lXClD61ip6vQM+H8Msb0w8zMT4ZKBpDg+rFAraNyWCDelB/2L6M3g6AXHPnsyvjMFnxtFw==", + "dev": true, + "hasInstallScript": true, + "bin": { + "esbuild": "bin/esbuild" + }, + "engines": { + "node": ">=12" + }, + "optionalDependencies": { + "@esbuild/android-arm": "0.17.4", + "@esbuild/android-arm64": "0.17.4", + "@esbuild/android-x64": "0.17.4", + "@esbuild/darwin-arm64": "0.17.4", + "@esbuild/darwin-x64": "0.17.4", + "@esbuild/freebsd-arm64": "0.17.4", + "@esbuild/freebsd-x64": "0.17.4", + "@esbuild/linux-arm": "0.17.4", + "@esbuild/linux-arm64": "0.17.4", + "@esbuild/linux-ia32": "0.17.4", + "@esbuild/linux-loong64": "0.17.4", + "@esbuild/linux-mips64el": "0.17.4", + "@esbuild/linux-ppc64": "0.17.4", + "@esbuild/linux-riscv64": "0.17.4", + "@esbuild/linux-s390x": "0.17.4", + "@esbuild/linux-x64": "0.17.4", + "@esbuild/netbsd-x64": "0.17.4", + "@esbuild/openbsd-x64": "0.17.4", + "@esbuild/sunos-x64": "0.17.4", + "@esbuild/win32-arm64": "0.17.4", + "@esbuild/win32-ia32": "0.17.4", + "@esbuild/win32-x64": "0.17.4" + } + }, + "node_modules/iconv-lite": { + "version": "0.6.3", + "resolved": "https://registry.npmjs.org/iconv-lite/-/iconv-lite-0.6.3.tgz", + "integrity": "sha512-4fCk79wshMdzMp2rH06qWrJE4iolqLhCUH+OiuIgU++RB0+94NlDL81atO7GX55uUKueo0txHNtvEyI6D7WdMw==", + "dependencies": { + "safer-buffer": ">= 2.1.2 < 3.0.0" + }, + "engines": { + "node": ">=0.10.0" + } + }, + "node_modules/lru-cache": { + "version": "7.14.1", + "resolved": "https://registry.npmjs.org/lru-cache/-/lru-cache-7.14.1.tgz", + "integrity": "sha512-ysxwsnTKdAx96aTRdhDOCQfDgbHnt8SK0KY8SEjO0wHinhWOFTESbjVCMPbU1uGXg/ch4lifqx0wfjOawU2+WA==", + "engines": { + "node": ">=12" + } + }, + "node_modules/mariadb": { + "version": "3.0.2", + "resolved": "https://registry.npmjs.org/mariadb/-/mariadb-3.0.2.tgz", + "integrity": "sha512-dVjiQZ6RW0IXFnX+T/ZEmnqs724DgkQsXqfCyInXn0XxVfO2Px6KbS4M3Ny6UiBg0zJ93SHHvfVBgYO4ZnFvvw==", + "dependencies": { + "@types/geojson": "^7946.0.10", + "@types/node": "^17.0.45", + "denque": "^2.1.0", + "iconv-lite": "^0.6.3", + "lru-cache": "^7.14.0", + "moment-timezone": "^0.5.38" + }, + "engines": { + "node": ">= 12" + } + }, + "node_modules/mariadb/node_modules/@types/node": { + "version": "17.0.45", + "resolved": "https://registry.npmjs.org/@types/node/-/node-17.0.45.tgz", + "integrity": "sha512-w+tIMs3rq2afQdsPJlODhoUEKzFP1ayaoyl1CcnwtIlsVe7K7bA1NGm4s3PraqTLlXnbIN84zuBlxBWo1u9BLw==" + }, + "node_modules/moment": { + "version": "2.29.4", + "resolved": "https://registry.npmjs.org/moment/-/moment-2.29.4.tgz", + "integrity": "sha512-5LC9SOxjSc2HF6vO2CyuTDNivEdoz2IvyJJGj6X8DJ0eFyfszE0QiEd+iXmBvUP3WHxSjFH/vIsA0EN00cgr8w==", + "engines": { + "node": "*" + } + }, + "node_modules/moment-timezone": { + "version": "0.5.40", + "resolved": "https://registry.npmjs.org/moment-timezone/-/moment-timezone-0.5.40.tgz", + "integrity": "sha512-tWfmNkRYmBkPJz5mr9GVDn9vRlVZOTe6yqY92rFxiOdWXbjaR0+9LwQnZGGuNR63X456NqmEkbskte8tWL5ePg==", + "dependencies": { + "moment": ">= 2.9.0" + }, + "engines": { + "node": "*" + } + }, + "node_modules/safer-buffer": { + "version": "2.1.2", + "resolved": "https://registry.npmjs.org/safer-buffer/-/safer-buffer-2.1.2.tgz", + "integrity": "sha512-YZo3K82SD7Riyi0E1EQPojLz7kpepnSQI9IyPbHHg1XXXevb5dJI7tpyN2ADxGcQbHG7vcyRHk0cbwqcQriUtg==" + }, + "node_modules/typescript": { + "version": "4.9.4", + "resolved": "https://registry.npmjs.org/typescript/-/typescript-4.9.4.tgz", + "integrity": "sha512-Uz+dTXYzxXXbsFpM86Wh3dKCxrQqUcVMxwU54orwlJjOpO3ao8L7j5lH+dWfTwgCwIuM9GQ2kvVotzYJMXTBZg==", + "dev": true, + "bin": { + "tsc": "bin/tsc", + "tsserver": "bin/tsserver" + }, + "engines": { + "node": ">=4.2.0" + } + } + } +} diff --git a/package.json b/package.json new file mode 100644 index 0000000..2702980 --- /dev/null +++ b/package.json @@ -0,0 +1,33 @@ +{ + "name": "dblang", + "version": "1.0.0", + "description": "", + "main": "dist/db.js", + "types": "dist/db.d.ts", + "type": "commonjs", + "directories": { + "dsit": "dist" + }, + "files": [ + "dist" + ], + "private": false, + "scripts": { + "build": "esbuild test.ts --bundle --outdir=dist", + "run": "npm run build && echo 'start:' && node dist/test.js", + "prepublish": "tsc" + }, + "author": "jusax23", + "license": "UNLICENSED", + "dependencies": { + "mariadb": "^3.0.2" + }, + "engines": { + "node": ">= 14" + }, + "devDependencies": { + "@types/node": "^18.11.18", + "esbuild": "^0.17.4", + "typescript": "^4.9.4" + } +} diff --git a/src/db.ts b/src/db.ts new file mode 100644 index 0000000..1266a2a --- /dev/null +++ b/src/db.ts @@ -0,0 +1,75 @@ +import mariadb from 'mariadb'; +import { Handler } from './defaultHandler'; +import { Query } from './query'; +import { onAction, primaryData } from './types'; + + +export class DB { + //pool:mariadb.Pool; + constructor(/*{ host, user, password, database, connectionLimit = 5 }*/) { + //this.pool = mariadb.createPool({ host, user, password, database, connectionLimit, multipleStatements: true }); + } + async query(query: Query) { + //return this.pool.query(query); + } + getHandler() { + return Handler; + } + + newTable(name:string){ + return new Table(name); + } +} + +export class Attribute { + name : string; + constructor(name: string){ + this.name = name; + } + serialize(){ + return this.toString(); + } + toString(){ + return this.name; + } +} + + +export class Table{ + dbLangTableName : string; + dbLangTableAttributes:{ [key: string]: Attribute; } = {}; + [key:string]: Attribute | any + constructor(name: string){ + this.dbLangTableName = name; + } + serialize(){ + return this.toString(); + } + toString(){ + return this.dbLangTableName; + } + addAttribute(name:string,ops:{ + unique?: boolean, + A_I?: boolean, + default?: primaryData, + notNull?: boolean + primaryKey?: boolean, + foreginKey?: { + link: Attribute, + onDelete?: onAction, + onUpdate?: onAction + } + },noErrorOnNameConflict = false){ + let attr = new Attribute(name); + this.dbLangTableAttributes[name] = attr; + if(["serialize", "toString","addAttribute","dbLangTableName","dbLangTableAttributes"].includes(name)){ + if(!noErrorOnNameConflict) throw new Error("You cannot name Attribute like Methode of this Table!"); + }else{ + this[name] = attr; + } + return attr; + } +} + +export * from './funcs'; +export {onAction}; \ No newline at end of file diff --git a/src/dbStructure.ts b/src/dbStructure.ts new file mode 100644 index 0000000..186b08e --- /dev/null +++ b/src/dbStructure.ts @@ -0,0 +1,39 @@ +import { Attribute } from "./db"; +import { Handler } from "./defaultHandler"; +import { allModifierInput, primaryData, serializeReturn } from "./types"; + + + +export abstract class Modifier { + t : string; + a : Array; + constructor(type: string, args: (allModifierInput)[]) { + this.t = type; + this.a = args; + } + serialize(handler = Handler):serializeReturn { + return handler.modifiers[this.t as keyof typeof handler.modifiers](this.a); + } +} + +export class BooleanModifier extends Modifier{} +export class NumberModifier extends Modifier{} +export class StringModifier extends Modifier{} + +export class Aggregation{ + t : string; + a : Attribute; + constructor(type: string, args: Attribute) { + this.t = type; + this.a = args; + } + serialize(handler = Handler):serializeReturn{ + return handler.aggregations[this.t as keyof typeof handler.aggregations](this.a); + } +} +export class Joins{ + + serialize(handler = Handler):serializeReturn { + return ["",[]]; + } +} \ No newline at end of file diff --git a/src/defaultHandler.ts b/src/defaultHandler.ts new file mode 100644 index 0000000..9299829 --- /dev/null +++ b/src/defaultHandler.ts @@ -0,0 +1,76 @@ +import { Attribute } from "./db" +import { Aggregation, Modifier } from "./dbStructure" +import { selectQuery } from "./query" +import { allModifierInput, primaryData, serializeReturn } from "./types" + +export class Handler { + static querys = { + select: (q: selectQuery): serializeReturn => { + let args: primaryData[] = []; + let w = joinArg(", ", this)(q.attr); + args.push(...w[1]); + let sql = `select ${w[0]} from ${q.from == null ? 'DUAL' : q.from.serialize(this)}`; + if (q.whereD) { + let whereS = q.whereD.serialize(this); + args.push(...whereS[1]); + sql += " where "+whereS[0]; + } + if (q.groupByD.length>0) { + let groupByS = joinArg(",", this)(q.groupByD); + args.push(...groupByS[1]); + sql += " group by "+groupByS[0]; + if (q.havingD) { + let havingS = q.havingD.serialize(this); + args.push(...havingS[1]); + sql += " having "+havingS[0]; + } + } + + return [sql, args]; + } + } + + static aggregations = { + count: (a: Attribute): serializeReturn => ["count(" + a + ")", []], + sum: (a: Attribute): serializeReturn => ["sum(" + a + ")", []], + avg: (a: Attribute): serializeReturn => ["avg(" + a + ")", []], + min: (a: Attribute): serializeReturn => ["min(" + a + ")", []], + max: (a: Attribute): serializeReturn => ["max(" + a + ")", []], + } + + static modifiers = { + and: joinArg("and", this), + or: joinArg("or", this), + eq: joinArg("=", this), + plus: joinArg("+", this), + minus: joinArg("-", this), + not: (a: allModifierInput[]):serializeReturn => { + let e = a[0]; + if (e instanceof Attribute) return ["not (" + e + ")", []]; + if (e instanceof Modifier || e instanceof selectQuery || e instanceof Aggregation) { + let [sqli, argsi] = e.serialize(this); + return ["not (" + sqli + ")", argsi] + } + return ["not (?)", [e]]; + } + } + +}; + +function joinArg(type: string, s: any) { + return (a: (allModifierInput)[]): serializeReturn => { + let args: primaryData[] = []; + let sql = "(" + a.map(d => { + if (d instanceof Attribute) return d; + if (d instanceof Modifier || d instanceof selectQuery || d instanceof Aggregation) { + let [sqli, argsi] = d.serialize(s); + args.push(...(argsi.flat(Infinity))); + return sqli; + } + args.push(d); + return "?"; + }).join(" " + type + " ") + ")"; + return [sql, args] + + } +} \ No newline at end of file diff --git a/src/funcs.ts b/src/funcs.ts new file mode 100644 index 0000000..700f537 --- /dev/null +++ b/src/funcs.ts @@ -0,0 +1,22 @@ +import { Attribute } from "./db"; +import { Aggregation, BooleanModifier, NumberModifier } from "./dbStructure"; +import { selectQuery } from "./query"; +import { allModifierInput, selectElements, selectFromElements } from "./types"; + +//modifiers +export const and = (...args: (BooleanModifier)[]) => new BooleanModifier("and",args); +export const or = (...args: (BooleanModifier)[]) => new BooleanModifier("or",args); +export const eq = (...args: (allModifierInput)[]) => new BooleanModifier("eq",args); +export const plus = (...args: (allModifierInput)[]) => new NumberModifier("plus",args); +export const minus = (...args: (allModifierInput)[]) => new NumberModifier("minus",args); + +//aggregations + +export const count = (a:Attribute) => new Aggregation("count",a); +export const sum = (a:Attribute) => new Aggregation("sum",a); +export const avg = (a:Attribute) => new Aggregation("avg",a); +export const min = (a:Attribute) => new Aggregation("min",a); +export const max = (a:Attribute) => new Aggregation("max",a); + +//query +export const select = (args: selectElements[],from: selectFromElements) => new selectQuery(args,from); diff --git a/src/query.ts b/src/query.ts new file mode 100644 index 0000000..10e6252 --- /dev/null +++ b/src/query.ts @@ -0,0 +1,47 @@ +import { Attribute, DB } from "./db"; +import { BooleanModifier, Modifier } from "./dbStructure"; +import { Handler } from "./defaultHandler"; +import { primaryData, selectElements, selectFromElements, serializeReturn } from "./types"; + + +export class Query { + sql : string; + values : primaryData[]; + constructor(sql: string, values: primaryData[]) { + this.sql = sql; + this.values = values; + } +} + +export class selectQuery { + attr:selectElements[] = []; + from:selectFromElements; + constructor(a: selectElements[], from:selectFromElements) { + this.attr.push(...a.flat(Infinity)); + this.from = from ? from : null; + } + whereD:BooleanModifier | null = null; + where(m: BooleanModifier) { + this.whereD = m; + return this; + } + groupByD: Attribute[] = []; + groupBy(a: Attribute[]) { + this.groupByD = a; + return this; + } + havingD:Modifier | null = null; + having(m: Modifier) { + this.havingD = m; + return this; + } + + serialize(handler = Handler) : serializeReturn { + return handler.querys.select(this); + } + + query(db: DB) { + const s = this.serialize(db.getHandler()); + return new Query(s[0],s[1]); + } +} \ No newline at end of file diff --git a/src/types.ts b/src/types.ts new file mode 100644 index 0000000..0ecfc4e --- /dev/null +++ b/src/types.ts @@ -0,0 +1,19 @@ +import { Attribute, Table } from "./db"; +import { Aggregation, Joins, Modifier } from "./dbStructure"; +import { selectQuery } from "./query"; + +export type primaryData = string | number | boolean | null; +export type allModifierInput = primaryData | Modifier | selectQuery | Attribute | Aggregation; + +export type selectElements = primaryData | Attribute | Aggregation | selectQuery +export type selectFromElements = Table | Joins | null; + +export type serializeReturn = [string, primaryData[]]; + + +export enum onAction{ + cascade, + noAction, + setNull, + setDefault +} \ No newline at end of file diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 0000000..408ec89 --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,21 @@ +{ + "compilerOptions": { + "module": "CommonJS", + "lib": ["es6"], + "types": ["node"], + "declaration": true, + "outDir": "dist", + "strict": true, + "esModuleInterop": true, + "moduleResolution": "node", + //"sourceMap": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "src/db.ts" + ], + "exclude": [ + "node_modules", + "dist" + ] +} \ No newline at end of file