Comparing version 1.0.241 to 1.0.242
@@ -6,2 +6,2 @@ import { GlobalTestMutation, GlobalTestSchema } from '../test_data/global_test_schema'; | ||
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, T, never>>; | ||
export declare const test_query: <T extends Record<string, any>>(query: T) => Promise<OrmaQueryResult<GlobalTestSchema, T>>; |
@@ -37,3 +37,3 @@ "use strict"; | ||
const query = (0, exports.get_delete_verification_query)(orma_schema, mutation_plan.guid_map, mutation_plan.mutation_pieces); | ||
const results = await (0, query_1.orma_query)(query, orma_schema, mysql_function); | ||
const results = (await (0, query_1.orma_query)(query, orma_schema, mysql_function)); | ||
const blocking_pieces = (0, exports.get_mutation_pieces_blocing_delete)(orma_schema, mutation_plan.mutation_pieces, mutation_plan.guid_map, results); | ||
@@ -40,0 +40,0 @@ const errors = (0, exports.get_delete_errors_from_blocking_rows)(orma_schema, blocking_pieces); |
@@ -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, Query, never>>; | ||
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>>; | ||
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; |
@@ -0,20 +1,15 @@ | ||
import { DeepMutable, GetAllEntities, GetFields, GetFieldType, Keyword } from '../schema/schema_helper_types'; | ||
import { OrmaSchema } from '../schema/schema_types'; | ||
import { DeepMutable, GetAllEntities, GetFields, GetFieldType, Keyword } from '../schema/schema_helper_types'; | ||
import { GetAliases, OrmaQueryAliases } from './query_types'; | ||
export type OrmaQueryResult<Schema extends OrmaSchema, Query extends object, Entity extends GetAllEntities<Schema> = never> = Omit<{ | ||
-readonly [Key in keyof Query]: Query[Key] extends { | ||
export type OrmaQueryResult<Schema extends OrmaSchema, Query extends object> = Omit<{ | ||
-readonly [Key in keyof Query]?: Query[Key] extends { | ||
$from: GetAllEntities<Schema>; | ||
} ? OrmaQueryResult<Schema, Query[Key], Query[Key]['$from']>[] | undefined : Query[Key] extends { | ||
$escape: any; | ||
} ? DeepMutable<Query[Key]['$escape']> : Key extends GetAllEntities<Schema> ? Query[Key] extends object ? Exclude<keyof Query[Key], Keyword> extends never ? never : OrmaQueryResult<Schema, Query[Key], Key>[] | undefined : never : GetSchemaTypeForField<Schema, Entity, Key, Query[Key]>; | ||
} ? OrmaRecord<Schema, Query[Key]['$from'], Query[Key]>[] : Query[Key] extends object ? Key extends GetAllEntities<Schema> ? OrmaRecord<Schema, Key, Query[Key]>[] : never : never; | ||
}, 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 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 { | ||
export type OrmaRecord<Schema extends OrmaSchema, 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[Key] extends { | ||
} ? OrmaRecord<Schema, Subquery[Key]['$from'], Subquery[Key]>[] | undefined : 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, Key, Subquery[Key]>[] | undefined : any : Subquery[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<Subquery[Key]['$escape']> : any; | ||
}, Keyword>; | ||
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 | ||
// > | ||
/* | ||
Subquery[Key] extends { $escape } | ||
? Subquery[Key]['$escape'] | ||
*/ |
@@ -152,3 +152,3 @@ "use strict"; | ||
(_1 = result.posts) === null || _1 === void 0 ? void 0 : _1.map(post => { | ||
const test = true; | ||
// const test: IsEqual<typeof post.my_title, 123> = true | ||
}); | ||
@@ -155,0 +155,0 @@ } |
@@ -53,7 +53,9 @@ import { OrmaSchema } from '../schema/schema_types'; | ||
}; | ||
export type QueryField<Schema extends OrmaSchema, Aliases extends OrmaQueryAliases<Schema>, Entity extends GetAllEntities<Schema>> = boolean | Expression<Schema, Aliases, Entity>; | ||
type QueryAliasedField<Schema extends OrmaSchema, Aliases extends OrmaQueryAliases<Schema>, Entity extends GetAllEntities<Schema>> = Expression<Schema, Aliases, Entity>; | ||
export type Expression<Schema extends OrmaSchema, Aliases extends OrmaQueryAliases<Schema>, Entity extends GetAllEntities<Schema>> = ExpressionFunction<Schema, Aliases, Entity, keyof typeof sql_function_definitions> | GetFields<Schema, Entity> | GetAliases<Schema, Aliases, Entity> | { | ||
$escape: any; | ||
export type QueryField<Schema extends OrmaSchema, Aliases extends OrmaQueryAliases<Schema>, Entity extends GetAllEntities<Schema>> = boolean | Expression<Schema, Aliases, Entity> | { | ||
$escape: number | string | any[] | Record<string, any>; | ||
}; | ||
type QueryAliasedField<Schema extends OrmaSchema, Aliases extends OrmaQueryAliases<Schema>, Entity extends GetAllEntities<Schema>> = Expression<Schema, Aliases, Entity> | { | ||
$escape: number | string | any[] | Record<string, any>; | ||
}; | ||
export type Expression<Schema extends OrmaSchema, Aliases extends OrmaQueryAliases<Schema>, Entity extends GetAllEntities<Schema>> = ExpressionFunction<Schema, Aliases, Entity, keyof typeof sql_function_definitions> | GetFields<Schema, Entity> | GetAliases<Schema, Aliases, Entity>; | ||
type ExpressionFunction<Schema extends OrmaSchema, Aliases extends OrmaQueryAliases<Schema>, Entity extends GetAllEntities<Schema>, FunctionNames extends string> = FunctionNames extends string ? { | ||
@@ -60,0 +62,0 @@ [Key in FunctionNames]: Expression<Schema, Aliases, Entity>; |
@@ -7,11 +7,8 @@ import { mysql_to_typescript_types } from '../../schema/introspector'; | ||
export type Keyword = `$${string}`; | ||
export type DeepMutable<T> = T extends Function ? T : { | ||
export type DeepMutable<T> = T extends Function ? T : T extends object ? { | ||
-readonly [P in keyof T]: DeepMutable<T[P]>; | ||
}; | ||
} : T; | ||
export type GetAllEntities<Schema extends OrmaSchema> = Extract<keyof Schema['$entities'], string>; | ||
export type GetFields<Schema extends OrmaSchema, Entity extends GetAllEntities<Schema>> = Entity extends GetAllEntities<Schema> ? keyof Schema['$entities'][Entity]['$fields'] : never; | ||
export type GetFieldNotNull<Schema extends OrmaSchema, Entity extends GetAllEntities<Schema>, Field extends GetFields<Schema, Entity>> = Schema['$entities'][Entity]['$fields'][Field]['$not_null'] extends true ? true : false; | ||
export type GetFieldsByRequired<Schema extends OrmaSchema, Entity extends GetAllEntities<Schema>, IsRequired extends boolean> = GetFieldsByRequired2<Schema, Entity, GetFields<Schema, Entity>, IsRequired>; | ||
type GetFieldsByRequired2<Schema extends OrmaSchema, Entity extends GetAllEntities<Schema>, Fields extends GetFields<Schema, Entity>, IsRequired extends boolean> = Fields extends GetFields<Schema, Entity> ? GetFieldIsRequired<Schema['$entities'][Entity]['$fields'][Fields], Fields, GetParentEdges<Schema, Entity>['from_field']> extends IsRequired ? Fields : never : never; | ||
export type GetFieldIsRequired<FieldSchema extends OrmaFieldSchema, Field extends any, ForeignKeyFields extends any> = ForeignKeyFields extends Field ? false : FieldSchema['$not_null'] extends true ? FieldSchema['$auto_increment'] extends true ? false : FieldSchema['$default'] extends OrmaFieldSchema['$default'] ? false : true : false; | ||
export type GetParentEdges<Schema extends OrmaSchema, Entities extends GetAllEntities<Schema>> = Entities extends GetAllEntities<Schema> ? ForeignKeyToEdge<Schema, Entities, NonNullable<Schema['$entities'][Entities]['$foreign_keys']>[number]> : never; | ||
@@ -18,0 +15,0 @@ export type GetChildEdges<Schema extends OrmaSchema, Entities extends GetAllEntities<Schema>> = CacheToEdge<Schema, NonNullable<Schema['$cache']>['$reversed_foreign_keys'][Entities][number]>; |
"use strict"; | ||
// generate type for orma schema which plays well with as const declarations | ||
Object.defineProperty(exports, "__esModule", { value: true }); |
@@ -64,9 +64,1 @@ "use strict"; | ||
} | ||
{ | ||
// gets regular required fields, but not auto increment fields. Ignores nullable fields | ||
const test1 = true; | ||
} | ||
{ | ||
// ignores fields with a default and foreign keys | ||
const test1 = true; | ||
} |
{ | ||
"name": "orma", | ||
"version": "1.0.241", | ||
"version": "1.0.242", | ||
"description": "A declarative relational syncronous orm", | ||
@@ -5,0 +5,0 @@ "main": "build/index.js", |
@@ -54,3 +54,7 @@ import { mutation_path_to_entity } from '../..' | ||
) | ||
const results = await orma_query(query, orma_schema, mysql_function) | ||
const results = (await orma_query( | ||
query, | ||
orma_schema, | ||
mysql_function | ||
)) as any | ||
const blocking_pieces = get_mutation_pieces_blocing_delete( | ||
@@ -57,0 +61,0 @@ orma_schema, |
@@ -186,3 +186,3 @@ import { | ||
result.posts?.map(post => { | ||
const test: IsEqual<typeof post.my_title, 123> = true | ||
// const test: IsEqual<typeof post.my_title, 123> = true | ||
}) | ||
@@ -216,3 +216,2 @@ } | ||
GlobalTestSchema, | ||
GlobalTestAliases, | ||
'users', | ||
@@ -219,0 +218,0 @@ { |
@@ -1,2 +0,1 @@ | ||
import { OrmaSchema } from '../schema/schema_types' | ||
import { | ||
@@ -9,169 +8,18 @@ DeepMutable, | ||
} from '../schema/schema_helper_types' | ||
import { | ||
GetAliases, | ||
GetSubqueryProps, | ||
OrmaQuery, | ||
OrmaQueryAliases, | ||
OrmaSubquery, | ||
SubqueryObj, | ||
GetRootAliases, | ||
OrmaAliasedSubquery, | ||
} from './query_types' | ||
import { OrmaSchema } from '../schema/schema_types' | ||
type SimplifyType<T> = T extends object | ||
? { [K in keyof T]: SimplifyType<T[K]> } | ||
: T | ||
// export type OrmaQueryResult< | ||
// Schema extends OrmaSchema, | ||
// Aliases extends OrmaQueryAliases<Schema>, | ||
// Query extends OrmaQuery<Schema, Aliases> | ||
// > = SimplifyType<QueryResultComplex<Schema, Aliases, Query>> | ||
// export type OrmaRecord< | ||
// Schema extends OrmaSchema, | ||
// Aliases extends OrmaQueryAliases<Schema>, | ||
// Entity extends GetAllEntities<Schema>, | ||
// Subquery extends SubqueryObj<Schema, Aliases, Entity> | ||
// > = SimplifyType<OrmaRecordComplex<Schema, Aliases, Entity, Subquery>> | ||
// /** | ||
// * This has a complicated internal representation in the typescript compiler. This means | ||
// * 1. the on-hover tooltip is hard to read | ||
// * 2. can cause the ts compiler to crash when using .map() in a result (ts 5.0.4, no idea why). | ||
// * Therefore we simplify the type, which fixes these problems | ||
// */ | ||
// export type QueryResultComplex< | ||
// Schema extends OrmaSchema, | ||
// Aliases extends OrmaQueryAliases<Schema>, | ||
// Query extends OrmaQuery<Schema, Aliases> | ||
// > = { | ||
// [Key in keyof Query]?: Key extends GetAllEntities<Schema> | ||
// ? Query[Key] extends OrmaSubquery<Schema, Aliases, Key> | ||
// ? OrmaRecordComplex<Schema, Aliases, Key, Query[Key]>[] | ||
// : never | ||
// : never | ||
// } & { | ||
// [Key in keyof Query & | ||
// GetRootAliases< | ||
// Schema, | ||
// Aliases | ||
// >]?: Query[Key] extends OrmaAliasedSubquery< | ||
// Schema, | ||
// Aliases, | ||
// GetAllEntities<Schema> | ||
// > | ||
// ? OrmaRecordComplex<Schema, Aliases, Query[Key]['$from'], Query[Key]>[] | ||
// : never | ||
// } | ||
// type OrmaRecordComplex< | ||
// Schema extends OrmaSchema, | ||
// Aliases extends OrmaQueryAliases<Schema>, | ||
// Entity extends GetAllEntities<Schema>, | ||
// Subquery extends SubqueryObj<Schema, Aliases, Entity> | ||
// > = { | ||
// [Key in keyof Subquery & | ||
// GetFields<Schema, Entity>]: Subquery[Key] extends true | ||
// ? GetFieldType<Schema, Entity, Key> | ||
// : Subquery[Key] extends GetFields<Schema, Entity> | ||
// ? GetFieldType<Schema, Entity, Subquery[Key]> | ||
// : any | ||
// } & { | ||
// [Key in keyof Subquery & | ||
// GetSubqueryProps<Schema, Entity>]?: Subquery[Key] extends OrmaSubquery< | ||
// Schema, | ||
// Aliases, | ||
// Key | ||
// > | ||
// ? OrmaRecordComplex<Schema, Aliases, Key, Subquery[Key]>[] | ||
// : never | ||
// } & { | ||
// [Key in keyof Subquery & | ||
// GetAliases<Schema, Aliases, Entity>]: Subquery[Key] extends GetFields< | ||
// Schema, | ||
// Entity | ||
// > | ||
// ? GetFieldType<Schema, Entity, Subquery[Key]> | ||
// : Subquery[Key] extends OrmaAliasedSubquery< | ||
// Schema, | ||
// Aliases, | ||
// GetAllEntities<Schema> | ||
// > | ||
// ? OrmaRecordComplex< | ||
// Schema, | ||
// Aliases, | ||
// Subquery[Key]['$from'], | ||
// Subquery[Key] | ||
// >[] | ||
// : any | ||
// } | ||
// type SimpleQuery< | ||
// Schema extends OrmaSchema, | ||
// Aliases extends OrmaQueryAliases<Schema> | ||
// > = { | ||
// [Key in GetAllEntities<Schema>]?: SimpleSubquery<Schema, Aliases, Key> | ||
// } | ||
// type SimpleSubquery< | ||
// Schema extends OrmaSchema, | ||
// Aliases extends OrmaQueryAliases<Schema>, | ||
// Entity extends GetAllEntities<Schema> | ||
// > = { | ||
// [Key in GetFields<Schema, Entity>]?: true | ||
// } | ||
// type SimpleSubquery< | ||
// Schema extends OrmaSchema, | ||
// Aliases extends OrmaQueryAliases<Schema>, | ||
// Entity extends GetAllEntities<Schema> | ||
// > = {} | ||
// --------------------------------------------------------- | ||
// export type OrmaQueryResult< | ||
// Schema extends OrmaSchema, | ||
// Aliases extends OrmaQueryAliases<Schema>, | ||
// Query extends object | ||
// > = SimplifyType<{ | ||
// -readonly [Key in keyof Query & | ||
// ( | ||
// | GetAllEntities<Schema> | ||
// | GetRootAliases<Schema, Aliases> | ||
// )]: Query[Key] extends { | ||
// $from: GetAllEntities<Schema> | ||
// } | ||
// ? | ||
// | OrmaRecord<Schema, Aliases, Query[Key]['$from'], Query[Key]>[] | ||
// | undefined | ||
// : Key extends GetAllEntities<Schema> | ||
// ? Query[Key] extends object | ||
// ? OrmaRecord<Schema, Aliases, Key, Query[Key]>[] | undefined | ||
// : never | ||
// : never | ||
// }> | ||
export type OrmaQueryResult< | ||
Schema extends OrmaSchema, | ||
Query extends object, | ||
Entity extends GetAllEntities<Schema> = never | ||
Query extends object | ||
> = 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 { | ||
-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 | ||
: 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 | ||
: OrmaQueryResult<Schema, Query[Key], Key>[] | undefined | ||
} | ||
? OrmaRecord<Schema, Query[Key]['$from'], Query[Key]>[] | ||
: Query[Key] extends object | ||
? Key extends GetAllEntities<Schema> | ||
? OrmaRecord<Schema, Key, Query[Key]>[] | ||
: never | ||
: GetSchemaTypeForField<Schema, Entity, Key, Query[Key]> | ||
: never | ||
}, | ||
@@ -181,51 +29,30 @@ 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 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 with $from | ||
: Subquery[Key] extends { $escape } // handle escaped constants | ||
? 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'} | ||
} | ||
> = Omit< | ||
{ | ||
-readonly [Key in keyof Subquery]: Subquery[Key] extends { | ||
$from: GetAllEntities<Schema> | ||
} | ||
? | ||
| OrmaRecord<Schema, Subquery[Key]['$from'], Subquery[Key]>[] | ||
| undefined // subquery with $from | ||
: 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, Key, Subquery[Key]>[] | undefined // subquery with no $from | ||
: any | ||
: Subquery[Key] extends { $escape } | ||
? DeepMutable<Subquery[Key]['$escape']> | ||
: any // unhandled case, like {$sum: 'quantity'} | ||
}, | ||
Keyword | ||
> | ||
@@ -237,61 +64,1 @@ export type OrmaField< | ||
> = 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 | ||
// > | ||
/* | ||
Subquery[Key] extends { $escape } | ||
? Subquery[Key]['$escape'] | ||
*/ |
@@ -159,3 +159,6 @@ import { OrmaSchema } from '../schema/schema_types' | ||
Entity extends GetAllEntities<Schema> | ||
> = boolean | Expression<Schema, Aliases, Entity> | ||
> = | ||
| boolean | ||
| Expression<Schema, Aliases, Entity> | ||
| { $escape: number | string | any[] | Record<string, any> } | ||
@@ -166,3 +169,5 @@ type QueryAliasedField< | ||
Entity extends GetAllEntities<Schema> | ||
> = Expression<Schema, Aliases, Entity> | ||
> = | ||
| Expression<Schema, Aliases, Entity> | ||
| { $escape: number | string | any[] | Record<string, any> } | ||
@@ -182,3 +187,2 @@ export type Expression< | ||
| GetAliases<Schema, Aliases, Entity> | ||
| { $escape: any } | ||
@@ -185,0 +189,0 @@ type ExpressionFunction< |
@@ -11,3 +11,2 @@ import { | ||
GetFields, | ||
GetFieldsByRequired, | ||
GetFieldType, | ||
@@ -108,14 +107,2 @@ GetParentEdges, | ||
> = true | ||
} | ||
{ | ||
type T = GetFieldsByRequired<typeof global_test_schema, 'users', true> | ||
// gets regular required fields, but not auto increment fields. Ignores nullable fields | ||
const test1: IsEqual<T, 'email'> = true | ||
} | ||
{ | ||
type T = GetFieldsByRequired<typeof global_test_schema, 'posts', true> | ||
// ignores fields with a default and foreign keys | ||
const test1: IsEqual<T, 'title'> = true | ||
} | ||
} |
@@ -1,8 +0,3 @@ | ||
// generate type for orma schema which plays well with as const declarations | ||
import { Edge } from '../../helpers/schema_helpers' | ||
import { mysql_to_typescript_types } from '../../schema/introspector' | ||
import { ForeignKeyEdge, OrmaSchema } from '../schema/schema_types' | ||
import { IsEqual } from '../helper_types' | ||
import { Schema } from 'jsonschema' | ||
@@ -30,3 +25,5 @@ export type DeepReadonly<T> = T extends (infer R)[] | ||
? T | ||
: { -readonly [P in keyof T]: DeepMutable<T[P]> } | ||
: T extends object | ||
? { -readonly [P in keyof T]: DeepMutable<T[P]> } | ||
: T | ||
@@ -58,40 +55,2 @@ // basic structure | ||
// get a union of fields that either are nullable or are not nullable | ||
export type GetFieldsByRequired< | ||
Schema extends OrmaSchema, | ||
Entity extends GetAllEntities<Schema>, | ||
IsRequired extends boolean | ||
> = GetFieldsByRequired2<Schema, Entity, GetFields<Schema, Entity>, IsRequired> | ||
type GetFieldsByRequired2< | ||
Schema extends OrmaSchema, | ||
Entity extends GetAllEntities<Schema>, | ||
Fields extends GetFields<Schema, Entity>, | ||
IsRequired extends boolean | ||
> = Fields extends GetFields<Schema, Entity> | ||
? GetFieldIsRequired< | ||
Schema['$entities'][Entity]['$fields'][Fields], | ||
Fields, | ||
GetParentEdges<Schema, Entity>['from_field'] | ||
> extends IsRequired | ||
? Fields | ||
: never | ||
: never | ||
// a field is required if it is not nullable, and there is no auto_increment or default. Also foreign keys are considered | ||
// not required, since the logic to require them is more complicated and handled elsewhere | ||
export type GetFieldIsRequired< | ||
FieldSchema extends OrmaFieldSchema, | ||
Field extends any, | ||
ForeignKeyFields extends any | ||
> = ForeignKeyFields extends Field | ||
? false | ||
: FieldSchema['$not_null'] extends true | ||
? FieldSchema['$auto_increment'] extends true | ||
? false | ||
: FieldSchema['$default'] extends OrmaFieldSchema['$default'] | ||
? false | ||
: true | ||
: false | ||
export type GetParentEdges< | ||
@@ -98,0 +57,0 @@ Schema extends OrmaSchema, |
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
1652625
40473