Skip to content
module@warp-drive/legacy/model @legacy

Function attr() ​

ts
function attr(): DataDecorator;
function attr<T>(type: TypeFromInstance<T>): DataDecorator;
function attr(type: string): DataDecorator;
function attr(options: AttrOptions): DataDecorator;
function attr<T>(type: TypeFromInstance<T>, options?: OptionsFromInstance<T>): DataDecorator;
function attr(type: string, options?: AttrOptions<
  | object
  | unknown[]
  | PrimitiveValue> & object): DataDecorator;
function attr(
   target: object, 
   key: string | symbol, 
   desc?: PropertyDescriptor
): void;

Call Signature ​

ts
function attr(): DataDecorator;

Defined in: warp-drive-packages/legacy/src/model/-private/attr.ts:295

attr defines an attribute on a Model. By default, attributes are passed through as-is, however you can specify an optional type to have the value automatically transformed. WarpDrive ships with four basic transform types: string, number, boolean and date. You can define your own transforms by subclassing Transform.

Note that you cannot use attr to define an attribute of id.

attr takes an optional hash as a second parameter, currently supported options are:

  • defaultValue: Pass a string or a function to be called to set the attribute to a default value if and only if the key is absent from the payload response.

Example

app/models/user.js
js
import { Model, attr } from '@warp-drive/legacy/model';

export default class UserModel extends Model {
  @attr('string') username;
  @attr('string') email;
  @attr('boolean', { defaultValue: false }) verified;
}

Default value can also be a function. This is useful it you want to return a new object for each attribute.

app/models/user.js
js
import { Model, attr } from '@warp-drive/legacy/model';

export default class UserModel extends Model {
  @attr('string') username;
  @attr('string') email;

  @attr({
    defaultValue() {
      return {};
    }
  })
  settings;
}

The options hash is passed as second argument to a transforms' serialize and deserialize method. This allows to configure a transformation and adapt the corresponding value, based on the config:

app/models/post.js
js
import { Model, attr } from '@warp-drive/legacy/model';

export default class PostModel extends Model {
  @attr('text', {
    uppercase: true
  })
  text;
}
app/transforms/text.js
js
export default class TextTransform {
  serialize(value, options) {
    if (options.uppercase) {
      return value.toUpperCase();
    }

    return value;
  }

  deserialize(value) {
    return value;
  }

  static create() {
    return new this();
  }
}

Returns ​

DataDecorator

Call Signature ​

ts
function attr<T>(type: TypeFromInstance<T>): DataDecorator;

Defined in: warp-drive-packages/legacy/src/model/-private/attr.ts:296

attr defines an attribute on a Model. By default, attributes are passed through as-is, however you can specify an optional type to have the value automatically transformed. WarpDrive ships with four basic transform types: string, number, boolean and date. You can define your own transforms by subclassing Transform.

Note that you cannot use attr to define an attribute of id.

attr takes an optional hash as a second parameter, currently supported options are:

  • defaultValue: Pass a string or a function to be called to set the attribute to a default value if and only if the key is absent from the payload response.

Example

app/models/user.js
js
import { Model, attr } from '@warp-drive/legacy/model';

export default class UserModel extends Model {
  @attr('string') username;
  @attr('string') email;
  @attr('boolean', { defaultValue: false }) verified;
}

Default value can also be a function. This is useful it you want to return a new object for each attribute.

app/models/user.js
js
import { Model, attr } from '@warp-drive/legacy/model';

export default class UserModel extends Model {
  @attr('string') username;
  @attr('string') email;

  @attr({
    defaultValue() {
      return {};
    }
  })
  settings;
}

The options hash is passed as second argument to a transforms' serialize and deserialize method. This allows to configure a transformation and adapt the corresponding value, based on the config:

app/models/post.js
js
import { Model, attr } from '@warp-drive/legacy/model';

export default class PostModel extends Model {
  @attr('text', {
    uppercase: true
  })
  text;
}
app/transforms/text.js
js
export default class TextTransform {
  serialize(value, options) {
    if (options.uppercase) {
      return value.toUpperCase();
    }

    return value;
  }

  deserialize(value) {
    return value;
  }

  static create() {
    return new this();
  }
}

Type Parameters ​

T ​

T

Parameters ​

type ​

TypeFromInstance<T>

the attribute type

Returns ​

DataDecorator

Call Signature ​

ts
function attr(type: string): DataDecorator;

Defined in: warp-drive-packages/legacy/src/model/-private/attr.ts:297

attr defines an attribute on a Model. By default, attributes are passed through as-is, however you can specify an optional type to have the value automatically transformed. WarpDrive ships with four basic transform types: string, number, boolean and date. You can define your own transforms by subclassing Transform.

