Skip to content

Commit 4b33197

Browse files
committed
Added strongly typed entries for better type checking
1 parent 5d986dc commit 4b33197

File tree

10 files changed

+40
-6
lines changed

10 files changed

+40
-6
lines changed

src/packages/pongo/src/main/client.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { getDbClient } from './dbClient';
2-
import type { PongoClient, PongoDb } from './typing';
2+
import type { PongoClient, PongoDb } from './typing/operations';
33

44
export const pongoClient = (connectionString: string): PongoClient => {
55
const dbClient = getDbClient(connectionString);

src/packages/pongo/src/main/dbClient.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { postgresClient } from '../postgres';
2-
import type { PongoCollection } from './typing';
2+
import type { PongoCollection } from './typing/operations';
33

44
export interface DbClient {
55
connect(): Promise<void>;

src/packages/pongo/src/main/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
export * from './client';
22
export * from './dbClient';
3-
export * from './typing';
3+
export * from './typing/operations';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
type Entry<T> = {
2+
[K in keyof Required<T>]: [K, Required<T>[K]];
3+
}[keyof Required<T>];
4+
5+
type IterableEntry<T> = Entry<T> & {
6+
[Symbol.iterator](): Iterator<Entry<T>>;
7+
};
8+
9+
export const entries = <T extends object>(obj: T): IterableEntry<T>[] =>
10+
Object.entries(obj).map(([key, value]) => [key as keyof T, value]);
11+
12+
export type NonPartial<T> = { [K in keyof Required<T>]: T[K] };
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export * from './entries';
2+
export * from './operations';

src/packages/pongo/src/postgres/filter/index.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { PongoFilter } from '../../main';
2+
import { entries } from '../../main/typing';
23
import { Operators, handleOperator, hasOperators } from './queryOperators';
34

45
const AND = 'AND';
@@ -18,7 +19,7 @@ const constructComplexFilterQuery = (
1819
): string => {
1920
const isEquality = !hasOperators(value);
2021

21-
return Object.entries(value)
22+
return entries(value)
2223
.map(
2324
([nestedKey, val]) =>
2425
isEquality

src/packages/pongo/src/postgres/filter/queryOperators.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import format from 'pg-format';
2+
import { entries } from '../../main/typing';
23

34
export const Operators = {
45
$eq: '$eq',
@@ -67,7 +68,7 @@ export const handleOperator = (
6768
(value as unknown[]).map((v) => format('%L', v)).join(', '),
6869
);
6970
case '$elemMatch': {
70-
const subQuery = Object.entries(value as Record<string, unknown>)
71+
const subQuery = entries(value as Record<string, unknown>)
7172
.map(([subKey, subValue]) =>
7273
format(`@."%s" == %s`, subKey, JSON.stringify(subValue)),
7374
)

src/packages/pongo/src/postgres/update/index.ts

+18-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,24 @@
11
import type { $inc, $push, $set, $unset, PongoUpdate } from '../../main';
2+
import { entries } from '../../main/typing';
23
import { sql, type SQL } from '../sql';
34

4-
export const buildUpdateQuery = <T>(update: PongoUpdate<T>): SQL => {
5+
export const buildUpdateQuery = <T>(update: PongoUpdate<T>): SQL =>
6+
entries(update).reduce((currentUpdateQuery, [op, value]) => {
7+
switch (op) {
8+
case '$set':
9+
return buildSetQuery(value, currentUpdateQuery);
10+
case '$unset':
11+
return buildUnsetQuery(value, currentUpdateQuery);
12+
case '$inc':
13+
return buildIncQuery(value, currentUpdateQuery);
14+
case '$push':
15+
return buildPushQuery(value, currentUpdateQuery);
16+
default:
17+
return currentUpdateQuery;
18+
}
19+
}, sql('data'));
20+
21+
export const buildUpdateQueryOld = <T>(update: PongoUpdate<T>): SQL => {
522
let updateQuery = sql('data');
623

724
if ('$set' in update && update.$set)

src/tsconfig.shared.json

+1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
// "alwaysStrict": true, /* Parse in strict mode and emit "use strict" for each source file. */
4040

4141
/* Additional Checks */
42+
"exactOptionalPropertyTypes": true,
4243
"noUnusedLocals": false /* Report errors on unused locals. */,
4344
"noUnusedParameters": false /* Report errors on unused parameters. */,
4445
"noImplicitReturns": true /* Report error when not all code paths in function return a value. */,

0 commit comments

Comments
 (0)