@mkja/o-data 0.0.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/README.md ADDED
@@ -0,0 +1,416 @@
1
+ ## o-data
2
+
3
+ **o-data** is a TypeScript‑first OData 4.01 client and schema generator.
4
+
5
+ It has two parts:
6
+
7
+ - **Runtime library** – a strongly‑typed client for querying and mutating OData services.
8
+ - **CLI parser / schema generator** – reads an OData CSDL XML document and generates a typed schema module the runtime can consume.
9
+
10
+ ---
11
+
12
+ ## Features
13
+
14
+ - **Schema‑driven, fully typed client**
15
+ - Describe your service once in a TypeScript schema; get strong types for queries, payloads, and responses.
16
+ - **Fluent query builder**
17
+ - `$select`, `$expand` (with nested options), `$filter`, `$orderby`, `$top`, `$skip`, `$count`.
18
+ - `$filter` DSL with navigation, `any` / `all`, enums, dates, and string functions.
19
+ - **Navigation‑aware create/update**
20
+ - Supports `@odata.bind` for single and collection navigations, deep inserts, and batch references.
21
+ - **Actions & functions**
22
+ - Bound and unbound operations, with correct URL shapes and parameter serialization.
23
+ - **Schema generator from CSDL**
24
+ - CLI reads your OData metadata XML and emits a typed `schema({...})` module.
25
+ - Powerful include/exclude/masking rules for keeping the generated surface small and relevant.
26
+
27
+ ---
28
+
29
+ ## Installation
30
+
31
+ ```bash
32
+ # with bun
33
+ bun add o-data
34
+
35
+ # or with npm
36
+ npm install o-data
37
+
38
+ # or with pnpm
39
+ pnpm add o-data
40
+
41
+ # or with yarn
42
+ yarn add o-data
43
+ ```
44
+
45
+ The runtime expects a `fetch`‑compatible environment (`Request`, `Response`, `Headers`); it works in modern Node (with `fetch`) and browsers.
46
+
47
+ ---
48
+
49
+ ## Runtime: Getting started
50
+
51
+ ### 1. Define or generate a schema
52
+
53
+ You can either write a schema by hand:
54
+
55
+ ```ts
56
+ // schema.ts
57
+ import { schema } from "o-data/schema";
58
+
59
+ export const crmSchema = schema({
60
+ namespace: "Microsoft.Dynamics.CRM",
61
+ alias: "mscrm",
62
+ enumtypes: {
63
+ IncidentStatus: {
64
+ isFlags: false,
65
+ members: {
66
+ Active: 0,
67
+ Resolved: 1,
68
+ Cancelled: 2,
69
+ },
70
+ },
71
+ },
72
+ entitytypes: {
73
+ Incident: {
74
+ properties: {
75
+ id: { type: "Edm.Guid" },
76
+ title: { type: "Edm.String" },
77
+ status: { type: "enum", target: "IncidentStatus" },
78
+ },
79
+ },
80
+ },
81
+ entitysets: {
82
+ incidents: { entitytype: "Incident" },
83
+ },
84
+ });
85
+ ```
86
+
87
+ …or generate one from a CSDL XML using the CLI (see **Schema generator (CLI)** below).
88
+
89
+ ### 2. Create an `OdataClient`
90
+
91
+ ```ts
92
+ // client.ts
93
+ import { OdataClient } from "o-data";
94
+ import { crmSchema } from "./schema"; // or generated-o-data-schema
95
+
96
+ const client = new OdataClient(crmSchema, {
97
+ baseUrl: "https://example.com/api/data/v9.0/",
98
+ transport: fetch, // any (req: Request) => Promise<Response>
99
+ });
100
+ ```
101
+
102
+ ---
103
+
104
+ ## Querying data
105
+
106
+ ### Collection queries
107
+
108
+ ```ts
109
+ // GET /incidents?$select=title,status&$top=10&$orderby=title asc
110
+ const response = await client.entitysets("incidents").query({
111
+ select: ["title", "status"],
112
+ top: 10,
113
+ orderby: ["title", "asc"],
114
+ });
115
+
116
+ if (response.ok) {
117
+ const incidents = response.result.data; // typed by schema + query
118
+ }
119
+ ```
120
+
121
+ ### Expands and nested options
122
+
123
+ ```ts
124
+ // GET /incidents?$expand=incident_contact($select=name,email)
125
+ const res = await client.entitysets("incidents").query({
126
+ expand: {
127
+ incident_contact: {
128
+ select: ["name", "email"],
129
+ },
130
+ },
131
+ });
132
+ ```
133
+
134
+ ### Filter builder
135
+
136
+ Filters use a small builder DSL that respects your schema:
137
+
138
+ ```ts
139
+ // GET /incidents?$filter=status eq Namespace.IncidentStatus'Active'
140
+ const res = await client.entitysets("incidents").query({
141
+ filter: (h) => h.clause("status", "eq", "Active"),
142
+ });
143
+
144
+ // Navigation + logical operators
145
+ const res2 = await client.entitysets("incidents").query({
146
+ filter: (h) =>
147
+ h
148
+ .clause("title", "contains", "Support")
149
+ .and(
150
+ h.nav("incident_contact", (nh) =>
151
+ nh.clause("email", "eq", "user@example.com"),
152
+ ),
153
+ ),
154
+ });
155
+ ```
156
+
157
+ Supported operators include `eq`, `ne`, `gt`, `ge`, `lt`, `le`, `in`, `contains`, `startswith`, `endswith`.
158
+ For enums, you can pass either the member name (`"Active"`) or the underlying numeric value (`1`); they are serialized as FQN enum literals.
159
+
160
+ ### Single‑entity queries and navigation
161
+
162
+ ```ts
163
+ // GET /incidents(guid-123)?$select=title
164
+ const incident = await client
165
+ .entitysets("incidents")
166
+ .key("guid-123")
167
+ .query({ select: ["title"] });
168
+
169
+ // GET /incidents(guid-123)/incident_contact
170
+ const contact = await client
171
+ .entitysets("incidents")
172
+ .key("guid-123")
173
+ .navigate("incident_contact")
174
+ .query({});
175
+ ```
176
+
177
+ ---
178
+
179
+ ## Creating and updating entities
180
+
181
+ The library infers create/update shapes from your schema and takes care of `@odata.bind` and deep inserts.
182
+
183
+ ### Create
184
+
185
+ ```ts
186
+ // Basic create
187
+ const created = await client.entitysets("incidents").create({
188
+ title: "New incident",
189
+ description: "Description",
190
+ });
191
+
192
+ // Create with navigation bind (single‑valued)
193
+ await client.entitysets("incidents").create({
194
+ title: "Linked to contact",
195
+ incident_contact: "guid-contact-id", // → "incident_contact@odata.bind": "/contacts(guid-contact-id)"
196
+ });
197
+
198
+ // Create with collection navigation bind
199
+ await client.entitysets("contacts").create({
200
+ name: "John",
201
+ contact_incidents: ["incident-id-1", "incident-id-2"],
202
+ // → "contact_incidents@odata.bind": ["/incidents(incident-id-1)", "/incidents(incident-id-2)"]
203
+ });
204
+
205
+ // Deep insert
206
+ await client.entitysets("incidents").create({
207
+ title: "Deep insert example",
208
+ incident_contact: {
209
+ name: "Nested contact",
210
+ email: "nested@example.com",
211
+ },
212
+ });
213
+ ```
214
+
215
+ You can control response shape via options:
216
+
217
+ ```ts
218
+ await client.entitysets("incidents").create(
219
+ { title: "Return representation" },
220
+ {
221
+ select: ["title", "description"],
222
+ prefer: { return_representation: true },
223
+ },
224
+ );
225
+ ```
226
+
227
+ ### Update
228
+
229
+ ```ts
230
+ // Simple PATCH
231
+ await client.entitysets("incidents").key("guid-123").update({
232
+ title: "Updated title",
233
+ });
234
+
235
+ // Repoint single navigation
236
+ await client.entitysets("incidents").key("guid-123").update({
237
+ incident_contact: "guid-new-contact",
238
+ });
239
+
240
+ // Collection navigation operations
241
+ await client
242
+ .entitysets("contacts")
243
+ .key("guid-contact")
244
+ .update({
245
+ contact_incidents: {
246
+ add: ["incident-id-3"],
247
+ remove: ["incident-id-1"],
248
+ },
249
+ });
250
+ ```
251
+
252
+ Options for update mirror create: `select`, `prefer.return_representation`, custom headers.
253
+
254
+ ---
255
+
256
+ ## Actions and functions
257
+
258
+ ### Bound actions
259
+
260
+ ```ts
261
+ // POST /incidents(guid-123)/Namespace.assignIncident
262
+ const res = await client
263
+ .entitysets("incidents")
264
+ .key("guid-123")
265
+ .action("assignIncident", {
266
+ parameters: {
267
+ assigneeId: "guid-user",
268
+ priority: 1,
269
+ },
270
+ });
271
+
272
+ if (res.ok) {
273
+ const ok: boolean = res.result.data; // mapped from Edm.Boolean
274
+ }
275
+ ```
276
+
277
+ ### Unbound actions (via imports)
278
+
279
+ ```ts
280
+ // POST /BulkCreate
281
+ const res = await client.action("BulkCreate", {
282
+ parameters: {
283
+ entities: ["1", "2", "3"],
284
+ },
285
+ });
286
+ ```
287
+
288
+ ### Bound functions
289
+
290
+ ```ts
291
+ // GET /incidents(guid-123)/Namespace.getRelatedCount(relationType=@relationType)?@relationType='contact'
292
+ const res = await client
293
+ .entitysets("incidents")
294
+ .key("guid-123")
295
+ .function("getRelatedCount", {
296
+ parameters: { relationType: "contact" },
297
+ });
298
+
299
+ if (res.ok) {
300
+ const count: number = res.result.data;
301
+ }
302
+ ```
303
+
304
+ ### Unbound functions (via imports)
305
+
306
+ ```ts
307
+ // GET /Search(query=@query,entityTypes=@entityTypes)?@query='test'&@entityTypes=...
308
+ const res = await client.function("Search", {
309
+ parameters: {
310
+ query: "test",
311
+ entityTypes: ["Incident", "Contact"],
312
+ },
313
+ });
314
+ ```
315
+
316
+ For navigation‑typed parameters (actions/functions), you can use the same patterns as for create/update: IDs, `[entityset, id]`, deep insert objects, or arrays thereof; the library converts them to `@odata.bind` or nested objects as needed.
317
+
318
+ ---
319
+
320
+ ## Schema generator (CLI)
321
+
322
+ The CLI reads an OData CSDL XML document and generates a strongly‑typed schema module that plugs into the runtime.
323
+
324
+ ### 1. Create a config file
325
+
326
+ Create `odata-parser.config.js` (or `.ts`) in your project root:
327
+
328
+ ```ts
329
+ // odata-parser.config.ts
330
+ import { defineConfig } from "o-data/parser";
331
+
332
+ export default defineConfig({
333
+ inputPath: "./metadata.xml",
334
+ outputPath: "./src/schema",
335
+ wantedEntities: "ALL", // or ['incidents', 'contacts']
336
+ wantedUnboundActions: "ALL",
337
+ wantedUnboundFunctions: "ALL",
338
+ excludeFilters: {
339
+ entities: [/^msdyn_/], // drop system sets
340
+ properties: [/^adx_/], // drop noisy props
341
+ },
342
+ selectionMode: "additive", // or 'only' for strict whitelists
343
+ // onlyEntities, onlyBoundActions, onlyUnboundActions, mask, ... are available
344
+ });
345
+ ```
346
+
347
+ Running the generator will produce e.g. `src/schema/generated-o-data-schema.ts` that looks like:
348
+
349
+ ```ts
350
+ import { schema } from "o-data/schema";
351
+
352
+ export const myservice_schema = schema({
353
+ namespace: "My.Service",
354
+ alias: "ms",
355
+ enumtypes: { /* ... */ },
356
+ complextypes: { /* ... */ },
357
+ entitytypes: { /* ... */ },
358
+ entitysets: { /* ... */ },
359
+ actions: { /* ... */ },
360
+ functions: { /* ... */ },
361
+ actionImports: { /* ... */ },
362
+ functionImports: { /* ... */ },
363
+ });
364
+ ```
365
+
366
+ ### 2. Run the CLI
367
+
368
+ From your project root:
369
+
370
+ ```bash
371
+ # using the global CLI name exposed by this package
372
+ npx o-data path/to/odata-parser.config.js
373
+
374
+ # or (when installed locally in a Node/Bun project)
375
+ bun x o-data path/to/odata-parser.config.js
376
+ ```
377
+
378
+ If you omit the path, the CLI looks for `odata-parser.config.js` (and then `.ts`) in the current working directory.
379
+
380
+ Then in your code:
381
+
382
+ ```ts
383
+ import { myservice_schema } from "./schema/generated-o-data-schema";
384
+ import { OdataClient } from "o-data";
385
+
386
+ const client = new OdataClient(myservice_schema, {
387
+ baseUrl: "https://example.com/odata/",
388
+ transport: fetch,
389
+ });
390
+ ```
391
+
392
+ ---
393
+
394
+ ## Status and limitations
395
+
396
+ - The library is still **early (0.0.x)**; APIs may change.
397
+ - Some operations are marked `TODO` in the runtime (e.g. delete support, collection‑bound actions/functions implementation, richer pagination).
398
+ - The generator doesn’t yet handle OData operation overloading beyond keeping the first operation per name.
399
+
400
+ ---
401
+
402
+ ## Development
403
+
404
+ - **Build**
405
+
406
+ ```bash
407
+ bun x tsc -p tsconfig.build.json
408
+ ```
409
+
410
+ - **Tests**
411
+
412
+ ```bash
413
+ bun test
414
+ ```
415
+
416
+ ---
package/dist/cli.d.ts ADDED
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env node
2
+ export {};
package/dist/cli.js ADDED
@@ -0,0 +1,13 @@
1
+ #!/usr/bin/env node
2
+ import { generateSchema } from "./parser/index.js";
3
+ async function main() {
4
+ const configPath = process.argv[2];
5
+ try {
6
+ await generateSchema(configPath);
7
+ }
8
+ catch (error) {
9
+ console.error(error instanceof Error ? error.message : error);
10
+ process.exit(1);
11
+ }
12
+ }
13
+ main();
@@ -0,0 +1,40 @@
1
+ import type { QueryableEntity, EntitySetToQueryableEntity } from './types';
2
+ import type { Schema } from './schema';
3
+ type NavTargetKey<TEntity extends QueryableEntity, N extends keyof TEntity['navigations']> = TEntity['navigations'][N]['targetEntitysetKey'];
4
+ type ResolveNavTargetQE<S extends Schema<S>, TEntity extends QueryableEntity, N extends keyof TEntity['navigations']> = NavTargetKey<TEntity, N> extends string ? NavTargetKey<TEntity, N> extends keyof S['entitysets'] ? EntitySetToQueryableEntity<S, NavTargetKey<TEntity, N>> : QueryableEntity : QueryableEntity;
5
+ export type ComparisonOperator = 'eq' | 'ne' | 'gt' | 'ge' | 'lt' | 'le' | 'contains' | 'startswith' | 'endswith' | 'in';
6
+ export type FilterableProperty<TEntity extends QueryableEntity> = keyof TEntity['properties'];
7
+ export type FilterPropertyValueType<TEntity extends QueryableEntity, P extends FilterableProperty<TEntity>> = any;
8
+ export type CollectionNavKeys<TEntity extends QueryableEntity> = {
9
+ [K in keyof TEntity['navigations']]: TEntity['navigations'][K]['collection'] extends true ? K : never;
10
+ }[keyof TEntity['navigations']];
11
+ export type SingleNavKeys<TEntity extends QueryableEntity> = {
12
+ [K in keyof TEntity['navigations']]: TEntity['navigations'][K]['collection'] extends true ? never : K;
13
+ }[keyof TEntity['navigations']];
14
+ export interface FilterBuilder<TEntity extends QueryableEntity> {
15
+ and: (expr: FilterBuilder<TEntity>) => FilterBuilder<TEntity>;
16
+ or: (expr: FilterBuilder<TEntity>) => FilterBuilder<TEntity>;
17
+ __brand: 'FilterBuilder';
18
+ }
19
+ export interface FilterHelpers<TEntity extends QueryableEntity, S extends Schema<S> = Schema<any>> {
20
+ /**
21
+ * Filter on a simple scalar property of the current entity.
22
+ */
23
+ clause: <P extends FilterableProperty<TEntity>>(prop: P, op: ComparisonOperator, value: FilterPropertyValueType<TEntity, P>) => FilterBuilder<TEntity>;
24
+ /**
25
+ * Filter on a Single-Valued Navigation Property (Lookup).
26
+ * This allows "stepping into" a related entity to filter on its properties.
27
+ */
28
+ nav: <N extends SingleNavKeys<TEntity>>(nav: N, cb: (h: FilterHelpers<ResolveNavTargetQE<S, TEntity, N>, S>) => FilterBuilder<ResolveNavTargetQE<S, TEntity, N>>) => FilterBuilder<TEntity>;
29
+ /**
30
+ * Filter on a Collection Navigation Property using 'any' (at least one match).
31
+ */
32
+ any: <N extends CollectionNavKeys<TEntity>>(nav: N, cb: (h: FilterHelpers<ResolveNavTargetQE<S, TEntity, N>, S>) => FilterBuilder<ResolveNavTargetQE<S, TEntity, N>>) => FilterBuilder<TEntity>;
33
+ /**
34
+ * Filter on a Collection Navigation Property using 'all' (all must match).
35
+ */
36
+ all: <N extends CollectionNavKeys<TEntity>>(nav: N, cb: (h: FilterHelpers<ResolveNavTargetQE<S, TEntity, N>, S>) => FilterBuilder<ResolveNavTargetQE<S, TEntity, N>>) => FilterBuilder<TEntity>;
37
+ }
38
+ export declare function createFilterHelpers<TEntity extends QueryableEntity, S extends Schema<S> = Schema<any>>(entityDef: TEntity, schema?: S): FilterHelpers<TEntity, S>;
39
+ export declare function serializeFilter<S extends Schema<S> = Schema<any>>(filterState: any[], depth?: number, lambdaVar?: string, entityDef?: QueryableEntity, schema?: S): string;
40
+ export {};