From 459f3bc3023ee35eca916941aaf5adb3ef574ae0 Mon Sep 17 00:00:00 2001 From: GhomKrosmonaute Date: Wed, 23 Oct 2024 13:27:13 +0200 Subject: [PATCH] updated the bg database template: added numeric type parsers --- package-lock.json | 100 ++++++++++++++++++++++++++++++++++++++++++++++ package.json | 1 + templates/pg | 16 +++++++- 3 files changed, 116 insertions(+), 1 deletion(-) diff --git a/package-lock.json b/package-lock.json index 6bba625..12ae789 100644 --- a/package-lock.json +++ b/package-lock.json @@ -12,6 +12,7 @@ "@ghom/handler": "^3.1.0", "@ghom/logger": "^2.0.2", "@ghom/orm": "^1.7.2", + "@types/pg": "*", "boxen": "^8.0.1", "dayjs": "^1.11.13", "dirname-filename-esm": "^1.1.2", @@ -57,6 +58,7 @@ "npm": ">=10.x.x" }, "optionalDependencies": { + "@types/pg": "^8.11.10", "del": "^6.1.1", "fancy-log": "^2.0.0", "git-commit-info": "^2.0.2", @@ -1280,6 +1282,80 @@ "undici-types": "~6.19.2" } }, + "node_modules/@types/pg": { + "version": "8.11.10", + "resolved": "https://registry.npmjs.org/@types/pg/-/pg-8.11.10.tgz", + "integrity": "sha512-LczQUW4dbOQzsH2RQ5qoeJ6qJPdrcM/DcMLoqWQkMLMsq83J5lAX3LXjdkWdpscFy67JSOWDnh7Ny/sPFykmkg==", + "license": "MIT", + "optional": true, + "dependencies": { + "@types/node": "*", + "pg-protocol": "*", + "pg-types": "^4.0.1" + } + }, + "node_modules/@types/pg/node_modules/pg-types": { + "version": "4.0.2", + "resolved": "https://registry.npmjs.org/pg-types/-/pg-types-4.0.2.tgz", + "integrity": "sha512-cRL3JpS3lKMGsKaWndugWQoLOCoP+Cic8oseVcbr0qhPzYD5DWXK+RZ9LY9wxRf7RQia4SCwQlXk0q6FCPrVng==", + "license": "MIT", + "optional": true, + "dependencies": { + "pg-int8": "1.0.1", + "pg-numeric": "1.0.2", + "postgres-array": "~3.0.1", + "postgres-bytea": "~3.0.0", + "postgres-date": "~2.1.0", + "postgres-interval": "^3.0.0", + "postgres-range": "^1.1.1" + }, + "engines": { + "node": ">=10" + } + }, + "node_modules/@types/pg/node_modules/postgres-array": { + "version": "3.0.2", + "resolved": "https://registry.npmjs.org/postgres-array/-/postgres-array-3.0.2.tgz", + "integrity": "sha512-6faShkdFugNQCLwucjPcY5ARoW1SlbnrZjmGl0IrrqewpvxvhSLHimCVzqeuULCbG0fQv7Dtk1yDbG3xv7Veog==", + "license": "MIT", + "optional": true, + "engines": { + "node": ">=12" + } + }, + "node_modules/@types/pg/node_modules/postgres-bytea": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/postgres-bytea/-/postgres-bytea-3.0.0.tgz", + "integrity": "sha512-CNd4jim9RFPkObHSjVHlVrxoVQXz7quwNFpz7RY1okNNme49+sVyiTvTRobiLV548Hx/hb1BG+iE7h9493WzFw==", + "license": "MIT", + "optional": true, + "dependencies": { + "obuf": "~1.1.2" + }, + "engines": { + "node": ">= 6" + } + }, + "node_modules/@types/pg/node_modules/postgres-date": { + "version": "2.1.0", + "resolved": "https://registry.npmjs.org/postgres-date/-/postgres-date-2.1.0.tgz", + "integrity": "sha512-K7Juri8gtgXVcDfZttFKVmhglp7epKb1K4pgrkLxehjqkrgPhfG6OO8LHLkfaqkbpjNRnra018XwAr1yQFWGcA==", + "license": "MIT", + "optional": true, + "engines": { + "node": ">=12" + } + }, + "node_modules/@types/pg/node_modules/postgres-interval": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/postgres-interval/-/postgres-interval-3.0.0.tgz", + "integrity": "sha512-BSNDnbyZCXSxgA+1f5UU2GmwhoI0aU5yMxRGO8CdFEcY2BQF9xm/7MqKnYoM1nJDk8nONNWDk9WeSmePFhQdlw==", + "license": "MIT", + "optional": true, + "engines": { + "node": ">=12" + } + }, "node_modules/@types/prettier": { "version": "2.7.3", "resolved": "https://registry.npmjs.org/@types/prettier/-/prettier-2.7.3.tgz", @@ -6408,6 +6484,13 @@ "url": "https://github.com/sponsors/ljharb" } }, + "node_modules/obuf": { + "version": "1.1.2", + "resolved": "https://registry.npmjs.org/obuf/-/obuf-1.1.2.tgz", + "integrity": "sha512-PX1wu0AmAdPqOL1mWhqmlOd8kOIZQwGZw6rh7uby9fTc5lhaOWFLX3I6R1hrF9k3zUY40e6igsLGkDXK92LJNg==", + "license": "MIT", + "optional": true + }, "node_modules/once": { "version": "1.4.0", "resolved": "https://registry.npmjs.org/once/-/once-1.4.0.tgz", @@ -6654,6 +6737,16 @@ "node": ">=4.0.0" } }, + "node_modules/pg-numeric": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/pg-numeric/-/pg-numeric-1.0.2.tgz", + "integrity": "sha512-BM/Thnrw5jm2kKLE5uJkXqqExRUY/toLHda65XgFTBTFYZyopbKjBe29Ii3RbkvlsMoFwD+tHeGaCjjv0gHlyw==", + "license": "ISC", + "optional": true, + "engines": { + "node": ">=4" + } + }, "node_modules/pg-pool": { "version": "3.6.1", "resolved": "https://registry.npmjs.org/pg-pool/-/pg-pool-3.6.1.tgz", @@ -6772,6 +6865,13 @@ "node": ">=0.10.0" } }, + "node_modules/postgres-range": { + "version": "1.1.4", + "resolved": "https://registry.npmjs.org/postgres-range/-/postgres-range-1.1.4.tgz", + "integrity": "sha512-i/hbxIE9803Alj/6ytL7UHQxRvZkI9O4Sy+J3HGc4F4oo/2eQAjTSNJ0bfxyse3bH0nuVesCk+3IRLaMtG3H6w==", + "license": "MIT", + "optional": true + }, "node_modules/prebuild-install": { "version": "7.1.1", "resolved": "https://registry.npmjs.org/prebuild-install/-/prebuild-install-7.1.1.tgz", diff --git a/package.json b/package.json index c12929b..d4779e2 100644 --- a/package.json +++ b/package.json @@ -92,6 +92,7 @@ "git": ">=2.x.x" }, "optionalDependencies": { + "@types/pg": "^8.11.10", "del": "^6.1.1", "fancy-log": "^2.0.0", "git-commit-info": "^2.0.2", diff --git a/templates/pg b/templates/pg index c6fe5ac..be07797 100644 --- a/templates/pg +++ b/templates/pg @@ -1,10 +1,13 @@ // system file, please don't modify it +import * as pg from "pg" import * as orm from "@ghom/orm" import * as logger from "./logger.ts" import env from "./env.ts" import path from "path" +setTypeParsers(pg) + const client = new orm.ORM({ location: path.join(process.cwd(), "dist", "tables"), database: { @@ -24,4 +27,15 @@ const client = new orm.ORM({ export * from "@ghom/orm" -export default client \ No newline at end of file +export default client + +function setTypeParsers({ types }: typeof pg) { + const int = (value: string) => parseInt(value) + const float = (value: string) => parseFloat(value) + + types.setTypeParser(types.builtins.INT2, int) + types.setTypeParser(types.builtins.INT4, int) + types.setTypeParser(types.builtins.INT8, int) + types.setTypeParser(types.builtins.FLOAT4, float) + types.setTypeParser(types.builtins.FLOAT8, float) +} \ No newline at end of file