Skip to content

Commit ae2bd71

Browse files
authored
Major serdes changes (#98)
* revamp of how codecs work * cleanup of unused serdes stuff * serdes code deduplication * ctx.afterMap * keytransformer overhaul * colorized check scirpt * internal serdes refactors + tests * made table codec properly recursive * serdes code deduplication/cleanup * work on tests * better detection of unit tests being skipped
1 parent 614ed4f commit ae2bd71

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

56 files changed

+3030
-813
lines changed

etc/astra-db-ts.api.md

Lines changed: 201 additions & 131 deletions
Large diffs are not rendered by default.

scripts/build.sh

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,9 @@ node scripts/utils/build-version-file.js > src/version.ts
2020

2121
# Transpiles the project
2222
if [ "$light" = true ]; then
23-
npx tsc --project tsconfig.production.json -d false --noCheck
23+
npx tsc --project tsconfig.production.json -d false --noCheck || exit 10
2424
else
25-
npx tsc --project tsconfig.production.json
26-
fi
27-
28-
if [ ! -d ./dist ]; then
29-
exit 10
25+
npx tsc --project tsconfig.production.json || exit 10
3026
fi
3127

3228
# Replaces alias paths with relative paths (e.g. `@/src/version` -> `../../src/version`)

scripts/check.sh

Lines changed: 58 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -46,63 +46,99 @@ fi
4646

4747
failed=false
4848

49+
print_green() {
50+
echo "$(tput setaf 2)$(tput bold)$1$(tput sgr0)"
51+
}
52+
53+
print_green_with_status() {
54+
if [ $failed = true ]; then
55+
print_green "- $1 :/"
56+
else
57+
print_green "- $1 :)"
58+
fi
59+
}
60+
61+
print_error() {
62+
echo "$(tput setaf 1)$1$(tput sgr0)"
63+
}
64+
4965
for check_type in $check_types; do
5066
case $check_type in
5167
"tc")
52-
echo "Running type-checker..."
53-
npx tsc --noEmit || failed=true
68+
print_green_with_status "Running type-checker..."
69+
70+
if ! npx tsc --noEmit > /dev/null; then
71+
npx tsc --noEmit
72+
failed=true
73+
fi
5474
;;
5575
"lint")
56-
echo "Running linter..."
57-
npm run lint -- --no-warn-ignored || failed=true
76+
print_green_with_status "Running linter..."
77+
78+
if ! npm run lint -- --no-warn-ignored > /dev/null; then
79+
npm run lint -- --no-warn-ignored
80+
failed=true
81+
fi
5882
;;
5983
"licensing")
60-
echo "Checking for missing licensing headers..."
84+
print_green_with_status "Checking for missing licensing headers..."
6185
offenders=$(find tests/ src/ -type f -exec grep -L "^// Copyright DataStax, Inc." {} +)
6286

6387
if [ -n "$offenders" ]; then
64-
echo "The following files are missing licensing headers:"
65-
echo "$offenders"
88+
print_error "The following files are missing licensing headers:"
89+
print_error "$offenders"
6690
failed=true
6791
fi
6892
;;
6993
"lib-check")
70-
echo "Checking library compiles..."
94+
print_green_with_status "Ensuring library compiles with skipLibCheck: false..."
7195

7296
tmp_dir="tmp-lib-check"
7397
rm -rf "$tmp_dir" "$main_dir/dist"
7498

