Skip to content

Commit

Permalink
entity init resolvers aliases
Browse files Browse the repository at this point in the history
  • Loading branch information
antoniopresto committed Mar 16, 2024
1 parent de9267f commit 1403cf5
Show file tree
Hide file tree
Showing 8 changed files with 140 additions and 8 deletions.
17 changes: 9 additions & 8 deletions packages/entity/src/Entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,15 +93,16 @@ export function setDefaultTransporter(transporter: Transporter | undefined) {

export function createEntity<
InputDefinition extends ObjectDefinitionInput,
Indexes extends DocumentIndexesConfig,
Options extends EntityOptions<InputDefinition, Indexes> = EntityOptions<
InputDefinition,
Indexes
>
Indexes extends DocumentIndexesConfig
>(
configOptions:
| EntityOptions<InputDefinition, Indexes>
| (() => EntityOptions<InputDefinition, Indexes>)
configOptions: () => EntityOptions<InputDefinition, Indexes>
): Entity<InputDefinition, Indexes>;

export function createEntity<
InputDefinition extends ObjectDefinitionInput,
Indexes extends DocumentIndexesConfig
>(
configOptions: EntityOptions<InputDefinition, Indexes>
): Entity<InputDefinition, Indexes>;

export function createEntity(
Expand Down
32 changes: 32 additions & 0 deletions packages/entity/src/EntityResolversChain.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { createResolver } from '@powership/schema';

import { AnyEntity } from './EntityInterfaces';

export class EntityResolversChain<TEntity extends AnyEntity = AnyEntity> {
private entity: TEntity;

constructor(entity: TEntity) {
this.entity = entity;
}

create = <Name extends string>(name: Name) => {
const {
type,
edgeType,
databaseType,
inputType,
paginationType,
originType,
indexGraphTypes,
} = this.entity;

const current = {
name,
} as const;
};
}




const x =
24 changes: 24 additions & 0 deletions packages/entity/src/utils/$array.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import type { $cast, $isKnown } from './index';

export type __$array<Value = any> = Value extends ReadonlyArray<infer T>
? T[]
: Value extends Array<infer T>
? T[]
: never;

export type _$array<Value = any> = [$isKnown<Value>] extends [1]
? __$array<Value>
: never;

export type $array<Value = any> = Value extends unknown
? $cast<_$array<Value>, any[]>
: never;

export function $array<T>(value: T): $array<T> {
return ((): any => {
if (value && typeof value === 'object' && !Array.isArray(value)) {
return value;
}
return Object.create(null);
})();
}
24 changes: 24 additions & 0 deletions packages/entity/src/utils/$cast.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import type { IsAny, IsNever, IsUnknown } from '@powership/utils';

/**
* @internal
*/
export type $isAny<T> = IsAny<T>;
export type $isNever<T> = IsNever<T>;
export type $isUnknown<T> = IsUnknown<T>;

export type $isKnown<T> = $isAny<T> extends true
? 0
: $isNever<T> extends true
? 0
: $isUnknown<T> extends true
? 0
: 1;

export type $cast<Value, Target> = [$isKnown<Value>] extends [1]
? Value extends Target
? Value
: Target
: never;

export {};
22 changes: 22 additions & 0 deletions packages/entity/src/utils/$number.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { captureStackTrace } from '@powership/utils';

import type { $cast } from './index';

export type $number<T = any> = $cast<T, number>;

export function $number<T>(value: T, defaults?: number): $number<T> {
return ((): any => {
if (typeof value === 'number') {
return value;
}
const s = `${defaults}`;

return s.match(/^\d+$/)
? +s
: (() => {
const err = new Error(`Expected value to be an number, found ${s}`);
captureStackTrace(err, $number);
throw err;
})();
})();
}
12 changes: 12 additions & 0 deletions packages/entity/src/utils/$object.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import type { $cast } from './index';

export type $object<T = any> = $cast<T, Record<string, any>>;

export function $object<T>(value: T): $object<T> {
return ((): any => {
if (value && typeof value === 'object' && !Array.isArray(value)) {
return value;
}
return Object.create(null);
})();
}
12 changes: 12 additions & 0 deletions packages/entity/src/utils/$string.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import type { $cast } from './index';

export type $string<T = any> = $cast<T, string>;

export function $string<T>(value: T): $string<T> {
return ((): any => {
if (typeof value === 'string') {
return value;
}
return '';
})();
}
5 changes: 5 additions & 0 deletions packages/entity/src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export * from './$array';
export * from './$cast';
export * from './$number';
export * from './$object';
export * from './$string';

0 comments on commit 1403cf5

Please sign in to comment.