data-api-client 2.0.0-beta.0 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/utils.js ADDED
@@ -0,0 +1,150 @@
1
+ 'use strict';
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.formatFromTimeStamp = exports.formatToTimeStamp = exports.getTypeHint = exports.isJSONString = exports.isUUIDString = exports.isDecimalString = exports.isTimeString = exports.isDateString = exports.isDate = exports.getType = exports.getSqlParams = exports.flatten = exports.pick = exports.omit = exports.parseSQL = exports.error = exports.supportedTypes = void 0;
4
+ exports.supportedTypes = [
5
+ 'arrayValue',
6
+ 'blobValue',
7
+ 'booleanValue',
8
+ 'doubleValue',
9
+ 'isNull',
10
+ 'longValue',
11
+ 'stringValue',
12
+ 'structValue'
13
+ ];
14
+ const error = (...err) => {
15
+ throw Error(...err);
16
+ };
17
+ exports.error = error;
18
+ const parseSQL = (args) => typeof args[0] === 'string'
19
+ ? args[0]
20
+ : typeof args[0] === 'object' && typeof args[0].sql === 'string'
21
+ ? args[0].sql
22
+ : (0, exports.error)(`No 'sql' statement provided.`);
23
+ exports.parseSQL = parseSQL;
24
+ const omit = (obj, values) => Object.keys(obj).reduce((acc, x) => (values.includes(x) ? acc : Object.assign(acc, { [x]: obj[x] })), {});
25
+ exports.omit = omit;
26
+ const pick = (obj, values) => Object.keys(obj).reduce((acc, x) => (values.includes(x) ? Object.assign(acc, { [x]: obj[x] }) : acc), {});
27
+ exports.pick = pick;
28
+ const flatten = (arr) => arr.reduce((acc, x) => acc.concat(x), []);
29
+ exports.flatten = flatten;
30
+ const getSqlParams = (sql) => {
31
+ const matches = [];
32
+ const regex = /:{1,2}\w+/g;
33
+ let match;
34
+ while ((match = regex.exec(sql)) !== null) {
35
+ const matchedText = match[0];
36
+ const matchIndex = match.index;
37
+ if (matchedText.startsWith('::')) {
38
+ const beforeMatch = sql.substring(Math.max(0, matchIndex - 20), matchIndex);
39
+ const hasParamBefore = /:\w+$/.test(beforeMatch);
40
+ if (!hasParamBefore) {
41
+ matches.push(matchedText);
42
+ }
43
+ }
44
+ else {
45
+ matches.push(matchedText);
46
+ }
47
+ }
48
+ return matches
49
+ .map((p) => {
50
+ return p.startsWith('::')
51
+ ? { type: 'n_id', label: p.substr(2) }
52
+ : { type: 'n_ph', label: p.substr(1) };
53
+ })
54
+ .reduce((acc, x) => {
55
+ return Object.assign(acc, {
56
+ [x.label]: {
57
+ type: x.type
58
+ }
59
+ });
60
+ }, {});
61
+ };
62
+ exports.getSqlParams = getSqlParams;
63
+ const getType = (val) => typeof val === 'string'
64
+ ? 'stringValue'
65
+ : typeof val === 'boolean'
66
+ ? 'booleanValue'
67
+ : typeof val === 'number' && parseInt(val.toString()) === val
68
+ ? 'longValue'
69
+ : typeof val === 'number' && parseFloat(val.toString()) === val
70
+ ? 'doubleValue'
71
+ : val === null
72
+ ? 'isNull'
73
+ : (0, exports.isDate)(val)
74
+ ? 'stringValue'
75
+ : Buffer.isBuffer(val)
76
+ ? 'blobValue'
77
+ :
78
+ typeof val === 'object' &&
79
+ val !== null &&
80
+ Object.keys(val).length === 1 &&
81
+ exports.supportedTypes.includes(Object.keys(val)[0])
82
+ ? null
83
+ : undefined;
84
+ exports.getType = getType;
85
+ const isDate = (val) => val instanceof Date;
86
+ exports.isDate = isDate;
87
+ const isDateString = (val) => /^\d{4}-\d{2}-\d{2}$/.test(val);
88
+ exports.isDateString = isDateString;
89
+ const isTimeString = (val) => /^\d{2}:\d{2}:\d{2}(\.\d{1,3})?$/.test(val);
90
+ exports.isTimeString = isTimeString;
91
+ const isDecimalString = (val) => /^-?\d+\.\d+$/.test(val);
92
+ exports.isDecimalString = isDecimalString;
93
+ const isUUIDString = (val) => /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i.test(val);
94
+ exports.isUUIDString = isUUIDString;
95
+ const isJSONString = (val) => {
96
+ if (typeof val !== 'string')
97
+ return false;
98
+ const trimmed = val.trim();
99
+ if (!trimmed.startsWith('{') && !trimmed.startsWith('['))
100
+ return false;
101
+ try {
102
+ const parsed = JSON.parse(val);
103
+ return typeof parsed === 'object' && parsed !== null;
104
+ }
105
+ catch (_a) {
106
+ return false;
107
+ }
108
+ };
109
+ exports.isJSONString = isJSONString;
110
+ const getTypeHint = (val) => {
111
+ if ((0, exports.isDate)(val)) {
112
+ return 'TIMESTAMP';
113
+ }
114
+ if (typeof val === 'string') {
115
+ if ((0, exports.isUUIDString)(val)) {
116
+ return 'UUID';
117
+ }
118
+ if ((0, exports.isDateString)(val)) {
119
+ return 'DATE';
120
+ }
121
+ if ((0, exports.isTimeString)(val)) {
122
+ return 'TIME';
123
+ }
124
+ if ((0, exports.isJSONString)(val)) {
125
+ return 'JSON';
126
+ }
127
+ if ((0, exports.isDecimalString)(val)) {
128
+ return 'DECIMAL';
129
+ }
130
+ }
131
+ return undefined;
132
+ };
133
+ exports.getTypeHint = getTypeHint;
134
+ const formatToTimeStamp = (date, treatAsLocalDate) => {
135
+ const pad = (val, num = 2) => '0'.repeat(num - (val + '').length) + val;
136
+ const year = treatAsLocalDate ? date.getFullYear() : date.getUTCFullYear();
137
+ const month = (treatAsLocalDate ? date.getMonth() : date.getUTCMonth()) + 1;
138
+ const day = treatAsLocalDate ? date.getDate() : date.getUTCDate();
139
+ const hours = treatAsLocalDate ? date.getHours() : date.getUTCHours();
140
+ const minutes = treatAsLocalDate ? date.getMinutes() : date.getUTCMinutes();
141
+ const seconds = treatAsLocalDate ? date.getSeconds() : date.getUTCSeconds();
142
+ const ms = treatAsLocalDate ? date.getMilliseconds() : date.getUTCMilliseconds();
143
+ const fraction = ms <= 0 ? '' : `.${pad(ms, 3)}`;
144
+ return `${year}-${pad(month)}-${pad(day)} ${pad(hours)}:${pad(minutes)}:${pad(seconds)}${fraction}`;
145
+ };
146
+ exports.formatToTimeStamp = formatToTimeStamp;
147
+ const formatFromTimeStamp = (value, treatAsLocalDate) => !treatAsLocalDate && /^\d{4}-\d{2}-\d{2}(\s\d{2}:\d{2}:\d{2}(\.\d+)?)?$/.test(value)
148
+ ? new Date(value + 'Z')
149
+ : new Date(value);
150
+ exports.formatFromTimeStamp = formatFromTimeStamp;
package/package.json CHANGED
@@ -1,12 +1,21 @@
1
1
  {
2
2
  "name": "data-api-client",
3
- "version": "2.0.0-beta.0",
3
+ "version": "2.0.0",
4
4
  "description": "A lightweight wrapper that simplifies working with the Amazon Aurora Serverless Data API",
5
- "main": "index.js",
5
+ "main": "dist/index.js",
6
+ "types": "dist/index.d.ts",
6
7
  "scripts": {
7
- "test": "jest",
8
- "test-ci": "eslint . && jest",
9
- "lint": "eslint ."
8
+ "build": "tsc",
9
+ "build:watch": "tsc --watch",
10
+ "prebuild": "rm -rf dist",
11
+ "test": "npm run build && vitest run src/",
12
+ "test:unit": "npm run build && vitest run src/",
13
+ "test:int": "npm run build && vitest run integration-tests/",
14
+ "test:int:mysql": "npm run build && vitest run integration-tests/mysql.int.test.ts",
15
+ "test:int:postgres": "npm run build && vitest run integration-tests/postgres.int.test.ts",
16
+ "test-ci": "npm run build && eslint src && vitest run src/",
17
+ "lint": "eslint src",
18
+ "prepublishOnly": "npm run build"
10
19
  },
11
20
  "repository": {
12
21
  "type": "git",
@@ -24,17 +33,37 @@
24
33
  "url": "https://github.com/jeremydaly/data-api-client/issues"
25
34
  },
26
35
  "homepage": "https://github.com/jeremydaly/data-api-client#readme",
36
+ "imports": {
37
+ "#fixtures/*": "./fixtures/*"
38
+ },
27
39
  "devDependencies": {
40
+ "@aws-sdk/client-rds-data": "^3.712.0",
41
+ "@types/node": "^24.6.2",
42
+ "@types/pg-escape": "^0.2.3",
43
+ "@types/sqlstring": "^2.3.2",
44
+ "@typescript-eslint/eslint-plugin": "^8.45.0",
45
+ "@typescript-eslint/parser": "^8.45.0",
46
+ "@vitest/ui": "^3.2.4",
28
47
  "eslint": "^8.12.0",
29
48
  "eslint-config-prettier": "^8.5.0",
30
- "jest": "^27.5.1",
31
- "rewire": "^6.0.0"
49
+ "prettier": "^2.6.2",
50
+ "tsx": "^4.20.6",
51
+ "typescript": "^5.9.3",
52
+ "vitest": "^3.2.4"
32
53
  },
33
54
  "dependencies": {
34
- "@aws-sdk/client-rds-data": "^3.58.0",
55
+ "pg-escape": "^0.2.0",
35
56
  "sqlstring": "^2.3.2"
36
57
  },
58
+ "peerDependencies": {
59
+ "@aws-sdk/client-rds-data": "^3.0.0"
60
+ },
61
+ "peerDependenciesMeta": {
62
+ "@aws-sdk/client-rds-data": {
63
+ "optional": true
64
+ }
65
+ },
37
66
  "files": [
38
- "index.js"
67
+ "dist"
39
68
  ]
40
69
  }