Skip to content

Documentation / @ember-data/model / migration-support / withDefaults

ts
function withDefaults(schema): LegacyResourceSchema;

Defined in: packages/model/src/migration-support.ts:307

A function which adds the necessary fields to a schema and marks it as being in LegacyMode. This is used to support the legacy features of @ember-data/model while migrating to WarpDrive.

Example:

ts
import { withDefaults, WithLegacy } from '@ember-data/model/migration-support';
import { Type } from '@warp-drive/core-types/symbols';
import type { HasMany } from '@ember-data/model';

export const UserSchema = withDefaults({
  type: 'user',
  fields: [
    { name: 'firstName', kind: 'attribute' },
    { name: 'lastName', kind: 'attribute' },
    { name: 'age', kind: 'attribute' },
    { name: 'friends',
      kind: 'hasMany',
      type: 'user',
      options: { inverse: 'friends', async: false }
    },
    { name: 'bestFriend',
      kind: 'belongsTo',
      type: 'user',
      options: { inverse: null, async: false }
    },
  ],
});

export type User = WithLegacy<{
  firstName: string;
  lastName: string;
  age: number;
  friends: HasMany<User>;
  bestFriend: User | null;
  [Type]: 'user';
}>

Using this function require registering the derivations it requires with the schema service.

ts
import { registerDerivations } from '@ember-data/model/migration-support';

registerDerivations(schema);

Parameters

schema

WithPartial<LegacyResourceSchema, "identity" | "legacy">

The schema to add legacy support to.

Returns

LegacyResourceSchema

The schema with legacy support added.

Released under the MIT License.