@zenstackhq/sdk 1.0.0-alpha.99 → 1.0.0-beta.1
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/code-gen.d.ts +10 -2
- package/code-gen.js +39 -7
- package/code-gen.js.map +1 -1
- package/constants.d.ts +1 -21
- package/constants.js +6 -23
- package/constants.js.map +1 -1
- package/package.json +3 -2
- package/types.d.ts +3 -1
- package/types.js +2 -1
- package/types.js.map +1 -1
- package/utils.d.ts +19 -0
- package/utils.js +93 -3
- package/utils.js.map +1 -1
package/code-gen.d.ts
CHANGED
|
@@ -1,5 +1,13 @@
|
|
|
1
|
-
import { Project } from 'ts-morph';
|
|
1
|
+
import { CompilerOptions, Project } from 'ts-morph';
|
|
2
|
+
/**
|
|
3
|
+
* Creates a TS code generation project
|
|
4
|
+
*/
|
|
5
|
+
export declare function createProject(options?: CompilerOptions): Project;
|
|
6
|
+
/**
|
|
7
|
+
* Persists a TS project to disk.
|
|
8
|
+
*/
|
|
9
|
+
export declare function saveProject(project: Project): Promise<void>;
|
|
2
10
|
/**
|
|
3
11
|
* Emit a TS project to JS files.
|
|
4
12
|
*/
|
|
5
|
-
export declare function emitProject(project: Project
|
|
13
|
+
export declare function emitProject(project: Project): Promise<void>;
|
package/code-gen.js
CHANGED
|
@@ -12,8 +12,10 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
12
12
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
13
|
};
|
|
14
14
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
-
exports.emitProject = void 0;
|
|
15
|
+
exports.emitProject = exports.saveProject = exports.createProject = void 0;
|
|
16
16
|
const prettier_1 = __importDefault(require("prettier"));
|
|
17
|
+
const ts_morph_1 = require("ts-morph");
|
|
18
|
+
const types_1 = require("./types");
|
|
17
19
|
const formatOptions = {
|
|
18
20
|
trailingComma: 'all',
|
|
19
21
|
tabWidth: 4,
|
|
@@ -37,17 +39,47 @@ function formatFile(sourceFile) {
|
|
|
37
39
|
}
|
|
38
40
|
});
|
|
39
41
|
}
|
|
42
|
+
/**
|
|
43
|
+
* Creates a TS code generation project
|
|
44
|
+
*/
|
|
45
|
+
function createProject(options) {
|
|
46
|
+
return new ts_morph_1.Project({
|
|
47
|
+
compilerOptions: Object.assign({ target: ts_morph_1.ScriptTarget.ES2016, module: ts_morph_1.ModuleKind.CommonJS, esModuleInterop: true, declaration: true, strict: true, skipLibCheck: true, noEmitOnError: true }, options),
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
exports.createProject = createProject;
|
|
51
|
+
/**
|
|
52
|
+
* Persists a TS project to disk.
|
|
53
|
+
*/
|
|
54
|
+
function saveProject(project) {
|
|
55
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
56
|
+
yield Promise.all(project.getSourceFiles().map((sf) => __awaiter(this, void 0, void 0, function* () {
|
|
57
|
+
yield formatFile(sf);
|
|
58
|
+
})));
|
|
59
|
+
yield project.save();
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
exports.saveProject = saveProject;
|
|
40
63
|
/**
|
|
41
64
|
* Emit a TS project to JS files.
|
|
42
65
|
*/
|
|
43
|
-
function emitProject(project
|
|
66
|
+
function emitProject(project) {
|
|
44
67
|
return __awaiter(this, void 0, void 0, function* () {
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
68
|
+
const errors = project.getPreEmitDiagnostics().filter((d) => d.getCategory() === ts_morph_1.DiagnosticCategory.Error);
|
|
69
|
+
if (errors.length > 0) {
|
|
70
|
+
console.error('Error compiling generated code:');
|
|
71
|
+
console.error(project.formatDiagnosticsWithColorAndContext(errors.slice(0, 10)));
|
|
72
|
+
yield project.save();
|
|
73
|
+
throw new types_1.PluginError('', `Error compiling generated code`);
|
|
74
|
+
}
|
|
75
|
+
const result = yield project.emit();
|
|
76
|
+
const emitErrors = result.getDiagnostics().filter((d) => d.getCategory() === ts_morph_1.DiagnosticCategory.Error);
|
|
77
|
+
if (emitErrors.length > 0) {
|
|
78
|
+
console.error('Some generated code is not emitted:');
|
|
79
|
+
console.error(project.formatDiagnosticsWithColorAndContext(emitErrors.slice(0, 10)));
|
|
80
|
+
yield project.save();
|
|
81
|
+
throw new types_1.PluginError('', `Error emitting generated code`);
|
|
49
82
|
}
|
|
50
|
-
yield project.emit();
|
|
51
83
|
});
|
|
52
84
|
}
|
|
53
85
|
exports.emitProject = emitProject;
|
package/code-gen.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"code-gen.js","sourceRoot":"","sources":["../src/code-gen.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAAA,wDAAgC;
|
|
1
|
+
{"version":3,"file":"code-gen.js","sourceRoot":"","sources":["../src/code-gen.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAAA,wDAAgC;AAChC,uCAA8G;AAC9G,mCAAsC;AAEtC,MAAM,aAAa,GAAG;IAClB,aAAa,EAAE,KAAK;IACpB,QAAQ,EAAE,CAAC;IACX,UAAU,EAAE,GAAG;IACf,cAAc,EAAE,IAAI;IACpB,IAAI,EAAE,IAAI;IACV,WAAW,EAAE,IAAI;IACjB,OAAO,EAAE,KAAK;IACd,MAAM,EAAE,YAAY;CACd,CAAC;AAEX,SAAe,UAAU,CAAC,UAAsB;;QAC5C,IAAI;YACA,MAAM,OAAO,GAAG,UAAU,CAAC,WAAW,EAAE,CAAC;YACzC,MAAM,SAAS,GAAG,kBAAQ,CAAC,MAAM,CAAC,OAAO,EAAE,aAAa,CAAC,CAAC;YAC1D,UAAU,CAAC,eAAe,CAAC,SAAS,CAAC,CAAC;YACtC,MAAM,UAAU,CAAC,IAAI,EAAE,CAAC;SAC3B;QAAC,WAAM;YACJ,WAAW;SACd;IACL,CAAC;CAAA;AAED;;GAEG;AACH,SAAgB,aAAa,CAAC,OAAyB;IACnD,OAAO,IAAI,kBAAO,CAAC;QACf,eAAe,kBACX,MAAM,EAAE,uBA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AAC,CAAC,WAAW,EAAE,KAAK,6BAAkB,CAAC,KAAK,CAAC,CAAC;QACvG,IAAI,UAAU,CAAC,MAAM,GAAG,CAAC,EAAE;YACvB,OAAO,CAAC,KAAK,CAAC,qCAAqC,CAAC,CAAC;YACrD,OAAO,CAAC,KAAK,CAAC,OAAO,CAAC,oCAAoC,CAAC,UAAU,CAAC,KAAK,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,CAAC;YACrF,MAAM,OAAO,CAAC,IAAI,EAAE,CAAC;YACrB,MAAM,IAAI,mBAAW,CAAC,EAAE,EAAE,+BAA+B,CAAC,CAAC;SAC9D;IACL,CAAC;CAAA;AAlBD,kCAkBC"}
|
package/constants.d.ts
CHANGED
|
@@ -1,25 +1,5 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Auxiliary database field for supporting policy check for nested writes
|
|
3
|
-
*/
|
|
4
|
-
export declare const TRANSACTION_FIELD_NAME = "zenstack_transaction";
|
|
5
|
-
/**
|
|
6
|
-
* Auxiliary database field for building up policy check queries
|
|
7
|
-
*/
|
|
8
|
-
export declare const GUARD_FIELD_NAME = "zenstack_guard";
|
|
9
|
-
/**
|
|
10
|
-
* All Auxiliary fields.
|
|
11
|
-
*/
|
|
12
|
-
export declare const AUXILIARY_FIELDS: string[];
|
|
13
|
-
/**
|
|
14
|
-
* Reasons for a CRUD operation to fail.
|
|
15
|
-
*/
|
|
16
|
-
export declare enum CrudFailureReason {
|
|
17
|
-
/**
|
|
18
|
-
* CRUD suceeded but the result was not readable.
|
|
19
|
-
*/
|
|
20
|
-
RESULT_NOT_READABLE = "RESULT_NOT_READABLE"
|
|
21
|
-
}
|
|
22
1
|
/**
|
|
23
2
|
* @zenstackhq/runtime package name
|
|
24
3
|
*/
|
|
25
4
|
export declare const RUNTIME_PACKAGE = "@zenstackhq/runtime";
|
|
5
|
+
export { AUXILIARY_FIELDS, GUARD_FIELD_NAME, TRANSACTION_FIELD_NAME, CrudFailureReason, } from '@zenstackhq/runtime/constants';
|
package/constants.js
CHANGED
|
@@ -1,30 +1,13 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
4
|
-
/**
|
|
5
|
-
* Auxiliary database field for supporting policy check for nested writes
|
|
6
|
-
*/
|
|
7
|
-
exports.TRANSACTION_FIELD_NAME = 'zenstack_transaction';
|
|
8
|
-
/**
|
|
9
|
-
* Auxiliary database field for building up policy check queries
|
|
10
|
-
*/
|
|
11
|
-
exports.GUARD_FIELD_NAME = 'zenstack_guard';
|
|
12
|
-
/**
|
|
13
|
-
* All Auxiliary fields.
|
|
14
|
-
*/
|
|
15
|
-
exports.AUXILIARY_FIELDS = [exports.TRANSACTION_FIELD_NAME, exports.GUARD_FIELD_NAME];
|
|
16
|
-
/**
|
|
17
|
-
* Reasons for a CRUD operation to fail.
|
|
18
|
-
*/
|
|
19
|
-
var CrudFailureReason;
|
|
20
|
-
(function (CrudFailureReason) {
|
|
21
|
-
/**
|
|
22
|
-
* CRUD suceeded but the result was not readable.
|
|
23
|
-
*/
|
|
24
|
-
CrudFailureReason["RESULT_NOT_READABLE"] = "RESULT_NOT_READABLE";
|
|
25
|
-
})(CrudFailureReason = exports.CrudFailureReason || (exports.CrudFailureReason = {}));
|
|
3
|
+
exports.CrudFailureReason = exports.TRANSACTION_FIELD_NAME = exports.GUARD_FIELD_NAME = exports.AUXILIARY_FIELDS = exports.RUNTIME_PACKAGE = void 0;
|
|
26
4
|
/**
|
|
27
5
|
* @zenstackhq/runtime package name
|
|
28
6
|
*/
|
|
29
7
|
exports.RUNTIME_PACKAGE = '@zenstackhq/runtime';
|
|
8
|
+
var constants_1 = require("@zenstackhq/runtime/constants");
|
|
9
|
+
Object.defineProperty(exports, "AUXILIARY_FIELDS", { enumerable: true, get: function () { return constants_1.AUXILIARY_FIELDS; } });
|
|
10
|
+
Object.defineProperty(exports, "GUARD_FIELD_NAME", { enumerable: true, get: function () { return constants_1.GUARD_FIELD_NAME; } });
|
|
11
|
+
Object.defineProperty(exports, "TRANSACTION_FIELD_NAME", { enumerable: true, get: function () { return constants_1.TRANSACTION_FIELD_NAME; } });
|
|
12
|
+
Object.defineProperty(exports, "CrudFailureReason", { enumerable: true, get: function () { return constants_1.CrudFailureReason; } });
|
|
30
13
|
//# sourceMappingURL=constants.js.map
|
package/constants.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"constants.js","sourceRoot":"","sources":["../src/constants.ts"],"names":[],"mappings":";;;AAAA;;GAEG;AACU,QAAA,
|
|
1
|
+
{"version":3,"file":"constants.js","sourceRoot":"","sources":["../src/constants.ts"],"names":[],"mappings":";;;AAAA;;GAEG;AACU,QAAA,eAAe,GAAG,qBAAqB,CAAC;AAErD,2DAKuC;AAJnC,6GAAA,gBAAgB,OAAA;AAChB,6GAAA,gBAAgB,OAAA;AAChB,mHAAA,sBAAsB,OAAA;AACtB,8GAAA,iBAAiB,OAAA"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zenstackhq/sdk",
|
|
3
|
-
"version": "1.0.0-
|
|
3
|
+
"version": "1.0.0-beta.1",
|
|
4
4
|
"description": "ZenStack plugin development SDK",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"publishConfig": {
|
|
@@ -14,7 +14,8 @@
|
|
|
14
14
|
"@prisma/generator-helper": "^4.7.1",
|
|
15
15
|
"prettier": "^2.8.3",
|
|
16
16
|
"ts-morph": "^16.0.0",
|
|
17
|
-
"@zenstackhq/language": "1.0.0-
|
|
17
|
+
"@zenstackhq/language": "1.0.0-beta.1",
|
|
18
|
+
"@zenstackhq/runtime": "1.0.0-beta.1"
|
|
18
19
|
},
|
|
19
20
|
"devDependencies": {
|
|
20
21
|
"copyfiles": "^2.4.1",
|
package/types.d.ts
CHANGED
|
@@ -10,6 +10,7 @@ export type OptionValue = string | number | boolean;
|
|
|
10
10
|
export type PluginOptions = {
|
|
11
11
|
provider?: string;
|
|
12
12
|
schemaPath: string;
|
|
13
|
+
name: string;
|
|
13
14
|
} & Record<string, OptionValue | OptionValue[]>;
|
|
14
15
|
/**
|
|
15
16
|
* Plugin entry point function definition
|
|
@@ -19,5 +20,6 @@ export type PluginFunction = (model: Model, options: PluginOptions, dmmf?: DMMF.
|
|
|
19
20
|
* Plugin error
|
|
20
21
|
*/
|
|
21
22
|
export declare class PluginError extends Error {
|
|
22
|
-
|
|
23
|
+
plugin: string;
|
|
24
|
+
constructor(plugin: string, message: string);
|
|
23
25
|
}
|
package/types.js
CHANGED
package/types.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":";;;AA0BA;;GAEG;AACH,MAAa,WAAY,SAAQ,KAAK;IAClC,YAAmB,MAAc,EAAE,OAAe;QAC9C,KAAK,CAAC,OAAO,CAAC,CAAC;QADA,WAAM,GAAN,MAAM,CAAQ;IAEjC,CAAC;CACJ;AAJD,kCAIC"}
|
package/utils.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { AstNode, DataModel, DataModelAttribute, DataModelField, DataModelFieldAttribute, Enum, EnumField, Expression, Model, Reference } from '@zenstackhq/language/ast';
|
|
2
|
+
import { PluginOptions } from './types';
|
|
2
3
|
/**
|
|
3
4
|
* Gets data models that are not ignored
|
|
4
5
|
*/
|
|
@@ -13,3 +14,21 @@ export declare function hasAttribute(decl: DataModel | DataModelField | Enum | E
|
|
|
13
14
|
export declare function getAttributeArgs(attr: DataModelAttribute | DataModelFieldAttribute): Record<string, Expression>;
|
|
14
15
|
export declare function getAttributeArg(attr: DataModelAttribute | DataModelFieldAttribute, name: string): Expression | undefined;
|
|
15
16
|
export declare function getAttributeArgLiteral<T extends string | number | boolean>(attr: DataModelAttribute | DataModelFieldAttribute, name: string): T | undefined;
|
|
17
|
+
/**
|
|
18
|
+
* Gets id fields declared at the data model level
|
|
19
|
+
*/
|
|
20
|
+
export declare function getIdFields(model: DataModel): DataModelField[];
|
|
21
|
+
/**
|
|
22
|
+
* Returns if the given field is declared as an id field.
|
|
23
|
+
*/
|
|
24
|
+
export declare function isIdField(field: DataModelField): boolean;
|
|
25
|
+
/**
|
|
26
|
+
* Returns if the given field is a relation field.
|
|
27
|
+
*/
|
|
28
|
+
export declare function isRelationshipField(field: DataModelField): boolean;
|
|
29
|
+
/**
|
|
30
|
+
* Returns if the given field is a relation foreign key field.
|
|
31
|
+
*/
|
|
32
|
+
export declare function isForeignKeyField(field: DataModelField): boolean;
|
|
33
|
+
export declare function resolvePath(_path: string, options: PluginOptions): string;
|
|
34
|
+
export declare function requireOption<T>(options: PluginOptions, name: string): T;
|
package/utils.js
CHANGED
|
@@ -1,7 +1,11 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
2
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.getAttributeArgLiteral = exports.getAttributeArg = exports.getAttributeArgs = exports.hasAttribute = exports.getObjectLiteral = exports.getLiteralArray = exports.getArray = exports.getLiteral = exports.resolved = exports.getDataModels = void 0;
|
|
6
|
+
exports.requireOption = exports.resolvePath = exports.isForeignKeyField = exports.isRelationshipField = exports.isIdField = exports.getIdFields = exports.getAttributeArgLiteral = exports.getAttributeArg = exports.getAttributeArgs = exports.hasAttribute = exports.getObjectLiteral = exports.getLiteralArray = exports.getArray = exports.getLiteral = exports.resolved = exports.getDataModels = void 0;
|
|
4
7
|
const ast_1 = require("@zenstackhq/language/ast");
|
|
8
|
+
const path_1 = __importDefault(require("path"));
|
|
5
9
|
/**
|
|
6
10
|
* Gets data models that are not ignored
|
|
7
11
|
*/
|
|
@@ -19,7 +23,7 @@ exports.resolved = resolved;
|
|
|
19
23
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
20
24
|
function getLiteral(expr) {
|
|
21
25
|
if (!(0, ast_1.isLiteralExpr)(expr)) {
|
|
22
|
-
return
|
|
26
|
+
return getObjectLiteral(expr);
|
|
23
27
|
}
|
|
24
28
|
return expr.value;
|
|
25
29
|
}
|
|
@@ -33,7 +37,7 @@ function getLiteralArray(expr) {
|
|
|
33
37
|
if (!arr) {
|
|
34
38
|
return undefined;
|
|
35
39
|
}
|
|
36
|
-
return arr.map((item) =>
|
|
40
|
+
return arr.map((item) => getLiteral(item));
|
|
37
41
|
}
|
|
38
42
|
exports.getLiteralArray = getLiteralArray;
|
|
39
43
|
function getObjectLiteral(expr) {
|
|
@@ -102,4 +106,90 @@ function getAttributeArgLiteral(attr, name) {
|
|
|
102
106
|
return undefined;
|
|
103
107
|
}
|
|
104
108
|
exports.getAttributeArgLiteral = getAttributeArgLiteral;
|
|
109
|
+
/**
|
|
110
|
+
* Gets id fields declared at the data model level
|
|
111
|
+
*/
|
|
112
|
+
function getIdFields(model) {
|
|
113
|
+
const idAttr = model.attributes.find((attr) => { var _a; return ((_a = attr.decl.ref) === null || _a === void 0 ? void 0 : _a.name) === '@@id'; });
|
|
114
|
+
if (!idAttr) {
|
|
115
|
+
return [];
|
|
116
|
+
}
|
|
117
|
+
const fieldsArg = idAttr.args.find((a) => { var _a; return ((_a = a.$resolvedParam) === null || _a === void 0 ? void 0 : _a.name) === 'fields'; });
|
|
118
|
+
if (!fieldsArg || !(0, ast_1.isArrayExpr)(fieldsArg.value)) {
|
|
119
|
+
return [];
|
|
120
|
+
}
|
|
121
|
+
return fieldsArg.value.items
|
|
122
|
+
.filter((item) => (0, ast_1.isReferenceExpr)(item))
|
|
123
|
+
.map((item) => resolved(item.target));
|
|
124
|
+
}
|
|
125
|
+
exports.getIdFields = getIdFields;
|
|
126
|
+
/**
|
|
127
|
+
* Returns if the given field is declared as an id field.
|
|
128
|
+
*/
|
|
129
|
+
function isIdField(field) {
|
|
130
|
+
// field-level @id attribute
|
|
131
|
+
if (field.attributes.some((attr) => { var _a; return ((_a = attr.decl.ref) === null || _a === void 0 ? void 0 : _a.name) === '@id'; })) {
|
|
132
|
+
return true;
|
|
133
|
+
}
|
|
134
|
+
// model-level @@id attribute with a list of fields
|
|
135
|
+
const model = field.$container;
|
|
136
|
+
const modelLevelIds = getIdFields(model);
|
|
137
|
+
if (modelLevelIds.includes(field)) {
|
|
138
|
+
return true;
|
|
139
|
+
}
|
|
140
|
+
return false;
|
|
141
|
+
}
|
|
142
|
+
exports.isIdField = isIdField;
|
|
143
|
+
/**
|
|
144
|
+
* Returns if the given field is a relation field.
|
|
145
|
+
*/
|
|
146
|
+
function isRelationshipField(field) {
|
|
147
|
+
var _a;
|
|
148
|
+
return (0, ast_1.isDataModel)((_a = field.type.reference) === null || _a === void 0 ? void 0 : _a.ref);
|
|
149
|
+
}
|
|
150
|
+
exports.isRelationshipField = isRelationshipField;
|
|
151
|
+
/**
|
|
152
|
+
* Returns if the given field is a relation foreign key field.
|
|
153
|
+
*/
|
|
154
|
+
function isForeignKeyField(field) {
|
|
155
|
+
const model = field.$container;
|
|
156
|
+
return model.fields.some((f) => {
|
|
157
|
+
// find @relation attribute
|
|
158
|
+
const relAttr = f.attributes.find((attr) => { var _a; return ((_a = attr.decl.ref) === null || _a === void 0 ? void 0 : _a.name) === '@relation'; });
|
|
159
|
+
if (relAttr) {
|
|
160
|
+
// find "fields" arg
|
|
161
|
+
const fieldsArg = relAttr.args.find((a) => { var _a; return ((_a = a.$resolvedParam) === null || _a === void 0 ? void 0 : _a.name) === 'fields'; });
|
|
162
|
+
if (fieldsArg && (0, ast_1.isArrayExpr)(fieldsArg.value)) {
|
|
163
|
+
// find a matching field reference
|
|
164
|
+
return fieldsArg.value.items.some((item) => {
|
|
165
|
+
if ((0, ast_1.isReferenceExpr)(item)) {
|
|
166
|
+
return item.target.ref === field;
|
|
167
|
+
}
|
|
168
|
+
else {
|
|
169
|
+
return false;
|
|
170
|
+
}
|
|
171
|
+
});
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
return false;
|
|
175
|
+
});
|
|
176
|
+
}
|
|
177
|
+
exports.isForeignKeyField = isForeignKeyField;
|
|
178
|
+
function resolvePath(_path, options) {
|
|
179
|
+
if (path_1.default.isAbsolute(_path)) {
|
|
180
|
+
return _path;
|
|
181
|
+
}
|
|
182
|
+
else {
|
|
183
|
+
return path_1.default.join(path_1.default.dirname(options.schemaPath), _path);
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
exports.resolvePath = resolvePath;
|
|
187
|
+
function requireOption(options, name) {
|
|
188
|
+
const value = options[name];
|
|
189
|
+
if (value === undefined) {
|
|
190
|
+
throw new Error(`Plugin "${options.name}" is missing required option: ${name}`);
|
|
191
|
+
}
|
|
192
|
+
return value;
|
|
193
|
+
}
|
|
194
|
+
exports.requireOption = requireOption;
|
|
105
195
|
//# sourceMappingURL=utils.js.map
|
package/utils.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"utils.js","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"utils.js","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":";;;;;;AAAA,kDAiBkC;AAClC,gDAAwB;AAGxB;;GAEG;AACH,SAAgB,aAAa,CAAC,KAAY;IACtC,OAAO,KAAK,CAAC,YAAY,CAAC,MAAM,CAAC,CAAC,CAAC,EAAkB,EAAE,CAAC,IAAA,iBAAW,EAAC,CAAC,CAAC,IAAI,CAAC,YAAY,CAAC,CAAC,EAAE,UAAU,CAAC,CAAC,CAAC;AAC5G,CAAC;AAFD,sCAEC;AAED,SAAgB,QAAQ,CAAoB,GAAiB;IACzD,IAAI,CAAC,GAAG,CAAC,GAAG,EAAE;QACV,MAAM,IAAI,KAAK,CAAC,2BAA2B,GAAG,CAAC,QAAQ,EA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