Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -168,12 +168,18 @@ Of course, the tools/platforms mentioned above offer capabilities that `@objectw

`npm run test` or `npm run test:cov`

| File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s |
| ------------ | ------- | -------- | ------- | ------- | ------------------------------------ |
| All files | 93 | 78.68 | 95.23 | 92.8 | |
| core.ts | 90.82 | 73.17 | 100 | 90.82 | 18,42,53,106,128,141,163,206,247,251 |
| singleton.ts | 100 | 100 | 75 | 100 | |
| util.ts | 100 | 89.47 | 100 | 100 | 23-24 |
| File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s |
| ------------ | ------- | -------- | ------- | ------- | ----------------- |
| All files | 100 | 94.11 | 95.23 | 100 | |
| core.ts | 100 | 95.83 | 100 | 100 | 28,66 |
| error.ts | 100 | 100 | 100 | 100 | |
| singleton.ts | 100 | 100 | 75 | 100 | |
| util.ts | 100 | 89.47 | 100 | 100 | 24-25 |

Test Suites: 5 passed, 5 total
Tests: 64 passed, 64 total
Snapshots: 0 total
Time: 1.56 s, estimated 2 s

# Customization

Expand Down
1 change: 1 addition & 0 deletions index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export { JoinData } from "./src/core";
export { joinData, SingletonJoinData } from "./src/singleton";
export * from "./src/type";
export * from "./src/error";
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@objectwow/join",
"version": "0.3.1",
"version": "0.3.2",
"license": "MIT",
"description": "Join objects with functionality similar to MongoDB's $lookup",
"publishConfig": {
Expand Down
54 changes: 30 additions & 24 deletions src/core.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { JoinError } from "./error";
import {
FromParam,
GenerateAsValueParam,
Expand All @@ -15,7 +16,7 @@ export class JoinData {
protected validateFields(arr: { key: string; value: any }[], metadata?: any) {
for (const item of arr) {
if (!item.value) {
throw new Error(`Missing ${item.key} value`);
throw new JoinError(`Missing ${item.key} value`);
}
}
}
Expand All @@ -38,10 +39,6 @@ export class JoinData {
}

if (typeOf(local) === Types.Object) {
if (!path) {
return local;
}

const parsePath = this.parseFieldPath(path);
if (!parsePath.newPath) {
return local[parsePath.path];
Expand All @@ -50,7 +47,7 @@ export class JoinData {
return this.getFieldValue(local[parsePath.path], parsePath.newPath);
}

return local;
throw new JoinError("local type not supported");
}

private async generateAsValue(param: GenerateAsValueParam) {
Expand Down Expand Up @@ -94,9 +91,13 @@ export class JoinData {
result[key] = fromFieldValue;
}
});
} else {
// function
} else if (
typeOf(asMap) === Types.Function ||
typeOf(asMap) === Types.AsyncFunction
) {
result = await (asMap as Function)(fromValue, local, metadata);
} else {
throw new JoinError("asMap type not supported");
}
} else {
result = fromValue;
Expand Down Expand Up @@ -130,16 +131,16 @@ export class JoinData {

if (typeOf(localValue) === Types.Array) {
if (!as) {
throw new Error("Not found as when local value is array");
throw new JoinError("Not found as when local value is array");
}

const parseAsField = this.parseFieldPath(as);
if (typeOf(local[parseAsField.path]) === Types.Array) {
const parseLocalField = this.parseFieldPath(localField);

if (parseLocalField.path !== parseAsField.path) {
throw new Error(
`First path of localField and as not matching, ${parseLocalField.path} !== ${parseAsField.path}`
throw new JoinError(
`When local[${parseAsField.path}] is an array, first path of 'localField' and 'as' need matching, ${parseLocalField.path} !== ${parseAsField.path}`
);
}

Expand All @@ -160,7 +161,7 @@ export class JoinData {
}

if (typeOf(local[as]) === Types.Object) {
throw new Error(
throw new JoinError(
`Field ${as} existed but is object. It must be an array when local value is array`
);
}
Expand All @@ -183,11 +184,19 @@ export class JoinData {

local[parseAsField.path].push(asValue);
}
return;
}

// Not array
if (as) {
const parseAsField = this.parseFieldPath(as);

if (typeOf(local[parseAsField.path]) === Types.Array) {
throw new JoinError(
`Field ${as} existed but is array. It must be an object when local value is object`
);
}

const asValue = await this.generateAsValue({
local,
localValue,
Expand All @@ -202,12 +211,6 @@ export class JoinData {
return;
}

if (typeOf(local[parseAsField.path]) === Types.Array) {
throw new Error(
`Field ${as} existed but is array. It must be an object when local value is object`
);
}

if (typeOf(local[parseAsField.path]) === Types.Object) {
Object.assign(local[parseAsField.path], asValue);
return;
Expand Down Expand Up @@ -243,23 +246,22 @@ export class JoinData {
localFieldValues: string[],
metadata?: any
): Promise<any[]> {
if (typeOf(from) === Types.Object) {
throw new Error("from must be an array of objects");
}

if (typeOf(from) === Types.Array) {
return from as object[];
}

if (typeOf(from) === Types.Function) {
if (
typeOf(from) === Types.Function ||
typeOf(from) === Types.AsyncFunction
) {
const result = await (from as Function)(localFieldValues, metadata);

if (typeOf(result) === Types.Array) {
return result as object[];
}
}

throw new Error("from must be an array of objects");
throw new JoinError("from must be an array of objects");
}

protected generateResult(
Expand All @@ -282,6 +284,10 @@ export class JoinData {
let { local } = param;
const joinFailedValues: Primitive[] = [];

if (as && as === localField) {
throw new JoinError("as and localField cannot be the same");
}

this.validateFields(
[
{ key: "local", value: local },
Expand Down
1 change: 1 addition & 0 deletions src/error.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export class JoinError extends Error {}
1 change: 1 addition & 0 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export enum Types {
Boolean = "Boolean",
RegExp = "RegExp",
Function = "Function",
AsyncFunction = "AsyncFunction",
}

export const typeOf = (value: any): Types =>
Expand Down
Loading