@smartive/graphql-magic 3.1.0 → 5.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +1 -6
- package/README.md +46 -0
- package/dist/cjs/index.cjs +260 -196
- package/dist/esm/api/execute.d.ts +10 -0
- package/dist/esm/api/execute.js +32 -0
- package/dist/esm/api/execute.js.map +1 -0
- package/dist/esm/api/index.d.ts +1 -0
- package/dist/esm/api/index.js +3 -0
- package/dist/esm/api/index.js.map +1 -0
- package/dist/esm/client/queries.d.ts +4 -1
- package/dist/esm/client/queries.js +7 -4
- package/dist/esm/client/queries.js.map +1 -1
- package/dist/esm/context.d.ts +1 -4
- package/dist/esm/db/generate.js +21 -13
- package/dist/esm/db/generate.js.map +1 -1
- package/dist/esm/generate/generate.d.ts +1 -1
- package/dist/esm/generate/generate.js +17 -22
- package/dist/esm/generate/generate.js.map +1 -1
- package/dist/esm/generate/mutations.d.ts +1 -1
- package/dist/esm/generate/utils.d.ts +10 -1
- package/dist/esm/generate/utils.js.map +1 -1
- package/dist/esm/index.d.ts +2 -2
- package/dist/esm/index.js +2 -2
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/migrations/generate.d.ts +1 -1
- package/dist/esm/migrations/generate.js +82 -89
- package/dist/esm/migrations/generate.js.map +1 -1
- package/dist/esm/models/index.d.ts +2 -0
- package/dist/esm/models/index.js +4 -0
- package/dist/esm/models/index.js.map +1 -0
- package/dist/esm/models/models.d.ts +192 -0
- package/dist/esm/models/models.js +2 -0
- package/dist/esm/models/models.js.map +1 -0
- package/dist/esm/models/utils.d.ts +205 -0
- package/dist/esm/{utils.js → models/utils.js} +69 -21
- package/dist/esm/models/utils.js.map +1 -0
- package/dist/esm/permissions/check.d.ts +1 -1
- package/dist/esm/permissions/check.js +2 -2
- package/dist/esm/permissions/check.js.map +1 -1
- package/dist/esm/permissions/generate.d.ts +1 -1
- package/dist/esm/permissions/generate.js +2 -2
- package/dist/esm/permissions/generate.js.map +1 -1
- package/dist/esm/resolvers/arguments.js +1 -1
- package/dist/esm/resolvers/arguments.js.map +1 -1
- package/dist/esm/resolvers/filters.js +2 -2
- package/dist/esm/resolvers/filters.js.map +1 -1
- package/dist/esm/resolvers/mutations.js +5 -6
- package/dist/esm/resolvers/mutations.js.map +1 -1
- package/dist/esm/resolvers/node.d.ts +1 -1
- package/dist/esm/resolvers/node.js +4 -5
- package/dist/esm/resolvers/node.js.map +1 -1
- package/dist/esm/resolvers/resolver.js +3 -3
- package/dist/esm/resolvers/resolver.js.map +1 -1
- package/dist/esm/resolvers/resolvers.d.ts +1 -1
- package/dist/esm/resolvers/resolvers.js +1 -1
- package/dist/esm/resolvers/resolvers.js.map +1 -1
- package/dist/esm/resolvers/utils.js +1 -1
- package/dist/esm/resolvers/utils.js.map +1 -1
- package/package.json +5 -5
- package/src/api/execute.ts +45 -0
- package/src/api/index.ts +3 -0
- package/src/client/queries.ts +16 -10
- package/src/context.ts +1 -3
- package/src/db/generate.ts +22 -15
- package/src/generate/generate.ts +26 -34
- package/src/generate/mutations.ts +1 -1
- package/src/generate/utils.ts +11 -1
- package/src/index.ts +2 -2
- package/src/migrations/generate.ts +84 -82
- package/src/models/index.ts +4 -0
- package/src/models/models.ts +184 -0
- package/src/models/utils.ts +288 -0
- package/src/permissions/check.ts +3 -3
- package/src/permissions/generate.ts +3 -3
- package/src/resolvers/arguments.ts +1 -1
- package/src/resolvers/filters.ts +2 -2
- package/src/resolvers/mutations.ts +10 -9
- package/src/resolvers/node.ts +6 -6
- package/src/resolvers/resolver.ts +3 -3
- package/src/resolvers/resolvers.ts +2 -2
- package/src/resolvers/utils.ts +1 -1
- package/tests/unit/resolve.spec.ts +4 -19
- package/tests/utils/models.ts +8 -7
- package/tests/utils/server.ts +13 -33
- package/dist/esm/models.d.ts +0 -170
- package/dist/esm/models.js +0 -27
- package/dist/esm/models.js.map +0 -1
- package/dist/esm/utils.d.ts +0 -25
- package/dist/esm/utils.js.map +0 -1
- package/src/models.ts +0 -228
- package/src/utils.ts +0 -187
package/dist/esm/models.d.ts
DELETED
|
@@ -1,170 +0,0 @@
|
|
|
1
|
-
import type { Context } from './context';
|
|
2
|
-
import type { OrderBy } from './resolvers/arguments';
|
|
3
|
-
import type { Directive, Value } from './values';
|
|
4
|
-
export type RawModels = RawModel[];
|
|
5
|
-
export type RawModel = ScalarModel | EnumModel | RawEnumModel | InterfaceModel | ObjectModel | RawObjectModel | JsonObjectModel;
|
|
6
|
-
type BaseModel = {
|
|
7
|
-
name: string;
|
|
8
|
-
plural?: string;
|
|
9
|
-
description?: string;
|
|
10
|
-
};
|
|
11
|
-
export type ScalarModel = BaseModel & {
|
|
12
|
-
type: 'scalar';
|
|
13
|
-
};
|
|
14
|
-
export type EnumModel = BaseModel & {
|
|
15
|
-
type: 'enum';
|
|
16
|
-
values: string[];
|
|
17
|
-
deleted?: true;
|
|
18
|
-
};
|
|
19
|
-
export type RawEnumModel = BaseModel & {
|
|
20
|
-
type: 'raw-enum';
|
|
21
|
-
values: string[];
|
|
22
|
-
};
|
|
23
|
-
export type InterfaceModel = BaseModel & {
|
|
24
|
-
type: 'interface';
|
|
25
|
-
fields: ModelField[];
|
|
26
|
-
};
|
|
27
|
-
export type RawObjectModel = BaseModel & {
|
|
28
|
-
type: 'raw-object';
|
|
29
|
-
fields: ModelField[];
|
|
30
|
-
rawFilters?: {
|
|
31
|
-
name: string;
|
|
32
|
-
type: string;
|
|
33
|
-
list?: boolean;
|
|
34
|
-
nonNull?: boolean;
|
|
35
|
-
}[];
|
|
36
|
-
};
|
|
37
|
-
export type JsonObjectModel = BaseModel & {
|
|
38
|
-
type: 'json-object';
|
|
39
|
-
json: true;
|
|
40
|
-
fields: Pick<ModelField, 'type' | 'name' | 'nonNull'>[];
|
|
41
|
-
};
|
|
42
|
-
export type Entity = Record<string, any>;
|
|
43
|
-
export type Action = 'create' | 'update' | 'delete' | 'restore';
|
|
44
|
-
export type MutationHook = (model: Model, action: Action, when: 'before' | 'after', data: {
|
|
45
|
-
prev: Entity;
|
|
46
|
-
input: Entity;
|
|
47
|
-
normalizedInput: Entity;
|
|
48
|
-
next: Entity;
|
|
49
|
-
}, ctx: Context) => Promise<void>;
|
|
50
|
-
export type ObjectModel = BaseModel & {
|
|
51
|
-
type: 'object';
|
|
52
|
-
interfaces?: string[];
|
|
53
|
-
nonStrict?: boolean;
|
|
54
|
-
queriable?: boolean;
|
|
55
|
-
listQueriable?: boolean;
|
|
56
|
-
creatable?: boolean;
|
|
57
|
-
updatable?: boolean;
|
|
58
|
-
deletable?: boolean;
|
|
59
|
-
displayField?: string;
|
|
60
|
-
defaultOrderBy?: OrderBy;
|
|
61
|
-
fields: ModelField[];
|
|
62
|
-
deleted?: true;
|
|
63
|
-
oldName?: string;
|
|
64
|
-
};
|
|
65
|
-
export type InputObject = {
|
|
66
|
-
name: string;
|
|
67
|
-
type: string;
|
|
68
|
-
nonNull?: boolean;
|
|
69
|
-
};
|
|
70
|
-
export declare const isObjectModel: (model: RawModel) => model is ObjectModel;
|
|
71
|
-
export declare const isEnumModel: (model: RawModel) => model is EnumModel;
|
|
72
|
-
export declare const isRawEnumModel: (model: RawModel) => model is RawEnumModel;
|
|
73
|
-
export declare const isScalarModel: (model: RawModel) => model is ScalarModel;
|
|
74
|
-
export declare const isRawObjectModel: (model: RawModel) => model is RawObjectModel;
|
|
75
|
-
export declare const isJsonObjectModel: (model: RawModel) => model is RawObjectModel;
|
|
76
|
-
export declare const isEnumList: (models: RawModels, field: ModelField) => boolean;
|
|
77
|
-
export declare const and: (...predicates: ((field: ModelField) => boolean)[]) => (field: ModelField) => boolean;
|
|
78
|
-
export declare const not: (predicate: (field: ModelField) => boolean) => (field: ModelField) => boolean;
|
|
79
|
-
export declare const isRelation: ({ relation }: ModelField) => boolean;
|
|
80
|
-
export type VisibleRelationsByRole = Record<string, Record<string, string[]>>;
|
|
81
|
-
export declare const isVisibleRelation: (visibleRelationsByRole: VisibleRelationsByRole, modelName: string, role: string) => ({ name }: Field) => boolean;
|
|
82
|
-
export declare const isToOneRelation: ({ toOne }: ModelField) => boolean;
|
|
83
|
-
export declare const isQueriableField: ({ queriable }: ModelField) => boolean;
|
|
84
|
-
export declare const isRaw: ({ raw }: ModelField) => boolean;
|
|
85
|
-
export declare const isVisible: ({ hidden }: ModelField) => boolean;
|
|
86
|
-
export declare const isSimpleField: (field: ModelField) => boolean;
|
|
87
|
-
export declare const isUpdatable: ({ updatable }: ModelField) => boolean;
|
|
88
|
-
export declare const isCreatable: ({ creatable }: ModelField) => boolean;
|
|
89
|
-
export declare const isQueriableBy: (role: string) => (field: ModelField) => boolean;
|
|
90
|
-
export declare const isUpdatableBy: (role: string) => (field: ModelField) => boolean;
|
|
91
|
-
export declare const isCreatableBy: (role: string) => (field: ModelField) => boolean;
|
|
92
|
-
export declare const actionableRelations: (model: Model, action: 'create' | 'update' | 'filter') => ModelField[];
|
|
93
|
-
export type Field = {
|
|
94
|
-
name: string;
|
|
95
|
-
type: string;
|
|
96
|
-
default?: Value;
|
|
97
|
-
list?: boolean;
|
|
98
|
-
nonNull?: boolean;
|
|
99
|
-
args?: Field[];
|
|
100
|
-
directives?: Directive[];
|
|
101
|
-
};
|
|
102
|
-
export type ModelField = Field & {
|
|
103
|
-
primary?: boolean;
|
|
104
|
-
unique?: boolean;
|
|
105
|
-
filterable?: boolean;
|
|
106
|
-
defaultFilter?: Value;
|
|
107
|
-
searchable?: boolean;
|
|
108
|
-
possibleValues?: Value[];
|
|
109
|
-
orderable?: boolean;
|
|
110
|
-
comparable?: boolean;
|
|
111
|
-
relation?: boolean;
|
|
112
|
-
onDelete?: 'cascade' | 'set-null';
|
|
113
|
-
reverse?: string;
|
|
114
|
-
toOne?: boolean;
|
|
115
|
-
foreignKey?: string;
|
|
116
|
-
queriable?: false;
|
|
117
|
-
queriableBy?: string[];
|
|
118
|
-
creatable?: boolean;
|
|
119
|
-
creatableBy?: string[];
|
|
120
|
-
updatable?: boolean;
|
|
121
|
-
updatableBy?: string[];
|
|
122
|
-
generated?: boolean;
|
|
123
|
-
raw?: boolean;
|
|
124
|
-
json?: boolean;
|
|
125
|
-
dateTimeType?: 'year' | 'date' | 'datetime' | 'year_and_month';
|
|
126
|
-
stringType?: 'email' | 'url' | 'phone';
|
|
127
|
-
floatType?: 'currency' | 'percentage';
|
|
128
|
-
unit?: 'million';
|
|
129
|
-
intType?: 'currency';
|
|
130
|
-
min?: number;
|
|
131
|
-
max?: number;
|
|
132
|
-
tooltip?: string;
|
|
133
|
-
description?: string;
|
|
134
|
-
large?: true;
|
|
135
|
-
maxLength?: number;
|
|
136
|
-
double?: boolean;
|
|
137
|
-
precision?: number;
|
|
138
|
-
scale?: number;
|
|
139
|
-
defaultValue?: string | number | ReadonlyArray<string> | undefined;
|
|
140
|
-
endOfDay?: boolean;
|
|
141
|
-
obfuscate?: true;
|
|
142
|
-
required?: boolean;
|
|
143
|
-
indent?: boolean;
|
|
144
|
-
hidden?: boolean;
|
|
145
|
-
deleted?: true;
|
|
146
|
-
oldName?: string;
|
|
147
|
-
};
|
|
148
|
-
export type Models = Model[];
|
|
149
|
-
export type Model = ObjectModel & {
|
|
150
|
-
fieldsByName: Record<string, ModelField>;
|
|
151
|
-
relations: Relation[];
|
|
152
|
-
relationsByName: Record<string, Relation>;
|
|
153
|
-
reverseRelations: ReverseRelation[];
|
|
154
|
-
reverseRelationsByName: Record<string, ReverseRelation>;
|
|
155
|
-
};
|
|
156
|
-
export type Relation = {
|
|
157
|
-
field: ModelField;
|
|
158
|
-
model: Model;
|
|
159
|
-
reverseRelation: ReverseRelation;
|
|
160
|
-
};
|
|
161
|
-
export type ReverseRelation = {
|
|
162
|
-
name: string;
|
|
163
|
-
type: string;
|
|
164
|
-
foreignKey: string;
|
|
165
|
-
toOne: boolean;
|
|
166
|
-
model: Model;
|
|
167
|
-
field: ModelField;
|
|
168
|
-
fieldModel: Model;
|
|
169
|
-
};
|
|
170
|
-
export {};
|
package/dist/esm/models.js
DELETED
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
export const isObjectModel = (model) => model.type === 'object';
|
|
2
|
-
export const isEnumModel = (model) => model.type === 'enum';
|
|
3
|
-
export const isRawEnumModel = (model) => model.type === 'raw-enum';
|
|
4
|
-
export const isScalarModel = (model) => model.type === 'scalar';
|
|
5
|
-
export const isRawObjectModel = (model) => model.type === 'raw-object';
|
|
6
|
-
export const isJsonObjectModel = (model) => model.type === 'json-object';
|
|
7
|
-
export const isEnumList = (models, field) => field?.list === true && models.find(({ name }) => name === field.type)?.type === 'enum';
|
|
8
|
-
export const and = (...predicates) => (field) => predicates.every((predicate) => predicate(field));
|
|
9
|
-
export const not = (predicate) => (field) => !predicate(field);
|
|
10
|
-
export const isRelation = ({ relation }) => !!relation;
|
|
11
|
-
export const isVisibleRelation = (visibleRelationsByRole, modelName, role) => {
|
|
12
|
-
const whitelist = visibleRelationsByRole[role]?.[modelName];
|
|
13
|
-
return ({ name }) => (whitelist ? whitelist.includes(name) : true);
|
|
14
|
-
};
|
|
15
|
-
export const isToOneRelation = ({ toOne }) => !!toOne;
|
|
16
|
-
export const isQueriableField = ({ queriable }) => queriable !== false;
|
|
17
|
-
export const isRaw = ({ raw }) => !!raw;
|
|
18
|
-
export const isVisible = ({ hidden }) => hidden !== true;
|
|
19
|
-
export const isSimpleField = and(not(isRelation), not(isRaw));
|
|
20
|
-
export const isUpdatable = ({ updatable }) => !!updatable;
|
|
21
|
-
export const isCreatable = ({ creatable }) => !!creatable;
|
|
22
|
-
export const isQueriableBy = (role) => (field) => isQueriableField(field) && (!field.queriableBy || field.queriableBy.includes(role));
|
|
23
|
-
export const isUpdatableBy = (role) => (field) => isUpdatable(field) && (!field.updatableBy || field.updatableBy.includes(role));
|
|
24
|
-
export const isCreatableBy = (role) => (field) => isCreatable(field) && (!field.creatableBy || field.creatableBy.includes(role));
|
|
25
|
-
export const actionableRelations = (model, action) => model.fields.filter(({ relation, ...field }) => relation &&
|
|
26
|
-
field[`${action === 'filter' ? action : action.slice(0, -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
|
2
|
-
export declare const merge: <T>(objects: (false | {
|
|
3
|
-
[name: string]: T;
|
|
4
|
-
})[]) => {
|
|
5
|
-
[name: string]: T;
|
|
6
|
-
};
|
|
7
|
-
export declare const typeToField: (type: string) => string;
|
|
8
|
-
export declare const getModelPlural: (model: ObjectModel | Model) => string;
|
|
9
|
-
export declare const getModelPluralField: (model: Model) => string;
|
|
10
|
-
export declare const getModelSlug: (model: Model) => string;
|
|
11
|
-
export declare const getModelLabelPlural: (model: Model) => string;
|
|
12
|
-
export declare const getModelLabel: (model: Model) => string;
|
|
13
|
-
export declare const getLabel: (s: string) => string;
|
|
14
|
-
export declare const getModels: (rawModels: RawModels) => Models;
|
|
15
|
-
export declare const summonByName: <T extends {
|
|
16
|
-
name: string;
|
|
17
|
-
}>(array: T[], value: string) => T;
|
|
18
|
-
export declare const summonByKey: <T>(array: readonly T[], key: string, value: unknown) => T;
|
|
19
|
-
export declare const summon: <T>(array: readonly T[], cb: any, errorMessage?: string) => T;
|
|
20
|
-
type ForSure<T> = T extends undefined | null ? never : T;
|
|
21
|
-
export declare const it: <T>(object: T) => ForSure<T>;
|
|
22
|
-
export declare const get: <T, U extends keyof ForSure<T>>(object: T, key: U) => ForSure<ForSure<T>[U]>;
|
|
23
|
-
export declare const getString: (v: unknown) => string;
|
|
24
|
-
export declare const retry: <T>(cb: () => Promise<T>, condition: (e: any) => boolean) => Promise<T>;
|
|
25
|
-
export {};
|
package/dist/esm/utils.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"utils.js","sourceRoot":"","sources":["../../src/utils.ts"],"names":[],"mappings":"AAAA,OAAO,MAAM,MAAM,QAAQ,CAAC;AAC5B,OAAO,EAAE,SAAS,EAAE,MAAM,YAAY,CAAC;AACvC,OAAO,SAAS,MAAM,kBAAkB,CAAC;AACzC,OAAO,SAAS,MAAM,YAAY,CAAC;AACnC,OAAO,SAAS,MAAM,kBAAkB,CAAC;AACzC,OAAO,SAAS,MAAM,kB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AAS,IAAI,KAAK,KAAK,IAAI,EAAE;QACzC,OAAO,CAAC,KAAK,EAAE,CAAC;QAChB,MAAM,IAAI,KAAK,CAAC,uBAAuB,MAAM,CAAC,GAAG,CAAC,EAAE,CAAC,CAAC;KACvD;IACD,OAAO,KAA+B,CAAC;AACzC,CAAC,CAAC;AAEF,MAAM,CAAC,MAAM,SAAS,GAAG,CAAC,CAAU,EAAE,EAAE;IACtC,MAAM,CAAC,OAAO,CAAC,KAAK,QAAQ,CAAC,CAAC;IAC9B,OAAO,CAAC,CAAC;AACX,CAAC,CAAC;AAEF,MAAM,CAAC,MAAM,KAAK,GAAG,KAAK,EAAK,EAAoB,EAAE,SAA8B,EAAE,EAAE;IACrF,IAAI;QACF,OAAO,MAAM,EAAE,EAAE,CAAC;KACnB;IAAC,OAAO,CAAC,EAAE;QACV,IAAI,SAAS,CAAC,CAAC,CAAC,EAAE;YAChB,OAAO,MAAM,EAAE,EAAE,CAAC;SACnB;aAAM;YACL,MAAM,CAAC,CAAC;SACT;KACF;AACH,CAAC,CAAC"}
|
package/src/models.ts
DELETED
|
@@ -1,228 +0,0 @@
|
|
|
1
|
-
import type { Context } from './context';
|
|
2
|
-
import type { OrderBy } from './resolvers/arguments';
|
|
3
|
-
import type { Directive, Value } from './values';
|
|
4
|
-
|
|
5
|
-
export type RawModels = RawModel[];
|
|
6
|
-
|
|
7
|
-
export type RawModel =
|
|
8
|
-
| ScalarModel
|
|
9
|
-
| EnumModel
|
|
10
|
-
| RawEnumModel
|
|
11
|
-
| InterfaceModel
|
|
12
|
-
| ObjectModel
|
|
13
|
-
| RawObjectModel
|
|
14
|
-
| JsonObjectModel;
|
|
15
|
-
|
|
16
|
-
type BaseModel = {
|
|
17
|
-
name: string;
|
|
18
|
-
plural?: string;
|
|
19
|
-
description?: string;
|
|
20
|
-
};
|
|
21
|
-
|
|
22
|
-
export type ScalarModel = BaseModel & { type: 'scalar' };
|
|
23
|
-
|
|
24
|
-
export type EnumModel = BaseModel & { type: 'enum'; values: string[]; deleted?: true };
|
|
25
|
-
|
|
26
|
-
export type RawEnumModel = BaseModel & { type: 'raw-enum'; values: string[] };
|
|
27
|
-
|
|
28
|
-
export type InterfaceModel = BaseModel & { type: 'interface'; fields: ModelField[] };
|
|
29
|
-
|
|
30
|
-
export type RawObjectModel = BaseModel & {
|
|
31
|
-
type: 'raw-object';
|
|
32
|
-
fields: ModelField[];
|
|
33
|
-
rawFilters?: { name: string; type: string; list?: boolean; nonNull?: boolean }[];
|
|
34
|
-
};
|
|
35
|
-
|
|
36
|
-
export type JsonObjectModel = BaseModel & {
|
|
37
|
-
type: 'json-object';
|
|
38
|
-
json: true;
|
|
39
|
-
fields: Pick<ModelField, 'type' | 'name' | 'nonNull'>[];
|
|
40
|
-
};
|
|
41
|
-
|
|
42
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- data is derived from the models
|
|
43
|
-
export type Entity = Record<string, any>;
|
|
44
|
-
|
|
45
|
-
export type Action = 'create' | 'update' | 'delete' | 'restore';
|
|
46
|
-
|
|
47
|
-
export type MutationHook = (
|
|
48
|
-
model: Model,
|
|
49
|
-
action: Action,
|
|
50
|
-
when: 'before' | 'after',
|
|
51
|
-
data: { prev: Entity; input: Entity; normalizedInput: Entity; next: Entity },
|
|
52
|
-
ctx: Context
|
|
53
|
-
) => Promise<void>;
|
|
54
|
-
|
|
55
|
-
export type ObjectModel = BaseModel & {
|
|
56
|
-
type: 'object';
|
|
57
|
-
interfaces?: string[];
|
|
58
|
-
// createdAt, createdBy, updatedAt, updatedBy can be null
|
|
59
|
-
nonStrict?: boolean;
|
|
60
|
-
queriable?: boolean;
|
|
61
|
-
listQueriable?: boolean;
|
|
62
|
-
creatable?: boolean;
|
|
63
|
-
updatable?: boolean;
|
|
64
|
-
deletable?: boolean;
|
|
65
|
-
displayField?: string;
|
|
66
|
-
defaultOrderBy?: OrderBy;
|
|
67
|
-
fields: ModelField[];
|
|
68
|
-
|
|
69
|
-
// temporary fields for the generation of migrations
|
|
70
|
-
deleted?: true;
|
|
71
|
-
oldName?: string;
|
|
72
|
-
};
|
|
73
|
-
|
|
74
|
-
export type InputObject = {
|
|
75
|
-
name: string;
|
|
76
|
-
type: string;
|
|
77
|
-
nonNull?: boolean;
|
|
78
|
-
};
|
|
79
|
-
|
|
80
|
-
export const isObjectModel = (model: RawModel): model is ObjectModel => model.type === 'object';
|
|
81
|
-
|
|
82
|
-
export const isEnumModel = (model: RawModel): model is EnumModel => model.type === 'enum';
|
|
83
|
-
|
|
84
|
-
export const isRawEnumModel = (model: RawModel): model is RawEnumModel => model.type === 'raw-enum';
|
|
85
|
-
|
|
86
|
-
export const isScalarModel = (model: RawModel): model is ScalarModel => model.type === 'scalar';
|
|
87
|
-
|
|
88
|
-
export const isRawObjectModel = (model: RawModel): model is RawObjectModel => model.type === 'raw-object';
|
|
89
|
-
|
|
90
|
-
export const isJsonObjectModel = (model: RawModel): model is RawObjectModel => model.type === 'json-object';
|
|
91
|
-
|
|
92
|
-
export const isEnumList = (models: RawModels, field: ModelField) =>
|
|
93
|
-
field?.list === true && models.find(({ name }) => name === field.type)?.type === 'enum';
|
|
94
|
-
|
|
95
|
-
export const and =
|
|
96
|
-
(...predicates: ((field: ModelField) => boolean)[]) =>
|
|
97
|
-
(field: ModelField) =>
|
|
98
|
-
predicates.every((predicate) => predicate(field));
|
|
99
|
-
|
|
100
|
-
export const not = (predicate: (field: ModelField) => boolean) => (field: ModelField) => !predicate(field);
|
|
101
|
-
|
|
102
|
-
export const isRelation = ({ relation }: ModelField) => !!relation;
|
|
103
|
-
|
|
104
|
-
export type VisibleRelationsByRole = Record<string, Record<string, string[]>>;
|
|
105
|
-
|
|
106
|
-
export const isVisibleRelation = (visibleRelationsByRole: VisibleRelationsByRole, modelName: string, role: string) => {
|
|
107
|
-
const whitelist = visibleRelationsByRole[role]?.[modelName];
|
|
108
|
-
return ({ name }: Field) => (whitelist ? whitelist.includes(name) : true);
|
|
109
|
-
};
|
|
110
|
-
|
|
111
|
-
export const isToOneRelation = ({ toOne }: ModelField) => !!toOne;
|
|
112
|
-
|
|
113
|
-
export const isQueriableField = ({ queriable }: ModelField) => queriable !== false;
|
|
114
|
-
|
|
115
|
-
export const isRaw = ({ raw }: ModelField) => !!raw;
|
|
116
|
-
|
|
117
|
-
export const isVisible = ({ hidden }: ModelField) => hidden !== true;
|
|
118
|
-
|
|
119
|
-
export const isSimpleField = and(not(isRelation), not(isRaw));
|
|
120
|
-
|
|
121
|
-
export const isUpdatable = ({ updatable }: ModelField) => !!updatable;
|
|
122
|
-
|
|
123
|
-
export const isCreatable = ({ creatable }: ModelField) => !!creatable;
|
|
124
|
-
|
|
125
|
-
export const isQueriableBy = (role: string) => (field: ModelField) =>
|
|
126
|
-
isQueriableField(field) && (!field.queriableBy || field.queriableBy.includes(role));
|
|
127
|
-
|
|
128
|
-
export const isUpdatableBy = (role: string) => (field: ModelField) =>
|
|
129
|
-
isUpdatable(field) && (!field.updatableBy || field.updatableBy.includes(role));
|
|
130
|
-
|
|
131
|
-
export const isCreatableBy = (role: string) => (field: ModelField) =>
|
|
132
|
-
isCreatable(field) && (!field.creatableBy || field.creatableBy.includes(role));
|
|
133
|
-
|
|
134
|
-
export const actionableRelations = (model: Model, action: 'create' | 'update' | 'filter') =>
|
|
135
|
-
model.fields.filter(
|
|
136
|
-
({ relation, ...field }) =>
|
|
137
|
-
relation &&
|
|
138
|
-
field[`${action === 'filter' ? action : action.slice(0, -1)}able` as 'filterable' | 'creatable' | 'updatable']
|
|
139
|
-
);
|
|
140
|
-
|
|
141
|
-
export type Field = {
|
|
142
|
-
name: string;
|
|
143
|
-
type: string;
|
|
144
|
-
default?: Value;
|
|
145
|
-
list?: boolean;
|
|
146
|
-
nonNull?: boolean;
|
|
147
|
-
args?: Field[];
|
|
148
|
-
directives?: Directive[];
|
|
149
|
-
};
|
|
150
|
-
|
|
151
|
-
export type ModelField = Field & {
|
|
152
|
-
primary?: boolean;
|
|
153
|
-
unique?: boolean;
|
|
154
|
-
filterable?: boolean;
|
|
155
|
-
defaultFilter?: Value;
|
|
156
|
-
searchable?: boolean;
|
|
157
|
-
possibleValues?: Value[];
|
|
158
|
-
orderable?: boolean;
|
|
159
|
-
comparable?: boolean;
|
|
160
|
-
relation?: boolean;
|
|
161
|
-
onDelete?: 'cascade' | 'set-null';
|
|
162
|
-
reverse?: string;
|
|
163
|
-
toOne?: boolean;
|
|
164
|
-
foreignKey?: string;
|
|
165
|
-
queriable?: false;
|
|
166
|
-
queriableBy?: string[];
|
|
167
|
-
creatable?: boolean;
|
|
168
|
-
creatableBy?: string[];
|
|
169
|
-
updatable?: boolean;
|
|
170
|
-
updatableBy?: string[];
|
|
171
|
-
generated?: boolean;
|
|
172
|
-
raw?: boolean;
|
|
173
|
-
json?: boolean;
|
|
174
|
-
dateTimeType?: 'year' | 'date' | 'datetime' | 'year_and_month';
|
|
175
|
-
stringType?: 'email' | 'url' | 'phone';
|
|
176
|
-
floatType?: 'currency' | 'percentage';
|
|
177
|
-
unit?: 'million';
|
|
178
|
-
intType?: 'currency';
|
|
179
|
-
min?: number;
|
|
180
|
-
max?: number;
|
|
181
|
-
// The tooltip is "hidden" behind an icon in the admin forms
|
|
182
|
-
tooltip?: string;
|
|
183
|
-
// The description is always visible below the inputs in the admin forms
|
|
184
|
-
description?: string;
|
|
185
|
-
large?: true;
|
|
186
|
-
maxLength?: number;
|
|
187
|
-
double?: boolean;
|
|
188
|
-
precision?: number;
|
|
189
|
-
scale?: number;
|
|
190
|
-
defaultValue?: string | number | ReadonlyArray<string> | undefined;
|
|
191
|
-
endOfDay?: boolean;
|
|
192
|
-
obfuscate?: true;
|
|
193
|
-
// If true the field must be filled within forms but can be null in the database
|
|
194
|
-
required?: boolean;
|
|
195
|
-
indent?: boolean;
|
|
196
|
-
// If true the field is hidden in the admin interface
|
|
197
|
-
hidden?: boolean;
|
|
198
|
-
|
|
199
|
-
// temporary fields for the generation of migrations
|
|
200
|
-
deleted?: true;
|
|
201
|
-
oldName?: string;
|
|
202
|
-
};
|
|
203
|
-
|
|
204
|
-
export type Models = Model[];
|
|
205
|
-
|
|
206
|
-
export type Model = ObjectModel & {
|
|
207
|
-
fieldsByName: Record<string, ModelField>;
|
|
208
|
-
relations: Relation[];
|
|
209
|
-
relationsByName: Record<string, Relation>;
|
|
210
|
-
reverseRelations: ReverseRelation[];
|
|
211
|
-
reverseRelationsByName: Record<string, ReverseRelation>;
|
|
212
|
-
};
|
|
213
|
-
|
|
214
|
-
export type Relation = {
|
|
215
|
-
field: ModelField;
|
|
216
|
-
model: Model;
|
|
217
|
-
reverseRelation: ReverseRelation;
|
|
218
|
-
};
|
|
219
|
-
|
|
220
|
-
export type ReverseRelation = {
|
|
221
|
-
name: string;
|
|
222
|
-
type: string;
|
|
223
|
-
foreignKey: string;
|
|
224
|
-
toOne: boolean;
|
|
225
|
-
model: Model;
|
|
226
|
-
field: ModelField;
|
|
227
|
-
fieldModel: Model;
|
|
228
|
-
};
|
package/src/utils.ts
DELETED
|
@@ -1,187 +0,0 @@
|
|
|
1
|
-
import assert from 'assert';
|
|
2
|
-
import { pluralize } from 'inflection';
|
|
3
|
-
import camelCase from 'lodash/camelCase';
|
|
4
|
-
import lodashGet from 'lodash/get';
|
|
5
|
-
import kebabCase from 'lodash/kebabCase';
|
|
6
|
-
import startCase from 'lodash/startCase';
|
|
7
|
-
import { Model, Models, ObjectModel, RawModels, Relation, ReverseRelation, isObjectModel } from './models';
|
|
8
|
-
|
|
9
|
-
const isNotFalsy = <T>(v: T | null | undefined | false): v is T => typeof v !== 'undefined' && v !== null && v !== false;
|
|
10
|
-
|
|
11
|
-
export const merge = <T>(objects: ({ [name: string]: T } | undefined | false)[] | undefined): { [name: string]: T } =>
|
|
12
|
-
(objects || []).filter(isNotFalsy).reduce((i, acc) => ({ ...acc, ...i }), {});
|
|
13
|
-
|
|
14
|
-
// Target -> target
|
|
15
|
-
export const typeToField = (type: string) => type.substr(0, 1).toLowerCase() + type.substr(1);
|
|
16
|
-
|
|
17
|
-
export const getModelPlural = (model: ObjectModel | Model) => model.plural || pluralize(model.name);
|
|
18
|
-
|
|
19
|
-
export const getModelPluralField = (model: Model) => typeToField(getModelPlural(model));
|
|
20
|
-
|
|
21
|
-
export const getModelSlug = (model: Model) => kebabCase(getModelPlural(model));
|
|
22
|
-
|
|
23
|
-
export const getModelLabelPlural = (model: Model) => getLabel(getModelPlural(model));
|
|
24
|
-
|
|
25
|
-
export const getModelLabel = (model: Model) => getLabel(model.name);
|
|
26
|
-
|
|
27
|
-
export const getLabel = (s: string) => startCase(camelCase(s));
|
|
28
|
-
|
|
29
|
-
export const getModels = (rawModels: RawModels): Models => {
|
|
30
|
-
const models: Models = rawModels.filter(isObjectModel).map((model) => {
|
|
31
|
-
const objectModel: Model = {
|
|
32
|
-
...model,
|
|
33
|
-
fieldsByName: {},
|
|
34
|
-
relations: [],
|
|
35
|
-
relationsByName: {},
|
|
36
|
-
reverseRelations: [],
|
|
37
|
-
reverseRelationsByName: {},
|
|
38
|
-
fields: [
|
|
39
|
-
{ name: 'id', type: 'ID', nonNull: true, unique: true, primary: true, generated: true },
|
|
40
|
-
...model.fields,
|
|
41
|
-
...(model.creatable
|
|
42
|
-
? [
|
|
43
|
-
{ name: 'createdAt', type: 'DateTime', nonNull: !model.nonStrict, orderable: true, generated: true },
|
|
44
|
-
{
|
|
45
|
-
name: 'createdBy',
|
|
46
|
-
type: 'User',
|
|
47
|
-
relation: true,
|
|
48
|
-
nonNull: !model.nonStrict,
|
|
49
|
-
reverse: `created${getModelPlural(model)}`,
|
|
50
|
-
generated: true,
|
|
51
|
-
},
|
|
52
|
-
]
|
|
53
|
-
: []),
|
|
54
|
-
...(model.updatable
|
|
55
|
-
? [
|
|
56
|
-
{ name: 'updatedAt', type: 'DateTime', nonNull: !model.nonStrict, orderable: true, generated: true },
|
|
57
|
-
{
|
|
58
|
-
name: 'updatedBy',
|
|
59
|
-
type: 'User',
|
|
60
|
-
relation: true,
|
|
61
|
-
nonNull: !model.nonStrict,
|
|
62
|
-
reverse: `updated${getModelPlural(model)}`,
|
|
63
|
-
generated: true,
|
|
64
|
-
},
|
|
65
|
-
]
|
|
66
|
-
: []),
|
|
67
|
-
...(model.deletable
|
|
68
|
-
? [
|
|
69
|
-
{
|
|
70
|
-
name: 'deleted',
|
|
71
|
-
type: 'Boolean',
|
|
72
|
-
nonNull: true,
|
|
73
|
-
default: false,
|
|
74
|
-
filterable: true,
|
|
75
|
-
defaultFilter: false,
|
|
76
|
-
generated: true,
|
|
77
|
-
},
|
|
78
|
-
{ name: 'deletedAt', type: 'DateTime', orderable: true, generated: true },
|
|
79
|
-
{
|
|
80
|
-
name: 'deletedBy',
|
|
81
|
-
type: 'User',
|
|
82
|
-
relation: true,
|
|
83
|
-
reverse: `deleted${getModelPlural(model)}`,
|
|
84
|
-
generated: true,
|
|
85
|
-
},
|
|
86
|
-
]
|
|
87
|
-
: []),
|
|
88
|
-
].map(({ foreignKey, ...field }) => ({
|
|
89
|
-
...field,
|
|
90
|
-
...(field.relation && {
|
|
91
|
-
foreignKey: foreignKey || `${field.name}Id`,
|
|
92
|
-
}),
|
|
93
|
-
})),
|
|
94
|
-
};
|
|
95
|
-
|
|
96
|
-
for (const field of objectModel.fields) {
|
|
97
|
-
objectModel.fieldsByName[field.name] = field;
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
return objectModel;
|
|
101
|
-
});
|
|
102
|
-
|
|
103
|
-
for (const model of models) {
|
|
104
|
-
for (const field of model.fields.filter(({ relation }) => relation)) {
|
|
105
|
-
const fieldModel = summonByName(models, field.type);
|
|
106
|
-
|
|
107
|
-
const reverseRelation: ReverseRelation = {
|
|
108
|
-
name: field.reverse || (field.toOne ? typeToField(model.name) : getModelPluralField(model)),
|
|
109
|
-
foreignKey: get(field, 'foreignKey'),
|
|
110
|
-
type: model.name,
|
|
111
|
-
toOne: !!field.toOne,
|
|
112
|
-
fieldModel,
|
|
113
|
-
field,
|
|
114
|
-
model,
|
|
115
|
-
};
|
|
116
|
-
|
|
117
|
-
const relation: Relation = {
|
|
118
|
-
field,
|
|
119
|
-
model: fieldModel,
|
|
120
|
-
reverseRelation,
|
|
121
|
-
};
|
|
122
|
-
model.relations.push(relation);
|
|
123
|
-
model.relationsByName[relation.field.name] = relation;
|
|
124
|
-
|
|
125
|
-
fieldModel.reverseRelations.push(reverseRelation);
|
|
126
|
-
|
|
127
|
-
fieldModel.reverseRelationsByName[reverseRelation.name] = reverseRelation;
|
|
128
|
-
}
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
return models;
|
|
132
|
-
};
|
|
133
|
-
|
|
134
|
-
export const summonByName = <T extends { name: string }>(array: T[], value: string) => summonByKey(array, 'name', value);
|
|
135
|
-
|
|
136
|
-
export const summonByKey = <T>(array: readonly T[] | undefined, key: string, value: unknown) =>
|
|
137
|
-
summon(array, (element: T) => lodashGet(element, key) === value, `No element found with ${key} ${value}`);
|
|
138
|
-
|
|
139
|
-
export const summon = <T>(array: readonly T[] | undefined, cb: Parameters<T[]['find']>[1], errorMessage?: string) => {
|
|
140
|
-
if (array === undefined) {
|
|
141
|
-
console.trace();
|
|
142
|
-
throw new Error('Base array is not defined.');
|
|
143
|
-
}
|
|
144
|
-
const result = array.find(cb);
|
|
145
|
-
if (result === undefined) {
|
|
146
|
-
console.trace();
|
|
147
|
-
throw new Error(errorMessage || 'Element not found.');
|
|
148
|
-
}
|
|
149
|
-
return result;
|
|
150
|
-
};
|
|
151
|
-
|
|
152
|
-
type ForSure<T> = T extends undefined | null ? never : T;
|
|
153
|
-
|
|
154
|
-
export const it = <T>(object: T | null | undefined): ForSure<T> => {
|
|
155
|
-
if (object === undefined || object === null) {
|
|
156
|
-
console.trace();
|
|
157
|
-
throw new Error('Base object is not defined.');
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
return object as ForSure<T>;
|
|
161
|
-
};
|
|
162
|
-
|
|
163
|
-
export const get = <T, U extends keyof ForSure<T>>(object: T | null | undefined, key: U): ForSure<ForSure<T>[U]> => {
|
|
164
|
-
const value = it(object)[key];
|
|
165
|
-
if (value === undefined || value === null) {
|
|
166
|
-
console.trace();
|
|
167
|
-
throw new Error(`Object doesn't have ${String(key)}`);
|
|
168
|
-
}
|
|
169
|
-
return value as ForSure<ForSure<T>[U]>;
|
|
170
|
-
};
|
|
171
|
-
|
|
172
|
-
export const getString = (v: unknown) => {
|
|
173
|
-
assert(typeof v === 'string');
|
|
174
|
-
return v;
|
|
175
|
-
};
|
|
176
|
-
|
|
177
|
-
export const retry = async <T>(cb: () => Promise<T>, condition: (e: any) => boolean) => {
|
|
178
|
-
try {
|
|
179
|
-
return await cb();
|
|
180
|
-
} catch (e) {
|
|
181
|
-
if (condition(e)) {
|
|
182
|
-
return await cb();
|
|
183
|
-
} else {
|
|
184
|
-
throw e;
|
|
185
|
-
}
|
|
186
|
-
}
|
|
187
|
-
};
|