-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat (private): implement resource relationships for SchemaRecord (#8946
- Loading branch information
Showing
5 changed files
with
191 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import { SchemaRecord } from '@warp-drive/schema-record/record'; | ||
import { SchemaService } from '@warp-drive/schema-record/schema'; | ||
import { module, test } from 'qunit'; | ||
|
||
import { setupTest } from 'ember-qunit'; | ||
|
||
import type Store from '@ember-data/store'; | ||
import { Document } from '@ember-data/store/-private/document'; | ||
|
||
interface User { | ||
id: string | null; | ||
$type: 'user'; | ||
name: string; | ||
bestFriend: Document<User | null>; | ||
} | ||
|
||
module('Reads | resource', function (hooks) { | ||
setupTest(hooks); | ||
|
||
test('we can use simple fields with no `type`', function (assert) { | ||
const store = this.owner.lookup('service:store') as Store; | ||
const schema = new SchemaService(); | ||
store.registerSchema(schema); | ||
|
||
function concat( | ||
record: SchemaRecord & { [key: string]: unknown }, | ||
options: Record<string, unknown> | null, | ||
_prop: string | ||
): string { | ||
if (!options) throw new Error(`options is required`); | ||
const opts = options as { fields: string[]; separator?: string }; | ||
return opts.fields.map((field) => record[field]).join(opts.separator ?? ''); | ||
} | ||
|
||
schema.registerDerivation('concat', concat); | ||
|
||
schema.defineSchema('user', [ | ||
{ | ||
name: 'name', | ||
type: null, | ||
kind: 'attribute', | ||
}, | ||
{ | ||
name: 'bestFriend', | ||
type: 'user', | ||
kind: 'resource', | ||
options: { inverse: 'bestFriend', async: true }, | ||
}, | ||
]); | ||
|
||
const record = store.push({ | ||
data: { | ||
type: 'user', | ||
id: '1', | ||
attributes: { | ||
name: 'Chris', | ||
}, | ||
relationships: { | ||
bestFriend: { | ||
data: { type: 'user', id: '2' }, | ||
}, | ||
}, | ||
}, | ||
included: [ | ||
{ | ||
type: 'user', | ||
id: '2', | ||
attributes: { | ||
name: 'Rey', | ||
}, | ||
relationships: { | ||
bestFriend: { | ||
data: { type: 'user', id: '1' }, | ||
}, | ||
}, | ||
}, | ||
], | ||
}) as User; | ||
|
||
assert.strictEqual(record.id, '1', 'id is accessible'); | ||
assert.strictEqual(record.$type, 'user', '$type is accessible'); | ||
assert.strictEqual(record.name, 'Chris', 'name is accessible'); | ||
assert.strictEqual(record.bestFriend.data?.id, '2', 'bestFriend.id is accessible'); | ||
assert.strictEqual(record.bestFriend.data?.$type, 'user', 'bestFriend.user is accessible'); | ||
assert.strictEqual(record.bestFriend.data?.name, 'Rey', 'bestFriend.name is accessible'); | ||
}); | ||
}); |