Skip to content

Commit

Permalink
Added strongly typed entries for better type checking
Browse files Browse the repository at this point in the history
  • Loading branch information
oskardudycz committed Jul 7, 2024
1 parent 5d986dc commit 4b33197
Show file tree
Hide file tree
Showing 10 changed files with 40 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/packages/pongo/src/main/client.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { getDbClient } from './dbClient';
import type { PongoClient, PongoDb } from './typing';
import type { PongoClient, PongoDb } from './typing/operations';

export const pongoClient = (connectionString: string): PongoClient => {
const dbClient = getDbClient(connectionString);
Expand Down
2 changes: 1 addition & 1 deletion src/packages/pongo/src/main/dbClient.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { postgresClient } from '../postgres';
import type { PongoCollection } from './typing';
import type { PongoCollection } from './typing/operations';

export interface DbClient {
connect(): Promise<void>;
Expand Down
2 changes: 1 addition & 1 deletion src/packages/pongo/src/main/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export * from './client';
export * from './dbClient';
export * from './typing';
export * from './typing/operations';
12 changes: 12 additions & 0 deletions src/packages/pongo/src/main/typing/entries.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
type Entry<T> = {
[K in keyof Required<T>]: [K, Required<T>[K]];
}[keyof Required<T>];

type IterableEntry<T> = Entry<T> & {
[Symbol.iterator](): Iterator<Entry<T>>;
};

export const entries = <T extends object>(obj: T): IterableEntry<T>[] =>
Object.entries(obj).map(([key, value]) => [key as keyof T, value]);

export type NonPartial<T> = { [K in keyof Required<T>]: T[K] };
2 changes: 2 additions & 0 deletions src/packages/pongo/src/main/typing/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './entries';
export * from './operations';
File renamed without changes.
3 changes: 2 additions & 1 deletion src/packages/pongo/src/postgres/filter/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { PongoFilter } from '../../main';
import { entries } from '../../main/typing';
import { Operators, handleOperator, hasOperators } from './queryOperators';

const AND = 'AND';
Expand All @@ -18,7 +19,7 @@ const constructComplexFilterQuery = (
): string => {
const isEquality = !hasOperators(value);

return Object.entries(value)
return entries(value)
.map(
([nestedKey, val]) =>
isEquality
Expand Down
3 changes: 2 additions & 1 deletion src/packages/pongo/src/postgres/filter/queryOperators.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import format from 'pg-format';
import { entries } from '../../main/typing';

export const Operators = {
$eq: '$eq',
Expand Down Expand Up @@ -67,7 +68,7 @@ export const handleOperator = (
(value as unknown[]).map((v) => format('%L', v)).join(', '),
);
case '$elemMatch': {
const subQuery = Object.entries(value as Record<string, unknown>)
const subQuery = entries(value as Record<string, unknown>)
.map(([subKey, subValue]) =>
format(`@."%s" == %s`, subKey, JSON.stringify(subValue)),
)
Expand Down
19 changes: 18 additions & 1 deletion src/packages/pongo/src/postgres/update/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,24 @@
import type { $inc, $push, $set, $unset, PongoUpdate } from '../../main';
import { entries } from '../../main/typing';
import { sql, type SQL } from '../sql';

export const buildUpdateQuery = <T>(update: PongoUpdate<T>): SQL => {
export const buildUpdateQuery = <T>(update: PongoUpdate<T>): SQL =>
entries(update).reduce((currentUpdateQuery, [op, value]) => {
switch (op) {
case '$set':
return buildSetQuery(value, currentUpdateQuery);
case '$unset':
return buildUnsetQuery(value, currentUpdateQuery);
case '$inc':
return buildIncQuery(value, currentUpdateQuery);
case '$push':
return buildPushQuery(value, currentUpdateQuery);
default:
return currentUpdateQuery;
}
}, sql('data'));

export const buildUpdateQueryOld = <T>(update: PongoUpdate<T>): SQL => {
let updateQuery = sql('data');

if ('$set' in update && update.$set)
Expand Down
1 change: 1 addition & 0 deletions src/tsconfig.shared.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
// "alwaysStrict": true, /* Parse in strict mode and emit "use strict" for each source file. */

/* Additional Checks */
"exactOptionalPropertyTypes": true,
"noUnusedLocals": false /* Report errors on unused locals. */,
"noUnusedParameters": false /* Report errors on unused parameters. */,
"noImplicitReturns": true /* Report error when not all code paths in function return a value. */,
Expand Down

0 comments on commit 4b33197

Please sign in to comment.