Skip to content

Commit 7c53fe2

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

5 files changed

Lines changed: 420 additions & 2 deletions

File tree

marklogic.d.ts

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,106 @@ 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+
* Documents interface for reading and writing documents.
133+
*/
134+
export interface Documents {
135+
/**
136+
* Checks whether a document exists.
137+
* @param uri - The URI of the document to check
138+
* @returns A result provider that resolves to probe result
139+
*/
140+
probe(uri: string): ResultProvider<ProbeResult>;
141+
142+
/**
143+
* Reads one or more documents.
144+
* @param uris - A URI string or array of URI strings
145+
* @returns A result provider that resolves to an array of document descriptors
146+
*/
147+
read(uris: string | string[]): ResultProvider<DocumentDescriptor[]>;
148+
149+
/**
150+
* Writes one or more documents.
151+
* @param documents - A document descriptor or array of document descriptors
152+
* @returns A result provider that resolves to an array of URIs for written documents
153+
*/
154+
write(documents: DocumentDescriptor | DocumentDescriptor[]): ResultProvider<string[]>;
155+
156+
/**
157+
* Removes one or more documents.
158+
* @param uris - A URI string or array of URI strings
159+
* @returns A result provider that resolves to a remove result
160+
*/
161+
remove(uris: string | string[]): ResultProvider<RemoveResult>;
162+
}
163+
75164
/**
76165
* A database client object returned by createDatabaseClient.
77166
* Provides access to document, graph, and query operations.
78167
*/
79168
export interface DatabaseClient {
169+
/**
170+
* Documents interface for reading and writing documents.
171+
* @since 1.0
172+
*/
173+
documents: Documents;
174+
80175
/**
81176
* Tests if a connection is successful.
82177
* 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: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
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 URI array', 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 uris = await resultProvider.result();
49+
50+
// Verify return type is string array
51+
uris.should.be.an.Array();
52+
uris.should.have.length(1);
53+
uris[0].should.equal(testUri);
54+
});
55+
56+
it('probe() returns ResultProvider with ProbeResult', async function() {
57+
const resultProvider = client.documents.probe(testUri);
58+
59+
// Verify ResultProvider has result() method
60+
resultProvider.should.have.property('result');
61+
resultProvider.result.should.be.a.Function();
62+
63+
const probeResult = await resultProvider.result();
64+
65+
// Verify ProbeResult structure
66+
probeResult.should.have.property('uri', testUri);
67+
probeResult.should.have.property('exists', true);
68+
probeResult.should.have.property('contentType');
69+
probeResult.should.have.property('contentLength');
70+
});
71+
72+
it('read() returns ResultProvider with DocumentDescriptor array', async function() {
73+
const resultProvider = client.documents.read(testUri);
74+
75+
// Verify ResultProvider has result() method
76+
resultProvider.should.have.property('result');
77+
resultProvider.result.should.be.a.Function();
78+
79+
const docs = await resultProvider.result();
80+
81+
// Verify return type is DocumentDescriptor array
82+
docs.should.be.an.Array();
83+
docs.should.have.length(1);
84+
85+
const doc = docs[0];
86+
doc.should.have.property('uri', testUri);
87+
doc.should.have.property('content');
88+
doc.content.should.have.property('message', testContent.message);
89+
});
90+
91+
it('remove() returns ResultProvider with RemoveResult', async function() {
92+
const resultProvider = client.documents.remove(testUri);
93+
94+
// Verify ResultProvider has result() method
95+
resultProvider.should.have.property('result');
96+
resultProvider.result.should.be.a.Function();
97+
98+
const result = await resultProvider.result();
99+
100+
// Verify RemoveResult structure
101+
result.should.have.property('uris');
102+
result.should.have.property('removed', true);
103+
result.uris.should.be.an.Array();
104+
result.uris.should.have.length(1);
105+
result.uris[0].should.equal(testUri);
106+
107+
// Verify document was actually removed
108+
const probeResult = await client.documents.probe(testUri).result();
109+
probeResult.exists.should.equal(false);
110+
});
111+
});

0 commit comments

Comments
 (0)