data-api-client 1.3.1 → 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/README.md +389 -136
- package/dist/client.d.ts +3 -0
- package/dist/client.d.ts.map +1 -0
- package/dist/client.js +69 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +3 -0
- package/dist/params.d.ts +19 -0
- package/dist/params.d.ts.map +1 -0
- package/dist/params.js +125 -0
- package/dist/query.d.ts +5 -0
- package/dist/query.d.ts.map +1 -0
- package/dist/query.js +38 -0
- package/dist/results.d.ts +12 -0
- package/dist/results.d.ts.map +1 -0
- package/dist/results.js +93 -0
- package/dist/transaction.d.ts +4 -0
- package/dist/transaction.d.ts.map +1 -0
- package/dist/transaction.js +57 -0
- package/dist/types.d.ts +90 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +2 -0
- package/dist/utils.d.ts +19 -0
- package/dist/utils.d.ts.map +1 -0
- package/dist/utils.js +150 -0
- package/package.json +37 -9
- package/index.js +0 -625
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": "
|
|
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
|
-
"
|
|
8
|
-
"
|
|
9
|
-
"
|
|
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,18 +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": {
|
|
28
|
-
"aws-sdk": "^
|
|
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",
|
|
29
47
|
"eslint": "^8.12.0",
|
|
30
48
|
"eslint-config-prettier": "^8.5.0",
|
|
31
|
-
"jest": "^27.5.1",
|
|
32
49
|
"prettier": "^2.6.2",
|
|
33
|
-
"
|
|
50
|
+
"tsx": "^4.20.6",
|
|
51
|
+
"typescript": "^5.9.3",
|
|
52
|
+
"vitest": "^3.2.4"
|
|
34
53
|
},
|
|
35
54
|
"dependencies": {
|
|
55
|
+
"pg-escape": "^0.2.0",
|
|
36
56
|
"sqlstring": "^2.3.2"
|
|
37
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
|
+
},
|
|
38
66
|
"files": [
|
|
39
|
-
"
|
|
67
|
+
"dist"
|
|
40
68
|
]
|
|
41
69
|
}
|