cloesce 0.0.4-unstable.8 → 0.0.5-unstable.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/dist/common.d.ts DELETED
@@ -1,330 +0,0 @@
1
- export declare enum ExtractorErrorCode {
2
- MissingExport = 0,
3
- AppMissingDefaultExport = 1,
4
- UnknownType = 2,
5
- MultipleGenericType = 3,
6
- InvalidDataSourceDefinition = 4,
7
- InvalidPartialType = 5,
8
- InvalidIncludeTree = 6,
9
- InvalidAttributeModifier = 7,
10
- InvalidApiMethodModifier = 8,
11
- UnknownNavigationPropertyReference = 9,
12
- InvalidNavigationPropertyReference = 10,
13
- MissingNavigationPropertyReference = 11,
14
- MissingManyToManyUniqueId = 12,
15
- MissingPrimaryKey = 13,
16
- MissingDatabaseBinding = 14,
17
- MissingWranglerEnv = 15,
18
- TooManyWranglerEnvs = 16,
19
- MissingFile = 17
20
- }
21
- export declare function getErrorInfo(code: ExtractorErrorCode): {
22
- description: string;
23
- suggestion: string;
24
- };
25
- export declare class ExtractorError {
26
- code: ExtractorErrorCode;
27
- context?: string;
28
- snippet?: string;
29
- constructor(code: ExtractorErrorCode);
30
- addContext(fn: (val: string | undefined) => string | undefined): void;
31
- }
32
- type DeepPartialInner<T> = T extends (infer U)[] ? DeepPartialInner<U>[] : T extends object ? {
33
- [K in keyof T]?: DeepPartialInner<T[K]>;
34
- } : T | (null extends T ? null : never);
35
- /**
36
- * Recursively makes all properties of a type optional — including nested objects and arrays.
37
- *
38
- * Similar to TypeScript's built-in `Partial<T>`, but applies the transformation deeply across
39
- * all nested structures. Useful for defining "patch" or "update" objects where only a subset
40
- * of properties may be provided.
41
- *
42
- * **Apart of the Cloesce method grammar**, meaning the type can be apart of method parameters
43
- * or return types and the generated workers and client API will act accordingly.
44
- *
45
- * @template T
46
- * The target type to make deeply partial.
47
- *
48
- * @remarks
49
- * - **Objects:** All properties become optional, and their values are recursively wrapped in `DeepPartial`.
50
- * - **Arrays:** Arrays are preserved, but their elements are recursively made partial.
51
- * - **Scalars:** Primitive values (string, number, boolean, etc.) remain unchanged.
52
- * - **Nullable types:** If `null` is assignable to the type, it remains allowed.
53
- *
54
- * @example
55
- * ```ts
56
- * class User {
57
- * id: string;
58
- * profile: {
59
- * name: string;
60
- * age: number;
61
- * };
62
- * tags: string[];
63
- * }
64
- *
65
- * // The resulting type:
66
- * // {
67
- * // id?: string;
68
- * // profile?: { name?: string; age?: number };
69
- * // tags?: (string | undefined)[];
70
- * // }
71
- * type PartialUser = DeepPartial<User>;
72
- *
73
- * const patch: PartialUser = {
74
- * profile: { age: 30 } // ok
75
- * };
76
- * ```
77
- */
78
- export type DeepPartial<T> = DeepPartialInner<T> & {
79
- __brand?: "Partial";
80
- };
81
- export declare class Either<L, R> {
82
- private readonly inner;
83
- private constructor();
84
- get value(): L | R;
85
- static left<L, R = never>(value: L): Either<L, R>;
86
- static right<R, L = never>(value: R): Either<L, R>;
87
- isLeft(): this is Either<L, never>;
88
- isRight(): this is Either<never, R>;
89
- unwrap(): R;
90
- unwrapLeft(): L;
91
- map<B>(fn: (val: R) => B): Either<L, B>;
92
- mapLeft<B>(fn: (val: L) => B): Either<B, R>;
93
- }
94
- /**
95
- * Represents the result of an HTTP operation in a monadic style.
96
- *
97
- * This type provides a uniform way to handle both success and error
98
- * outcomes of HTTP requests, similar to a `Result` or `Either` monad.
99
- *
100
- * It ensures that every HTTP response can be handled in a type-safe,
101
- * predictable way without throwing exceptions.
102
- *
103
- * @template T The type of the successful response data.
104
- *
105
- * @property {boolean} ok
106
- * Indicates whether the HTTP request was successful (`true` for success, `false` for error).
107
- * This is analogous to `Response.ok` in the Fetch API.
108
- *
109
- * @property {number} status
110
- * The numeric HTTP status code (e.g., 200, 404, 500).
111
- *
112
- * @property {T} [data]
113
- * The parsed response payload, present only when `ok` is `true`.
114
- *
115
- * @property {string} [message]
116
- * An optional human-readable error message or diagnostic information,
117
- * typically provided when `ok` is `false`.
118
- *
119
- * ## Worker APIs
120
- *
121
- * HttpResult is a first-class-citizen in the grammar in Cloesce. Methods can return HttpResults
122
- * which will be serialized on the client api.
123
- *
124
- * @example
125
- * ```ts
126
- * bar(): HttpResult<Integer> {
127
- * return { ok: false, status: 401, message: "forbidden"}
128
- * }
129
- * ```
130
- */
131
- export type HttpResult<T = unknown> = {
132
- ok: boolean;
133
- status: number;
134
- data?: T;
135
- message?: string;
136
- };
137
- /**
138
- * Dependency injection container, mapping an object type name to an instance of that object.
139
- *
140
- * Comes with the WranglerEnv and Request by default.
141
- */
142
- export type InstanceRegistry = Map<string, any>;
143
- export type MiddlewareFn = (request: Request, env: any, ir: InstanceRegistry) => Promise<HttpResult | undefined>;
144
- export type KeysOfType<T, U> = {
145
- [K in keyof T]: T[K] extends U ? (K extends string ? K : never) : never;
146
- }[keyof T];
147
- /**
148
- * Represents the core middleware container for a Cloesce application.
149
- *
150
- * The `CloesceApp` class provides scoped middleware registration and
151
- * management across three primary levels of execution:
152
- *
153
- * 1. **Global Middleware** — Executed before any routing or model resolution occurs.
154
- * 2. **Model-Level Middleware** — Executed for requests targeting a specific model type.
155
- * 3. **Method-Level Middleware** — Executed for requests targeting a specific method on a model.
156
- *
157
- * When an instance of `CloesceApp` is exported from `app.cloesce.ts`,
158
- * it becomes the central container that the Cloesce runtime uses to
159
- * assemble and apply middleware in the correct execution order.
160
- *
161
- * ### Middleware Execution Order
162
- * Middleware is executed in FIFO order per scope. For example:
163
- * ```ts
164
- * app.use(Foo, A);
165
- * app.use(Foo, B);
166
- * app.use(Foo, C);
167
- * // Executed in order: A → B → C
168
- * ```
169
- *
170
- * Each middleware function (`MiddlewareFn`) can optionally short-circuit
171
- * execution by returning a result, in which case subsequent middleware
172
- * at the same or lower scope will not run.
173
- *
174
- * ### Example Usage
175
- * ```ts
176
- * import { app } from "cloesce";
177
- *
178
- * // Global authentication middleware
179
- * app.useGlobal((request, env, di) => {
180
- * // ... authenticate and inject user
181
- * });
182
- *
183
- * // Model-level authorization
184
- * app.useModel(User, (user) => user.hasPermissions([UserPermissions.canUseFoo]));
185
- *
186
- * // Method-level middleware (e.g., CRUD operation)
187
- * app.useMethod(Foo, "someMethod", (user) => user.hasPermissions([UserPermissions.canUseFooMethod]));
188
- * ```
189
- */
190
- export declare class CloesceApp {
191
- global: MiddlewareFn[];
192
- model: Map<string, MiddlewareFn[]>;
193
- method: Map<string, Map<string, MiddlewareFn[]>>;
194
- /**
195
- * Registers a new global middleware function.
196
- *
197
- * Global middleware runs before all routing and model resolution.
198
- * It is the ideal place to perform tasks such as:
199
- * - Authentication (e.g., JWT verification)
200
- * - Global request logging
201
- * - Dependency injection of shared context
202
- *
203
- * @param m - The middleware function to register.
204
- */
205
- useGlobal(m: MiddlewareFn): void;
206
- /**
207
- * Registers middleware for a specific model type.
208
- *
209
- * Model-level middleware runs after all global middleware,
210
- * but before method-specific middleware. This scope allows
211
- * logic to be applied consistently across all endpoints
212
- * associated with a given model (e.g., authorization).
213
- *
214
- * @typeParam T - The model type.
215
- * @param ctor - The model constructor (used to derive its name).
216
- * @param m - The middleware function to register.
217
- */
218
- useModel<T>(ctor: new () => T, m: MiddlewareFn): void;
219
- /**
220
- * Registers middleware for a specific method on a model.
221
- *
222
- * Method-level middleware is executed after model middleware,
223
- * and before the method implementation itself. It can be used for:
224
- * - Fine-grained permission checks
225
- * - Custom logging or tracing per endpoint
226
- *
227
- * @typeParam T - The model type.
228
- * @param ctor - The model constructor (used to derive its name).
229
- * @param method - The method name on the model.
230
- * @param m - The middleware function to register.
231
- */
232
- useMethod<T>(ctor: new () => T, method: KeysOfType<T, (...args: any) => any>, m: MiddlewareFn): void;
233
- }
234
- export type CrudKind = "SAVE" | "GET" | "LIST";
235
- export type CidlType = "Void" | "Integer" | "Real" | "Text" | "Blob" | "DateIso" | "Boolean" | {
236
- DataSource: string;
237
- } | {
238
- Inject: string;
239
- } | {
240
- Object: string;
241
- } | {
242
- Partial: string;
243
- } | {
244
- Nullable: CidlType;
245
- } | {
246
- Array: CidlType;
247
- } | {
248
- HttpResult: CidlType;
249
- };
250
- export declare function isNullableType(ty: CidlType): boolean;
251
- export declare enum HttpVerb {
252
- GET = "GET",
253
- POST = "POST",
254
- PUT = "PUT",
255
- PATCH = "PATCH",
256
- DELETE = "DELETE"
257
- }
258
- export interface NamedTypedValue {
259
- name: string;
260
- cidl_type: CidlType;
261
- }
262
- export interface ModelAttribute {
263
- value: NamedTypedValue;
264
- foreign_key_reference: string | null;
265
- }
266
- export interface ModelMethod {
267
- name: string;
268
- is_static: boolean;
269
- http_verb: HttpVerb;
270
- return_type: CidlType | null;
271
- parameters: NamedTypedValue[];
272
- }
273
- export type NavigationPropertyKind = {
274
- OneToOne: {
275
- reference: string;
276
- };
277
- } | {
278
- OneToMany: {
279
- reference: string;
280
- };
281
- } | {
282
- ManyToMany: {
283
- unique_id: string;
284
- };
285
- };
286
- export interface NavigationProperty {
287
- var_name: string;
288
- model_name: string;
289
- kind: NavigationPropertyKind;
290
- }
291
- export declare function getNavigationPropertyCidlType(nav: NavigationProperty): CidlType;
292
- export interface Model {
293
- name: string;
294
- primary_key: NamedTypedValue;
295
- attributes: ModelAttribute[];
296
- navigation_properties: NavigationProperty[];
297
- methods: Record<string, ModelMethod>;
298
- data_sources: Record<string, DataSource>;
299
- cruds: CrudKind[];
300
- source_path: string;
301
- }
302
- export interface PlainOldObject {
303
- name: string;
304
- attributes: NamedTypedValue[];
305
- source_path: string;
306
- }
307
- export interface CidlIncludeTree {
308
- [key: string]: CidlIncludeTree;
309
- }
310
- export declare const NO_DATA_SOURCE = "none";
311
- export interface DataSource {
312
- name: string;
313
- tree: CidlIncludeTree;
314
- }
315
- export interface WranglerEnv {
316
- name: string;
317
- source_path: string;
318
- db_binding: string;
319
- }
320
- export interface CloesceAst {
321
- version: string;
322
- project_name: string;
323
- language: string;
324
- wrangler_env: WranglerEnv;
325
- models: Record<string, Model>;
326
- poos: Record<string, PlainOldObject>;
327
- app_source: string | null;
328
- }
329
- export {};
330
- //# sourceMappingURL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-1,274 +0,0 @@
1
- export var ExtractorErrorCode;
2
- (function (ExtractorErrorCode) {
3
- ExtractorErrorCode[ExtractorErrorCode["MissingExport"] = 0] = "MissingExport";
4
- ExtractorErrorCode[ExtractorErrorCode["AppMissingDefaultExport"] = 1] = "AppMissingDefaultExport";
5
- ExtractorErrorCode[ExtractorErrorCode["UnknownType"] = 2] = "UnknownType";
6
- ExtractorErrorCode[ExtractorErrorCode["MultipleGenericType"] = 3] = "MultipleGenericType";
7
- ExtractorErrorCode[ExtractorErrorCode["InvalidDataSourceDefinition"] = 4] = "InvalidDataSourceDefinition";
8
- ExtractorErrorCode[ExtractorErrorCode["InvalidPartialType"] = 5] = "InvalidPartialType";
9
- ExtractorErrorCode[ExtractorErrorCode["InvalidIncludeTree"] = 6] = "InvalidIncludeTree";
10
- ExtractorErrorCode[ExtractorErrorCode["InvalidAttributeModifier"] = 7] = "InvalidAttributeModifier";
11
- ExtractorErrorCode[ExtractorErrorCode["InvalidApiMethodModifier"] = 8] = "InvalidApiMethodModifier";
12
- ExtractorErrorCode[ExtractorErrorCode["UnknownNavigationPropertyReference"] = 9] = "UnknownNavigationPropertyReference";
13
- ExtractorErrorCode[ExtractorErrorCode["InvalidNavigationPropertyReference"] = 10] = "InvalidNavigationPropertyReference";
14
- ExtractorErrorCode[ExtractorErrorCode["MissingNavigationPropertyReference"] = 11] = "MissingNavigationPropertyReference";
15
- ExtractorErrorCode[ExtractorErrorCode["MissingManyToManyUniqueId"] = 12] = "MissingManyToManyUniqueId";
16
- ExtractorErrorCode[ExtractorErrorCode["MissingPrimaryKey"] = 13] = "MissingPrimaryKey";
17
- ExtractorErrorCode[ExtractorErrorCode["MissingDatabaseBinding"] = 14] = "MissingDatabaseBinding";
18
- ExtractorErrorCode[ExtractorErrorCode["MissingWranglerEnv"] = 15] = "MissingWranglerEnv";
19
- ExtractorErrorCode[ExtractorErrorCode["TooManyWranglerEnvs"] = 16] = "TooManyWranglerEnvs";
20
- ExtractorErrorCode[ExtractorErrorCode["MissingFile"] = 17] = "MissingFile";
21
- })(ExtractorErrorCode || (ExtractorErrorCode = {}));
22
- const errorInfoMap = {
23
- [ExtractorErrorCode.MissingExport]: {
24
- description: "All Cloesce types must be exported.",
25
- suggestion: "Add `export` to the class definition.",
26
- },
27
- [ExtractorErrorCode.AppMissingDefaultExport]: {
28
- description: "app.cloesce.ts does not export a CloesceApp by default",
29
- suggestion: "Export an instantiated CloesceApp in app.cloesce.ts",
30
- },
31
- [ExtractorErrorCode.UnknownType]: {
32
- description: "Encountered an unknown or unsupported type",
33
- suggestion: "Refer to the documentation on valid Cloesce TS types",
34
- },
35
- [ExtractorErrorCode.InvalidPartialType]: {
36
- description: "Partial types must only contain a model or plain old object",
37
- suggestion: "Refer to the documentation on valid Cloesce TS types",
38
- },
39
- [ExtractorErrorCode.MultipleGenericType]: {
40
- description: "Cloesce does not yet support types with multiple generics",
41
- suggestion: "Simplify your type to use only a single generic parameter, ie Foo<T>",
42
- },
43
- [ExtractorErrorCode.InvalidDataSourceDefinition]: {
44
- description: "Data Sources must be explicitly typed as a static Include Tree",
45
- suggestion: "Declare your data source as `static readonly _: IncludeTree<Model>`",
46
- },
47
- [ExtractorErrorCode.InvalidIncludeTree]: {
48
- description: "Invalid Include Tree",
49
- suggestion: "Include trees must only contain references to a model's navigation properties.",
50
- },
51
- [ExtractorErrorCode.InvalidAttributeModifier]: {
52
- description: "Attributes can only be public on a Model, Plain Old Object or Wrangler Environment",
53
- suggestion: "Change the attribute modifier to just `public`",
54
- },
55
- [ExtractorErrorCode.InvalidApiMethodModifier]: {
56
- description: "Model methods must be public if they are decorated as GET, POST, PUT, PATCH",
57
- suggestion: "Change the method modifier to just `public`",
58
- },
59
- [ExtractorErrorCode.UnknownNavigationPropertyReference]: {
60
- description: "Unknown Navigation Property Reference",
61
- suggestion: "Verify that the navigation property reference model exists, or create a model.",
62
- },
63
- [ExtractorErrorCode.InvalidNavigationPropertyReference]: {
64
- description: "Invalid Navigation Property Reference",
65
- suggestion: "Ensure the navigation property points to a valid model field",
66
- },
67
- [ExtractorErrorCode.MissingNavigationPropertyReference]: {
68
- description: "Missing Navigation Property Reference",
69
- suggestion: "Navigation properties require a foreign key model attribute reference",
70
- },
71
- [ExtractorErrorCode.MissingManyToManyUniqueId]: {
72
- description: "Missing unique id on Many to Many navigation property",
73
- suggestion: "Define a unique identifier field for the Many-to-Many relationship",
74
- },
75
- [ExtractorErrorCode.MissingPrimaryKey]: {
76
- description: "Missing primary key on a model",
77
- suggestion: "Add a primary key field to your model (e.g., `id: number`)",
78
- },
79
- [ExtractorErrorCode.MissingDatabaseBinding]: {
80
- description: "Missing a database binding in the WranglerEnv definition",
81
- suggestion: "Add a `D1Database` to your WranglerEnv",
82
- },
83
- [ExtractorErrorCode.MissingWranglerEnv]: {
84
- description: "Missing a wrangler environment definition in the project",
85
- suggestion: "Add a @WranglerEnv class in your project.",
86
- },
87
- [ExtractorErrorCode.TooManyWranglerEnvs]: {
88
- description: "Too many wrangler environments defined in the project",
89
- suggestion: "Consolidate or remove unused @WranglerEnv's",
90
- },
91
- [ExtractorErrorCode.MissingFile]: {
92
- description: "A specified input file could not be found",
93
- suggestion: "Verify the input file path is correct",
94
- },
95
- };
96
- export function getErrorInfo(code) {
97
- return errorInfoMap[code];
98
- }
99
- export class ExtractorError {
100
- code;
101
- context;
102
- snippet;
103
- constructor(code) {
104
- this.code = code;
105
- }
106
- addContext(fn) {
107
- this.context = fn(this.context ?? "");
108
- }
109
- }
110
- export class Either {
111
- inner;
112
- constructor(inner) {
113
- this.inner = inner;
114
- }
115
- get value() {
116
- return this.inner.ok ? this.inner.right : this.inner.left;
117
- }
118
- static left(value) {
119
- return new Either({ ok: false, left: value });
120
- }
121
- static right(value) {
122
- return new Either({ ok: true, right: value });
123
- }
124
- isLeft() {
125
- return !this.inner.ok;
126
- }
127
- isRight() {
128
- return this.inner.ok;
129
- }
130
- unwrap() {
131
- if (!this.inner.ok) {
132
- throw new Error("Tried to unwrap a Left value");
133
- }
134
- return this.inner.right;
135
- }
136
- unwrapLeft() {
137
- if (this.inner.ok) {
138
- throw new Error("Tried to unwrapLeft a Right value");
139
- }
140
- return this.inner.left;
141
- }
142
- map(fn) {
143
- return this.inner.ok
144
- ? Either.right(fn(this.inner.right))
145
- : Either.left(this.inner.left);
146
- }
147
- mapLeft(fn) {
148
- return this.inner.ok
149
- ? Either.right(this.inner.right)
150
- : Either.left(fn(this.inner.left));
151
- }
152
- }
153
- /**
154
- * Represents the core middleware container for a Cloesce application.
155
- *
156
- * The `CloesceApp` class provides scoped middleware registration and
157
- * management across three primary levels of execution:
158
- *
159
- * 1. **Global Middleware** — Executed before any routing or model resolution occurs.
160
- * 2. **Model-Level Middleware** — Executed for requests targeting a specific model type.
161
- * 3. **Method-Level Middleware** — Executed for requests targeting a specific method on a model.
162
- *
163
- * When an instance of `CloesceApp` is exported from `app.cloesce.ts`,
164
- * it becomes the central container that the Cloesce runtime uses to
165
- * assemble and apply middleware in the correct execution order.
166
- *
167
- * ### Middleware Execution Order
168
- * Middleware is executed in FIFO order per scope. For example:
169
- * ```ts
170
- * app.use(Foo, A);
171
- * app.use(Foo, B);
172
- * app.use(Foo, C);
173
- * // Executed in order: A → B → C
174
- * ```
175
- *
176
- * Each middleware function (`MiddlewareFn`) can optionally short-circuit
177
- * execution by returning a result, in which case subsequent middleware
178
- * at the same or lower scope will not run.
179
- *
180
- * ### Example Usage
181
- * ```ts
182
- * import { app } from "cloesce";
183
- *
184
- * // Global authentication middleware
185
- * app.useGlobal((request, env, di) => {
186
- * // ... authenticate and inject user
187
- * });
188
- *
189
- * // Model-level authorization
190
- * app.useModel(User, (user) => user.hasPermissions([UserPermissions.canUseFoo]));
191
- *
192
- * // Method-level middleware (e.g., CRUD operation)
193
- * app.useMethod(Foo, "someMethod", (user) => user.hasPermissions([UserPermissions.canUseFooMethod]));
194
- * ```
195
- */
196
- export class CloesceApp {
197
- global = [];
198
- model = new Map();
199
- method = new Map();
200
- /**
201
- * Registers a new global middleware function.
202
- *
203
- * Global middleware runs before all routing and model resolution.
204
- * It is the ideal place to perform tasks such as:
205
- * - Authentication (e.g., JWT verification)
206
- * - Global request logging
207
- * - Dependency injection of shared context
208
- *
209
- * @param m - The middleware function to register.
210
- */
211
- useGlobal(m) {
212
- this.global.push(m);
213
- }
214
- /**
215
- * Registers middleware for a specific model type.
216
- *
217
- * Model-level middleware runs after all global middleware,
218
- * but before method-specific middleware. This scope allows
219
- * logic to be applied consistently across all endpoints
220
- * associated with a given model (e.g., authorization).
221
- *
222
- * @typeParam T - The model type.
223
- * @param ctor - The model constructor (used to derive its name).
224
- * @param m - The middleware function to register.
225
- */
226
- useModel(ctor, m) {
227
- if (this.model.has(ctor.name)) {
228
- this.model.get(ctor.name).push(m);
229
- }
230
- else {
231
- this.model.set(ctor.name, [m]);
232
- }
233
- }
234
- /**
235
- * Registers middleware for a specific method on a model.
236
- *
237
- * Method-level middleware is executed after model middleware,
238
- * and before the method implementation itself. It can be used for:
239
- * - Fine-grained permission checks
240
- * - Custom logging or tracing per endpoint
241
- *
242
- * @typeParam T - The model type.
243
- * @param ctor - The model constructor (used to derive its name).
244
- * @param method - The method name on the model.
245
- * @param m - The middleware function to register.
246
- */
247
- useMethod(ctor, method, m) {
248
- if (!this.method.has(ctor.name)) {
249
- this.method.set(ctor.name, new Map());
250
- }
251
- const methods = this.method.get(ctor.name);
252
- if (!methods.has(method)) {
253
- methods.set(method, []);
254
- }
255
- methods.get(method).push(m);
256
- }
257
- }
258
- export function isNullableType(ty) {
259
- return typeof ty === "object" && ty !== null && "Nullable" in ty;
260
- }
261
- export var HttpVerb;
262
- (function (HttpVerb) {
263
- HttpVerb["GET"] = "GET";
264
- HttpVerb["POST"] = "POST";
265
- HttpVerb["PUT"] = "PUT";
266
- HttpVerb["PATCH"] = "PATCH";
267
- HttpVerb["DELETE"] = "DELETE";
268
- })(HttpVerb || (HttpVerb = {}));
269
- export function getNavigationPropertyCidlType(nav) {
270
- return "OneToOne" in nav.kind
271
- ? { Object: nav.model_name }
272
- : { Array: { Object: nav.model_name } };
273
- }
274
- export const NO_DATA_SOURCE = "none";