Skip to content

RDBC-909 - Returning class instances in client causes issues in Next.js — responses should be plain objects #481

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: v7.0
Choose a base branch
from
Open
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
19 changes: 19 additions & 0 deletions src/Documents/Conventions/DocumentConventions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,25 @@ export class DocumentConventions {
return this._sharding;
}

private _returnPlainJsObjects: boolean = false;

/**
* Gets whether all values returned from API will be plain objects (not class instances).
* This is useful for environments like Next.js that require serializable results.
*/
public get returnPlainJsObjects(): boolean {
return this._returnPlainJsObjects;
}

/**
* Sets whether all values returned from API will be plain objects (not class instances).
* This is useful for environments like Next.js that require serializable results.
*/
public set returnPlainJsObjects(value: boolean) {
this._assertNotFrozen();
this._returnPlainJsObjects = value;
}

public constructor() {
this._readBalanceBehavior = "None";
this._identityPartsSeparator = "/";
Expand Down
27 changes: 26 additions & 1 deletion src/Documents/Session/AbstractDocumentQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ import {
} from "./IVectorFieldFactory.js";
import { VectorEmbeddingFieldFactory } from "../Queries/VectorSearch/VectorEmbeddingFieldFactory.js";
import { Field } from "../../Types/index.js";
import { JsonSerializer } from "../../Mapping/Json/Serializer.js";

/**
* A query against a Raven index
Expand Down Expand Up @@ -2193,7 +2194,13 @@ export abstract class AbstractDocumentQuery<T extends object, TSelf extends Abst

public async all(): Promise<T[]> {
const results = await this.iterator();
return [...results];
const array = [...results];

if (this.conventions.returnPlainJsObjects) {
return array.map(item => JsonSerializer.toPlainObject(item))
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can item be undefined? if yes we will get SyntaxError.

}

return array;
}

public async getQueryResult(): Promise<QueryResult> {
Expand All @@ -2208,11 +2215,20 @@ export abstract class AbstractDocumentQuery<T extends object, TSelf extends Abst
throwError("InvalidOperationException", "Expected at least one result.");
}

if (this.conventions.returnPlainJsObjects) {
return JsonSerializer.toPlainObject(entries[0]);
}

return entries[0];
}

public async firstOrNull(): Promise<T | null> {
const entries = await this._executeQueryOperation(1);

if (this.conventions.returnPlainJsObjects) {
return JsonSerializer.toPlainObject(entries[0]) || null;
}

return entries[0] || null;
}

Expand All @@ -2224,6 +2240,10 @@ export abstract class AbstractDocumentQuery<T extends object, TSelf extends Abst
`Expected single result, but got ${ entries.length ? "more than that" : 0 }.`);
}

if (this.conventions.returnPlainJsObjects) {
return JsonSerializer.toPlainObject(entries[0]);
}

return entries[0];
}

Expand All @@ -2233,6 +2253,11 @@ export abstract class AbstractDocumentQuery<T extends object, TSelf extends Abst
throwError("InvalidOperationException",
`Expected single result, but got more than that.`);
}

if (this.conventions.returnPlainJsObjects) {
return entries.length === 1 ? JsonSerializer.toPlainObject(entries[0]) : null;
}

return entries.length === 1 ? entries[0] : null;
}

Expand Down
4 changes: 4 additions & 0 deletions src/Mapping/Json/Serializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,8 @@ export class JsonSerializer {
public static getDefaultForCommandPayload(): JsonSerializer {
return new JsonSerializer(camelCaseReviver, pascalCaseReplacer);
}

public static toPlainObject<T>(obj: T): T {
return JSON.parse(JSON.stringify(obj));
}
}