Skip to content

Commit

Permalink
fixup lint
Browse files Browse the repository at this point in the history
  • Loading branch information
runspired committed May 22, 2024
1 parent 6cbca3b commit d7aba33
Show file tree
Hide file tree
Showing 13 changed files with 205 additions and 131 deletions.
2 changes: 1 addition & 1 deletion tests/builders/app/services/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export default class Store extends DataStore {
manager.useCache(CacheHandler);
}

createSchemaService() {
createSchemaService(): ReturnType<typeof buildSchema> {
return buildSchema(this);
}

Expand Down
2 changes: 1 addition & 1 deletion tests/ember-data__adapter/app/services/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export default class Store extends BaseStore {
this.requestManager.useCache(CacheHandler);
}

createSchemaService() {
createSchemaService(): ReturnType<typeof buildSchema> {
return buildSchema(this);
}

Expand Down
2 changes: 1 addition & 1 deletion tests/ember-data__graph/app/services/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export default class Store extends BaseStore {
this.requestManager.useCache(CacheHandler);
}

createSchemaService() {
createSchemaService(): ReturnType<typeof buildSchema> {
return buildSchema(this);
}

Expand Down
118 changes: 24 additions & 94 deletions tests/ember-data__model/tests/integration/model-for-test.ts
Original file line number Diff line number Diff line change
@@ -1,57 +1,11 @@
import Store from '@ember-data/store';
import type { SchemaService } from '@ember-data/store/types';
import type { FieldSchema } from '@warp-drive/core-types/schema/fields';
import { module, test } from '@warp-drive/diagnostic';

import { TestSchema } from '../utils/schema';

module('modelFor without @ember-data/model', function () {
test('We can call modelFor', function (assert) {
class TestSchema implements SchemaService {
attributesDefinitionFor(_identifier: { type: string }) {
return {
name: {
name: 'name',
kind: 'attribute' as const,
type: null,
},
};
}

_fieldsDefCache = {} as Record<string, Map<string, FieldSchema>>;

fields(identifier: { type: string }): Map<string, FieldSchema> {
const { type } = identifier;
let fieldDefs: Map<string, FieldSchema> | undefined = this._fieldsDefCache[type];

if (fieldDefs === undefined) {
fieldDefs = new Map();
this._fieldsDefCache[type] = fieldDefs;

const attributes = this.attributesDefinitionFor(identifier);
const relationships = this.relationshipsDefinitionFor(identifier);

for (const attr of Object.values(attributes)) {
fieldDefs.set(attr.name, attr);
}

for (const rel of Object.values(relationships)) {
fieldDefs.set(rel.name, rel);
}
}

return fieldDefs;
}

relationshipsDefinitionFor(_identifier: {
type: string;
}): ReturnType<SchemaService['relationshipsDefinitionFor']> {
return {};
}

hasResource({ type }: { type: string }) {
return type === 'user';
}
}

class TestStore extends Store {
createSchemaService(): SchemaService {
return new TestSchema();
Expand All @@ -68,6 +22,17 @@ module('modelFor without @ember-data/model', function () {
}
}
const store = new TestStore();
store.schema.registerResource({
identity: { name: 'id', kind: '@id' },
type: 'user',
fields: [
{
name: 'name',
type: null,
kind: 'attribute',
},
],
});

try {
store.modelFor('user');
Expand All @@ -89,52 +54,6 @@ module('modelFor without @ember-data/model', function () {
});

test('modelFor returns a stable reference', function (assert) {
class TestSchema implements SchemaService {
attributesDefinitionFor(_identifier: { type: string }) {
return {
name: {
name: 'name',
kind: 'attribute' as const,
type: null,
},
};
}

_fieldsDefCache = {} as Record<string, Map<string, FieldSchema>>;

fields(identifier: { type: string }): Map<string, FieldSchema> {
const { type } = identifier;
let fieldDefs: Map<string, FieldSchema> | undefined = this._fieldsDefCache[type];

if (fieldDefs === undefined) {
fieldDefs = new Map();
this._fieldsDefCache[type] = fieldDefs;

const attributes = this.attributesDefinitionFor(identifier);
const relationships = this.relationshipsDefinitionFor(identifier);

for (const attr of Object.values(attributes)) {
fieldDefs.set(attr.name, attr);
}

for (const rel of Object.values(relationships)) {
fieldDefs.set(rel.name, rel);
}
}

return fieldDefs;
}

relationshipsDefinitionFor(_identifier: {
type: string;
}): ReturnType<SchemaService['relationshipsDefinitionFor']> {
return {};
}

hasResource({ type }: { type: string }) {
return type === 'user';
}
}
class TestStore extends Store {
createSchemaService(): SchemaService {
return new TestSchema();
Expand All @@ -151,6 +70,17 @@ module('modelFor without @ember-data/model', function () {
}
}
const store = new TestStore();
store.schema.registerResource({
identity: { name: 'id', kind: '@id' },
type: 'user',
fields: [
{
name: 'name',
type: null,
kind: 'attribute',
},
],
});

const ShimUser1 = store.modelFor('user');
const ShimUser2 = store.modelFor('user');
Expand Down
126 changes: 126 additions & 0 deletions tests/ember-data__model/tests/utils/schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
import type { SchemaService } from '@ember-data/store/types';
import { assert } from '@warp-drive/build-config/macros';
import type { StableRecordIdentifier } from '@warp-drive/core-types';
import type { Value } from '@warp-drive/core-types/json/raw';
import type { Derivation, HashFn, Transformation } from '@warp-drive/core-types/schema/concepts';
import type {
FieldSchema,
LegacyAttributeField,
LegacyRelationshipSchema,
ResourceSchema,
} from '@warp-drive/core-types/schema/fields';
import { Type } from '@warp-drive/core-types/symbols';

type InternalSchema = {
original: ResourceSchema;
traits: Set<string>;
fields: Map<string, FieldSchema>;
attributes: Record<string, LegacyAttributeField>;
relationships: Record<string, LegacyRelationshipSchema>;
};

export class TestSchema implements SchemaService {
declare _schemas: Map<string, InternalSchema>;
declare _transforms: Map<string, Transformation>;
declare _hashFns: Map<string, HashFn>;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
declare _derivations: Map<string, Derivation<any, any>>;
declare _traits: Set<string>;

constructor() {
this._schemas = new Map();
this._transforms = new Map();
this._hashFns = new Map();
this._derivations = new Map();
}
hasTrait(type: string): boolean {
return this._traits.has(type);
}
resourceHasTrait(resource: StableRecordIdentifier | { type: string }, trait: string): boolean {
return this._schemas.get(resource.type)!.traits.has(trait);
}
transformation(name: string): Transformation {
assert(`No transformation registered with name ${name}`, this._transforms.has(name));
return this._transforms.get(name)!;
}
derivation(name: string): Derivation {
assert(`No derivation registered with name ${name}`, this._derivations.has(name));
return this._derivations.get(name)!;
}
resource(resource: StableRecordIdentifier | { type: string }): ResourceSchema {
assert(`No resource registered with name ${resource.type}`, this._schemas.has(resource.type));
return this._schemas.get(resource.type)!.original;
}
hashFn(name: string): HashFn {
assert(`No hash function registered with name ${name}`, this._hashFns.has(name));
return this._hashFns.get(name)!;
}

registerTransformation<T extends Value = string, PT = unknown>(transformation: Transformation<T, PT>): void {
this._transforms.set(transformation[Type], transformation as Transformation);
}

registerDerivation<R, T>(derivation: Derivation<R, T>): void {
this._derivations.set(derivation[Type], derivation);
}

registerHashFn<T extends object>(hashFn: HashFn<T>): void {
this._hashFns.set(hashFn[Type], hashFn as HashFn);
}

registerResource(schema: ResourceSchema): void {
const fields = new Map<string, FieldSchema>();
const relationships: Record<string, LegacyRelationshipSchema> = {};
const attributes: Record<string, LegacyAttributeField> = {};

schema.fields.forEach((field) => {
assert(
`${field.kind} is not valid inside a ResourceSchema's fields.`,
// @ts-expect-error we are checking for mistakes at runtime
field.kind !== '@id' && field.kind !== '@hash'
);
fields.set(field.name, field);
if (field.kind === 'attribute') {
attributes[field.name] = field;
} else if (field.kind === 'belongsTo' || field.kind === 'hasMany') {
relationships[field.name] = field;
}
});

const traits = new Set<string>(schema.traits);
traits.forEach((trait) => {
this._traits.add(trait);
});

const internalSchema: InternalSchema = { original: schema, fields, relationships, attributes, traits };
this._schemas.set(schema.type, internalSchema);
}

registerResources(resources: ResourceSchema[]) {
resources.forEach((resource) => {
this.registerResource(resource);
});
}

fields({ type }: { type: string }): InternalSchema['fields'] {
const schema = this._schemas.get(type);

if (!schema) {
if (this._schemas.size === 0) {
return new Map();
}
throw new Error(`No schema defined for ${type}`);
}

return schema.fields;
}

hasResource({ type }: { type: string }) {
return this._schemas.has(type)
? true
: // in tests we intentionally allow "schemaless" resources
this._schemas.size === 0
? true
: false;
}
}
10 changes: 7 additions & 3 deletions tests/main/tests/helpers/reactive-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import Component from '@glimmer/component';
import { hbs } from 'ember-cli-htmlbars';

import type Model from '@ember-data/model';
import type { ResourceSchema } from '@warp-drive/core-types/schema/fields';
import type { FieldSchema, IdentityField, ResourceSchema } from '@warp-drive/core-types/schema/fields';

export interface ReactiveContext {
counters: Record<string, number | undefined>;
Expand All @@ -19,6 +19,10 @@ export async function reactiveContext<T extends Model>(
resource: ResourceSchema
): Promise<ReactiveContext> {
const _fields: string[] = [];
const fields: Array<FieldSchema | IdentityField> = resource.fields.slice();
if (resource.identity?.name) {
fields.unshift(resource.identity as IdentityField);
}
fields.forEach((field) => {
_fields.push(field.name + 'Count');
_fields.push(field.name);
Expand All @@ -41,15 +45,15 @@ export async function reactiveContext<T extends Model>(
Object.defineProperty(ReactiveComponent.prototype, field.name, {
get() {
counters[field.name]++;
switch (field.type) {
switch (field.kind) {
case 'hasMany':
return `[${(record[field.name as keyof T] as Model[]).map((r) => r.id).join(',')}]`;
case 'belongsTo':
return (record[field.name as keyof T] as Model).id;
case 'field':
return record[field.name as keyof T] as unknown;
default:
throw new Error(`Unknown field type ${field.type} for field ${field.name}`);
throw new Error(`Unknown field kind ${field.kind} for field ${field.name}`);
}
},
});
Expand Down
15 changes: 12 additions & 3 deletions tests/main/tests/integration/cache-handler/lifetimes-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,16 @@ import Store, { CacheHandler } from '@ember-data/store';
import type { CacheCapabilitiesManager, SchemaService } from '@ember-data/store/types';
import type { Cache } from '@warp-drive/core-types/cache';
import type { StableDocumentIdentifier, StableRecordIdentifier } from '@warp-drive/core-types/identifier';
import type { Derivation, Transformation } from '@warp-drive/core-types/schema/concepts';
import type { ObjectValue } from '@warp-drive/core-types/json/raw';
import type { Derivation, HashFn, Transformation } from '@warp-drive/core-types/schema/concepts';
import type { FieldSchema, ResourceSchema } from '@warp-drive/core-types/schema/fields';
import type { ResourceType } from '@warp-drive/core-types/symbols';

type FakeRecord = { [key: string]: unknown; destroy: () => void };

class BaseTestStore extends Store {
createSchemaService(): SchemaService {
return {
const schemaService: SchemaService = {
fields(identifier: StableRecordIdentifier | { type: string }): Map<string, FieldSchema> {
return new Map();
},
Expand Down Expand Up @@ -52,10 +53,18 @@ class BaseTestStore extends Store {
registerTransformation: function (transform: Transformation): void {
throw new Error('Function not implemented.');
},
registerDerivation: function (derivation: Derivation): void {
registerDerivation<R, T, FM extends ObjectValue | null>(derivation: Derivation<R, T, FM>): void {
throw new Error('Function not implemented.');
},
hashFn: function (name: string): HashFn {
throw new Error('Function not implemented.');
},
registerHashFn: function (hashFn: HashFn): void {
throw new Error('Function not implemented.');
},
};

return schemaService;
}

override createCache(wrapper: CacheCapabilitiesManager) {
Expand Down
Loading

0 comments on commit d7aba33

Please sign in to comment.