diff --git a/src/concerns/has-attributes.js b/src/concerns/has-attributes.js index 06d775e..fd614a1 100644 --- a/src/concerns/has-attributes.js +++ b/src/concerns/has-attributes.js @@ -138,9 +138,8 @@ const HasAttributes = (Model) => { const attrMethod = getAttrMethod(key); if (typeof this[attrMethod] === 'function') { const attribute = this[attrMethod](); - const callback = attribute.set || ((value) => { - this.attributes[key] = value; - }); + + const callback = attribute.set || (value => value); this.attributes = { ...this.attributes, diff --git a/test/index.test.js b/test/index.test.js index 8bfd0fe..4c6d7cd 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -244,9 +244,6 @@ describe('Model', () => { const data = testModel.setVisible(['firstName', 'lastName']).setHidden(['lastName']).toData(); expect(data).toEqual({firstName: 'Joe'}); - - const knex = require('knex')({ client: 'mysql' }); - console.log(knex.client.JoinClause); }); it('append virtual attribute', () => { @@ -394,6 +391,12 @@ describe('Integration test', () => { newUniqueId() { return crypto.randomUUID(); } + + attributeName() { + return Attribute.make({ + get: (value, attributes) => attributes.name || attributes.id, + }) + } } class Post extends Base { diff --git a/types/browser/index.d.ts b/types/browser/index.d.ts index ef97dae..340a6b8 100644 --- a/types/browser/index.d.ts +++ b/types/browser/index.d.ts @@ -84,7 +84,7 @@ declare module 'sutando' { relationLoaded(relation: string): boolean; makeVisible(attributes: string | string[]): this; makeHidden(attributes: string | string[]): this; - newCollection(models?: any[]): Collection; + newCollection(this: T, models?: any[]): Collection; usesTimestamps(): boolean; updateTimestamps(): this; getCreatedAtColumn(): string; diff --git a/types/index.d.ts b/types/index.d.ts index cdead29..278387f 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -411,7 +411,7 @@ declare module 'sutando' { relationLoaded(relation: string): boolean; makeVisible(attributes: string | string[]): this; makeHidden(attributes: string | string[]): this; - newCollection(models?: any[]): Collection; + newCollection(this: T, models?: any[]): Collection; load(relations: WithRelationType): Promise; load(...relations: WithRelationType[]): Promise; loadAggregate(relations: WithRelationType, column: any, callback?: any): Promise; @@ -762,14 +762,16 @@ declare module 'sutando' { export class RelationNotFoundError extends Error {} export class InvalidArgumentError extends Error {} - export function HasUniqueIds Model>(Base: T): T & { - new (...args: ConstructorParameters): { + interface IHasUniqueIds { + new (...args: any[]): { useUniqueIds: boolean; }; - }; + } + + export function HasUniqueIds Model>(Base: T): T & IHasUniqueIds; - export interface ISoftDeletes Model> { - new (...args: ConstructorParameters): { + export interface ISoftDeletes { + new (...args: any[]): { forceDeleting: boolean; initializeSoftDeletes(): void; forceDelete(): Promise; @@ -777,7 +779,7 @@ declare module 'sutando' { performDeleteOnModel(options?: {}): Promise; exists: boolean; runSoftDelete(options?: {}): Promise; - restore(options?: any): Promise; + restore(options?: any): Promise; restoreQuietly(): any; trashed(): boolean; isForceDeleting(): boolean; @@ -792,7 +794,7 @@ declare module 'sutando' { forceDeleted(callback: Function): void; } - export function SoftDeletes Model>(Base: T): T & ISoftDeletes; + export function SoftDeletes Model>(Base: T): T & ISoftDeletes; export class Migration { protected connection: AnyQueryBuilder; @@ -803,7 +805,6 @@ declare module 'sutando' { export function getRelationMethod(name: string): string; export function getScopeMethod(name: string): string; - export const compose: MixinFunction; export function make(modelClass: new (...args: any[]) => T, attributes: Record): T; export function make(modelClass: new (...args: any[]) => T, attributes: Record[]): Collection; @@ -817,6 +818,7 @@ declare module 'sutando' { export interface MixinFunction { (modelClass: MC, ...plugins: Plugin[]): MC; } + export const compose: MixinFunction; export function migrateRun( config: any,