/
contracts.ts
99 lines (84 loc) 路 2.16 KB
/
contracts.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import type { Builder } from './builder/builder'
import type { FactoryModel } from './model'
import type { faker } from '@faker-js/faker'
import type { Knex } from 'knex'
type Optional<T, K extends keyof T> = Pick<Partial<T>, K> & Omit<T, K>
export type CasingStrategy = 'camel' | 'snake' | 'none'
/**
* Callback that must be passed to the `defineFactory` function.
*/
export type DefineFactoryCallback<T> = (args: { faker: typeof faker }) => {
[K in keyof T]: T[K] | (() => T[K] | Promise<T[K]>)
}
/**
* Callback that must be passed to the `state` function.
*/
export type DefineStateCallback<T> = (attributes: T) => Partial<T>
/**
* The Factorio configuration.
*/
export interface FactorioConfig {
database: Knex.Config
/**
* Configure the casing conversion for the database operations
*/
casing?: {
/**
* Casing to which the keys will be converted before inserting into the database
*
* Default: `snake`
*/
insert: CasingStrategy
/**
* Casing to which the keys will be converted before returning
*
* Default: `camel`
*/
return: CasingStrategy
}
}
/**
* Extract generics from FactoryModel class
*/
export type FactoryExtractGeneric<
Factory extends FactoryModel<any, any>,
Extracted extends 'states' | 'model'
> = Factory extends FactoryModel<infer Model, infer States>
? Extracted extends 'states'
? States
: Model
: never
/**
* Callback that must be passed to the `with` function.
*/
export type WithCallback = (builder: Builder<any>) => void
/**
* Possible relations type
*/
export enum RelationType {
HasOne = 'has-one',
HasMany = 'has-many',
BelongsTo = 'belongs-to',
}
/**
* Metadata for a relationship.
*/
export interface RelationshipMeta {
type: RelationType
/**
* If no localKey is defined, we gonna assume that it's "id"
*/
localKey: string
/**
* If no foreignKey is defined, we gonna assume that it's "{tableName}_id"
*/
foreignKey: string
/**
* Reference to the relation factory
*/
factory: () => Builder<any, any, any>
}
export type RelationshipMetaOptions = Optional<
Omit<RelationshipMeta, 'type' | 'factory'>,
'foreignKey' | 'localKey'
>