@contrail/cli 3.6.0-alpha.f45f126 → 3.7.0-alpha.1c66a91
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/README.md +207 -3
- package/dist/api/types.d.ts +35 -0
- package/dist/api/types.js +49 -1
- package/dist/auth/login.d.ts +1 -1
- package/dist/auth/login.js +46 -29
- package/dist/auth/login.spec.js +31 -15
- package/dist/auth/sign-in-error.d.ts +7 -0
- package/dist/auth/sign-in-error.js +24 -0
- package/dist/auth/sign-in-error.spec.d.ts +1 -0
- package/dist/auth/sign-in-error.spec.js +22 -0
- package/dist/auth/sso.d.ts +5 -3
- package/dist/auth/sso.js +34 -18
- package/dist/auth/sso.spec.js +63 -10
- package/dist/basecommand.d.ts +3 -0
- package/dist/basecommand.js +10 -3
- package/dist/commands/app/getApiKey.js +2 -2
- package/dist/commands/app/publish.js +1 -1
- package/dist/commands/app/version/prerelease.d.ts +11 -0
- package/dist/commands/app/version/prerelease.js +32 -0
- package/dist/commands/entities/create.d.ts +49 -0
- package/dist/commands/entities/create.js +618 -0
- package/dist/commands/entities/create.spec.d.ts +1 -0
- package/dist/commands/entities/create.spec.js +581 -0
- package/dist/commands/entities/delete.d.ts +20 -0
- package/dist/commands/entities/delete.js +146 -0
- package/dist/commands/entities/get.d.ts +25 -0
- package/dist/commands/entities/get.js +298 -0
- package/dist/commands/entities/grammar.spec.d.ts +1 -0
- package/dist/commands/entities/grammar.spec.js +79 -0
- package/dist/commands/entities/output.spec.d.ts +1 -0
- package/dist/commands/entities/output.spec.js +72 -0
- package/dist/commands/entities/update.d.ts +32 -0
- package/dist/commands/entities/update.js +212 -0
- package/dist/commands/login.js +1 -1
- package/dist/commands/webhooks/delete.js +2 -3
- package/dist/common.d.ts +10 -1
- package/dist/common.js +37 -1
- package/dist/common.spec.d.ts +1 -0
- package/dist/common.spec.js +40 -0
- package/dist/helpers/app/app-config-validators.js +4 -4
- package/dist/helpers/app/app-config-validators.spec.js +36 -0
- package/dist/helpers/app/app-deployer.d.ts +1 -0
- package/dist/helpers/app/app-deployer.js +34 -0
- package/dist/helpers/app/upgrade-version.d.ts +3 -1
- package/dist/helpers/app/upgrade-version.js +7 -8
- package/dist/helpers/app/upgrade-version.spec.js +15 -0
- package/dist/helpers/config/config.d.ts +3 -3
- package/dist/helpers/config/config.js +3 -3
- package/dist/helpers/entities/assortment-items.d.ts +11 -0
- package/dist/helpers/entities/assortment-items.js +76 -0
- package/dist/helpers/entities/batchFetch.d.ts +11 -0
- package/dist/helpers/entities/batchFetch.js +50 -0
- package/dist/helpers/entities/index.d.ts +3 -0
- package/dist/helpers/entities/index.js +6 -0
- package/dist/helpers/entities/items.d.ts +6 -0
- package/dist/helpers/entities/items.js +60 -0
- package/dist/helpers/entities/items.spec.d.ts +1 -0
- package/dist/helpers/entities/items.spec.js +19 -0
- package/dist/helpers/entities/project-items.d.ts +2 -0
- package/dist/helpers/entities/project-items.js +44 -0
- package/dist/helpers/file-helpers.d.ts +1 -0
- package/dist/helpers/file-helpers.js +20 -3
- package/dist/helpers/propertySpecifications/dateRanges.d.ts +34 -0
- package/dist/helpers/propertySpecifications/dateRanges.js +91 -0
- package/dist/helpers/propertySpecifications/dateRanges.spec.d.ts +1 -0
- package/dist/helpers/propertySpecifications/dateRanges.spec.js +97 -0
- package/dist/helpers/propertySpecifications/propertySpecifications.d.ts +39 -0
- package/dist/helpers/propertySpecifications/propertySpecifications.js +102 -0
- package/dist/helpers/propertySpecifications/propertySpecifications.spec.d.ts +1 -0
- package/dist/helpers/propertySpecifications/propertySpecifications.spec.js +387 -0
- package/dist/helpers/propertySpecifications/propertyValues.d.ts +31 -0
- package/dist/helpers/propertySpecifications/propertyValues.js +132 -0
- package/dist/helpers/propertySpecifications/propertyValues.spec.d.ts +1 -0
- package/dist/helpers/propertySpecifications/propertyValues.spec.js +156 -0
- package/dist/logger.js +4 -1
- package/dist/logger.spec.d.ts +1 -0
- package/dist/logger.spec.js +12 -0
- package/node_modules/@contrail/entity-compare/package.json +6 -3
- package/node_modules/@contrail/loader/lib/common/conditional-columns.js +1 -1
- package/node_modules/@contrail/loader/lib/constants.d.ts +0 -8
- package/node_modules/@contrail/loader/lib/constants.js +1 -9
- package/node_modules/@contrail/loader/lib/entities-v2/linesheet-v2/item-processor.d.ts +0 -4
- package/node_modules/@contrail/loader/lib/entities-v2/linesheet-v2/item-processor.js +0 -57
- package/node_modules/@contrail/loader/lib/entities-v2/linesheet-v2/item-processor.spec.js +34 -492
- package/node_modules/@contrail/loader/lib/pipeline/validate.js +52 -2
- package/node_modules/@contrail/loader/lib/pipeline/validate.spec.js +85 -0
- package/node_modules/@contrail/loader/package.json +2 -2
- package/node_modules/@contrail/sdk/.github/workflows/publish-to-npm.yml +1 -4
- package/node_modules/@contrail/sdk/lib/auth/login.js +1 -1
- package/node_modules/@contrail/sdk/lib/config/config.d.ts +3 -0
- package/node_modules/@contrail/sdk/lib/config/config.js +13 -7
- package/node_modules/@contrail/sdk/lib/core/request.js +6 -2
- package/node_modules/@contrail/sdk/lib/entities/entities.client.d.ts +2 -0
- package/node_modules/@contrail/sdk/lib/entities/entities.client.js +3 -1
- package/node_modules/@contrail/sdk/lib/entities/entity.resource.registry.js +1 -0
- package/node_modules/@contrail/sdk/lib/image-search/image-search.d.ts +34 -0
- package/node_modules/@contrail/sdk/lib/image-search/image-search.js +42 -0
- package/node_modules/@contrail/sdk/lib/image-search/index.d.ts +1 -0
- package/node_modules/@contrail/sdk/lib/image-search/index.js +17 -0
- package/node_modules/@contrail/sdk/lib/index.d.ts +1 -0
- package/node_modules/@contrail/sdk/lib/index.js +1 -0
- package/node_modules/@contrail/sdk/package.json +1 -1
- package/node_modules/@contrail/sdk/scripts/replicate-bug.js +69 -0
- package/oclif.manifest.json +492 -2
- package/package.json +6 -12
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { PropertyLevel } from '@contrail/types';
|
|
2
|
+
import { ColonDelimited, TypeProperty as ExtendedTypeProperty, TypeReferencesCacheByPath } from '../../api/types';
|
|
3
|
+
import { TypeWithProperties } from '../../api/types';
|
|
4
|
+
import { BaseCommand } from '../../basecommand';
|
|
5
|
+
import { Role } from '../../helpers/entities';
|
|
6
|
+
export default class CreateEntity extends BaseCommand {
|
|
7
|
+
static description: string;
|
|
8
|
+
static examples: string[];
|
|
9
|
+
static flags: {
|
|
10
|
+
count: import("@oclif/core/lib/interfaces").OptionFlag<number, import("@oclif/core/lib/interfaces/parser").CustomOptions>;
|
|
11
|
+
dryRun: import("@oclif/core/lib/interfaces").BooleanFlag<boolean>;
|
|
12
|
+
inputFile: import("@oclif/core/lib/interfaces").OptionFlag<string, import("@oclif/core/lib/interfaces/parser").CustomOptions>;
|
|
13
|
+
outputFile: import("@oclif/core/lib/interfaces").OptionFlag<string, import("@oclif/core/lib/interfaces/parser").CustomOptions>;
|
|
14
|
+
generateValues: import("@oclif/core/lib/interfaces").BooleanFlag<boolean>;
|
|
15
|
+
readableIds: import("@oclif/core/lib/interfaces").BooleanFlag<boolean>;
|
|
16
|
+
skipHydrateRelationships: import("@oclif/core/lib/interfaces").BooleanFlag<boolean>;
|
|
17
|
+
suffix: import("@oclif/core/lib/interfaces").OptionFlag<string, import("@oclif/core/lib/interfaces/parser").CustomOptions>;
|
|
18
|
+
apiVersion: import("@oclif/core/lib/interfaces").OptionFlag<string, import("@oclif/core/lib/interfaces/parser").CustomOptions>;
|
|
19
|
+
alwaysConfirmEnv: import("@oclif/core/lib/interfaces").OptionFlag<string, import("@oclif/core/lib/interfaces/parser").CustomOptions>;
|
|
20
|
+
logLevel: import("@oclif/core/lib/interfaces").OptionFlag<string, import("@oclif/core/lib/interfaces/parser").CustomOptions>;
|
|
21
|
+
};
|
|
22
|
+
static args: {
|
|
23
|
+
typePath: import("@oclif/core/lib/interfaces/parser").Arg<string, Record<string, unknown>>;
|
|
24
|
+
};
|
|
25
|
+
static strict: boolean;
|
|
26
|
+
init(): Promise<void>;
|
|
27
|
+
run(): Promise<void>;
|
|
28
|
+
}
|
|
29
|
+
export type CreateSummary = {
|
|
30
|
+
created: any[];
|
|
31
|
+
failed: {
|
|
32
|
+
input: any;
|
|
33
|
+
error: string;
|
|
34
|
+
}[];
|
|
35
|
+
};
|
|
36
|
+
export declare function summarizeSettledCreates(inputs: any[], settled: PromiseSettledResult<any>[]): CreateSummary;
|
|
37
|
+
export declare function pickRandomPropertyValues(type: TypeWithProperties, objectReferencesCache: ObjectReferenceCachesByTypePath, typeReferencesCache: TypeReferencesCacheByPath, roles?: Role[], suppliedSlugs?: Set<string>): any;
|
|
38
|
+
export type WithId = {
|
|
39
|
+
id: string;
|
|
40
|
+
[key: string]: any;
|
|
41
|
+
};
|
|
42
|
+
export type ObjectReferenceCache = Map<string, WithId>;
|
|
43
|
+
type ObjectReferenceCachesByTypePath = Map<string, ObjectReferenceCache>;
|
|
44
|
+
export declare function pickRandomValueForProperty(property: ExtendedTypeProperty, objectReferencesCache: ObjectReferenceCachesByTypePath, typeReferencesCache: TypeReferencesCacheByPath): number | string | string[] | boolean | null;
|
|
45
|
+
export declare function getReferencedObjectPaths(type: TypeWithProperties): ColonDelimited[];
|
|
46
|
+
export declare function buildObjectReferenceCache(entities: any[]): ObjectReferenceCache;
|
|
47
|
+
export declare function selectRandomObject(cache: ObjectReferenceCache): WithId;
|
|
48
|
+
export declare function doesPropertyLevelMatchRole(propertyLevel: PropertyLevel, role: Role): boolean;
|
|
49
|
+
export {};
|
|
@@ -0,0 +1,618 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.doesPropertyLevelMatchRole = exports.selectRandomObject = exports.buildObjectReferenceCache = exports.getReferencedObjectPaths = exports.pickRandomValueForProperty = exports.pickRandomPropertyValues = exports.summarizeSettledCreates = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const fs_1 = tslib_1.__importDefault(require("fs"));
|
|
6
|
+
const unique_names_generator_1 = require("unique-names-generator");
|
|
7
|
+
const p_limit_1 = tslib_1.__importDefault(require("p-limit"));
|
|
8
|
+
const types_1 = require("@contrail/types");
|
|
9
|
+
const types_2 = require("../../api/types");
|
|
10
|
+
const core_1 = require("@oclif/core");
|
|
11
|
+
const types_3 = require("../../api/types");
|
|
12
|
+
const login_1 = require("../../auth/login");
|
|
13
|
+
const common_1 = require("../../common");
|
|
14
|
+
const basecommand_1 = require("../../basecommand");
|
|
15
|
+
const sdk_1 = require("@contrail/sdk");
|
|
16
|
+
const entities_1 = require("../../helpers/entities");
|
|
17
|
+
const propertyValues_1 = require("../../helpers/propertySpecifications/propertyValues");
|
|
18
|
+
const propertySpecifications_1 = require("../../helpers/propertySpecifications/propertySpecifications");
|
|
19
|
+
const file_helpers_1 = require("../../helpers/file-helpers");
|
|
20
|
+
let IS_DRY_RUN = false;
|
|
21
|
+
let SHOULD_USE_READABLE_IDS = false;
|
|
22
|
+
let SHOULD_SKIP_HYDRATE_RELATIONSHIPS = false;
|
|
23
|
+
let SHOULD_GENERATE_MISSING_VALUES = false;
|
|
24
|
+
let NUM_ENTITIES_TO_CREATE = 1; // Default to creating one entity if not specified
|
|
25
|
+
const getNumEntitiesToCreate = () => NUM_ENTITIES_TO_CREATE; // method to get the latest number in case this changes later
|
|
26
|
+
const SLUGS_THAT_MUST_BE_SPECIFIED_BY_TYPE_PATH = {
|
|
27
|
+
item: ['roles'],
|
|
28
|
+
'project-item': ['projectId'],
|
|
29
|
+
'assortment-item': ['assortmentId'],
|
|
30
|
+
'user-org': ['role'],
|
|
31
|
+
};
|
|
32
|
+
function throwIfParentsAreMixed(entities, parentSlug, parentLabel) {
|
|
33
|
+
const parentIds = new Set(entities.map((entity) => entity[parentSlug]));
|
|
34
|
+
if (parentIds.size > 1) {
|
|
35
|
+
throw new Error(`Generated item ids are drawn from one ${parentLabel}'s candidates, but the input spans ${parentIds.size} ${parentLabel}s. Run one ${parentLabel} per command, or supply itemId for each row.`);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
const FIND_VALID_VALUE_MAP = {
|
|
39
|
+
assortment: [
|
|
40
|
+
{
|
|
41
|
+
slug: 'name',
|
|
42
|
+
findValidValuesFn: async (assortments) => new Set(Array.from({ length: assortments.length }, () => generateRandomReadableString())),
|
|
43
|
+
applyValueToEntityFn: (entity, validValues) => {
|
|
44
|
+
if (entity.name)
|
|
45
|
+
return; // skip if already specified
|
|
46
|
+
entity.name = Array.from(validValues)[Math.floor(Math.random() * validValues.size)];
|
|
47
|
+
validValues.delete(entity.name);
|
|
48
|
+
},
|
|
49
|
+
},
|
|
50
|
+
],
|
|
51
|
+
'assortment-item': [
|
|
52
|
+
{
|
|
53
|
+
slug: 'itemId',
|
|
54
|
+
findValidValuesFn: async (assortmentItems, count) => {
|
|
55
|
+
throwIfParentsAreMixed(assortmentItems, 'assortmentId', 'assortment');
|
|
56
|
+
return new Set((await (0, entities_1.findItemsNotInAssortment)(assortmentItems[0].assortmentId, count)).map(({ id }) => id));
|
|
57
|
+
},
|
|
58
|
+
applyValueToEntityFn: (entity, validValues) => {
|
|
59
|
+
if (entity.itemId)
|
|
60
|
+
return; // skip if already specified
|
|
61
|
+
entity.itemId = Array.from(validValues)[Math.floor(Math.random() * validValues.size)];
|
|
62
|
+
validValues.delete(entity.itemId);
|
|
63
|
+
},
|
|
64
|
+
},
|
|
65
|
+
],
|
|
66
|
+
'project-item': [
|
|
67
|
+
{
|
|
68
|
+
slug: 'itemId',
|
|
69
|
+
findValidValuesFn: async (projectItems) => {
|
|
70
|
+
throwIfParentsAreMixed(projectItems, 'projectId', 'project');
|
|
71
|
+
return new Set((await (0, entities_1.findItemsNotInProject)(projectItems[0].projectId)).map(({ id }) => id));
|
|
72
|
+
},
|
|
73
|
+
applyValueToEntityFn: (entity, validValues) => {
|
|
74
|
+
if (entity.itemId)
|
|
75
|
+
return; // skip if already specified
|
|
76
|
+
entity.itemId = Array.from(validValues)[Math.floor(Math.random() * validValues.size)];
|
|
77
|
+
validValues.delete(entity.itemId);
|
|
78
|
+
},
|
|
79
|
+
},
|
|
80
|
+
],
|
|
81
|
+
item: [
|
|
82
|
+
{
|
|
83
|
+
slug: 'itemFamilyId',
|
|
84
|
+
findValidValuesFn: async (_items) => new Set(await (0, entities_1.getItemFamilies)(getNumEntitiesToCreate()).then((families) => families === undefined ? undefined : families.map(({ id }) => id))),
|
|
85
|
+
applyValueToEntityFn: (entity, validValues) => {
|
|
86
|
+
if (entity.itemFamilyId)
|
|
87
|
+
return; // skip if already specified
|
|
88
|
+
if (entity.roles?.includes('option')) {
|
|
89
|
+
entity.itemFamilyId =
|
|
90
|
+
Array.from(validValues)[Math.floor(Math.random() * validValues.size)];
|
|
91
|
+
// can reuse itemFamilyId for options
|
|
92
|
+
}
|
|
93
|
+
},
|
|
94
|
+
},
|
|
95
|
+
{
|
|
96
|
+
slug: 'name',
|
|
97
|
+
findValidValuesFn: async (items) => new Set(Array.from({ length: items.length }, () => generateRandomReadableString())),
|
|
98
|
+
applyValueToEntityFn: (entity, validValues) => {
|
|
99
|
+
if (entity.name)
|
|
100
|
+
return; // skip if already specified
|
|
101
|
+
entity.name = Array.from(validValues)[Math.floor(Math.random() * validValues.size)];
|
|
102
|
+
validValues.delete(entity.name);
|
|
103
|
+
},
|
|
104
|
+
},
|
|
105
|
+
{
|
|
106
|
+
slug: 'optionName',
|
|
107
|
+
findValidValuesFn: async (items) => new Set(Array.from({ length: items.length }, () => generateRandomReadableString())),
|
|
108
|
+
applyValueToEntityFn: (entity, validValues) => {
|
|
109
|
+
if (entity.optionName)
|
|
110
|
+
return; // skip if already specified
|
|
111
|
+
if (!entity.roles?.includes('option'))
|
|
112
|
+
return; // only apply to options
|
|
113
|
+
entity.optionName = Array.from(validValues)[Math.floor(Math.random() * validValues.size)];
|
|
114
|
+
validValues.delete(entity.optionName);
|
|
115
|
+
},
|
|
116
|
+
},
|
|
117
|
+
{
|
|
118
|
+
slug: 'optionGroup',
|
|
119
|
+
findValidValuesFn: async (_items) => new Set(),
|
|
120
|
+
applyValueToEntityFn: (entity, _validValues) => {
|
|
121
|
+
if (entity.optionGroup)
|
|
122
|
+
return; // skip if already specified
|
|
123
|
+
if (!entity.roles?.includes('option'))
|
|
124
|
+
return; // only apply to options
|
|
125
|
+
entity.optionGroup = 'color';
|
|
126
|
+
},
|
|
127
|
+
},
|
|
128
|
+
],
|
|
129
|
+
workspace: [
|
|
130
|
+
{
|
|
131
|
+
slug: 'name',
|
|
132
|
+
findValidValuesFn: async (items) => new Set(Array.from({ length: items.length }, () => generateRandomReadableString())),
|
|
133
|
+
applyValueToEntityFn: (entity, validValues) => {
|
|
134
|
+
if (entity.name)
|
|
135
|
+
return; // skip if already specified
|
|
136
|
+
entity.name = Array.from(validValues)[Math.floor(Math.random() * validValues.size)];
|
|
137
|
+
validValues.delete(entity.name);
|
|
138
|
+
},
|
|
139
|
+
},
|
|
140
|
+
],
|
|
141
|
+
};
|
|
142
|
+
class CreateEntity extends basecommand_1.BaseCommand {
|
|
143
|
+
static description = 'Create entities';
|
|
144
|
+
static examples = [
|
|
145
|
+
'contrail entities create color -g -n=10 # creates 10 random colors',
|
|
146
|
+
'contrail entities create color -- name="Electric Blue" green=1 red=2 blue=3 # creates one color with specified properties only',
|
|
147
|
+
'contrail entities create custom-entity:franchise',
|
|
148
|
+
'contrail entities create project-item -n=10 -- projectId=abc # creates 10 project items within project ABC',
|
|
149
|
+
'contrail entities create color -g -r # creates a random color with a human readable ID',
|
|
150
|
+
'contrail entities create color -d -- name=test # simulates the creation of a color without actually creating it',
|
|
151
|
+
'contrail entities create custom-entity:franchise -S # creates an entity and the response does not contain hydrated relationships',
|
|
152
|
+
'contrail entities create color -g -- name="Electric Blue" # creates a color with the name specified and other properties auto-generated',
|
|
153
|
+
'contrail entities create color -o /tmp/colors.json -- name=Blue green=1 red=2 blue=3 # creates a color and saves the output to /tmp/colors.json',
|
|
154
|
+
];
|
|
155
|
+
static flags = {
|
|
156
|
+
...basecommand_1.BaseCommand.baseFlags,
|
|
157
|
+
count: core_1.Flags.integer({
|
|
158
|
+
char: 'n',
|
|
159
|
+
description: 'Number of random entities to create',
|
|
160
|
+
required: false,
|
|
161
|
+
}),
|
|
162
|
+
dryRun: core_1.Flags.boolean({
|
|
163
|
+
char: 'd',
|
|
164
|
+
description: 'If true, does not create the entity but simulates the creation',
|
|
165
|
+
required: false,
|
|
166
|
+
default: false,
|
|
167
|
+
}),
|
|
168
|
+
inputFile: core_1.Flags.string({
|
|
169
|
+
char: 'i',
|
|
170
|
+
description: 'A file with a JSON array of objects where each object is one entity to create. Cannot be used with --count. Unless --generateValues is specified, only the properties in the file will be used.',
|
|
171
|
+
required: false,
|
|
172
|
+
multiple: false,
|
|
173
|
+
}),
|
|
174
|
+
outputFile: core_1.Flags.string({
|
|
175
|
+
char: 'o',
|
|
176
|
+
description: 'File to save the output (optional)',
|
|
177
|
+
required: false,
|
|
178
|
+
multiple: false,
|
|
179
|
+
}),
|
|
180
|
+
generateValues: core_1.Flags.boolean({
|
|
181
|
+
char: 'g',
|
|
182
|
+
description: 'If true, generates random values for properties that are not specified. By default, only specified properties are used.',
|
|
183
|
+
required: false,
|
|
184
|
+
default: false,
|
|
185
|
+
}),
|
|
186
|
+
readableIds: core_1.Flags.boolean({
|
|
187
|
+
char: 'r',
|
|
188
|
+
description: 'If true, uses readable IDs for the created entities',
|
|
189
|
+
required: false,
|
|
190
|
+
default: false,
|
|
191
|
+
}),
|
|
192
|
+
skipHydrateRelationships: core_1.Flags.boolean({
|
|
193
|
+
char: 'S',
|
|
194
|
+
description: 'If true, does not hydrate relationships for the created entities',
|
|
195
|
+
required: false,
|
|
196
|
+
default: false,
|
|
197
|
+
}),
|
|
198
|
+
suffix: core_1.Flags.string({
|
|
199
|
+
description: 'Append a sub-route to the entity endpoint (e.g. --suffix=synchronous targets /<entities>/synchronous)',
|
|
200
|
+
required: false,
|
|
201
|
+
}),
|
|
202
|
+
apiVersion: core_1.Flags.string({
|
|
203
|
+
description: 'Api-Version header to send; selects the V1 or V2 variant of the endpoint',
|
|
204
|
+
options: ['1', '2'],
|
|
205
|
+
required: false,
|
|
206
|
+
}),
|
|
207
|
+
};
|
|
208
|
+
static args = {
|
|
209
|
+
typePath: core_1.Args.string({
|
|
210
|
+
description: 'The type path of the entity to create (e.g. color, custom-entity:franchise)',
|
|
211
|
+
required: true,
|
|
212
|
+
multiple: false,
|
|
213
|
+
}),
|
|
214
|
+
};
|
|
215
|
+
static strict = false;
|
|
216
|
+
async init() {
|
|
217
|
+
(0, common_1.routeConsoleLogToStderr)();
|
|
218
|
+
await super.init();
|
|
219
|
+
}
|
|
220
|
+
async run() {
|
|
221
|
+
await (0, login_1.authUser)();
|
|
222
|
+
const { flags, argv, args } = await this.parse(CreateEntity);
|
|
223
|
+
const { count, dryRun: isDryRun, inputFile: inputFilename, outputFile: outputFilename, generateValues: shouldGenerateMissingValues, readableIds: shouldUseReadableIds, skipHydrateRelationships, suffix, apiVersion, } = flags;
|
|
224
|
+
const { typePath: entityTypePath } = args;
|
|
225
|
+
// Global flags is a little hacky, but it was easier
|
|
226
|
+
// and it doesn't see *that* bad because they are only valid
|
|
227
|
+
// for the lifecycle of the command.
|
|
228
|
+
IS_DRY_RUN = isDryRun;
|
|
229
|
+
SHOULD_USE_READABLE_IDS = shouldUseReadableIds;
|
|
230
|
+
SHOULD_SKIP_HYDRATE_RELATIONSHIPS = skipHydrateRelationships;
|
|
231
|
+
SHOULD_GENERATE_MISSING_VALUES = shouldGenerateMissingValues;
|
|
232
|
+
const { ids: strayTokens, specs } = (0, propertySpecifications_1.partitionIdsAndSpecs)(argv.slice(1));
|
|
233
|
+
if (strayTokens.length > 0) {
|
|
234
|
+
this.error(`create takes no positional ids (got: ${strayTokens.join(' ')}); did you forget -- before property values?`);
|
|
235
|
+
}
|
|
236
|
+
const propertySpecBySlug = (0, propertySpecifications_1.parsePropertySpecificationsIntoMap)(specs);
|
|
237
|
+
if (Array.isArray(propertySpecBySlug.get('typePath'))) {
|
|
238
|
+
this.error('typePath takes a single value. Repeat the key only for multi-value properties.');
|
|
239
|
+
}
|
|
240
|
+
// Validate args
|
|
241
|
+
if (!entityTypePath) {
|
|
242
|
+
this.error('You must specify an entity type path.');
|
|
243
|
+
}
|
|
244
|
+
if (inputFilename && count) {
|
|
245
|
+
throw new Error('You cannot use --inputFile and --count together. Please specify one or the other.');
|
|
246
|
+
}
|
|
247
|
+
if (count !== undefined && count <= 0) {
|
|
248
|
+
this.error('Count must be a positive integer.');
|
|
249
|
+
}
|
|
250
|
+
const unknownSubtype = await findUnknownSubtypeError(entityTypePath);
|
|
251
|
+
if (unknownSubtype) {
|
|
252
|
+
this.error(unknownSubtype);
|
|
253
|
+
}
|
|
254
|
+
const partialEntities = inputFilename
|
|
255
|
+
? (0, file_helpers_1.parseFileAsJsonArray)(inputFilename)
|
|
256
|
+
: Array.from({ length: count || 1 }).fill({});
|
|
257
|
+
NUM_ENTITIES_TO_CREATE = partialEntities.length;
|
|
258
|
+
const { root: entityName } = (0, types_2.parseTypePath)(entityTypePath);
|
|
259
|
+
// Ensure we have the required properties for the entity type
|
|
260
|
+
if (SLUGS_THAT_MUST_BE_SPECIFIED_BY_TYPE_PATH[entityName]) {
|
|
261
|
+
const requiredSlugs = SLUGS_THAT_MUST_BE_SPECIFIED_BY_TYPE_PATH[entityName];
|
|
262
|
+
for (const slug of requiredSlugs) {
|
|
263
|
+
// Check if the property is specified in command-line args
|
|
264
|
+
const isInCommandLineArgs = propertySpecBySlug.has(slug);
|
|
265
|
+
// Check if all entities from input file have this property
|
|
266
|
+
const allEntitiesHaveProperty = inputFilename
|
|
267
|
+
? partialEntities.every((entity) => entity[slug] !== undefined && entity[slug] !== null)
|
|
268
|
+
: false;
|
|
269
|
+
if (!isInCommandLineArgs && !allEntitiesHaveProperty) {
|
|
270
|
+
this.error(`Property '${slug}' must be specified for entity type '${entityName}'.`);
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
try {
|
|
275
|
+
const entitiesToCreate = await buildEntitiesToCreate(entityTypePath, partialEntities, propertySpecBySlug);
|
|
276
|
+
const summary = await createEntities(entityName, entitiesToCreate, {
|
|
277
|
+
suffix,
|
|
278
|
+
apiVersion: apiVersion,
|
|
279
|
+
});
|
|
280
|
+
if (outputFilename) {
|
|
281
|
+
fs_1.default.writeFileSync(outputFilename, JSON.stringify(summary, null, 2));
|
|
282
|
+
console.error(`Output saved to ${outputFilename}`);
|
|
283
|
+
}
|
|
284
|
+
else {
|
|
285
|
+
this.log(JSON.stringify(summary, null, 2));
|
|
286
|
+
}
|
|
287
|
+
if (summary.failed.length > 0) {
|
|
288
|
+
console.error(`${summary.failed.length} of ${entitiesToCreate.length} entities failed.`);
|
|
289
|
+
process.exitCode = 1;
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
catch (error) {
|
|
293
|
+
this.error(`Failed to create entities: ${error.message}, ${error.stack}`);
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
exports.default = CreateEntity;
|
|
298
|
+
function summarizeSettledCreates(inputs, settled) {
|
|
299
|
+
const created = [];
|
|
300
|
+
const failed = [];
|
|
301
|
+
settled.forEach((result, index) => {
|
|
302
|
+
if (result.status === 'fulfilled') {
|
|
303
|
+
created.push(result.value);
|
|
304
|
+
}
|
|
305
|
+
else {
|
|
306
|
+
failed.push({ input: inputs[index], error: (0, common_1.sdkErrorMessage)(result.reason) });
|
|
307
|
+
}
|
|
308
|
+
});
|
|
309
|
+
return { created, failed };
|
|
310
|
+
}
|
|
311
|
+
exports.summarizeSettledCreates = summarizeSettledCreates;
|
|
312
|
+
async function createEntities(entityName, entities, routing = {}) {
|
|
313
|
+
if (IS_DRY_RUN) {
|
|
314
|
+
console.error(`Dry run mode: not creating entities, but simulating creation for ${entityName}`);
|
|
315
|
+
return { created: entities, failed: [] };
|
|
316
|
+
}
|
|
317
|
+
const limit = (0, p_limit_1.default)(20);
|
|
318
|
+
const settled = await Promise.allSettled(entities.map(async (e) => limit(() => createEntity(entityName, e, routing))));
|
|
319
|
+
return summarizeSettledCreates(entities, settled);
|
|
320
|
+
}
|
|
321
|
+
/**
|
|
322
|
+
* A subtype that does not exist under a type-managed root would otherwise be dropped,
|
|
323
|
+
* creating a root entity instead. The API cannot catch that, because the request it
|
|
324
|
+
* receives is a valid root create. A root that does not exist is left to the API.
|
|
325
|
+
*/
|
|
326
|
+
async function findUnknownSubtypeError(entityTypePath) {
|
|
327
|
+
const { root } = (0, types_2.parseTypePath)(entityTypePath);
|
|
328
|
+
if (root === entityTypePath || (await (0, types_3.getTypeByPath)(entityTypePath))) {
|
|
329
|
+
return undefined;
|
|
330
|
+
}
|
|
331
|
+
return (await (0, types_3.getTypeByPath)(root))
|
|
332
|
+
? `Type not found for path: ${entityTypePath}. The root ${root} exists, so this subtype is misspelled or missing.`
|
|
333
|
+
: undefined;
|
|
334
|
+
}
|
|
335
|
+
async function buildEntitiesToCreate(entityTypePath, entities, propertySpecificationBySlug) {
|
|
336
|
+
let type = await (0, types_3.getTypeByPath)(entityTypePath);
|
|
337
|
+
if (!type) {
|
|
338
|
+
console.warn(`Type not found for path: ${entityTypePath}`);
|
|
339
|
+
// an untyped entity such as a webhook has no type record; keep the requested path
|
|
340
|
+
// so the root still resolves for the generated-value lookups
|
|
341
|
+
type = { typePath: entityTypePath, typeProperties: [] };
|
|
342
|
+
}
|
|
343
|
+
const [typesCache, objectReferencesCache] = await Promise.all([
|
|
344
|
+
(0, types_2.buildLocalTypesCache)([...(0, types_2.getReferencedTypePaths)(type), entityTypePath]),
|
|
345
|
+
buildLocalObjectReferencesCache(getReferencedObjectPaths(type)),
|
|
346
|
+
]);
|
|
347
|
+
const populatedEntities = entities.map((entity) => ({
|
|
348
|
+
...createEntityWithPopulatedValues(type, typesCache, objectReferencesCache, propertySpecificationBySlug, entity),
|
|
349
|
+
...entity,
|
|
350
|
+
}));
|
|
351
|
+
// Skip system value generation unless -g flag is set (generateValues mode)
|
|
352
|
+
if (!SHOULD_GENERATE_MISSING_VALUES) {
|
|
353
|
+
console.error('Using only specified properties. Use --generateValues to auto-generate missing values.');
|
|
354
|
+
return populatedEntities;
|
|
355
|
+
}
|
|
356
|
+
// Add system values if needed
|
|
357
|
+
const { root } = (0, types_2.parseTypePath)(type.typePath);
|
|
358
|
+
const valueFinderApplierForEntity = FIND_VALID_VALUE_MAP[root] || [];
|
|
359
|
+
for (const { slug, findValidValuesFn, applyValueToEntityFn } of valueFinderApplierForEntity) {
|
|
360
|
+
// If the property is already specified in command-line args, skip it
|
|
361
|
+
if (propertySpecificationBySlug?.has(slug)) {
|
|
362
|
+
console.error(`Skipping property ${slug} because it is already specified.`);
|
|
363
|
+
continue;
|
|
364
|
+
}
|
|
365
|
+
// If all entities already have this property, skip finding values
|
|
366
|
+
const allEntitiesHaveProperty = populatedEntities.every((entity) => entity[slug] !== undefined && entity[slug] !== null);
|
|
367
|
+
if (allEntitiesHaveProperty) {
|
|
368
|
+
console.error(`Skipping property ${slug} because all entities already have this value.`);
|
|
369
|
+
continue;
|
|
370
|
+
}
|
|
371
|
+
const values = await findValidValuesFn(populatedEntities, entities.length);
|
|
372
|
+
if (values) {
|
|
373
|
+
populatedEntities.forEach((e) => applyValueToEntityFn(e, values));
|
|
374
|
+
}
|
|
375
|
+
else {
|
|
376
|
+
console.warn(`No valid values found for property ${slug}. Skipping.`);
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
return populatedEntities;
|
|
380
|
+
}
|
|
381
|
+
function createEntityWithPopulatedValues(type, typeReferencesCache, objectReferencesCache, propertySpecificationBySlug, inputRow) {
|
|
382
|
+
const rolesValue = inputRow?.roles ?? propertySpecificationBySlug?.get('roles');
|
|
383
|
+
const roles = rolesValue === undefined ? undefined : [].concat(rolesValue);
|
|
384
|
+
const suppliedSlugs = new Set([...(propertySpecificationBySlug?.entries() ?? []), ...Object.entries(inputRow ?? {})]
|
|
385
|
+
.filter(([, value]) => value !== undefined)
|
|
386
|
+
.map(([slug]) => slug));
|
|
387
|
+
const entity = SHOULD_GENERATE_MISSING_VALUES
|
|
388
|
+
? pickRandomPropertyValues(type, objectReferencesCache, typeReferencesCache, roles, suppliedSlugs)
|
|
389
|
+
: {};
|
|
390
|
+
// Handle property specifications
|
|
391
|
+
for (const [propertySlug, specifiedValue] of propertySpecificationBySlug.entries()) {
|
|
392
|
+
const correspondingProperty = type.typeProperties.find((prop) => (0, propertyValues_1.getPropertySlug)(prop) === propertySlug);
|
|
393
|
+
if (specifiedValue === undefined) {
|
|
394
|
+
continue;
|
|
395
|
+
}
|
|
396
|
+
entity[propertySlug] = (0, propertyValues_1.castValueToPropertyType)(specifiedValue, correspondingProperty?.propertyType || types_1.PropertyType.String, propertySlug);
|
|
397
|
+
}
|
|
398
|
+
// Give Readbable IDs if specified
|
|
399
|
+
if (SHOULD_USE_READABLE_IDS) {
|
|
400
|
+
entity.specifiedId = generateRandomReadableString();
|
|
401
|
+
}
|
|
402
|
+
// roles is a multi-value system property with no type property to drive
|
|
403
|
+
// coercion; a single value still has to reach the API as an array
|
|
404
|
+
if (typeof entity.roles === 'string') {
|
|
405
|
+
if (entity.roles.includes(',')) {
|
|
406
|
+
console.error(`roles value "${entity.roles}" contains a comma and is treated as one literal value. To pass several roles, repeat the key: roles=${entity.roles
|
|
407
|
+
.split(',')
|
|
408
|
+
.join(' roles=')}`);
|
|
409
|
+
}
|
|
410
|
+
entity.roles = [entity.roles];
|
|
411
|
+
}
|
|
412
|
+
// Support specifiedId
|
|
413
|
+
if (propertySpecificationBySlug?.has('specifiedId')) {
|
|
414
|
+
const specifiedId = propertySpecificationBySlug.get('specifiedId');
|
|
415
|
+
entity.specifiedId = specifiedId;
|
|
416
|
+
}
|
|
417
|
+
// There are some required values for certain entity types
|
|
418
|
+
const requiredValuesForType = requiredValues(type.typePath, typeReferencesCache);
|
|
419
|
+
if (requiredValuesForType) {
|
|
420
|
+
requiredValuesForType.forEach(({ slug, value }) => {
|
|
421
|
+
entity[slug] = value;
|
|
422
|
+
});
|
|
423
|
+
}
|
|
424
|
+
return entity;
|
|
425
|
+
}
|
|
426
|
+
function pickRandomPropertyValues(type, objectReferencesCache, typeReferencesCache, roles, suppliedSlugs) {
|
|
427
|
+
const entity = {};
|
|
428
|
+
// Get values for properties
|
|
429
|
+
for (const property of type.typeProperties) {
|
|
430
|
+
// Validate property level
|
|
431
|
+
if (roles?.length && roles.every((r) => !doesPropertyLevelMatchRole(property.propertyLevel, r)))
|
|
432
|
+
continue;
|
|
433
|
+
if (suppliedSlugs?.has((0, propertyValues_1.getPropertySlug)(property)))
|
|
434
|
+
continue;
|
|
435
|
+
// Generate random value
|
|
436
|
+
if (property.propertyType === types_1.PropertyType.ObjectReference) {
|
|
437
|
+
const cache = objectReferencesCache.get(property.referencedTypePath);
|
|
438
|
+
if (!cache || cache.size === 0) {
|
|
439
|
+
throw new Error(`No object references found for referencedTypePath: ${property.referencedTypePath}`);
|
|
440
|
+
}
|
|
441
|
+
entity[(0, propertyValues_1.getPropertySlug)(property)] = selectRandomObject(cache).id;
|
|
442
|
+
}
|
|
443
|
+
else {
|
|
444
|
+
entity[(0, propertyValues_1.getPropertySlug)(property)] = pickRandomValueForProperty(property, objectReferencesCache, typeReferencesCache);
|
|
445
|
+
}
|
|
446
|
+
}
|
|
447
|
+
return entity;
|
|
448
|
+
}
|
|
449
|
+
exports.pickRandomPropertyValues = pickRandomPropertyValues;
|
|
450
|
+
function pickRandomValueForProperty(property, objectReferencesCache, typeReferencesCache) {
|
|
451
|
+
const getRandomObjectReference = (referencedTypePath) => {
|
|
452
|
+
if (!objectReferencesCache.has(referencedTypePath)) {
|
|
453
|
+
throw new Error(`No object references found for referencedTypePath: ${referencedTypePath}`);
|
|
454
|
+
}
|
|
455
|
+
const cache = objectReferencesCache.get(referencedTypePath);
|
|
456
|
+
if (cache.size === 0) {
|
|
457
|
+
return null;
|
|
458
|
+
}
|
|
459
|
+
const randomIndex = Math.floor(Math.random() * cache.size);
|
|
460
|
+
const randomObjectReference = Array.from(cache.values())[randomIndex];
|
|
461
|
+
return randomObjectReference;
|
|
462
|
+
};
|
|
463
|
+
switch (property.propertyType) {
|
|
464
|
+
case types_1.PropertyType.String:
|
|
465
|
+
return generateRandomReadableString();
|
|
466
|
+
case types_1.PropertyType.Text:
|
|
467
|
+
return generateRandomReadableString();
|
|
468
|
+
case types_1.PropertyType.Boolean:
|
|
469
|
+
return generateRandomBooleanValue();
|
|
470
|
+
case types_1.PropertyType.Number:
|
|
471
|
+
return generateRandomNumber();
|
|
472
|
+
case types_1.PropertyType.Currency:
|
|
473
|
+
return generateRandomNumber();
|
|
474
|
+
case types_1.PropertyType.SingleSelect:
|
|
475
|
+
if (!property.options)
|
|
476
|
+
throw new Error(`Option set is required for SingleSelect property ${property.slug}, ${JSON.stringify(property, null, 2)}`);
|
|
477
|
+
return pickRandomOptions(property.options, 1)[0].value;
|
|
478
|
+
case types_1.PropertyType.MultiSelect:
|
|
479
|
+
if (!property.options)
|
|
480
|
+
throw new Error('Option set is required for MultiSelect property');
|
|
481
|
+
const randomSelection = pickRandomOptions(property.options, 1 + Math.floor(Math.random() * property.options.length));
|
|
482
|
+
return randomSelection.map((option) => option.value);
|
|
483
|
+
case types_1.PropertyType.Sequence:
|
|
484
|
+
return generateRandomNumber();
|
|
485
|
+
case types_1.PropertyType.Date:
|
|
486
|
+
return generateRandomDateString();
|
|
487
|
+
case types_1.PropertyType.Percent:
|
|
488
|
+
return generateRandomNumber(0, 100);
|
|
489
|
+
case types_1.PropertyType.ObjectReference:
|
|
490
|
+
const { referencedTypePath: typeForObjectReference } = property;
|
|
491
|
+
const randomObjectReference = getRandomObjectReference(typeForObjectReference);
|
|
492
|
+
if (!randomObjectReference) {
|
|
493
|
+
throw new Error(`No object references found for referencedTypePath: ${typeForObjectReference}`);
|
|
494
|
+
}
|
|
495
|
+
return randomObjectReference.id; // Assuming the object reference has an 'id' field
|
|
496
|
+
case types_1.PropertyType.TypeReference:
|
|
497
|
+
const { referencedTypePath } = property;
|
|
498
|
+
const type = typeReferencesCache.get(referencedTypePath);
|
|
499
|
+
if (!type) {
|
|
500
|
+
throw new Error(`Referenced type not found for path: ${referencedTypePath}`);
|
|
501
|
+
}
|
|
502
|
+
return type.id;
|
|
503
|
+
case types_1.PropertyType.Formula:
|
|
504
|
+
return undefined; // Placeholder for formula properties because they are evaluated on save anyway
|
|
505
|
+
default:
|
|
506
|
+
throw new Error(`Cannot handle property type: ${property.propertyType}.\n\n See property: ${JSON.stringify(property, null, 2)}`);
|
|
507
|
+
// TODO: Handle UserList
|
|
508
|
+
// TODO: Handle ObjectReferenceList
|
|
509
|
+
}
|
|
510
|
+
}
|
|
511
|
+
exports.pickRandomValueForProperty = pickRandomValueForProperty;
|
|
512
|
+
function pickRandomOptions(options, count) {
|
|
513
|
+
const shuffled = [...options].sort(() => 0.5 - Math.random());
|
|
514
|
+
return shuffled.slice(0, count);
|
|
515
|
+
}
|
|
516
|
+
function generateRandomBooleanValue() {
|
|
517
|
+
return Math.random() < 0.5;
|
|
518
|
+
}
|
|
519
|
+
function generateRandomNumber(min, max) {
|
|
520
|
+
if (min === undefined) {
|
|
521
|
+
min = 0;
|
|
522
|
+
}
|
|
523
|
+
if (max === undefined) {
|
|
524
|
+
max = 100;
|
|
525
|
+
}
|
|
526
|
+
return Math.floor(Math.random() * (max - min + 1)) + min;
|
|
527
|
+
}
|
|
528
|
+
function generateRandomReadableString() {
|
|
529
|
+
return (0, unique_names_generator_1.uniqueNamesGenerator)({
|
|
530
|
+
dictionaries: [unique_names_generator_1.adjectives, unique_names_generator_1.animals, unique_names_generator_1.colors, unique_names_generator_1.animals, unique_names_generator_1.colors],
|
|
531
|
+
length: 5,
|
|
532
|
+
});
|
|
533
|
+
}
|
|
534
|
+
function generateRandomDateString() {
|
|
535
|
+
return new Date().toISOString();
|
|
536
|
+
}
|
|
537
|
+
async function createEntity(entityName, entity, routing = {}) {
|
|
538
|
+
const client = new sdk_1.Entities();
|
|
539
|
+
const skipHydrateRelationships = SHOULD_SKIP_HYDRATE_RELATIONSHIPS;
|
|
540
|
+
const { suffix, apiVersion } = routing;
|
|
541
|
+
if (entityName === 'assortment-item' && !suffix)
|
|
542
|
+
return (0, entities_1.createAssortmentItem)(entity, { skipHydrateRelationships, apiVersion }); // special case
|
|
543
|
+
const response = await client.create({
|
|
544
|
+
entityName,
|
|
545
|
+
object: entity,
|
|
546
|
+
skipHydrateRelationships,
|
|
547
|
+
suffix,
|
|
548
|
+
apiVersion,
|
|
549
|
+
});
|
|
550
|
+
return response;
|
|
551
|
+
}
|
|
552
|
+
function getReferencedObjectPaths(type) {
|
|
553
|
+
const properties = type.typeProperties || [];
|
|
554
|
+
return properties
|
|
555
|
+
.filter((prop) => prop.propertyType === types_1.PropertyType.ObjectReference)
|
|
556
|
+
.map(({ referencedTypePath }) => referencedTypePath);
|
|
557
|
+
}
|
|
558
|
+
exports.getReferencedObjectPaths = getReferencedObjectPaths;
|
|
559
|
+
async function buildLocalObjectReferencesCache(paths) {
|
|
560
|
+
const cache = new Map();
|
|
561
|
+
const getExampleCustomEntitiesByTypePath = (path) => {
|
|
562
|
+
const client = new sdk_1.Entities();
|
|
563
|
+
const entities = client.get({
|
|
564
|
+
entityName: 'custom-entity',
|
|
565
|
+
criteria: {
|
|
566
|
+
typePath: path,
|
|
567
|
+
},
|
|
568
|
+
});
|
|
569
|
+
return entities;
|
|
570
|
+
};
|
|
571
|
+
const getExampleEntites = async (path) => {
|
|
572
|
+
if (path.startsWith('custom-entity')) {
|
|
573
|
+
return getExampleCustomEntitiesByTypePath(path);
|
|
574
|
+
}
|
|
575
|
+
const { root } = (0, types_2.parseTypePath)(path);
|
|
576
|
+
const client = new sdk_1.Entities();
|
|
577
|
+
const entities = await client.get({
|
|
578
|
+
entityName: root,
|
|
579
|
+
});
|
|
580
|
+
return entities;
|
|
581
|
+
};
|
|
582
|
+
const entitiesByPath = await Promise.all((paths || []).map(async (path) => {
|
|
583
|
+
const entities = await getExampleEntites(path);
|
|
584
|
+
return { path, entities };
|
|
585
|
+
}));
|
|
586
|
+
entitiesByPath.forEach(({ path, entities }) => {
|
|
587
|
+
const cacheForPath = buildObjectReferenceCache(entities);
|
|
588
|
+
cache.set(path, cacheForPath);
|
|
589
|
+
});
|
|
590
|
+
return cache;
|
|
591
|
+
}
|
|
592
|
+
function buildObjectReferenceCache(entities) {
|
|
593
|
+
const cache = new Map();
|
|
594
|
+
entities.forEach((entity) => {
|
|
595
|
+
const { id } = entity;
|
|
596
|
+
if (id && !cache.has(id)) {
|
|
597
|
+
cache.set(id, entity);
|
|
598
|
+
}
|
|
599
|
+
});
|
|
600
|
+
return cache;
|
|
601
|
+
}
|
|
602
|
+
exports.buildObjectReferenceCache = buildObjectReferenceCache;
|
|
603
|
+
function selectRandomObject(cache) {
|
|
604
|
+
return Array.from(cache.values())[Math.floor(Math.random() * cache.size)];
|
|
605
|
+
}
|
|
606
|
+
exports.selectRandomObject = selectRandomObject;
|
|
607
|
+
function requiredValues(path, typeCache) {
|
|
608
|
+
if (path.startsWith('custom-entity')) {
|
|
609
|
+
return [{ slug: 'typeId', value: typeCache.get(path)?.id || '' }];
|
|
610
|
+
}
|
|
611
|
+
}
|
|
612
|
+
function doesPropertyLevelMatchRole(propertyLevel, role) {
|
|
613
|
+
const isMatchingLevel = propertyLevel === role;
|
|
614
|
+
return (isMatchingLevel ||
|
|
615
|
+
propertyLevel === types_1.PropertyLevel.ALL ||
|
|
616
|
+
propertyLevel === types_1.PropertyLevel.OVERRIDABLE);
|
|
617
|
+
}
|
|
618
|
+
exports.doesPropertyLevelMatchRole = doesPropertyLevelMatchRole;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|