Note that you cannot use attr to define an attribute of id.

attr takes an optional hash as a second parameter, currently supported options are:

  • defaultValue: Pass a string or a function to be called to set the attribute to a default value if and only if the key is absent from the payload response.

Example

app/models/user.js
js
import { Model, attr } from '@warp-drive/legacy/model';

export default class UserModel extends Model {
  @attr('string') username;
  @attr('string') email;
  @attr('boolean', { defaultValue: false }) verified;
}

Default value can also be a function. This is useful it you want to return a new object for each attribute.

app/models/user.js
js
import { Model, attr } from '@warp-drive/legacy/model';

export default class UserModel extends Model {
  @attr('string') username;
  @attr('string') email;

  @attr({
    defaultValue() {
      return {};
    }
  })
  settings;
}

The options hash is passed as second argument to a transforms' serialize and deserialize method. This allows to configure a transformation and adapt the corresponding value, based on the config:

app/models/post.js
js
import { Model, attr } from '@warp-drive/legacy/model';

export default class PostModel extends Model {
  @attr('text', {
    uppercase: true
  })
  text;
}
app/transforms/text.js
js
export default class TextTransform {
  serialize(value, options) {
    if (options.uppercase) {
      return value.toUpperCase();
    }

    return value;
  }

  deserialize(value) {
    return value;
  }

  static create() {
    return new this();
  }
}

Parameters ​

type ​

string

the attribute type

Returns ​

DataDecorator

Call Signature ​

ts
function attr(options: AttrOptions): DataDecorator;

Defined in: warp-drive-packages/legacy/src/model/-private/attr.ts:298

attr defines an attribute on a Model. By default, attributes are passed through as-is, however you can specify an optional type to have the value automatically transformed. WarpDrive ships with four basic transform types: string, number, boolean and date. You can define your own transforms by subclassing Transform.

Note that you cannot use attr to define an attribute of id.

attr takes an optional hash as a second parameter, currently supported options are:

  • defaultValue: Pass a string or a function to be called to set the attribute to a default value if and only if the key is absent from the payload response.

Example

app/models/user.js
js
import { Model, attr } from '@warp-drive/legacy/model';

export default class UserModel extends Model {
  @attr('string') username;
  @attr('string') email;
  @attr('boolean', { defaultValue: false }) verified;
}

Default value can also be a function. This is useful it you want to return a new object for each attribute.

app/models/user.js
js
import { Model, attr } from '@warp-drive/legacy/model';

export default class UserModel extends Model {
  @attr('string') username;
  @attr('string') email;

  @attr({
    defaultValue() {
      return {};
    }
  })
  settings;
}

The options hash is passed as second argument to a transforms' serialize and deserialize method. This allows to configure a transformation and adapt the corresponding value, based on the config:

app/models/post.js
js
import { Model, attr } from '@warp-drive/legacy/model';

export default class PostModel extends Model {
  @attr('text', {
    uppercase: true
  })
  text;
}
app/transforms/text.js
js
export default class TextTransform {
  serialize(value, options) {
    if (options.uppercase) {
      return value.toUpperCase();
    }

    return value;
  }

  deserialize(value) {
    return value;
  }

  static create() {
    return new this();
  }
}

Parameters ​

options ​

AttrOptions

a hash of options

Returns ​

DataDecorator

Call Signature ​

ts
function attr<T>(type: TypeFromInstance<T>, options?: OptionsFromInstance<T>): DataDecorator;

Defined in: warp-drive-packages/legacy/src/model/-private/attr.ts:299

attr defines an attribute on a Model. By default, attributes are passed through as-is, however you can specify an optional type to have the value automatically transformed. WarpDrive ships with four basic transform types: string, number, boolean and date. You can define your own transforms by subclassing Transform.

Note that you cannot use attr to define an attribute of id.

attr takes an optional hash as a second parameter, currently supported options are:

  • defaultValue: Pass a string or a function to be called to set the attribute to a default value if and only if the key is absent from the payload response.

Example

app/models/user.js
js
import { Model, attr } from '@warp-drive/legacy/model';

export default class UserModel extends Model {
  @attr('string') username;
  @attr('string') email;
  @attr('boolean', { defaultValue: false }) verified;
}

Default value can also be a function. This is useful it you want to return a new object for each attribute.

app/models/user.js
js
import { Model, attr } from '@warp-drive/legacy/model';

export default class UserModel extends Model {
  @attr('string') username;
  @attr('string') email;

  @attr({
    defaultValue() {
      return {};
    }
  })
  settings;
}

The options hash is passed as second argument to a transforms' serialize and deserialize method. This allows to configure a transformation and adapt the corresponding value, based on the config:

