Skip to content

Commit bdc9b4d

Browse files
committed
Revert "chore: remove restriction on new field kinds being used with legacy mode"
This reverts commit 3fd81bf.
1 parent 11ca20f commit bdc9b4d

File tree

2 files changed

+159
-0
lines changed

2 files changed

+159
-0
lines changed

packages/schema-record/src/-private/record.ts

+20
Original file line numberDiff line numberDiff line change
@@ -316,18 +316,30 @@ export class SchemaRecord {
316316
return lastValue;
317317
}
318318
case 'field':
319+
assert(
320+
`SchemaRecord.${field.name} is not available in legacy mode because it has type '${field.kind}'`,
321+
!target[Legacy]
322+
);
319323
entangleSignal(signals, receiver, field.name);
320324
return computeField(schema, cache, target, identifier, field, propArray, IS_EDITABLE);
321325
case 'attribute':
322326
entangleSignal(signals, receiver, field.name);
323327
return computeAttribute(cache, identifier, prop as string, IS_EDITABLE);
324328
case 'resource':
329+
assert(
330+
`SchemaRecord.${field.name} is not available in legacy mode because it has type '${field.kind}'`,
331+
!target[Legacy]
332+
);
325333
entangleSignal(signals, receiver, field.name);
326334
return computeResource(store, cache, target, identifier, field, prop as string, IS_EDITABLE);
327335
case 'derived':
328336
return computeDerivation(schema, receiver as unknown as SchemaRecord, identifier, field, prop as string);
329337
case 'schema-array':
330338
case 'array':
339+
assert(
340+
`SchemaRecord.${field.name} is not available in legacy mode because it has type '${field.kind}'`,
341+
!target[Legacy]
342+
);
331343
entangleSignal(signals, receiver, field.name);
332344
return computeArray(
333345
store,
@@ -341,9 +353,17 @@ export class SchemaRecord {
341353
Mode[Legacy]
342354
);
343355
case 'object':
356+
assert(
357+
`SchemaRecord.${field.name} is not available in legacy mode because it has type '${field.kind}'`,
358+
!target[Legacy]
359+
);
344360
entangleSignal(signals, receiver, field.name);
345361
return computeObject(schema, cache, target, identifier, field, propArray, Mode[Editable], Mode[Legacy]);
346362
case 'schema-object':
363+
assert(
364+
`SchemaRecord.${field.name} is not available in legacy mode because it has type '${field.kind}'`,
365+
!target[Legacy]
366+
);
347367
entangleSignal(signals, receiver, field.name);
348368
// run transform, then use that value as the object to manage
349369
return computeSchemaObject(

tests/warp-drive__schema-record/tests/legacy/legacy-mode-activation-test.ts

+139
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,145 @@ module('Legacy Mode', function (hooks) {
178178
assert.strictEqual(record.constructor.name, 'Record<user>', 'it has a useful constructor name');
179179
});
180180

181+
test('records not in legacy mode can access values with type "field"', function (assert) {
182+
const store = this.owner.lookup('service:store') as Store;
183+
const { schema } = store;
184+
registerDerivations(schema);
185+
186+
schema.registerResource(
187+
withDefaults({
188+
type: 'user',
189+
fields: [
190+
{
191+
name: 'name',
192+
kind: 'field',
193+
},
194+
],
195+
})
196+
);
197+
198+
const record = store.push<User>({
199+
data: {
200+
type: 'user',
201+
id: '1',
202+
attributes: { name: 'Rey Pupatine' },
203+
},
204+
});
205+
206+
assert.false(record[Legacy], 'record is NOT in legacy mode');
207+
assert.strictEqual(record.$type, 'user', '$type is accessible');
208+
209+
assert.strictEqual(record.name, 'Rey Pupatine', 'can access name field');
210+
});
211+
212+
test('records in legacy mode cannot access values with type "field"', function (assert) {
213+
const store = this.owner.lookup('service:store') as Store;
214+
const { schema } = store;
215+
registerLegacyDerivations(schema);
216+
217+
schema.registerResource(
218+
withLegacyFields({
219+
type: 'user',
220+
fields: [
221+
{
222+
name: 'name',
223+
kind: 'field',
224+
},
225+
],
226+
})
227+
);
228+
229+
const record = store.push<User>({
230+
data: {
231+
type: 'user',
232+
id: '1',
233+
attributes: { name: 'Rey Pupatine' },
234+
},
235+
});
236+
237+
assert.true(record[Legacy], 'record is in legacy mode');
238+
239+
try {
240+
// eslint-disable-next-line @typescript-eslint/no-unused-expressions
241+
record.name;
242+
assert.ok(false, 'record.name should throw');
243+
} catch (e) {
244+
assert.strictEqual(
245+
(e as Error).message,
246+
"SchemaRecord.name is not available in legacy mode because it has type 'field'",
247+
'record.name throws'
248+
);
249+
}
250+
});
251+
252+
test('records in legacy mode cannot access resources', function (assert) {
253+
const store = this.owner.lookup('service:store') as Store;
254+
const { schema } = store;
255+
registerLegacyDerivations(schema);
256+
257+
schema.registerResource(
258+
withLegacyFields({
259+
type: 'user',
260+
fields: [
261+
{
262+
name: 'name',
263+
type: null,
264+
kind: 'attribute',
265+
},
266+
{
267+
name: 'bestFriend',
268+
type: 'user',
269+
kind: 'resource',
270+
options: { inverse: 'bestFriend', async: true },
271+
},
272+
],
273+
})
274+
);
275+
276+
const record = store.push<User>({
277+
data: {
278+
type: 'user',
279+
id: '1',
280+
attributes: {
281+
name: 'Chris',
282+
},
283+
relationships: {
284+
bestFriend: {
285+
data: { type: 'user', id: '2' },
286+
},
287+
},
288+
},
289+
included: [
290+
{
291+
type: 'user',
292+
id: '2',
293+
attributes: {
294+
name: 'Rey',
295+
},
296+
relationships: {
297+
bestFriend: {
298+
data: { type: 'user', id: '1' },
299+
},
300+
},
301+
},
302+
],
303+
});
304+
305+
assert.true(record[Legacy], 'record is in legacy mode');
306+
307+
try {
308+
// eslint-disable-next-line @typescript-eslint/no-unused-expressions
309+
record.bestFriend;
310+
assert.ok(false, 'record.bestFriend should throw');
311+
} catch (e) {
312+
assert.strictEqual(
313+
(e as Error).message,
314+
"SchemaRecord.bestFriend is not available in legacy mode because it has type 'resource'",
315+
'record.bestFriend throws'
316+
);
317+
}
318+
});
319+
181320
test('we can access errors', function (assert) {
182321
const store = this.owner.lookup('service:store') as Store;
183322
const { schema } = store;

0 commit comments

Comments
 (0)