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
33 changes: 28 additions & 5 deletions src/browser/concerns/has-relations.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,34 @@ const HasRelations = (Model) => {
}

guessBelongsToRelation() {
let e = new Error();
let frame = e.stack.split("\n")[2];
// let lineNumber = frame.split(":").reverse()[1];
let functionName = frame.split(" ")[5];
return getRelationName(functionName);
const e = new Error();
const stack = e.stack || e.stackTrace;

if (!stack) {
return getRelationName('unknown');
}

const frames = stack.split('\n');
const frame = frames[2] || frames[1] || frames[0];

let functionName = 'anonymous';

if (frame.includes('@')) {
// Safari: functionName@file:line:column
functionName = frame.split('@')[0].trim();
} else if (frame.includes('at ')) {
// Chrome: at functionName (file:line:column)
const match = frame.match(/at\s+([^(]+)\s*\(/);
functionName = match ? match[1].trim() : 'anonymous';

if (functionName.includes('.')) {
functionName = functionName.split('.').pop();
}
}

functionName = functionName.replace(/^</, '').replace(/>$/, '').trim();

return getRelationName(functionName || 'anonymous');
}

joiningTable(related, instance = null) {
Expand Down
12 changes: 8 additions & 4 deletions types/browser/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,10 @@ declare module 'sutando' {
fill(attributes: any): this;
setAppends(appends: string[]): this;
append(key: string | string[]): this;
getRelation<T extends Model>(relation: string): T | Collection<T> | null | undefined;
getRelation<
T extends Model,
IsCollection extends boolean = false
>(relation: string): IsCollection extends true ? Collection<T> | undefined : T | null | undefined;
setRelation<T extends Model>(relation: string, value: T | Collection<T> | null): this;
unsetRelation(relation: string): this;
relationLoaded(relation: string): boolean;
Expand Down Expand Up @@ -161,9 +164,10 @@ declare module 'sutando' {
export class RelationNotFoundError extends Error {}
export class InvalidArgumentError extends Error {}

export function make<T extends new (...args: any[]) => Model>(modelClass: T, attributes: Record<string, any>[]): Collection<T>;
export function makeCollection<T extends new (...args: any[]) => Model>(modelClass: T, attributes: Record<string, any>[]): Collection<T>;
export function makePaginator<T extends new (...args: any[]) => Model>(modelClass: T, attributes: Record<string, any>[]): Paginator<T>;
export function make<T extends Model>(modelClass: new (...args: any[]) => T, attributes: Record<string, any>): T;
export function make<T extends Model>(modelClass: new (...args: any[]) => T, attributes: Record<string, any>[]): Collection<T>;
export function makeCollection<T extends Model>(modelClass: new (...args: any[]) => T, attributes: Record<string, any>[]): Collection<T>;
export function makePaginator<T extends Model>(modelClass: new (...args: any[]) => T, attributes: Record<string, any>[]): Paginator<T>;

export function HasUniqueIds<T extends new (...args: any[]) => Model>(Base: T): T & {
new (...args: ConstructorParameters<T>): {
Expand Down
12 changes: 8 additions & 4 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,10 @@ declare module 'sutando' {
fill(attributes: any): this;
setAppends(appends: string[]): this;
append(key: string | string[]): this;
getRelation<T extends Model>(relation: string): T | Collection<T> | null | undefined;
getRelation<
T extends Model,
IsCollection extends boolean = false
>(relation: string): IsCollection extends true ? Collection<T> | undefined : T | null | undefined;
setRelation<T extends Model>(relation: string, value: T | Collection<T> | null): this;
unsetRelation(relation: string): this;
relationLoaded(relation: string): boolean;
Expand Down Expand Up @@ -802,9 +805,10 @@ declare module 'sutando' {
export function getScopeMethod(name: string): string;
export const compose: MixinFunction;

export function make<T extends new (...args: any[]) => Model>(modelClass: T, attributes: Record<string, any>[]): Collection<T>;
export function makeCollection<T extends new (...args: any[]) => Model>(modelClass: T, attributes: Record<string, any>[]): Collection<T>;
export function makePaginator<T extends new (...args: any[]) => Model>(modelClass: T, attributes: Record<string, any>[]): Paginator<T>;
export function make<T extends Model>(modelClass: new (...args: any[]) => T, attributes: Record<string, any>): T;
export function make<T extends Model>(modelClass: new (...args: any[]) => T, attributes: Record<string, any>[]): Collection<T>;
export function makeCollection<T extends Model>(modelClass: new (...args: any[]) => T, attributes: Record<string, any>[]): Collection<T>;
export function makePaginator<T extends Model>(modelClass: new (...args: any[]) => T, attributes: Record<string, any>[]): Paginator<T>;

export interface Plugin {
<M extends typeof Model>(modelClass: M): M;
Expand Down