75-
(scripts/build.sh -no-report \
76-
&& mkdir "$tmp_dir" \
77-
&& cd "$tmp_dir" \
78-
&& npm init -y \
79-
&& npm install typescript "$main_dir" \
80-
&& echo "import '@datastax/astra-db-ts'" > src.ts \
81-
&& npx tsc --init --skipLibCheck false --typeRoots "./node_modules/**" --target es2020 \
82-
&& npx tsc) || failed=true
99+
scripts/build.sh -no-report > /dev/null
100+
101+
if [ ! $? ]; then
102+
print_error "Could not build library for lib-check phase"
103+
failed=true
104+
else
105+
mkdir "$tmp_dir" \
106+
&& cd "$tmp_dir" \
107+
&& npm init -y > /dev/null \
108+
&& npm install typescript "$main_dir" > /dev/null \
109+
&& echo "import '@datastax/astra-db-ts'" > src.ts \
110+
&& npx tsc --init --skipLibCheck false --typeRoots "./node_modules/**" --target es2020 > /dev/null
111+
112+
if [ -f tsconfig.json ]; then
113+
npx tsc || failed=true
114+
else
115+
print_error "Could not set up library for lib-check phase"
116+
failed=true
117+
fi
118+
fi
83119

84120
cd "$main_dir" && rm -rf "$tmp_dir"
85121
;;
86122
"test-ext")
87-
echo "Checking test file extensions..."
123+
print_green_with_status "Checking for test files that do not end in '.test.ts'..."
88124
offenders=$(find tests/unit tests/integration -type f -not -name "*.test.ts")
89125

90126
if [ -n "$offenders" ]; then
91-
echo "The following test files do not end in '.test.ts':"
92-
echo "$offenders"
127+
print_error "The following test files do not end in '.test.ts':"
128+
print_error "$offenders"
93129
failed=true
94130
fi
95131
;;
96132
"*")
97-
echo "Invalid check type '$check_type'"
133+
print_error "Invalid check type '$check_type'"
98134
exit 1
99135
;;
100136
esac
101137
done
102138

103139
if [ "$failed" = true ]; then
104-
echo "Checks failed"
140+
print_error "$(tput bold)Checks failed :("
105141
exit 1
106142
else
107-
echo "Checks passed"
143+
print_green "Checks passed :)"
108144
fi

scripts/test.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,5 +268,7 @@ if [ -n "$CLIENT_RUN_VECTORIZE_TESTS" ] && [ "$test_type" != 'code' ]; then
268268
fi
269269
fi
270270

271+
export CLIENT_DYNAMIC_JS_ENV_CHECK=1
272+
271273
# Run it
272274
eval "$cmd_to_run"

