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
5 changes: 2 additions & 3 deletions src/concerns/has-attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
9 changes: 6 additions & 3 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion types/browser/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<this>;
newCollection<T extends Model>(this: T, models?: any[]): Collection<T>;
usesTimestamps(): boolean;
updateTimestamps(): this;
getCreatedAtColumn(): string;
Expand Down
20 changes: 11 additions & 9 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<this>;
newCollection<T extends Model>(this: T, models?: any[]): Collection<T>;
load(relations: WithRelationType): Promise<this>;
load(...relations: WithRelationType[]): Promise<this>;
loadAggregate(relations: WithRelationType, column: any, callback?: any): Promise<this>;
Expand Down Expand Up @@ -762,22 +762,24 @@ declare module 'sutando' {
export class RelationNotFoundError extends Error {}
export class InvalidArgumentError extends Error {}

export function HasUniqueIds<T extends new (...args: any[]) => Model>(Base: T): T & {
new (...args: ConstructorParameters<T>): {
interface IHasUniqueIds {
new (...args: any[]): {
useUniqueIds: boolean;
};
};
}

export function HasUniqueIds<T extends new (...args: any[]) => Model>(Base: T): T & IHasUniqueIds;

export interface ISoftDeletes<T extends new (...args: any[]) => Model> {
new (...args: ConstructorParameters<T>): {
export interface ISoftDeletes {
new (...args: any[]): {
forceDeleting: boolean;
initializeSoftDeletes(): void;
forceDelete(): Promise<boolean>;
forceDeleteQuietly(): any;
performDeleteOnModel(options?: {}): Promise<any>;
exists: boolean;
runSoftDelete(options?: {}): Promise<void>;
restore(options?: any): Promise<any>;
restore(options?: any): Promise<boolean>;
restoreQuietly(): any;
trashed(): boolean;
isForceDeleting(): boolean;
Expand All @@ -792,7 +794,7 @@ declare module 'sutando' {
forceDeleted(callback: Function): void;
}

export function SoftDeletes<T extends new (...args: any[]) => Model>(Base: T): T & ISoftDeletes<T>;
export function SoftDeletes<T extends new (...args: any[]) => Model>(Base: T): T & ISoftDeletes;

export class Migration {
protected connection: AnyQueryBuilder;
Expand All @@ -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<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>;
Expand All @@ -817,6 +818,7 @@ declare module 'sutando' {
export interface MixinFunction {
<MC extends AnyModelConstructor>(modelClass: MC, ...plugins: Plugin[]): MC;
}
export const compose: MixinFunction;

export function migrateRun(
config: any,
Expand Down