@cubicweb/client 3.0.0-alpha.11 → 3.0.0-alpha.14
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 +11 -20
- package/lib/api/Api.d.ts +29 -21
- package/lib/api/Api.d.ts.map +1 -1
- package/lib/api/Api.js +30 -16
- package/lib/api/Api.js.map +1 -1
- package/lib/api/Client.d.ts +19 -7
- package/lib/api/Client.d.ts.map +1 -1
- package/lib/api/Client.js +20 -9
- package/lib/api/Client.js.map +1 -1
- package/lib/api/ResultSet.d.ts +25 -0
- package/lib/api/ResultSet.d.ts.map +1 -0
- package/lib/api/ResultSet.js +12 -0
- package/lib/api/ResultSet.js.map +1 -0
- package/lib/api/binary/RQLBinary.d.ts +6 -0
- package/lib/api/binary/RQLBinary.d.ts.map +1 -0
- package/lib/api/binary/RQLBinary.js +10 -0
- package/lib/api/binary/RQLBinary.js.map +1 -0
- package/lib/api/query/RQLQuery.d.ts +40 -0
- package/lib/api/query/RQLQuery.d.ts.map +1 -0
- package/lib/api/query/RQLQuery.js +48 -0
- package/lib/api/query/RQLQuery.js.map +1 -0
- package/lib/api/query/references.d.ts +41 -0
- package/lib/api/query/references.d.ts.map +1 -0
- package/lib/api/query/references.js +50 -0
- package/lib/api/query/references.js.map +1 -0
- package/lib/api/transaction/Transaction.d.ts +25 -0
- package/lib/api/transaction/Transaction.d.ts.map +1 -0
- package/lib/api/transaction/Transaction.js +28 -0
- package/lib/api/transaction/Transaction.js.map +1 -0
- package/lib/api/transaction/TransactionResult.d.ts +32 -0
- package/lib/api/transaction/TransactionResult.d.ts.map +1 -0
- package/lib/api/transaction/TransactionResult.js +41 -0
- package/lib/api/transaction/TransactionResult.js.map +1 -0
- package/lib/{Errors.d.ts → errors.d.ts} +7 -4
- package/lib/errors.d.ts.map +1 -0
- package/lib/{Errors.js → errors.js} +7 -7
- package/lib/errors.js.map +1 -0
- package/lib/index.d.ts +8 -3
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +7 -3
- package/lib/index.js.map +1 -1
- package/lib/schema/classes/RelationDefinitionSchema.js +1 -1
- package/lib/schema/classes/RelationDefinitionSchema.js.map +1 -1
- package/lib/schema/classes/Schema.js.map +1 -1
- package/lib/schema/classes/Utils.d.ts.map +1 -1
- package/lib/schema/classes/Utils.js.map +1 -1
- package/lib/schema/raw/Constraints.d.ts +27 -0
- package/lib/schema/raw/Constraints.d.ts.map +1 -1
- package/lib/utils.d.ts +39 -0
- package/lib/utils.d.ts.map +1 -0
- package/lib/utils.js +58 -0
- package/lib/utils.js.map +1 -0
- package/package.json +7 -1
- package/src/api/Api.ts +60 -43
- package/src/api/Client.ts +23 -18
- package/src/api/ResultSet.ts +29 -0
- package/src/api/binary/RQLBinary.ts +9 -0
- package/src/api/query/RQLQuery.ts +51 -0
- package/src/api/query/references.ts +43 -0
- package/src/api/transaction/Transaction.ts +31 -0
- package/src/api/transaction/TransactionResult.ts +46 -0
- package/src/{Errors.ts → errors.ts} +6 -7
- package/src/index.ts +11 -7
- package/src/schema/classes/RelationDefinitionSchema.ts +1 -1
- package/src/schema/raw/Constraints.ts +28 -0
- package/src/utils.ts +91 -0
- package/lib/Errors.d.ts.map +0 -1
- package/lib/Errors.js.map +0 -1
- package/lib/api/Transaction.d.ts +0 -46
- package/lib/api/Transaction.d.ts.map +0 -1
- package/lib/api/Transaction.js +0 -72
- package/lib/api/Transaction.js.map +0 -1
- package/src/api/Transaction.ts +0 -94
package/src/api/Api.ts
CHANGED
|
@@ -1,46 +1,35 @@
|
|
|
1
|
-
import { ValidationError } from "../
|
|
1
|
+
import { ValidationError } from "../errors.js";
|
|
2
2
|
import { RawSchema } from "../schema/raw/Schema.js";
|
|
3
|
-
import {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
} from "./
|
|
3
|
+
import { getBinariesFromTransaction, serializeQueryParams } from "../utils.js";
|
|
4
|
+
import { ResultSet, ResultSetCell } from "./ResultSet.js";
|
|
5
|
+
import { RQLBinary } from "./binary/RQLBinary.js";
|
|
6
|
+
import { RQLQueryCellRef } from "./query/references.js";
|
|
7
|
+
import { Transaction } from "./transaction/Transaction.js";
|
|
8
|
+
import { TransactionResult } from "./transaction/TransactionResult.js";
|
|
9
9
|
|
|
10
10
|
/**
|
|
11
|
-
* Type representing
|
|
11
|
+
* Type representing valid final values to use as parameters in a RQL query.
|
|
12
|
+
*
|
|
13
|
+
* For references to requests/binaries, see {@link RQLQueryCellRef} and {@link RQLQueryBinaryRef}
|
|
12
14
|
*
|
|
13
15
|
* @category Api
|
|
14
16
|
*/
|
|
15
|
-
export type
|
|
17
|
+
export type RQLQueryParamValue = string | number | boolean | null;
|
|
16
18
|
|
|
17
|
-
export type RQLParamValue = string | number | boolean | null;
|
|
18
19
|
/**
|
|
19
20
|
* Type representing RQL parameters to send along a request.
|
|
20
21
|
*
|
|
22
|
+
* Each key must be referenced in the RQL query.
|
|
23
|
+
*
|
|
21
24
|
* @category Api
|
|
22
25
|
*/
|
|
23
|
-
export type
|
|
26
|
+
export type RQLQueryParams = Record<
|
|
24
27
|
string,
|
|
25
|
-
|
|
28
|
+
RQLQueryParamValue | RQLQueryCellRef | RQLBinary
|
|
26
29
|
>;
|
|
27
30
|
|
|
28
31
|
/**
|
|
29
|
-
* Type representing
|
|
30
|
-
*
|
|
31
|
-
* @category Api
|
|
32
|
-
*/
|
|
33
|
-
export type RqlRow = Array<string | number | boolean | null>;
|
|
34
|
-
|
|
35
|
-
/**
|
|
36
|
-
* Type representing a RQL request result.
|
|
37
|
-
*
|
|
38
|
-
* @category Api
|
|
39
|
-
*/
|
|
40
|
-
export type ResultSet = Array<RqlRow>;
|
|
41
|
-
|
|
42
|
-
/**
|
|
43
|
-
* Type representing the currently logged user's data
|
|
32
|
+
* Type representing the currently logged user's data as returned by the `/current-user` route
|
|
44
33
|
*
|
|
45
34
|
* @category Api
|
|
46
35
|
*/
|
|
@@ -64,17 +53,24 @@ type ApiError = {
|
|
|
64
53
|
/**
|
|
65
54
|
* Type representing the error data sent to clients when retrieving an API error.
|
|
66
55
|
*
|
|
56
|
+
* It simply adds the HTTP status code in a `status` key to the regular CubicWeb api error,
|
|
57
|
+
* so users can handle HTTP status codes in their code.
|
|
58
|
+
*
|
|
67
59
|
* @category Api
|
|
68
60
|
*/
|
|
69
61
|
export type ApiErrorResponse = {
|
|
70
62
|
status: number;
|
|
71
63
|
} & ApiError;
|
|
72
64
|
|
|
65
|
+
type RawTransactionResult = ReadonlyArray<
|
|
66
|
+
ReadonlyArray<ReadonlyArray<ResultSetCell>>
|
|
67
|
+
>;
|
|
68
|
+
|
|
73
69
|
/**
|
|
74
70
|
* Class used to handle requests to the CubicWeb API.
|
|
75
71
|
*
|
|
76
72
|
* You should never have to create this object yourself.
|
|
77
|
-
* Instead, retrieve this object from the methods in the
|
|
73
|
+
* Instead, retrieve this object from the methods in the {@link Client} class.
|
|
78
74
|
*
|
|
79
75
|
* @internal
|
|
80
76
|
*/
|
|
@@ -92,7 +88,7 @@ export default class Api {
|
|
|
92
88
|
* Class used to handle requests to the CubicWeb API.
|
|
93
89
|
*
|
|
94
90
|
* You should never have to create this object yourself.
|
|
95
|
-
* Instead, retrieve this object from the methods in the
|
|
91
|
+
* Instead, retrieve this object from the methods in the {@link Client} class.
|
|
96
92
|
*
|
|
97
93
|
* @param apiUrl The API url. It should not include the API version as it will be automatically added.
|
|
98
94
|
* Example: if you can retrieve the schema with https://example.com/api/v1/schema,
|
|
@@ -132,7 +128,7 @@ export default class Api {
|
|
|
132
128
|
* Tries to log in the user on the configured CubicWeb instance with the given credentials.
|
|
133
129
|
*
|
|
134
130
|
* @param login The user login
|
|
135
|
-
* @param password
|
|
131
|
+
* @param password The user password
|
|
136
132
|
* @returns A promise returning true if the login was successful, false otherwise
|
|
137
133
|
*/
|
|
138
134
|
async login(login: string, password: string): Promise<boolean> {
|
|
@@ -178,7 +174,9 @@ export default class Api {
|
|
|
178
174
|
/**
|
|
179
175
|
* Get the binary for the given entity attribute
|
|
180
176
|
*
|
|
181
|
-
* @
|
|
177
|
+
* @param eid Entity eid to fetch the binary from
|
|
178
|
+
* @param attribute Attribute on the entity in which the binary is stored
|
|
179
|
+
* @returns A promise resolving to the binary as a Blob
|
|
182
180
|
*/
|
|
183
181
|
async binary(eid: number, attribute: string): Promise<Blob> {
|
|
184
182
|
const params = new URLSearchParams({
|
|
@@ -191,10 +189,20 @@ export default class Api {
|
|
|
191
189
|
});
|
|
192
190
|
}
|
|
193
191
|
|
|
192
|
+
/**
|
|
193
|
+
* Executes the given transaction.
|
|
194
|
+
*
|
|
195
|
+
* If the {@link Transaction} object contains binaries, the request is automatically
|
|
196
|
+
* sent using `multipart/form-data` to allow uploading binaries.
|
|
197
|
+
*
|
|
198
|
+
* @param transaction Transaction to execute
|
|
199
|
+
* @returns A promise returning the transaction result on success
|
|
200
|
+
*/
|
|
194
201
|
async rql(transaction: Transaction): Promise<TransactionResult> {
|
|
202
|
+
const binaries = getBinariesFromTransaction(transaction);
|
|
195
203
|
try {
|
|
196
|
-
if (
|
|
197
|
-
return await this.multipartTransaction(transaction);
|
|
204
|
+
if (binaries.length > 0) {
|
|
205
|
+
return await this.multipartTransaction(transaction, binaries);
|
|
198
206
|
} else {
|
|
199
207
|
return await this.jsonTransaction(transaction);
|
|
200
208
|
}
|
|
@@ -204,21 +212,24 @@ export default class Api {
|
|
|
204
212
|
}
|
|
205
213
|
|
|
206
214
|
private getTransactionParams(transaction: Transaction) {
|
|
207
|
-
return transaction.queries.map((
|
|
208
|
-
query:
|
|
209
|
-
params:
|
|
215
|
+
return transaction.queries.map((rqlQuery) => ({
|
|
216
|
+
query: rqlQuery.rql,
|
|
217
|
+
params: serializeQueryParams(transaction, rqlQuery.params),
|
|
210
218
|
}));
|
|
211
219
|
}
|
|
212
220
|
|
|
213
|
-
private async multipartTransaction(
|
|
221
|
+
private async multipartTransaction(
|
|
222
|
+
transaction: Transaction,
|
|
223
|
+
binaries: RQLBinary[]
|
|
224
|
+
) {
|
|
214
225
|
const jsonParams = this.getTransactionParams(transaction);
|
|
215
226
|
const formData = new FormData();
|
|
216
227
|
formData.append("queries", JSON.stringify(jsonParams));
|
|
217
|
-
|
|
218
|
-
formData.append(
|
|
228
|
+
binaries.forEach((binary) => {
|
|
229
|
+
formData.append(binary.uuid, binary.blob);
|
|
219
230
|
});
|
|
220
231
|
|
|
221
|
-
const resultSets = await nonNullFetchApi<
|
|
232
|
+
const resultSets = await nonNullFetchApi<RawTransactionResult>(
|
|
222
233
|
this._rqlApiUrl,
|
|
223
234
|
{
|
|
224
235
|
method: "POST",
|
|
@@ -226,11 +237,14 @@ export default class Api {
|
|
|
226
237
|
body: formData,
|
|
227
238
|
}
|
|
228
239
|
);
|
|
229
|
-
return new TransactionResult(
|
|
240
|
+
return new TransactionResult(
|
|
241
|
+
transaction,
|
|
242
|
+
resultSets.map((r) => new ResultSet(r))
|
|
243
|
+
);
|
|
230
244
|
}
|
|
231
245
|
|
|
232
246
|
private async jsonTransaction(transaction: Transaction) {
|
|
233
|
-
const resultSets = await nonNullFetchApi<
|
|
247
|
+
const resultSets = await nonNullFetchApi<RawTransactionResult>(
|
|
234
248
|
this._rqlApiUrl,
|
|
235
249
|
{
|
|
236
250
|
method: "POST",
|
|
@@ -238,7 +252,10 @@ export default class Api {
|
|
|
238
252
|
body: JSON.stringify(this.getTransactionParams(transaction)),
|
|
239
253
|
}
|
|
240
254
|
);
|
|
241
|
-
return new TransactionResult(
|
|
255
|
+
return new TransactionResult(
|
|
256
|
+
transaction,
|
|
257
|
+
resultSets.map((r) => new ResultSet(r))
|
|
258
|
+
);
|
|
242
259
|
}
|
|
243
260
|
|
|
244
261
|
private handleUserErrors(error: unknown): never {
|
package/src/api/Client.ts
CHANGED
|
@@ -1,10 +1,9 @@
|
|
|
1
1
|
import { Schema } from "../schema/classes/Schema.js";
|
|
2
|
-
import Api, {
|
|
3
|
-
import {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
} from "./Transaction.js";
|
|
2
|
+
import Api, { RQLQueryParams, CurrentUser } from "./Api.js";
|
|
3
|
+
import { ResultSet } from "./ResultSet.js";
|
|
4
|
+
import { RQLQuery } from "./query/RQLQuery.js";
|
|
5
|
+
import { Transaction } from "./transaction/Transaction.js";
|
|
6
|
+
import { TransactionResult } from "./transaction/TransactionResult.js";
|
|
8
7
|
|
|
9
8
|
/**
|
|
10
9
|
* Class used to communicate with a CubicWeb instance's API.
|
|
@@ -32,20 +31,19 @@ export default class Client {
|
|
|
32
31
|
/**
|
|
33
32
|
* Executes the given query and waits for the response.
|
|
34
33
|
*
|
|
35
|
-
*
|
|
34
|
+
* If binaries are given, the request is automatically
|
|
35
|
+
* sent using `multipart/form-data` to allow uploading binaries.
|
|
36
|
+
*
|
|
37
|
+
* @param rql The RQL query to send
|
|
36
38
|
* @param params The additional parameters for the request
|
|
37
39
|
* @returns A promise with the request's result set
|
|
38
40
|
*/
|
|
39
|
-
async execute(
|
|
40
|
-
query: string,
|
|
41
|
-
params: RQLParams = {},
|
|
42
|
-
binaries?: BinariesParam
|
|
43
|
-
): Promise<ResultSet> {
|
|
41
|
+
async execute(rql: string, params: RQLQueryParams = {}): Promise<ResultSet> {
|
|
44
42
|
const transaction = new Transaction();
|
|
45
|
-
const
|
|
46
|
-
transaction.
|
|
43
|
+
const query = new RQLQuery(rql, params);
|
|
44
|
+
transaction.push(query);
|
|
47
45
|
const result = await this.executeTransaction(transaction);
|
|
48
|
-
return result.
|
|
46
|
+
return result.getResultSet(query);
|
|
49
47
|
}
|
|
50
48
|
|
|
51
49
|
/**
|
|
@@ -62,7 +60,7 @@ export default class Client {
|
|
|
62
60
|
* Tries to log in the user on the configured CubicWeb instance with the given credentials.
|
|
63
61
|
*
|
|
64
62
|
* @param login The user login
|
|
65
|
-
* @param password
|
|
63
|
+
* @param password The user password
|
|
66
64
|
* @returns A promise returning true if the login was successful, false otherwise
|
|
67
65
|
*/
|
|
68
66
|
async login(login: string, password: string) {
|
|
@@ -88,8 +86,13 @@ export default class Client {
|
|
|
88
86
|
}
|
|
89
87
|
|
|
90
88
|
/**
|
|
89
|
+
* Executes the given transaction.
|
|
90
|
+
*
|
|
91
|
+
* If the {@link Transaction} object contains binaries, the request is automatically
|
|
92
|
+
* sent using `multipart/form-data` to allow uploading binaries.
|
|
93
|
+
*
|
|
91
94
|
* @param transaction Transaction to execute
|
|
92
|
-
* @returns A promise
|
|
95
|
+
* @returns A promise returning the transaction result on success
|
|
93
96
|
*/
|
|
94
97
|
async executeTransaction(
|
|
95
98
|
transaction: Transaction
|
|
@@ -100,7 +103,9 @@ export default class Client {
|
|
|
100
103
|
/**
|
|
101
104
|
* Get the binary for the given entity attribute
|
|
102
105
|
*
|
|
103
|
-
* @
|
|
106
|
+
* @param eid Entity eid to fetch the binary from
|
|
107
|
+
* @param attribute Attribute on the entity in which the binary is stored
|
|
108
|
+
* @returns A promise resolving to the binary as a Blob
|
|
104
109
|
*/
|
|
105
110
|
async getBinary(eid: number, attribute: string) {
|
|
106
111
|
return await this.api.binary(eid, attribute);
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Type representing a final value in a result set.
|
|
3
|
+
*
|
|
4
|
+
* Please not this type may not be accurate if you use entity methods,
|
|
5
|
+
* as those methods may return any data structure.
|
|
6
|
+
*
|
|
7
|
+
* @category Api
|
|
8
|
+
*/
|
|
9
|
+
export type ResultSetCell = string | number | boolean | null;
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Type representing a row returned in a result set.
|
|
13
|
+
*
|
|
14
|
+
* @category Api
|
|
15
|
+
*/
|
|
16
|
+
export type ResultSetRow<
|
|
17
|
+
T extends ReadonlyArray<ResultSetCell> = ReadonlyArray<ResultSetCell>
|
|
18
|
+
> = T;
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Class representing a RQL request result.
|
|
22
|
+
*
|
|
23
|
+
* @category Api
|
|
24
|
+
*/
|
|
25
|
+
export class ResultSet<
|
|
26
|
+
T extends ReadonlyArray<ResultSetCell> = ReadonlyArray<ResultSetCell>
|
|
27
|
+
> {
|
|
28
|
+
constructor(public readonly rows: ReadonlyArray<T>) {}
|
|
29
|
+
}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { v4 as uuidv4 } from "uuid";
|
|
2
|
+
import { RQLQueryCellRef, RQLQueryRowRef } from "./references.js";
|
|
3
|
+
import { RQLQueryParams } from "../Api.js";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Class representing a RQL query to be sent to the API.
|
|
7
|
+
*
|
|
8
|
+
* Queries are identified by a uuid and are independent from transactions.
|
|
9
|
+
*
|
|
10
|
+
* If a query has been used in a transaction, RQLQuery objects can easily retrieve
|
|
11
|
+
* a reference to the query for later resolving the values in a {@link TransactionResult}.
|
|
12
|
+
*
|
|
13
|
+
* @category Api
|
|
14
|
+
*/
|
|
15
|
+
export class RQLQuery {
|
|
16
|
+
public readonly queryUUID: string;
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Class representing a RQL query to be sent to the API.
|
|
20
|
+
*
|
|
21
|
+
* Queries are identified by a uuid and are independent from transactions.
|
|
22
|
+
*
|
|
23
|
+
* If a query has been used in a transaction, RQLQuery objects can easily retrieve
|
|
24
|
+
* a reference to the query for later resolving the values in a {@link TransactionResult}.
|
|
25
|
+
*
|
|
26
|
+
* @param rql The rql query
|
|
27
|
+
* @param params The parameters for the rql query
|
|
28
|
+
*
|
|
29
|
+
* @category Api
|
|
30
|
+
*/
|
|
31
|
+
constructor(
|
|
32
|
+
public readonly rql: string,
|
|
33
|
+
public readonly params: RQLQueryParams = {}
|
|
34
|
+
) {
|
|
35
|
+
this.queryUUID = uuidv4();
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* @returns A reference to a {@link ResultSetRow} in the result of this query.
|
|
40
|
+
*/
|
|
41
|
+
rowRef(row: number): RQLQueryRowRef {
|
|
42
|
+
return new RQLQueryRowRef(this.queryUUID, row);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* @returns A reference to a {@link ResultSetCell} in the result of this query.
|
|
47
|
+
*/
|
|
48
|
+
cellRef(row: number, column: number): RQLQueryCellRef {
|
|
49
|
+
return new RQLQueryCellRef(this.queryUUID, row, column);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Class representing a reference to a {@link ResultSetRow} in the result of a {@link RQLQuery}.
|
|
3
|
+
*
|
|
4
|
+
* Use this to resolve a {@link ResultSetRow} of the referenced {@link RQLQuery}
|
|
5
|
+
* from a {@link TransactionResult}.
|
|
6
|
+
*
|
|
7
|
+
* @category Api
|
|
8
|
+
*/
|
|
9
|
+
export class RQLQueryRowRef {
|
|
10
|
+
constructor(public readonly queryUUID: string, public readonly row: number) {}
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Retrieve a reference to the given column in this row.
|
|
14
|
+
*
|
|
15
|
+
* @param c The column to get a reference for
|
|
16
|
+
* @returns A reference to the given column in the current row
|
|
17
|
+
*
|
|
18
|
+
* @category Api
|
|
19
|
+
*/
|
|
20
|
+
column(c: number): RQLQueryCellRef {
|
|
21
|
+
return new RQLQueryCellRef(this.queryUUID, this.row, c);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Class representing a reference to a {@link ResultSetCell} in the result of a {@link RQLQuery}.
|
|
27
|
+
*
|
|
28
|
+
* Use this to resolve a {@link ResultSetCell} of the referenced {@link RQLQuery}
|
|
29
|
+
* from a {@link TransactionResult}.
|
|
30
|
+
*
|
|
31
|
+
* This reference can also be used in parameters of other {@link RQLQuery}.
|
|
32
|
+
* If both {@link RQLQuery} are used in the same {@link RQLQuery},
|
|
33
|
+
* then the resolved value from the first query will be used in the second.
|
|
34
|
+
*
|
|
35
|
+
* @category Api
|
|
36
|
+
*/
|
|
37
|
+
export class RQLQueryCellRef {
|
|
38
|
+
constructor(
|
|
39
|
+
public readonly queryUUID: string,
|
|
40
|
+
public readonly row: number,
|
|
41
|
+
public readonly column: number
|
|
42
|
+
) {}
|
|
43
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { RQLQuery } from "../query/RQLQuery.js";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Class representing a set of {@link RQLQuery} executed one after the other in a single HTTP request.
|
|
5
|
+
*
|
|
6
|
+
* These {@link RQLQuery} can be linked together by using a {@link RQLQueryCellRef}
|
|
7
|
+
* of one {@link RQLQuery} in the parameters of a subsequent {@link RQLQuery}.
|
|
8
|
+
*
|
|
9
|
+
* Binary files are global to the transaction and can be referenced using a {@link RQLQueryBinaryRef}
|
|
10
|
+
* in the parameters of a {@link RQLQuery}.
|
|
11
|
+
*/
|
|
12
|
+
export class Transaction {
|
|
13
|
+
private _queries: Array<RQLQuery> = [];
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Adds the given queries to the transaction.
|
|
17
|
+
*
|
|
18
|
+
* Each {@link RQLQuery} will be executed one after the other.
|
|
19
|
+
* These {@link RQLQuery} can be linked together by using a {@link RQLQueryCellRef}
|
|
20
|
+
* of one {@link RQLQuery} in the parameters of a subsequent {@link RQLQuery}.
|
|
21
|
+
*
|
|
22
|
+
* @param queries New queries to add to the transaction
|
|
23
|
+
*/
|
|
24
|
+
push(...queries: RQLQuery[]): void {
|
|
25
|
+
this._queries.push(...queries);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
get queries(): ReadonlyArray<RQLQuery> {
|
|
29
|
+
return this._queries;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { getQueryIndexInTransaction } from "../../utils.js";
|
|
2
|
+
import { ResultSet, ResultSetCell, ResultSetRow } from "../ResultSet.js";
|
|
3
|
+
import { RQLQuery } from "../query/RQLQuery.js";
|
|
4
|
+
import { RQLQueryCellRef, RQLQueryRowRef } from "../query/references.js";
|
|
5
|
+
import { Transaction } from "./Transaction.js";
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Class representing the result of a transaction.
|
|
9
|
+
*
|
|
10
|
+
* Retrieving the values is done using query references.
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
*
|
|
14
|
+
* Retrieving the first value in a result set
|
|
15
|
+
*
|
|
16
|
+
* ```tsx
|
|
17
|
+
* const transaction = new Transaction()
|
|
18
|
+
* const query = new RQLQuery("Any X WHERE X name 'test'", {})
|
|
19
|
+
* transaction.push(query)
|
|
20
|
+
* const result = client.executeTransaction(transaction)
|
|
21
|
+
* const eid = result.resolve(query.cellRef(0, 0))
|
|
22
|
+
* ```
|
|
23
|
+
*
|
|
24
|
+
* @category Api
|
|
25
|
+
*/
|
|
26
|
+
export class TransactionResult {
|
|
27
|
+
constructor(
|
|
28
|
+
private transaction: Transaction,
|
|
29
|
+
private resultSets: ReadonlyArray<ResultSet>
|
|
30
|
+
) {}
|
|
31
|
+
|
|
32
|
+
getResultSet(query: RQLQuery): ResultSet {
|
|
33
|
+
const index = getQueryIndexInTransaction(this.transaction, query.queryUUID);
|
|
34
|
+
return this.resultSets[index];
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
getRow(ref: RQLQueryRowRef): ResultSetRow {
|
|
38
|
+
const index = getQueryIndexInTransaction(this.transaction, ref.queryUUID);
|
|
39
|
+
return this.resultSets[index].rows[ref.row];
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
getCell(ref: RQLQueryCellRef): ResultSetCell {
|
|
43
|
+
const index = getQueryIndexInTransaction(this.transaction, ref.queryUUID);
|
|
44
|
+
return this.resultSets[index].rows[ref.row][ref.column];
|
|
45
|
+
}
|
|
46
|
+
}
|
|
@@ -1,3 +1,6 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* There was an error processing the CubicWeb schema.
|
|
3
|
+
*/
|
|
1
4
|
export class InconsistentSchemaError extends Error {
|
|
2
5
|
constructor(message: string) {
|
|
3
6
|
super(`Inconsistent schema: ${message}`);
|
|
@@ -5,16 +8,12 @@ export class InconsistentSchemaError extends Error {
|
|
|
5
8
|
}
|
|
6
9
|
}
|
|
7
10
|
|
|
11
|
+
/**
|
|
12
|
+
* There was an error while validating the data sent to the server.
|
|
13
|
+
*/
|
|
8
14
|
export class ValidationError extends Error {
|
|
9
15
|
constructor(message: string) {
|
|
10
16
|
super(message);
|
|
11
17
|
Object.setPrototypeOf(this, new.target.prototype);
|
|
12
18
|
}
|
|
13
19
|
}
|
|
14
|
-
|
|
15
|
-
export class TransactionClosedError extends Error {
|
|
16
|
-
constructor() {
|
|
17
|
-
super("Trying to use a closed transaction");
|
|
18
|
-
Object.setPrototypeOf(this, new.target.prototype);
|
|
19
|
-
}
|
|
20
|
-
}
|
package/src/index.ts
CHANGED
|
@@ -11,15 +11,19 @@ export * from "./schema/classes/RelationDefinitionSchema.js";
|
|
|
11
11
|
export * from "./schema/classes/Schema.js";
|
|
12
12
|
export * from "./schema/classes/Utils.js";
|
|
13
13
|
|
|
14
|
-
export * from "./
|
|
14
|
+
export * from "./errors.js";
|
|
15
15
|
|
|
16
16
|
// API
|
|
17
|
-
export { default as Client } from "./api/Client.js";
|
|
18
|
-
export * from "./api/Transaction.js";
|
|
19
17
|
export {
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
RqlRow,
|
|
23
|
-
ResultSet,
|
|
18
|
+
RQLQueryParamValue,
|
|
19
|
+
RQLQueryParams,
|
|
24
20
|
ApiErrorResponse,
|
|
21
|
+
CurrentUser,
|
|
25
22
|
} from "./api/Api.js";
|
|
23
|
+
export { ResultSet, ResultSetRow, ResultSetCell } from "./api/ResultSet.js";
|
|
24
|
+
export { default as Client } from "./api/Client.js";
|
|
25
|
+
export { Transaction } from "./api/transaction/Transaction.js";
|
|
26
|
+
export { TransactionResult } from "./api/transaction/TransactionResult.js";
|
|
27
|
+
export { RQLQuery } from "./api/query/RQLQuery.js";
|
|
28
|
+
export { RQLBinary } from "./api/binary/RQLBinary.js";
|
|
29
|
+
export { RQLQueryRowRef, RQLQueryCellRef } from "./api/query/references.js";
|
|
@@ -14,16 +14,25 @@ type ConstraintBase<
|
|
|
14
14
|
*/
|
|
15
15
|
type ConstraintMessage = string | null;
|
|
16
16
|
|
|
17
|
+
/**
|
|
18
|
+
* A Unique constraint on an attribute.
|
|
19
|
+
*/
|
|
17
20
|
export type UniqueConstraint = ConstraintBase<
|
|
18
21
|
"UniqueConstraint",
|
|
19
22
|
{ msg: ConstraintMessage }
|
|
20
23
|
>;
|
|
21
24
|
|
|
25
|
+
/**
|
|
26
|
+
* A constraint on the size of a string
|
|
27
|
+
*/
|
|
22
28
|
export type SizeConstraint = ConstraintBase<
|
|
23
29
|
"SizeConstraint",
|
|
24
30
|
{ min: number | null; max: number | null; msg: ConstraintMessage }
|
|
25
31
|
>;
|
|
26
32
|
|
|
33
|
+
/**
|
|
34
|
+
* A Regexp constraint on a string
|
|
35
|
+
*/
|
|
27
36
|
export type RegexpConstraint = ConstraintBase<
|
|
28
37
|
"RegexpConstraint",
|
|
29
38
|
{ regexp: string; flags: number; msg: ConstraintMessage }
|
|
@@ -81,6 +90,9 @@ export interface Now {
|
|
|
81
90
|
*/
|
|
82
91
|
export type ConstraintOperator = ">" | ">=" | "<" | "<=";
|
|
83
92
|
|
|
93
|
+
/**
|
|
94
|
+
* A boundary constraint on a number attribute.
|
|
95
|
+
*/
|
|
84
96
|
export type BoundaryConstraint = ConstraintBase<
|
|
85
97
|
"BoundaryConstraint",
|
|
86
98
|
{
|
|
@@ -89,6 +101,10 @@ export type BoundaryConstraint = ConstraintBase<
|
|
|
89
101
|
msg: ConstraintMessage;
|
|
90
102
|
}
|
|
91
103
|
>;
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* An interval constraint on a number attribute.
|
|
107
|
+
*/
|
|
92
108
|
export type IntervalBoundConstraint = ConstraintBase<
|
|
93
109
|
"IntervalBoundConstraint",
|
|
94
110
|
{
|
|
@@ -98,11 +114,17 @@ export type IntervalBoundConstraint = ConstraintBase<
|
|
|
98
114
|
}
|
|
99
115
|
>;
|
|
100
116
|
|
|
117
|
+
/**
|
|
118
|
+
* A static vocabulary constraint on a string attribute.
|
|
119
|
+
*/
|
|
101
120
|
export type StaticVocabularyConstraint = ConstraintBase<
|
|
102
121
|
"StaticVocabularyConstraint",
|
|
103
122
|
{ values: string[]; msg: ConstraintMessage }
|
|
104
123
|
>;
|
|
105
124
|
|
|
125
|
+
/**
|
|
126
|
+
* A format constraint on a string attribute.
|
|
127
|
+
*/
|
|
106
128
|
export type FormatConstraint = ConstraintBase<
|
|
107
129
|
"FormatConstraint",
|
|
108
130
|
{
|
|
@@ -111,11 +133,17 @@ export type FormatConstraint = ConstraintBase<
|
|
|
111
133
|
}
|
|
112
134
|
>;
|
|
113
135
|
|
|
136
|
+
/**
|
|
137
|
+
* A vocabulary constraint on a string attribute described by a RQL.
|
|
138
|
+
*/
|
|
114
139
|
export type RQLVocabularyConstraint = ConstraintBase<
|
|
115
140
|
"RQLVocabularyConstraint",
|
|
116
141
|
{ expression: string; mainvars: string[] }
|
|
117
142
|
>;
|
|
118
143
|
|
|
144
|
+
/**
|
|
145
|
+
* A constraint on an attribute described by a RQL.
|
|
146
|
+
*/
|
|
119
147
|
export type RQLConstraint = ConstraintBase<
|
|
120
148
|
"RQLConstraint",
|
|
121
149
|
{ expression: string; mainvars: string[]; msg: ConstraintMessage }
|