Comparing version 1.0.239 to 1.0.240
@@ -7,3 +7,3 @@ export { orma_query } from './query/query'; | ||
export { type OrmaQuery } from './types/query/query_types'; | ||
export { type OrmaQueryResult, type OrmaRecord, } from './types/query/query_result_types'; | ||
export { type OrmaQueryResult, type OrmaRecord, type OrmaField } from './types/query/query_result_types'; | ||
export { orma_mutate, orma_mutate_prepare, orma_mutate_run, type MysqlFunction, } from './mutate/mutate'; | ||
@@ -10,0 +10,0 @@ export { type OrmaStatement } from './mutate/statement_generation/mutation_statements'; |
@@ -1,2 +0,2 @@ | ||
import { GlobalTestAliases, GlobalTestMutation, GlobalTestSchema } from '../test_data/global_test_schema'; | ||
import { GlobalTestMutation, GlobalTestSchema } from '../test_data/global_test_schema'; | ||
import { OrmaQueryResult } from '../types/query/query_result_types'; | ||
@@ -6,2 +6,2 @@ import { WhereConnected } from '../types/query/query_types'; | ||
export declare const test_mutate: (mutation: GlobalTestMutation, where_connecteds?: WhereConnected<GlobalTestSchema>) => Promise<void>; | ||
export declare const test_query: <T extends Record<string, any>>(query: T) => Promise<OrmaQueryResult<GlobalTestSchema, GlobalTestAliases, T>>; | ||
export declare const test_query: <T extends Record<string, any>>(query: T) => Promise<OrmaQueryResult<GlobalTestSchema, T, never>>; |
@@ -11,4 +11,4 @@ import { NesterModification } from '../helpers/nester'; | ||
export declare const orma_nester: (orma_schema: OrmaSchema, query: any, nester_modifications: NesterModification[], results: [string[], Record<string, unknown>[]][]) => Record<string, any>; | ||
export declare const orma_query: <Schema extends OrmaSchema, Aliases extends OrmaQueryAliases<Schema>, Query extends OrmaQuery<Schema, Aliases>>(raw_query: Query, orma_schema_input: Schema, query_function: MysqlFunction, connection_edges?: ConnectionEdges) => Promise<OrmaQueryResult<Schema, Aliases, Query>>; | ||
export declare const orma_query: <Schema extends OrmaSchema, Aliases extends OrmaQueryAliases<Schema>, Query extends OrmaQuery<Schema, Aliases>>(raw_query: Query, orma_schema_input: Schema, query_function: MysqlFunction, connection_edges?: ConnectionEdges) => Promise<OrmaQueryResult<Schema, Query, never>>; | ||
export declare const as_orma_schema: <Schema extends OrmaSchema>(schema: Schema) => Schema; | ||
export declare const as_orma_query: <Schema extends OrmaSchema, T extends DeepReadonly<OrmaQuery<Schema, {}>>>(schema: Schema, query: T) => T; |
import { OrmaSchema } from '../schema/schema_types'; | ||
import { DeepMutable, GetAllEntities, GetFields, GetFieldType } from '../schema/schema_helper_types'; | ||
import { GetAliases, OrmaQueryAliases, GetRootAliases } from './query_types'; | ||
export type OrmaQueryResult<Schema extends OrmaSchema, Aliases extends OrmaQueryAliases<Schema>, Query extends object> = { | ||
-readonly [Key in keyof Query & (GetAllEntities<Schema> | GetRootAliases<Schema, Aliases>)]?: Query[Key] extends { | ||
$from: GetAllEntities<Schema>; | ||
} ? OrmaRecord<Schema, Aliases, Query[Key]['$from'], Query[Key]>[] : Query[Key] extends object ? Key extends GetAllEntities<Schema> ? OrmaRecord<Schema, Aliases, Key, Query[Key]>[] : never : never; | ||
}; | ||
export type OrmaRecord<Schema extends OrmaSchema, Aliases extends OrmaQueryAliases<Schema>, Entity extends GetAllEntities<Schema>, Subquery extends object> = { | ||
-readonly [Key in keyof Subquery & (GetAllEntities<Schema> | GetFields<Schema, Entity> | GetAliases<Schema, Aliases, Entity>)]: Subquery[Key] extends { | ||
$from: GetAllEntities<Schema>; | ||
} ? OrmaRecord<Schema, Aliases, Subquery[Key]['$from'], Subquery[Key]>[] | undefined : Subquery[Key] extends { | ||
import { DeepMutable, GetAllEntities, GetFields, GetFieldType, Keyword } from '../schema/schema_helper_types'; | ||
export type OrmaQueryResult<Schema extends OrmaSchema, Query extends object, Entity extends GetAllEntities<Schema> = never> = OrmaRecord<Schema, Entity, Query>; | ||
export type OrmaRecord<Schema extends OrmaSchema, Entity extends GetAllEntities<Schema>, Query extends object> = Omit<{ | ||
-readonly [Key in keyof Query]: Query[Key] extends { | ||
$from: any; | ||
} ? OrmaRecord<Schema, Query[Key]['$from'], Query[Key]>[] | undefined : Query[Key] extends { | ||
$escape: any; | ||
} ? DeepMutable<Subquery[Key]['$escape']> : Subquery[Key] extends true ? Key extends GetFields<Schema, Entity> ? GetFieldType<Schema, Entity, Key> : "Unrecognized field name for value 'true'" : Subquery[Key] extends GetFields<Schema, Entity> ? GetFieldType<Schema, Entity, Subquery[Key]> : Key extends GetAllEntities<Schema> ? Subquery[Key] extends object ? OrmaRecord<Schema, Aliases, Key, Subquery[Key]>[] | undefined : any : any; | ||
}; | ||
} ? DeepMutable<Query[Key]['$escape']> : Key extends GetAllEntities<Schema> ? Query[Key] extends object ? Exclude<keyof Query[Key], Keyword> extends never ? never : OrmaRecord<Schema, Key, Query[Key]>[] | undefined : never : GetSchemaTypeForField<Schema, Entity, Key, Query[Key]>; | ||
}, Keyword>; | ||
type GetSchemaTypeForField<Schema extends OrmaSchema, Entity extends GetAllEntities<Schema>, Key, Value> = Value extends true ? Key extends GetFields<Schema, Entity> ? GetFieldType<Schema, Entity, Key> : any : Value extends GetFields<Schema, Entity> ? GetFieldType<Schema, Entity, Value> : any; | ||
export type OrmaField<Schema extends OrmaSchema, Entity extends GetAllEntities<Schema>, Field extends GetFields<Schema, Entity>> = GetFieldType<Schema, Entity, Field>; | ||
export {}; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
// export type OrmaQueryResult< | ||
// Schema extends OrmaSchema, | ||
// Aliases extends OrmaQueryAliases<Schema>, | ||
// Query extends object | ||
// > = Omit< | ||
// { | ||
// -readonly [Key in keyof Query]?: Query[Key] extends { | ||
// $from: GetAllEntities<Schema> | ||
// } | ||
// ? OrmaRecord<Schema, Aliases, Query[Key]['$from'], Query[Key]>[] | ||
// : Query[Key] extends object | ||
// ? Key extends GetAllEntities<Schema> | ||
// ? OrmaRecord<Schema, Aliases, Key, Query[Key]>[] | ||
// : never | ||
// : never | ||
// }, | ||
// Keyword | ||
// > | ||
// export type OrmaRecord< | ||
// Schema extends OrmaSchema, | ||
// Aliases extends OrmaQueryAliases<Schema>, | ||
// Entity extends GetAllEntities<Schema>, | ||
// Subquery extends object | ||
// > = Omit< | ||
// { | ||
// -readonly [Key in keyof Subquery]: Subquery[Key] extends { | ||
// $from: GetAllEntities<Schema> | ||
// } | ||
// ? | ||
// | OrmaRecord< | ||
// Schema, | ||
// Aliases, | ||
// Subquery[Key]['$from'], | ||
// Subquery[Key] | ||
// >[] | ||
// | undefined // subquery with $from | ||
// : Subquery[Key] extends { $escape } | ||
// ? DeepMutable<Subquery[Key]['$escape']> | ||
// : Subquery[Key] extends true | ||
// ? Key extends GetFields<Schema, Entity> | ||
// ? GetFieldType<Schema, Entity, Key> // field_name: true | ||
// : "Unrecognized field name for value 'true'" | ||
// : Subquery[Key] extends GetFields<Schema, Entity> | ||
// ? GetFieldType<Schema, Entity, Subquery[Key]> // renamed_field: 'field_name' | ||
// : Key extends GetAllEntities<Schema> | ||
// ? Subquery[Key] extends object | ||
// ? OrmaRecord<Schema, Aliases, Key, Subquery[Key]>[] | undefined // subquery with no $from | ||
// : any | ||
// : any // unhandled case, like {$sum: 'quantity'} | ||
// }, | ||
// Keyword | ||
// > | ||
/* | ||
@@ -4,0 +56,0 @@ |
@@ -176,2 +176,11 @@ "use strict"; | ||
} | ||
{ | ||
const fn = (test_arg) => { | ||
const id = test_arg.users[0].id; | ||
const test = true; | ||
}; | ||
} | ||
{ | ||
const test = true; | ||
} | ||
}; |
import { mysql_to_typescript_types } from '../../schema/introspector'; | ||
import { ForeignKeyEdge, OrmaSchema } from '../schema/schema_types'; | ||
import { IsEqual } from '../helper_types'; | ||
export type DeepReadonly<T> = T extends (infer R)[] ? readonly DeepReadonly<R>[] : T extends Function ? T : T extends object ? { | ||
@@ -28,18 +27,12 @@ readonly [P in keyof T]: DeepReadonly<T[P]>; | ||
export type GetAllEdges<Schema extends OrmaSchema, Entities extends GetAllEntities<Schema>> = GetParentEdges<Schema, Entities> | GetChildEdges<Schema, Entities>; | ||
export type GetFieldType<Schema extends OrmaSchema, Entity extends GetAllEntities<Schema>, Field extends GetFields<Schema, Entity>> = GetFieldType2<Schema['$entities'][Entity]['$fields'][Field]>; | ||
type GetFieldType2<FieldSchema extends OrmaFieldSchema> = FieldSchema['$not_null'] extends true ? GetFieldType3<FieldSchema> : GetFieldType3<FieldSchema> | null; | ||
type GetFieldType3<FieldSchema extends OrmaFieldSchema> = FieldSchema extends { | ||
export type GetFieldType<Schema extends OrmaSchema, Entity extends GetAllEntities<Schema>, Field extends GetFields<Schema, Entity>> = Field extends GetFields<Schema, Entity> ? AddNull<Schema['$entities'][Entity]['$fields'][Field] extends { | ||
$data_type: 'enum'; | ||
$enum_values: readonly any[]; | ||
} ? FieldSchema['$enum_values'][number] : FieldTypeStringToType<MysqlToTypescriptTypeString<NonNullable<FieldSchema['$data_type']>>>; | ||
} ? Schema['$entities'][Entity]['$fields'][Field]['$enum_values'][number] : FieldTypeStringToType<MysqlToTypescriptTypeString<NonNullable<Schema['$entities'][Entity]['$fields'][Field]['$data_type']>>>, Schema['$entities'][Entity]['$fields'][Field]['$not_null'] extends true ? false : true> : never; | ||
type AddNull<T, AddNull extends boolean> = AddNull extends true ? T | null : T; | ||
export type GetFieldSchema<Schema extends OrmaSchema, Entity extends GetAllEntities<Schema>, Field extends GetFields<Schema, Entity>> = Schema['$entities'][Entity]['$fields'][Field] extends OrmaFieldSchema ? Schema['$entities'][Entity]['$fields'][Field] : any; | ||
type MysqlToTypescriptTypeString<TypeString extends keyof typeof mysql_to_typescript_types> = (typeof mysql_to_typescript_types)[TypeString]; | ||
type FieldTypeStringToType<TypeString extends (typeof mysql_to_typescript_types)[keyof typeof mysql_to_typescript_types]> = TypeString extends 'string' ? string : TypeString extends 'number' ? number : TypeString extends 'boolean' ? number | boolean : TypeString extends 'date' ? Date : any; | ||
export type FilterFieldsBySchemaProp<Schema extends OrmaSchema, Entity extends GetAllEntities<Schema>, SchemaProp extends string, value> = FilterFieldsBySchemaPropWithFieldsExplicit<Schema, Entity, GetFields<Schema, Entity>, SchemaProp, value>; | ||
type FilterFieldsBySchemaPropWithFieldsExplicit<Schema extends OrmaSchema, Entity extends GetAllEntities<Schema>, Fields extends GetFields<Schema, Entity>, SchemaProp extends string, value> = Fields extends any ? FieldSchemaPropEq<Schema, Entity, Fields, SchemaProp, value> extends true ? Fields : never : never; | ||
export type FieldSchemaPropEq<Schema extends OrmaSchema, Entity extends GetAllEntities<Schema>, Field extends GetFields<Schema, Entity>, SchemaProp extends string, value> = GetFieldSchema<Schema, Entity, Field> extends { | ||
[prop in SchemaProp]: value; | ||
} ? IsEqual<GetFieldSchema<Schema, Entity, Field>[SchemaProp], value> : false; | ||
type OrmaForeignKey = NonNullable<OrmaSchema['$entities'][string]['$foreign_keys']>[number]; | ||
type OrmaFieldSchema = NonNullable<OrmaSchema['$entities'][string]['$fields']>[string]; | ||
export {}; |
{ | ||
"name": "orma", | ||
"version": "1.0.239", | ||
"version": "1.0.240", | ||
"description": "A declarative relational syncronous orm", | ||
@@ -5,0 +5,0 @@ "main": "build/index.js", |
@@ -17,2 +17,3 @@ // query | ||
type OrmaRecord, | ||
type OrmaField | ||
} from './types/query/query_result_types' | ||
@@ -19,0 +20,0 @@ |
@@ -108,3 +108,3 @@ import { before, beforeEach } from 'mocha' | ||
query: T | ||
): Promise<OrmaQueryResult<GlobalTestSchema, GlobalTestAliases, T>> => { | ||
): Promise<OrmaQueryResult<GlobalTestSchema, T>> => { | ||
validate_errors([validate_query(query, global_test_schema)]) | ||
@@ -111,0 +111,0 @@ const res = await (orma_query as any)( |
@@ -95,3 +95,3 @@ import { clone, deep_get, drop_last, last } from '../helpers/helpers' | ||
connection_edges: ConnectionEdges = {} | ||
): Promise<OrmaQueryResult<Schema, Aliases, Query>> => { | ||
): Promise<OrmaQueryResult<Schema, Query>> => { | ||
const query = clone(raw_query) // clone query so we can apply macros without mutating the actual input query | ||
@@ -98,0 +98,0 @@ const orma_schema = orma_schema_input as any // this is just because the codebase isnt properly typed |
@@ -8,3 +8,3 @@ import { | ||
import { GetAllEdges } from '../schema/schema_helper_types' | ||
import { OrmaQueryResult } from './query_result_types' | ||
import { OrmaField, OrmaQueryResult, OrmaRecord } from './query_result_types' | ||
@@ -14,3 +14,3 @@ const test = () => { | ||
query: Query | ||
): OrmaQueryResult<GlobalTestSchema, GlobalTestAliases, Query> => '' as any | ||
): OrmaQueryResult<GlobalTestSchema, Query> => '' as any | ||
@@ -213,2 +213,25 @@ { | ||
} | ||
{ | ||
// handles orma record | ||
type TestType = OrmaRecord< | ||
GlobalTestSchema, | ||
'users', | ||
{ | ||
users: { | ||
id: true | ||
} | ||
} | ||
> | ||
const fn = (test_arg: TestType) => { | ||
const id = test_arg.users![0].id | ||
const test: IsEqual<typeof id, number> = true | ||
} | ||
} | ||
{ | ||
// handles orma record | ||
type TestType = OrmaField<GlobalTestSchema, 'users', 'first_name'> | ||
const test: IsEqual<TestType, string | null> = true | ||
} | ||
} |
@@ -153,46 +153,31 @@ import { OrmaSchema } from '../schema/schema_types' | ||
// }> | ||
// export type OrmaQueryResult< | ||
// Schema extends OrmaSchema, | ||
// Query extends object, | ||
// Entity extends GetAllEntities<Schema> = never | ||
// > = | ||
// Omit< | ||
// { | ||
// // should be returned as a result if the key is not a keyword and the value is not a subquery | ||
// -readonly [Key in keyof Query]: Query[Key] extends { | ||
// $from: GetAllEntities<Schema> | ||
// } // if the value has a $from prop, it is always a subquery | ||
// ? | ||
// | OrmaQueryResult<Schema, Query[Key], Query[Key]['$from']>[] | ||
// | undefined | ||
// : Key extends GetAllEntities<Schema> // The other option for a subquery is that the prop is an entity name | ||
// ? Query[Key] extends object // and the value is an object | ||
// ? Exclude<keyof Query[Key], Keyword> extends never // and the value has at least one non-keyword prop | ||
// ? never | ||
// : OrmaQueryResult<Schema, Query[Key], Key>[] | undefined | ||
// : never | ||
// : GetSchemaTypeForField<Schema, Entity, Key, Query[Key]> | ||
// }, | ||
// Keyword | ||
// > | ||
export type OrmaQueryResult< | ||
Schema extends OrmaSchema, | ||
Aliases extends OrmaQueryAliases<Schema>, | ||
Query extends object, | ||
Entity extends GetAllEntities<Schema> = never | ||
> = OrmaRecord<Schema, Entity, Query> | ||
export type OrmaRecord< | ||
Schema extends OrmaSchema, | ||
Entity extends GetAllEntities<Schema>, | ||
Query extends object | ||
> = { | ||
-readonly [Key in keyof Query & | ||
( | ||
| GetAllEntities<Schema> | ||
| GetRootAliases<Schema, Aliases> | ||
)]?: Query[Key] extends { | ||
$from: GetAllEntities<Schema> | ||
} | ||
? OrmaRecord<Schema, Aliases, Query[Key]['$from'], Query[Key]>[] | ||
: Query[Key] extends object | ||
? Key extends GetAllEntities<Schema> | ||
? OrmaRecord<Schema, Aliases, Key, Query[Key]>[] | ||
: never | ||
: never | ||
} | ||
> = Omit< | ||
{ | ||
// should be returned as a result if the key is not a keyword and the value is not a subquery | ||
-readonly [Key in keyof Query]: Query[Key] extends { | ||
$from | ||
} // if the value has a $from prop, it is always a subquery | ||
? OrmaRecord<Schema, Query[Key]['$from'], Query[Key]>[] | undefined | ||
: Query[Key] extends { $escape } // handle escaped constants | ||
? DeepMutable<Query[Key]['$escape']> | ||
: Key extends GetAllEntities<Schema> // The other option for a subquery is that the prop is an entity name | ||
? Query[Key] extends object // and the value is an object | ||
? Exclude<keyof Query[Key], Keyword> extends never // and the value has at least one non-keyword prop | ||
? never | ||
: OrmaRecord<Schema, Key, Query[Key]>[] | undefined | ||
: never | ||
: GetSchemaTypeForField<Schema, Entity, Key, Query[Key]> | ||
}, | ||
Keyword | ||
> | ||
@@ -212,39 +197,62 @@ type GetSchemaTypeForField< | ||
export type OrmaRecord< | ||
export type OrmaField< | ||
Schema extends OrmaSchema, | ||
Aliases extends OrmaQueryAliases<Schema>, | ||
Entity extends GetAllEntities<Schema>, | ||
Subquery extends object | ||
> = { | ||
-readonly [Key in keyof Subquery & | ||
( | ||
| GetAllEntities<Schema> | ||
| GetFields<Schema, Entity> | ||
| GetAliases<Schema, Aliases, Entity> | ||
)]: Subquery[Key] extends { | ||
$from: GetAllEntities<Schema> | ||
} | ||
? | ||
| OrmaRecord< | ||
Schema, | ||
Aliases, | ||
Subquery[Key]['$from'], | ||
Subquery[Key] | ||
>[] | ||
| undefined // subquery with $from | ||
: Subquery[Key] extends { $escape } | ||
? DeepMutable<Subquery[Key]['$escape']> | ||
: Subquery[Key] extends true | ||
? Key extends GetFields<Schema, Entity> | ||
? GetFieldType<Schema, Entity, Key> // field_name: true | ||
: "Unrecognized field name for value 'true'" | ||
: Subquery[Key] extends GetFields<Schema, Entity> | ||
? GetFieldType<Schema, Entity, Subquery[Key]> // renamed_field: 'field_name' | ||
: Key extends GetAllEntities<Schema> | ||
? Subquery[Key] extends object | ||
? OrmaRecord<Schema, Aliases, Key, Subquery[Key]>[] | undefined // subquery with no $from | ||
: any | ||
: any // unhandled case, like {$sum: 'quantity'} | ||
} | ||
Field extends GetFields<Schema, Entity> | ||
> = GetFieldType<Schema, Entity, Field> | ||
// export type OrmaQueryResult< | ||
// Schema extends OrmaSchema, | ||
// Aliases extends OrmaQueryAliases<Schema>, | ||
// Query extends object | ||
// > = Omit< | ||
// { | ||
// -readonly [Key in keyof Query]?: Query[Key] extends { | ||
// $from: GetAllEntities<Schema> | ||
// } | ||
// ? OrmaRecord<Schema, Aliases, Query[Key]['$from'], Query[Key]>[] | ||
// : Query[Key] extends object | ||
// ? Key extends GetAllEntities<Schema> | ||
// ? OrmaRecord<Schema, Aliases, Key, Query[Key]>[] | ||
// : never | ||
// : never | ||
// }, | ||
// Keyword | ||
// > | ||
// export type OrmaRecord< | ||
// Schema extends OrmaSchema, | ||
// Aliases extends OrmaQueryAliases<Schema>, | ||
// Entity extends GetAllEntities<Schema>, | ||
// Subquery extends object | ||
// > = Omit< | ||
// { | ||
// -readonly [Key in keyof Subquery]: Subquery[Key] extends { | ||
// $from: GetAllEntities<Schema> | ||
// } | ||
// ? | ||
// | OrmaRecord< | ||
// Schema, | ||
// Aliases, | ||
// Subquery[Key]['$from'], | ||
// Subquery[Key] | ||
// >[] | ||
// | undefined // subquery with $from | ||
// : Subquery[Key] extends { $escape } | ||
// ? DeepMutable<Subquery[Key]['$escape']> | ||
// : Subquery[Key] extends true | ||
// ? Key extends GetFields<Schema, Entity> | ||
// ? GetFieldType<Schema, Entity, Key> // field_name: true | ||
// : "Unrecognized field name for value 'true'" | ||
// : Subquery[Key] extends GetFields<Schema, Entity> | ||
// ? GetFieldType<Schema, Entity, Subquery[Key]> // renamed_field: 'field_name' | ||
// : Key extends GetAllEntities<Schema> | ||
// ? Subquery[Key] extends object | ||
// ? OrmaRecord<Schema, Aliases, Key, Subquery[Key]>[] | undefined // subquery with no $from | ||
// : any | ||
// : any // unhandled case, like {$sum: 'quantity'} | ||
// }, | ||
// Keyword | ||
// > | ||
/* | ||
@@ -251,0 +259,0 @@ |
@@ -106,36 +106,2 @@ // generate type for orma schema which plays well with as const declarations | ||
// export type GetParentEdgesForFields< | ||
// Schema extends OrmaSchema, | ||
// Entity extends GetAllEntities<Schema>, | ||
// Fields extends GetFields<Schema, Entity> | ||
// > = Fields extends GetFields<Schema, Entity> // map over fields | ||
// ? Schema[Entity][Fields] extends { references: any } // filter fields to only include ones with foreign keys | ||
// ? { | ||
// from_entity: Entity | ||
// to_entity: GetStringKeys<Schema[Entity][Fields]['references']> // pull out entity from { references: { ... }} | ||
// from_field: Fields | ||
// to_field: GetStringKeys< | ||
// Schema[Entity][Fields]['references'][GetStringKeys< | ||
// Schema[Entity][Fields]['references'] | ||
// >] | ||
// > // pull out field from references objecs | ||
// } | ||
// : never | ||
// : never | ||
// Fields extends any // map over fields | ||
// ? Schema[Entity][Fields] extends { references: any } // filter fields to only include ones with foreign keys | ||
// ? { | ||
// from_entity: Entity | ||
// to_entity: GetStringKeys<Schema[Entity][Fields]['references']> // pull out entity from { references: { ... }} | ||
// from_field: Fields | ||
// to_field: GetStringKeys< | ||
// Schema[Entity][Fields]['references'][GetStringKeys< | ||
// Schema[Entity][Fields]['references'] | ||
// >] | ||
// > // pull out field from references objecs | ||
// } | ||
// : never | ||
// : never | ||
export type GetChildEdges< | ||
@@ -173,5 +139,2 @@ Schema extends OrmaSchema, | ||
? { | ||
// from_entity: Edges['from_entity'] extends GetAllEntities<Schema> | ||
// ? Edges['from_entity'] | ||
// : never | ||
from_field: Edges['from_field'] extends GetFields< | ||
@@ -186,8 +149,2 @@ Schema, | ||
: never | ||
// to_field: Edges['to_field'] extends GetFields< | ||
// Schema, | ||
// GetAllEntities<Schema> | ||
// > | ||
// ? Edges['to_field'] | ||
// : never | ||
} | ||
@@ -201,2 +158,7 @@ : never | ||
/* | ||
So this type actually used to be split into 3 for readability, but it was causing performance issues | ||
so I uglified it, endowing it with lots of repetition and making it hard to follow. Pretty sure its faster | ||
now though. | ||
*/ | ||
export type GetFieldType< | ||
@@ -206,20 +168,24 @@ Schema extends OrmaSchema, | ||
Field extends GetFields<Schema, Entity> | ||
> = GetFieldType2<Schema['$entities'][Entity]['$fields'][Field]> | ||
> = Field extends GetFields<Schema, Entity> | ||
? AddNull< | ||
Schema['$entities'][Entity]['$fields'][Field] extends { | ||
$data_type: 'enum' | ||
$enum_values: readonly any[] | ||
} | ||
? Schema['$entities'][Entity]['$fields'][Field]['$enum_values'][number] | ||
: FieldTypeStringToType< | ||
MysqlToTypescriptTypeString< | ||
NonNullable< | ||
Schema['$entities'][Entity]['$fields'][Field]['$data_type'] | ||
> | ||
> | ||
>, | ||
Schema['$entities'][Entity]['$fields'][Field]['$not_null'] extends true | ||
? false | ||
: true | ||
> | ||
: never | ||
// handle nullable | ||
type GetFieldType2<FieldSchema extends OrmaFieldSchema> = | ||
FieldSchema['$not_null'] extends true | ||
? GetFieldType3<FieldSchema> | ||
: GetFieldType3<FieldSchema> | null | ||
type AddNull<T, AddNull extends boolean> = AddNull extends true ? T | null : T | ||
// handle enum | ||
type GetFieldType3<FieldSchema extends OrmaFieldSchema> = FieldSchema extends { | ||
$data_type: 'enum' | ||
$enum_values: readonly any[] | ||
} | ||
? FieldSchema['$enum_values'][number] | ||
: FieldTypeStringToType< | ||
MysqlToTypescriptTypeString<NonNullable<FieldSchema['$data_type']>> | ||
> | ||
export type GetFieldSchema< | ||
@@ -249,46 +215,2 @@ Schema extends OrmaSchema, | ||
// This type is equivalent to | ||
// fields.filter(field => schema.entities[entity].fields[field][SchemaProp] === value) | ||
// but typescript doesn't have higher kinded types which would allow a filter type, | ||
// so we need this massive, badly abstracted monstrosity instead. | ||
export type FilterFieldsBySchemaProp< | ||
Schema extends OrmaSchema, | ||
Entity extends GetAllEntities<Schema>, | ||
SchemaProp extends string, | ||
value | ||
> = FilterFieldsBySchemaPropWithFieldsExplicit< | ||
Schema, | ||
Entity, | ||
GetFields<Schema, Entity>, | ||
SchemaProp, | ||
value | ||
> | ||
// type distribution of a union (in the case Fields) via a ternary operator only seems to work if the | ||
// union is passed directly in to the type, but not if its generated (e.g. by GetFields<> type). | ||
// So we need this intermediary layer to make it work. I hate typescript. | ||
type FilterFieldsBySchemaPropWithFieldsExplicit< | ||
Schema extends OrmaSchema, | ||
Entity extends GetAllEntities<Schema>, | ||
Fields extends GetFields<Schema, Entity>, | ||
SchemaProp extends string, | ||
value | ||
> = Fields extends any | ||
? FieldSchemaPropEq<Schema, Entity, Fields, SchemaProp, value> extends true | ||
? Fields | ||
: never //FilterFieldBySchemaProp<Schema, Entity, Fields, SchemaProp, value> | ||
: never | ||
export type FieldSchemaPropEq< | ||
Schema extends OrmaSchema, | ||
Entity extends GetAllEntities<Schema>, | ||
Field extends GetFields<Schema, Entity>, | ||
SchemaProp extends string, | ||
value | ||
> = GetFieldSchema<Schema, Entity, Field> extends { | ||
[prop in SchemaProp]: value | ||
} | ||
? IsEqual<GetFieldSchema<Schema, Entity, Field>[SchemaProp], value> | ||
: false | ||
type OrmaForeignKey = NonNullable< | ||
@@ -295,0 +217,0 @@ OrmaSchema['$entities'][string]['$foreign_keys'] |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
40684
1661667