Skip to content

Commit 387f954

Browse files
committed
MLE-25554 TS for a handful of documents functions
1 parent 74264b0 commit 387f954

File tree

5 files changed

+438
-2
lines changed

5 files changed

+438
-2
lines changed

marklogic.d.ts

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,116 @@ declare module 'marklogic' {
7272
httpStatusMessage?: string;
7373
}
7474

75+
/**
76+
* Generic document content type - can be JSON, XML, text, or binary
77+
*/
78+
export type DocumentContent = any;
79+
80+
/**
81+
* A document descriptor for reading or writing documents.
82+
*/
83+
export interface DocumentDescriptor {
84+
/** The URI identifier for the document */
85+
uri: string;
86+
/** The content of the document (JSON, XML, text, or Buffer for binary) */
87+
content?: DocumentContent;
88+
/** The MIME type of the document */
89+
contentType?: string;
90+
/** Collections to which the document belongs */
91+
collections?: string | string[];
92+
/** Permissions controlling document access */
93+
permissions?: Array<{
94+
'role-name': string;
95+
capabilities: string[];
96+
}>;
97+
/** Properties (metadata) for the document */
98+
properties?: Record<string, any>;
99+
/** Quality ranking for the document */
100+
quality?: number;
101+
/** Metadata values for the document */
102+
metadataValues?: Record<string, any>;
103+
}
104+
105+
/**
106+
* Result from a probe operation indicating if a document exists.
107+
*/
108+
export interface ProbeResult {
109+
/** The URI of the document */
110+
uri: string;
111+
/** Whether the document exists */
112+
exists: boolean;
113+
/** Content type if document exists */
114+
contentType?: string;
115+
/** Content length if document exists */
116+
contentLength?: number;
117+
}
118+
119+
/**
120+
* Result from a remove operation.
121+
*/
122+
export interface RemoveResult {
123+
/** Array of removed document URIs */
124+
uris: string[];
125+
/** Whether documents were removed */
126+
removed: boolean;
127+
/** System time of the operation */
128+
systemTime?: string;
129+
}
130+
131+
/**
132+
* Result from a write operation.
133+
*/
134+
export interface WriteResult {
135+
/** Array of document descriptors with URIs of written documents */
136+
documents: DocumentDescriptor[];
137+
/** System time of the operation */
138+
systemTime?: string;
139+
}
140+
141+
/**
142+
* Documents interface for reading and writing documents.
143+
*/
144+
export interface Documents {
145+
/**
146+
* Checks whether a document exists.
147+
* @param uri - The URI of the document to check
148+
* @returns A result provider that resolves to probe result
149+
*/
150+
probe(uri: string): ResultProvider<ProbeResult>;
151+
152+
/**
153+
* Reads one or more documents.
154+
* @param uris - A URI string or array of URI strings
155+
* @returns A result provider that resolves to an array of document descriptors
156+
*/
157+
read(uris: string | string[]): ResultProvider<DocumentDescriptor[]>;
158+
159+
/**
160+
* Writes one or more documents.
161+
* @param documents - A document descriptor or array of document descriptors
162+
* @returns A result provider that resolves to a write result with document URIs
163+
*/
164+
write(documents: DocumentDescriptor | DocumentDescriptor[]): ResultProvider<WriteResult>;
165+
166+
/**
167+
* Removes one or more documents.
168+
* @param uris - A URI string or array of URI strings
169+
* @returns A result provider that resolves to a remove result
170+
*/
171+
remove(uris: string | string[]): ResultProvider<RemoveResult>;
172+
}
173+
75174
/**
76175
* A database client object returned by createDatabaseClient.
77176
* Provides access to document, graph, and query operations.
78177
*/
79178
export interface DatabaseClient {
179+
/**
180+
* Documents interface for reading and writing documents.
181+
* @since 1.0
182+
*/
183+
documents: Documents;
184+
80185
/**
81186
* Tests if a connection is successful.
82187
* Call .result() to get a promise.

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,10 @@
1818
"scripts": {
1919
"doc": "jsdoc -c jsdoc.json lib/*.js README.md",
2020
"lint": "gulp lint",
21+
"pretest:typescript": "npm run test:compile",
2122
"test:types": "tsc --noEmit",
22-
"test:compile": "tsc test-typescript/checkConnection-runtime.test.ts",
23-
"pretest:typescript": "npm run test:compile"
23+
"test:compile": "tsc test-typescript/*-runtime.test.ts",
24+
"test:typescript": "npx mocha test-typescript/*.js"
2425
},
2526
"keywords": [
2627
"marklogic",
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
/*
2+
* Copyright (c) 2015-2025 Progress Software Corporation and/or its subsidiaries or affiliates. All Rights Reserved.
3+
*/
4+
5+
/// <reference path="../marklogic.d.ts" />
6+
7+
/**
8+
* Runtime validation tests for Documents API.
9+
*
10+
* These tests make actual calls to MarkLogic to verify:
11+
* - Type definitions match runtime behavior
12+
* - ResultProvider pattern works correctly
13+
* - Methods return expected data structures
14+
*
15+
* Run with: npm run test:compile && npx mocha test-typescript/*.js
16+
*/
17+
18+
import should = require('should');
19+
import type { DatabaseClient } from 'marklogic';
20+
21+
const testConfig = require('../etc/test-config.js');
22+
const marklogic = require('../lib/marklogic.js');
23+
24+
describe('Documents API runtime validation', function() {
25+
let client: DatabaseClient;
26+
const testUri = '/test-typescript/documents-runtime-test.json';
27+
const testContent = { message: 'TypeScript test document', timestamp: Date.now() };
28+
29+
before(function() {
30+
client = marklogic.createDatabaseClient(testConfig.restWriterConnection);
31+
});
32+
33+
after(function() {
34+
client.release();
35+
});
36+
37+
it('write() returns ResultProvider with WriteResult', async function() {
38+
const resultProvider = client.documents.write({
39+
uri: testUri,
40+
content: testContent,
41+
contentType: 'application/json'
42+
});
43+
44+
// Verify ResultProvider has result() method
45+
resultProvider.should.have.property('result');
46+
resultProvider.result.should.be.a.Function();
47+
48+
const writeResult = await resultProvider.result();
49+
50+
// Verify return type is WriteResult with documents array
51+
writeResult.should.be.an.Object();
52+
writeResult.should.have.property('documents');
53+
writeResult.documents.should.be.an.Array();
54+
writeResult.documents.should.have.length(1);
55+
writeResult.documents[0].should.have.property('uri');
56+
writeResult.documents[0].uri.should.equal(testUri);
57+
});
58+
59+
it('probe() returns ResultProvider with ProbeResult', async function() {
60+
const resultProvider = client.documents.probe(testUri);
61+
62+
// Verify ResultProvider has result() method
63+
resultProvider.should.have.property('result');
64+
resultProvider.result.should.be.a.Function();
65+
66+
const probeResult = await resultProvider.result();
67+
68+
// Verify ProbeResult structure
69+
probeResult.should.have.property('uri', testUri);
70+
probeResult.should.have.property('exists', true);
71+
probeResult.should.have.property('contentType');
72+
probeResult.should.have.property('contentLength');
73+
});
74+
75+
it('read() returns ResultProvider with DocumentDescriptor array', async function() {
76+
const resultProvider = client.documents.read(testUri);
77+
78+
// Verify ResultProvider has result() method
79+
resultProvider.should.have.property('result');
80+
resultProvider.result.should.be.a.Function();
81+
82+
const docs = await resultProvider.result();
83+
84+
// Verify return type is DocumentDescriptor array
85+
docs.should.be.an.Array();
86+
docs.should.have.length(1);
87+
88+
const doc = docs[0];
89+
doc.should.have.property('uri', testUri);
90+
doc.should.have.property('content');
91+
doc.content.should.have.property('message', testContent.message);
92+
});
93+
94+
it('remove() returns ResultProvider with RemoveResult', async function() {
95+
const resultProvider = client.documents.remove(testUri);
96+
97+
// Verify ResultProvider has result() method
98+
resultProvider.should.have.property('result');
99+
resultProvider.result.should.be.a.Function();
100+
101+
const result = await resultProvider.result();
102+
103+
// Verify RemoveResult structure
104+
result.should.have.property('uris');
105+
result.should.have.property('removed', true);
106+
result.uris.should.be.an.Array();
107+
result.uris.should.have.length(1);
108+
result.uris[0].should.equal(testUri);
109+
110+
// Verify document was actually removed
111+
const probeResult = await client.documents.probe(testUri).result();
112+
probeResult.exists.should.equal(false);
113+
});
114+
});

0 commit comments

Comments
 (0)