app/models/post.js
js
import { Model, attr } from '@warp-drive/legacy/model';

export default class PostModel extends Model {
  @attr('text', {
    uppercase: true
  })
  text;
}
app/transforms/text.js
js
export default class TextTransform {
  serialize(value, options) {
    if (options.uppercase) {
      return value.toUpperCase();
    }

    return value;
  }

  deserialize(value) {
    return value;
  }

  static create() {
    return new this();
  }
}

Type Parameters ​

T ​

T

Parameters ​

type ​

TypeFromInstance<T>

the attribute type

options? ​

OptionsFromInstance<T>

a hash of options

Returns ​

DataDecorator

Call Signature ​

ts
function attr(type: string, options?: AttrOptions<
  | object
  | unknown[]
  | PrimitiveValue> & object): DataDecorator;

Defined in: warp-drive-packages/legacy/src/model/-private/attr.ts:300

attr defines an attribute on a Model. By default, attributes are passed through as-is, however you can specify an optional type to have the value automatically transformed. WarpDrive ships with four basic transform types: string, number, boolean and date. You can define your own transforms by subclassing Transform.

Note that you cannot use attr to define an attribute of id.

attr takes an optional hash as a second parameter, currently supported options are:

  • defaultValue: Pass a string or a function to be called to set the attribute to a default value if and only if the key is absent from the payload response.

Example

app/models/user.js
js
import { Model, attr } from '@warp-drive/legacy/model';

export default class UserModel extends Model {
  @attr('string') username;
  @attr('string') email;
  @attr('boolean', { defaultValue: false }) verified;
}

Default value can also be a function. This is useful it you want to return a new object for each attribute.

app/models/user.js
js
import { Model, attr } from '@warp-drive/legacy/model';

export default class UserModel extends Model {
  @attr('string') username;
  @attr('string') email;

  @attr({
    defaultValue() {
      return {};
    }
  })
  settings;
}

The options hash is passed as second argument to a transforms' serialize and deserialize method. This allows to configure a transformation and adapt the corresponding value, based on the config:

app/models/post.js
js
import { Model, attr } from '@warp-drive/legacy/model';

export default class PostModel extends Model {
  @attr('text', {
    uppercase: true
  })
  text;
}
app/transforms/text.js
js
export default class TextTransform {
  serialize(value, options) {
    if (options.uppercase) {
      return value.toUpperCase();
    }

    return value;
  }

  deserialize(value) {
    return value;
  }

  static create() {
    return new this();
  }
}

Parameters ​

type ​

string

the attribute type

options? ​

AttrOptions< | object | unknown[] | PrimitiveValue> & object

a hash of options

Returns ​

DataDecorator

Call Signature ​

ts
function attr(
   target: object, 
   key: string | symbol, 
   desc?: PropertyDescriptor
): void;

Defined in: warp-drive-packages/legacy/src/model/-private/attr.ts:301

attr defines an attribute on a Model. By default, attributes are passed through as-is, however you can specify an optional type to have the value automatically transformed. WarpDrive ships with four basic transform types: string, number, boolean and date. You can define your own transforms by subclassing Transform.

Note that you cannot use attr to define an attribute of id.

attr takes an optional hash as a second parameter, currently supported options are:

  • defaultValue: Pass a string or a function to be called to set the attribute to a default value if and only if the key is absent from the payload response.

Example

app/models/user.js
js
import { Model, attr } from '@warp-drive/legacy/model';

export default class UserModel extends Model {
  @attr('string') username;
  @attr('string') email;
  @attr('boolean', { defaultValue: false }) verified;
}

Default value can also be a function. This is useful it you want to return a new object for each attribute.

app/models/user.js
js
import { Model, attr } from '@warp-drive/legacy/model';

export default class UserModel extends Model {
  @attr('string') username;
  @attr('string') email;

  @attr({
    defaultValue() {
      return {};
    }
  })
  settings;
}

The options hash is passed as second argument to a transforms' serialize and deserialize method. This allows to configure a transformation and adapt the corresponding value, based on the config:

app/models/post.js
js
import { Model, attr } from '@warp-drive/legacy/model';

export default class PostModel extends Model {
  @attr('text', {
    uppercase: true
  })
  text;
}
app/transforms/text.js
js
export default class TextTransform {
  serialize(value, options) {
    if (options.uppercase) {
      return value.toUpperCase();
    }

    return value;
  }

  deserialize(value) {
    return value;
  }

  static create() {
    return new this();
  }
}

Parameters ​

target ​

object

key ​

string | symbol

desc? ​

PropertyDescriptor

Returns ​

void

Released under the MIT License.