Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Backport #9319 to lts-4-12: Add @ember-data/legacy-compat/builders #9386

Merged
merged 2 commits into from
Apr 29, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions packages/legacy-compat/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,15 @@
"dependenciesMeta": {
"@ember-data/private-build-infra": {
"injected": true
},
"@ember/string": {
"injected": true
}
},
"peerDependencies": {
"@ember-data/graph": "workspace:4.12.7",
"@ember-data/json-api": "workspace:4.12.7",
"@ember/string": "^3.1.1"
"@ember/string": "^3.0.1"
},
"peerDependenciesMeta": {
"@ember-data/graph": {
Expand All @@ -75,9 +78,10 @@
"@babel/preset-typescript": "^7.21.4",
"@babel/preset-env": "^7.21.4",
"@babel/runtime": "^7.21.0",
"@ember/string": "^3.1.1",
"@ember/string": "^3.0.1",
"@rollup/plugin-babel": "^6.0.3",
"@rollup/plugin-node-resolve": "^15.0.1",
"@types/ember__string": "^3.0.15",
"tslib": "^2.5.0",
"walk-sync": "^3.0.0",
"typescript": "^5.0.3"
Expand Down
27 changes: 15 additions & 12 deletions packages/legacy-compat/src/builders/find-all.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,26 @@
*/
import { assert } from '@ember/debug';

import type { StoreRequestInput } from '@ember-data/store';
import type { FindAllOptions } from '@ember-data/store/-types/q/store';
import type { TypedRecordInstance, TypeFromInstance } from '@warp-drive/core-types/record';
import { SkipCache } from '@warp-drive/core-types/request';
import type { RequestSignature } from '@warp-drive/core-types/symbols';

import type Model from '@ember-data/model';
import { SkipCache } from '@ember-data/request';
import type { ImmutableRequestInfo } from '@ember-data/request/-private/types';
import { normalizeModelName } from './utils';

type FindAllRequestInput<T extends string = string, RT = unknown[]> = StoreRequestInput & {
// Keeping unused generics for consistency with 5x types
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know we aren't publishing these types, but removing them was more work than just keeping them so....

type FindAllRequestInput<T extends string = string, RT = unknown[]> = ImmutableRequestInfo & {
op: 'findAll';
data: {
type: T;
options: FindAllBuilderOptions;
};
[RequestSignature]?: RT;
};

type FindAllBuilderOptions = FindAllOptions;
type FindAllBuilderOptions = {
reload?: boolean;
backgroundReload?: boolean;
include?: string | string[];
adapterOptions?: Record<string, unknown>;
};

/**
This function builds a request config to perform a `findAll` request for the given type.
Expand All @@ -41,10 +43,11 @@ type FindAllBuilderOptions = FindAllOptions;
@param {FindAllBuilderOptions} [options] optional, may include `adapterOptions` hash which will be passed to adapter.findAll
@return {FindAllRequestInput} request config
*/
export function findAllBuilder<T extends TypedRecordInstance>(
type: TypeFromInstance<T>,
// Keeping this generic for consistency with 5x types
export function findAllBuilder<T extends Model>(
type: string,
options?: FindAllBuilderOptions
): FindAllRequestInput<TypeFromInstance<T>, T[]>;
): FindAllRequestInput<string, Model[]>;
export function findAllBuilder(type: string, options?: FindAllBuilderOptions): FindAllRequestInput;
export function findAllBuilder(type: string, options: FindAllBuilderOptions = {}): FindAllRequestInput {
assert(`You need to pass a model name to the findAll builder`, type);
Expand Down
40 changes: 21 additions & 19 deletions packages/legacy-compat/src/builders/find-record.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,28 @@
*/
import { assert } from '@ember/debug';

import type { StoreRequestInput } from '@ember-data/store';
import type Model from '@ember-data/model';
import { SkipCache } from '@ember-data/request';
import type { ImmutableRequestInfo } from '@ember-data/request/-private/types';
import { constructResource, ensureStringId } from '@ember-data/store/-private';
import type { BaseFinderOptions, FindRecordOptions } from '@ember-data/store/-types/q/store';
import type { TypedRecordInstance, TypeFromInstance } from '@warp-drive/core-types/record';
import { SkipCache } from '@warp-drive/core-types/request';
import type { ResourceIdentifierObject } from '@warp-drive/core-types/spec/raw';
import type { RequestSignature } from '@warp-drive/core-types/symbols';

import type { ResourceIdentifierObject } from '@ember-data/types/q/ember-data-json-api';
import { isMaybeIdentifier, normalizeModelName } from './utils';

type FindRecordRequestInput<T extends string = string, RT = unknown> = StoreRequestInput & {
// Keeping unused generics for consistency with 5x types
type FindRecordRequestInput<T extends string = string, RT = unknown> = ImmutableRequestInfo & {
op: 'findRecord';
data: {
record: ResourceIdentifierObject<T>;
record: ResourceIdentifierObject;
options: FindRecordBuilderOptions;
};
[RequestSignature]?: RT;
};

type FindRecordBuilderOptions = Omit<FindRecordOptions, 'preload'>;
type FindRecordBuilderOptions = {
reload?: boolean;
backgroundReload?: boolean;
include?: string | string[];
adapterOptions?: Record<string, unknown>;
};

/**
This function builds a request config to find the record for a given identifier or type and id combination.
Expand Down Expand Up @@ -61,20 +63,20 @@ type FindRecordBuilderOptions = Omit<FindRecordOptions, 'preload'>;
@param {FindRecordBuilderOptions} [options] - if the first param is a string this will be the optional options for the request. See examples for available options.
@return {FindRecordRequestInput} request config
*/
export function findRecordBuilder<T extends TypedRecordInstance>(
resource: TypeFromInstance<T>,
export function findRecordBuilder<T extends Model>(
resource: string,
id: string,
options?: FindRecordBuilderOptions
): FindRecordRequestInput<TypeFromInstance<T>, T>;
): FindRecordRequestInput<string, T>;
export function findRecordBuilder(
resource: string,
id: string,
options?: FindRecordBuilderOptions
): FindRecordRequestInput;
export function findRecordBuilder<T extends TypedRecordInstance>(
resource: ResourceIdentifierObject<TypeFromInstance<T>>,
export function findRecordBuilder<T extends Model>(
resource: ResourceIdentifierObject,
options?: FindRecordBuilderOptions
): FindRecordRequestInput<TypeFromInstance<T>, T>;
): FindRecordRequestInput<string, T>;
export function findRecordBuilder(
resource: ResourceIdentifierObject,
options?: FindRecordBuilderOptions
Expand All @@ -89,7 +91,7 @@ export function findRecordBuilder(
resource
);
if (isMaybeIdentifier(resource)) {
options = idOrOptions as BaseFinderOptions | undefined;
options = idOrOptions as FindRecordBuilderOptions | undefined;
} else {
assert(
`You need to pass a modelName or resource identifier as the first argument to the findRecord builder (passed ${resource})`,
Expand All @@ -102,7 +104,7 @@ export function findRecordBuilder(

options = options || {};

assert('findRecord builder does not support options.preload', !(options as FindRecordOptions).preload);
assert('findRecord builder does not support options.preload', !(options as any).preload);

return {
op: 'findRecord' as const,
Expand Down
31 changes: 14 additions & 17 deletions packages/legacy-compat/src/builders/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,23 @@
*/
import { assert } from '@ember/debug';

import type { StoreRequestInput } from '@ember-data/store';
import type { QueryOptions } from '@ember-data/store/-types/q/store';
import type { TypedRecordInstance, TypeFromInstance } from '@warp-drive/core-types/record';
import { SkipCache } from '@warp-drive/core-types/request';
import type { RequestSignature } from '@warp-drive/core-types/symbols';

import type Model from '@ember-data/model';
import { SkipCache } from '@ember-data/request';
import type { ImmutableRequestInfo } from '@ember-data/request/-private/types';
import { normalizeModelName } from './utils';

type QueryRequestInput<T extends string = string, RT = unknown[]> = StoreRequestInput & {
type QueryRequestInput<T extends string = string, RT = unknown[]> = ImmutableRequestInfo & {
op: 'query';
data: {
type: T;
query: Record<string, unknown>;
options: QueryBuilderOptions;
};
[RequestSignature]?: RT;
};

type QueryBuilderOptions = QueryOptions;
type QueryBuilderOptions = {
[K in string | 'adapterOptions']?: K extends 'adapterOptions' ? Record<string, unknown> : unknown;
};

/**
This function builds a request config for a given type and query object.
Expand All @@ -42,11 +40,11 @@ type QueryBuilderOptions = QueryOptions;
@param {QueryBuilderOptions} [options] optional, may include `adapterOptions` hash which will be passed to adapter.query
@return {QueryRequestInput} request config
*/
export function queryBuilder<T extends TypedRecordInstance>(
type: TypeFromInstance<T>,
export function queryBuilder<T extends Model>(
type: string,
query: Record<string, unknown>,
options?: QueryBuilderOptions
): QueryRequestInput<TypeFromInstance<T>, T[]>;
): QueryRequestInput<string, T[]>;
export function queryBuilder(
type: string,
query: Record<string, unknown>,
Expand Down Expand Up @@ -75,14 +73,13 @@ export function queryBuilder(
};
}

type QueryRecordRequestInput<T extends string = string, RT = unknown> = StoreRequestInput & {
type QueryRecordRequestInput<T extends string = string, RT = unknown> = ImmutableRequestInfo & {
op: 'queryRecord';
data: {
type: T;
query: Record<string, unknown>;
options: QueryBuilderOptions;
};
[RequestSignature]?: RT;
};

/**
Expand All @@ -104,11 +101,11 @@ type QueryRecordRequestInput<T extends string = string, RT = unknown> = StoreReq
@param {QueryBuilderOptions} [options] optional, may include `adapterOptions` hash which will be passed to adapter.query
@return {QueryRecordRequestInput} request config
*/
export function queryRecordBuilder<T extends TypedRecordInstance>(
type: TypeFromInstance<T>,
export function queryRecordBuilder<T extends Model>(
type: string,
query: Record<string, unknown>,
options?: QueryBuilderOptions
): QueryRecordRequestInput<TypeFromInstance<T>, T | null>;
): QueryRecordRequestInput<string, T | null>;
export function queryRecordBuilder(
type: string,
query: Record<string, unknown>,
Expand Down
25 changes: 12 additions & 13 deletions packages/legacy-compat/src/builders/save-record.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,21 @@
*/
import { assert } from '@ember/debug';

import { recordIdentifierFor, storeFor, type StoreRequestInput } from '@ember-data/store';
import type Model from '@ember-data/model';
import { SkipCache } from '@ember-data/request';
import type { ImmutableRequestInfo } from '@ember-data/request/-private/types';
import { recordIdentifierFor, storeFor } from '@ember-data/store';
import type { InstanceCache } from '@ember-data/store/-private/caches/instance-cache';
import type { StableRecordIdentifier } from '@warp-drive/core-types';
import type { Cache } from '@warp-drive/core-types/cache';
import type { TypedRecordInstance, TypeFromInstance } from '@warp-drive/core-types/record';
import { SkipCache } from '@warp-drive/core-types/request';
import type { RequestSignature } from '@warp-drive/core-types/symbols';
import type { Cache } from '@ember-data/types/cache/cache';
import type { StableRecordIdentifier } from '@ember-data/types/q/identifier';

type SaveRecordRequestInput<T extends string = string, RT = unknown> = StoreRequestInput & {
type SaveRecordRequestInput<T extends string = string, RT = unknown> = ImmutableRequestInfo & {
op: 'createRecord' | 'deleteRecord' | 'updateRecord';
data: {
record: StableRecordIdentifier<T>;
record: StableRecordIdentifier;
options: SaveRecordBuilderOptions;
};
records: [StableRecordIdentifier<T>];
[RequestSignature]?: RT;
records: [StableRecordIdentifier];
};

type SaveRecordBuilderOptions = Record<string, unknown>;
Expand Down Expand Up @@ -50,13 +49,13 @@ function resourceIsFullyDeleted(instanceCache: InstanceCache, identifier: Stable
@param {SaveRecordBuilderOptions} options optional, may include `adapterOptions` hash which will be passed to adapter.saveRecord
@return {SaveRecordRequestInput} request config
*/
export function saveRecordBuilder<T extends TypedRecordInstance>(
export function saveRecordBuilder<T extends Model>(
record: T,
options: Record<string, unknown> = {}
): SaveRecordRequestInput<TypeFromInstance<T>, T> {
): SaveRecordRequestInput<string, T> {
const store = storeFor(record);
assert(`Unable to initiate save for a record in a disconnected state`, store);
const identifier = recordIdentifierFor<T>(record);
const identifier = recordIdentifierFor(record);

if (!identifier) {
// this commonly means we're disconnected
Expand Down
27 changes: 2 additions & 25 deletions packages/legacy-compat/src/builders/utils.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import { deprecate } from '@ember/debug';
import type { ResourceIdentifierObject } from '@ember-data/types/q/ember-data-json-api';
import { dasherize } from '@ember/string';

import { DEPRECATE_NON_STRICT_TYPES } from '@warp-drive/build-config/deprecations';
import type { ResourceIdentifierObject } from '@warp-drive/core-types/spec/raw';

export function isMaybeIdentifier(
maybeIdentifier: string | ResourceIdentifierObject
): maybeIdentifier is ResourceIdentifierObject {
Expand All @@ -16,25 +13,5 @@ export function isMaybeIdentifier(
}

export function normalizeModelName(type: string): string {
if (DEPRECATE_NON_STRICT_TYPES) {
const result = dasherize(type);

deprecate(
`The resource type '${type}' is not normalized. Update your application code to use '${result}' instead of '${type}'.`,
result === type,
{
id: 'ember-data:deprecate-non-strict-types',
until: '6.0',
for: 'ember-data',
since: {
available: '5.3',
enabled: '5.3',
},
}
);

return result;
}

return type;
return dasherize(type);
}
Loading
Loading