src/client/data-api-client.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ import { Timeouts } from '@/src/lib/api/timeouts';
4747
* @public
4848
*/
4949
export const DataAPIClientEventEmitterBase = (() => {
50+
/* istanbul ignore next: exceptional case that can't be manually reproduced */
5051
try {
5152
return (require('events') as { EventEmitter: (new () => TypedEmitter<DataAPIClientEventMap>) }).EventEmitter;
5253
} catch (_) {
@@ -55,6 +56,8 @@ export const DataAPIClientEventEmitterBase = (() => {
5556
})();
5657

5758
/**
59+
* ##### Overview
60+
*
5861
* The main entrypoint into working with the Data API. It sits at the top of the
5962
* [conceptual hierarchy](https://github.com/datastax/astra-db-ts/tree/signature-cleanup?tab=readme-ov-file#abstraction-diagram)
6063
* of the SDK.
@@ -87,7 +90,7 @@ export const DataAPIClientEventEmitterBase = (() => {
8790
* const admin1 = client1.admin();
8891
* const admin2 = client1.admin({ adminToken: '<stronger_token>' });
8992
*
90-
* console.log(await coll.insertOne({ name: 'RATATATA' }));
93+
* console.log(await coll.insertOne({ name: 'John Joe' }));
9194
* console.log(await admin1.listDatabases());
9295
* ```
9396
*

src/documents/collections/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ export {
2424
} from './ser-des/ser-des';
2525

2626
export {
27-
CollCodecSerDesFns,
2827
CollCodecClass,
2928
CollCodecs,
3029
CollCodec,

src/documents/collections/ser-des/big-nums.ts

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export type CollNumRep =
2828
/**
2929
* @public
3030
*/
31-
export type GetCollNumRepFn = (path: readonly string[]) => CollNumRep;
31+
export type GetCollNumRepFn = (path: readonly (string | number)[]) => CollNumRep;
3232

3333
/**
3434
* @public
@@ -45,7 +45,7 @@ interface NumRepTree {
4545
export const collNumRepFnFromCfg = (cfg: CollNumRepCfg): GetCollNumRepFn => {
4646
const tree = buildNumRepTree(cfg);
4747

48-
return (path: readonly string[]) => {
48+
return (path) => {
4949
return findMatchingPath(path, tree) ?? 'number';
5050
};
5151
};
@@ -71,7 +71,7 @@ const buildNumRepTree = (cfg: CollNumRepCfg): NumRepTree => {
7171
return result;
7272
};
7373

74-
const findMatchingPath = (path: readonly string[], tree: NumRepTree | undefined): CollNumRep | undefined => {
74+
const findMatchingPath = (path: readonly (string | number)[], tree: NumRepTree | undefined): CollNumRep | undefined => {
7575
let rep: CollNumRep | undefined = undefined;
7676

7777
for (let i = 0; tree && i <= path.length; i++) {
@@ -96,7 +96,7 @@ const findMatchingPath = (path: readonly string[], tree: NumRepTree | undefined)
9696
* @public
9797
*/
9898
export class NumCoercionError extends Error {
99-
public readonly path: string[];
99+
public readonly path: (string | number)[];
100100
public readonly value: number | BigNumber;
101101
public readonly from: 'number' | 'bignumber';
102102
public readonly to: CollNumRep;
@@ -106,7 +106,7 @@ export class NumCoercionError extends Error {
106106
*
107107
* @internal
108108
*/
109-
public constructor(path: string[], value: number | BigNumber, from: 'number' | 'bignumber', to: CollNumRep) {
109+
public constructor(path: (string | number)[], value: number | BigNumber, from: 'number' | 'bignumber', to: CollNumRep) {
110110
super(`Failed to coerce value from ${from} to ${to} at path: ${path.join('.')}`);
111111
this.path = path;
112112
this.value = value;
@@ -124,19 +124,19 @@ export const coerceBigNumber = (value: BigNumber, ctx: CollDesCtx): readonly [0
124124
throw new NumCoercionError(ctx.path, value, 'bignumber', 'number');
125125
}
126126

127-
return ctx.next(asNum);
127+
return ctx.recurse(asNum);
128128
}
129129
case 'bigint': {
130130
if (!value.isInteger()) {
131131
throw new NumCoercionError(ctx.path, value, 'bignumber', 'bigint');
132132
}
133-
return ctx.next(BigInt(value.toFixed(0)));
133+
return ctx.recurse(BigInt(value.toFixed(0)));
134134
}
135135
case 'bignumber':
136-
return ctx.next(value);
136+
return ctx.recurse(value);
137137
case 'string':
138138
case 'number_or_string':
139-
return ctx.next(value.toString());
139+
return ctx.recurse(value.toString());
140140
}
141141
};
142142

@@ -146,14 +146,14 @@ export const coerceNumber = (value: number, ctx: CollDesCtx): readonly [0 | 1 |
146146
if (!Number.isInteger(value)) {
147147
throw new NumCoercionError(ctx.path, value, 'number', 'bigint');
148148
}
149-
return ctx.next(BigInt(value));
149+
return ctx.recurse(BigInt(value));
150150
}
151151
case 'bignumber':
152-
return ctx.next(BigNumber(value));
152+
return ctx.recurse(BigNumber(value));
153153
case 'string':
154-
return ctx.next(value.toString());
154+
return ctx.recurse(value.toString());
155155
case 'number':
156156
case 'number_or_string':
157-
return ctx.next(value);
157+
return ctx.recurse(value);
158158
}
159159
};

0 commit comments

Comments
 (0)