@cubicweb/client 1.9.0 → 2.0.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.
@@ -1,11 +1,12 @@
1
1
  import { Schema } from "../schema/classes/Schema";
2
2
  import { RQLParams, ResultSet, CurrentUser } from "./Api";
3
- import Transaction from "./Transaction";
3
+ import { Transaction, TransactionBackend, TransactionResult } from "./Transaction";
4
4
  /**
5
5
  * Class used to communicate with a CubicWeb instance's API.
6
6
  */
7
7
  export default class Client {
8
8
  private api;
9
+ private transactionBackend;
9
10
  /**
10
11
  * Class used to communicate with a CubicWeb instance's API.
11
12
  *
@@ -13,7 +14,9 @@ export default class Client {
13
14
  * Example: if you can retrieve the schema with https://example.com/api/v1/schema,
14
15
  * use https://example.com/api as `apiUrl`.
15
16
  */
16
- constructor(apiUrl: string);
17
+ constructor(apiUrl: string, { transactionBackend: transactionBackend, }: {
18
+ transactionBackend?: TransactionBackend;
19
+ });
17
20
  get apiUrl(): string;
18
21
  /**
19
22
  * Executes the given query and waits for the response.
@@ -44,15 +47,9 @@ export default class Client {
44
47
  */
45
48
  currentUser(): Promise<CurrentUser>;
46
49
  /**
47
- * Creates a new transaction and exposes the Transaction object in the given callback.
48
- * Use this callback to execute RQL requests as part of the transaction.
49
- * Once the callback's end is reached, the transaction is automatically committed.
50
- * Note that any exception thrown by the callback will lead to the
51
- * transaction being rolled back.
52
- *
53
- * @param callback Function to execute transaction requests in.
54
- * @returns The promise returned by the callback
50
+ * @param transaction Transaction to execute
51
+ * @returns A promise fulfilled when the transaction is completed
55
52
  */
56
- transaction<T>(callback: (tr: Transaction) => Promise<T>): Promise<T>;
53
+ executeTransaction(transaction: Transaction): Promise<TransactionResult>;
57
54
  }
58
55
  //# sourceMappingURL=Client.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"Client.d.ts","sourceRoot":"","sources":["../../src/api/Client.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM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MAAM,WAET;IAED;;;;;;OAMG;IACG,OAAO,CAAC,KAAK,EAAE,MAAM,EAAE,MAAM,GAAE,SAAc,GAAG,OAAO,CAAC,SAAS,CAAC;IAIxE;;;;OAIG;IACG,MAAM;IAKZ;;;;;;OAMG;IACG,KAAK,CAAC,KAAK,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM;IAI3C;;;;OAIG;IACG,WAAW,IAAI,OAAO,CAAC,WAAW,CAAC;IAIzC;;;OAGG;IACG,kBAAkB,CACtB,WAAW,EAAE,WAAW,GACvB,OAAO,CAAC,iBAAiB,CAAC;CAG9B"}
package/lib/api/Client.js CHANGED
@@ -14,7 +14,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
14
14
  Object.defineProperty(exports, "__esModule", { value: true });
15
15
  const Schema_1 = require("../schema/classes/Schema");
16
16
  const Api_1 = __importDefault(require("./Api"));
17
- const Transaction_1 = __importDefault(require("./Transaction"));
17
+ const Transaction_1 = require("./Transaction");
18
18
  /**
19
19
  * Class used to communicate with a CubicWeb instance's API.
20
20
  */
@@ -26,8 +26,14 @@ class Client {
26
26
  * Example: if you can retrieve the schema with https://example.com/api/v1/schema,
27
27
  * use https://example.com/api as `apiUrl`.
28
28
  */
29
- constructor(apiUrl) {
29
+ constructor(apiUrl, { transactionBackend: transactionBackend, }) {
30
30
  this.api = new Api_1.default(apiUrl);
31
+ if (transactionBackend === undefined) {
32
+ this.transactionBackend = new Transaction_1.NativeTransactionBackend();
33
+ }
34
+ else {
35
+ this.transactionBackend = transactionBackend;
36
+ }
31
37
  }
32
38
  get apiUrl() {
33
39
  return this.api.apiUrl;
@@ -78,33 +84,12 @@ class Client {
78
84
  });
79
85
  }
80
86
  /**
81
- * Creates a new transaction and exposes the Transaction object in the given callback.
82
- * Use this callback to execute RQL requests as part of the transaction.
83
- * Once the callback's end is reached, the transaction is automatically committed.
84
- * Note that any exception thrown by the callback will lead to the
85
- * transaction being rolled back.
86
- *
87
- * @param callback Function to execute transaction requests in.
88
- * @returns The promise returned by the callback
87
+ * @param transaction Transaction to execute
88
+ * @returns A promise fulfilled when the transaction is completed
89
89
  */
90
- transaction(callback) {
90
+ executeTransaction(transaction) {
91
91
  return __awaiter(this, void 0, void 0, function* () {
92
- const transaction = new Transaction_1.default(this.api);
93
- yield transaction.begin();
94
- let result;
95
- try {
96
- result = yield callback(transaction);
97
- }
98
- catch (error) {
99
- if (!transaction.isClosed()) {
100
- yield transaction.rollback();
101
- }
102
- throw error;
103
- }
104
- if (!transaction.isClosed()) {
105
- yield transaction.commit();
106
- }
107
- return result;
92
+ return yield this.transactionBackend.execute(transaction);
108
93
  });
109
94
  }
110
95
  }
@@ -1 +1 @@
1
- {"version":3,"file":"Client.js","sourceRoot":"","sources":["../../src/api/Client.ts"],"names":[],"mappings":";;;;;;;;;;;;;;AAAA,qDAAkD;AAClD,gDAA+D;AAC/D,gEAAwC;AAExC;;GAEG;AACH,MAAqB,MAAM;IAGzB;;;;;;OAMG;IACH,YAAY,MAAc;QACxB,IAAI,CAAC,GAAG,GAAG,IAAI,aAAG,CAAC,MAAM,CAAC,CAAC;IAC7B,CAAC;IAED,IAAI,MAAM;QACR,OAAO,IAAI,CAAC,GAAG,CAAC,MAAM,CAAC;IACzB,CAAC;IAED;;;;;;OAMG;IACG,OAAO,CAAC,KAAa,EAAE,SAAoB,EAAE;;YACjD,OAAO,MAAM,IAAI,CAAC,GAAG,CAAC,OAAO,CAAC,KAAK,EAAE,MAAM,CAAC,CAAC;QAC/C,CAAC;KAAA;IAED;;;;OAIG;IACG,MAAM;;YACV,MAAM,MAAM,GAAG,MAAM,IAAI,CAAC,GAAG,CAAC,MAAM,EAAE,CAAC;YACvC,OAAO,IAAI,eAAM,CAAC,MAAM,CAAC,CAAC;QAC5B,CAAC;KAAA;IAED;;;;;;OAMG;IACG,KAAK,CAAC,KAAa,EAAE,QAAgB;;YACzC,OAAO,MAAM,IAAI,CAAC,GAAG,CAAC,KAAK,CAAC,KAAK,EAAE,QAAQ,CAAC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AAC,MAAM,CAAC;IACzB,CAAC;IAED;;;;;;OAMG;IACG,OAAO,CAAC,KAAa,EAAE,SAAoB,EAAE;;YACjD,OAAO,MAAM,IAAI,CAAC,GAAG,CAAC,OAAO,CAAC,KAAK,EAAE,MAAM,CAAC,CAAC;QAC/C,CAAC;KAAA;IAED;;;;OAIG;IACG,MAAM;;YACV,MAAM,MAAM,GAAG,MAAM,IAAI,CAAC,GAAG,CAAC,MAAM,EAAE,CAAC;YACvC,OAAO,IAAI,eAAM,CAAC,MAAM,CAAC,CAAC;QAC5B,CAAC;KAAA;IAED;;;;;;OAMG;IACG,KAAK,CAAC,KAAa,EAAE,QAAgB;;YACzC,OAAO,MAAM,IAAI,CAAC,GAAG,CAAC,KAAK,CAAC,KAAK,EAAE,QAAQ,CAAC,CAAC;QAC/C,CAAC;KAAA;IAED;;;;OAIG;IACG,WAAW;;YACf,OAAO,MAAM,IAAI,CAAC,GAAG,CAAC,WAAW,EAAE,CAAC;QACtC,CAAC;KAAA;IAED;;;OAGG;IACG,kBAAkB,CACtB,WAAwB;;YAExB,OAAO,MAAM,IAAI,CAAC,kBAAkB,CAAC,OAAO,CAAC,WAAW,CAAC,CAAC;QAC5D,CAAC;KAAA;CACF;AA/ED,yBA+EC"}
@@ -1,57 +1,31 @@
1
- import Api, { RQLParams } from "./Api";
2
- /**
3
- * Class used to handle CubicWeb's transactions.
4
- * A transaction allows to execute several RQL requests without altering the database until all
5
- * changes are committed.
6
- *
7
- * You should never have to create this object yourself.
8
- * Instead, retrieve this object from the methods in the `Client` class.
9
- */
10
- export default class Transaction {
11
- private api;
12
- private _uuid;
13
- private _closed;
14
- /**
15
- * Class used to handle CubicWeb's transactions.
16
- * A transaction allows to execute several RQL requests without altering the database until all
17
- * changes are committed.
18
- *
19
- * You should never have to create this object yourself.
20
- * Instead, retrieve this object from the methods in the `Client` class.
21
- *
22
- * @param api The API object used to communicate with the CubicWeb backend.
23
- */
24
- constructor(api: Api);
25
- /**
26
- * @returns True if the connection is closed.
27
- */
28
- isClosed(): boolean;
29
- /**
30
- * This transaction unique identifier.
31
- */
32
- get uuid(): string | null;
33
- /**
34
- * Begins a new transaction an retrieves the associated uuid.
35
- */
36
- begin(): Promise<void>;
37
- /**
38
- * Executes the given query and waits for the response.
39
- * Make sure you called `begin` before using this method.
40
- * As this is a transaction changes will only be applied when you call the `commit` method.
41
- *
42
- * @param query The RQL query to send
43
- * @param params The additional parameters for the request
44
- * @returns A promise with the request's result set
45
- */
46
- execute(query: string, params?: RQLParams): Promise<import("./Api").ResultSet | null>;
47
- /**
48
- * Commits the transaction to apply all the RQL queries sent with `execute`.
49
- */
50
- commit(): Promise<void>;
51
- /**
52
- * Rollbacks all the queries sent with `execute` and closes the transaction.
53
- */
54
- rollback(): Promise<void>;
55
- private preventUsageIfClosed;
1
+ import { ResultSet } from "./Api";
2
+ export declare class Transaction {
3
+ private _queries;
4
+ push(query: string, params: Record<string, string | number | TransactionQueryRef>): TransactionQuery;
5
+ get queries(): ReadonlyArray<TransactionQuery>;
6
+ }
7
+ export declare class TransactionResult {
8
+ private resultSets;
9
+ constructor(resultSets: ReadonlyArray<ResultSet>);
10
+ resolve(ref: TransactionQueryRef): string | number | boolean | null;
11
+ }
12
+ export interface TransactionQueryRef {
13
+ queryIndex: number;
14
+ row: number;
15
+ column: number;
16
+ }
17
+ export declare class TransactionQuery {
18
+ readonly query: string;
19
+ readonly params: Record<string, string | number | TransactionQueryRef>;
20
+ readonly index: number;
21
+ constructor(query: string, params: Record<string, string | number | TransactionQueryRef>, index: number);
22
+ ref(row?: number, column?: number): TransactionQueryRef;
23
+ }
24
+ export declare function isTransactionQueryRef(value: string | number | TransactionQueryRef): value is TransactionQueryRef;
25
+ export interface TransactionBackend {
26
+ execute(transaction: Transaction): Promise<TransactionResult>;
27
+ }
28
+ export declare class NativeTransactionBackend {
29
+ execute(): Promise<TransactionResult>;
56
30
  }
57
31
  //# sourceMappingURL=Transaction.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"Transaction.d.ts","sourceRoot":"","sources":["../../src/api/Transaction.ts"],"names":[],"mappings":"AACA,OAAO,GAAG,EAAE,EAAE,SAAS,EAAE,MAAM,OAAO,CAAC;AAEvC;;;;;;;GAOG;AACH,MAAM,CAAC,OAAO,OAAO,WAAW;IAclB,OAAO,CAAC,GAAG;IAbvB,OAAO,CAAC,KAAK,CAAuB;IACpC,OAAO,CAAC,OAAO,CAAS;IAExB;;;;;;;;;OASG;gBACiB,GAAG,EAAE,GAAG;IAE5B;;OAEG;IACH,QAAQ;IAIR;;OAEG;IACH,IAAI,IAAI,kBAEP;IAED;;OAEG;IACG,KAAK;IAKX;;;;;;;;OAQG;IACG,OAAO,CAAC,KAAK,EAAE,MAAM,EAAE,MAAM,GAAE,SAAc;IAKnD;;OAEG;IACG,MAAM;IAMZ;;OAEG;IACG,QAAQ;IAMd,OAAO,CAAC,oBAAoB;CAK7B"}
1
+ {"version":3,"file":"Transaction.d.ts","sourceRoot":"","sources":["../../src/api/Transaction.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,OAAO,CAAC;AAElC,qBAAa,WAAW;IACtB,OAAO,CAAC,QAAQ,CAA+B;IAE/C,IAAI,CACF,KAAK,EAAE,MAAM,EACb,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,GAAG,mBAAmB,CAAC,GAC5D,gBAAgB;IAUnB,IAAI,OAAO,IAAI,aAAa,CAAC,gBAAgB,CAAC,CAE7C;CACF;AAED,qBAAa,iBAAiB;IAChB,OAAO,CAAC,UAAU;gBAAV,UAAU,EAAE,aAAa,CAAC,SAAS,CAAC;IAExD,OAAO,CAAC,GAAG,EAAE,mBAAmB;CAGjC;AAED,MAAM,WAAW,mBAAmB;IAClC,UAAU,EAAE,MAAM,CAAC;IACnB,GAAG,EAAE,MAAM,CAAC;IACZ,MAAM,EAAE,MAAM,CAAC;CAChB;AAED,qBAAa,gBAAgB;aAET,KAAK,EAAE,MAAM;aACb,MAAM,EAAE,MAAM,CAC5B,MAAM,EACN,MAAM,GAAG,MAAM,GAAG,mBAAmB,CACtC;aACe,KAAK,EAAE,MAAM;gBALb,KAAK,EAAE,MAAM,EACb,MAAM,EAAE,MAAM,CAC5B,MAAM,EACN,MAAM,GAAG,MAAM,GAAG,mBAAmB,CACtC,EACe,KAAK,EAAE,MAAM;IAG/B,GAAG,CAAC,GAAG,SAAI,EAAE,MAAM,SAAI,GAAG,mBAAmB;CAO9C;AAED,wBAAgB,qBAAqB,CACnC,KAAK,EAAE,MAAM,GAAG,MAAM,GAAG,mBAAmB,GAC3C,KAAK,IAAI,mBAAmB,CAO9B;AAED,MAAM,WAAW,kBAAkB;IACjC,OAAO,CAAC,WAAW,EAAE,WAAW,GAAG,OAAO,CAAC,iBAAiB,CAAC,CAAC;CAC/D;AAED,qBAAa,wBAAwB;IAC7B,OAAO,IAAI,OAAO,CAAC,iBAAiB,CAAC;CAG5C"}
@@ -9,92 +9,56 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
9
9
  });
10
10
  };
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
- const Errors_1 = require("../Errors");
13
- /**
14
- * Class used to handle CubicWeb's transactions.
15
- * A transaction allows to execute several RQL requests without altering the database until all
16
- * changes are committed.
17
- *
18
- * You should never have to create this object yourself.
19
- * Instead, retrieve this object from the methods in the `Client` class.
20
- */
12
+ exports.NativeTransactionBackend = exports.isTransactionQueryRef = exports.TransactionQuery = exports.TransactionResult = exports.Transaction = void 0;
21
13
  class Transaction {
22
- /**
23
- * Class used to handle CubicWeb's transactions.
24
- * A transaction allows to execute several RQL requests without altering the database until all
25
- * changes are committed.
26
- *
27
- * You should never have to create this object yourself.
28
- * Instead, retrieve this object from the methods in the `Client` class.
29
- *
30
- * @param api The API object used to communicate with the CubicWeb backend.
31
- */
32
- constructor(api) {
33
- this.api = api;
34
- this._uuid = null;
35
- this._closed = false;
14
+ constructor() {
15
+ this._queries = [];
36
16
  }
37
- /**
38
- * @returns True if the connection is closed.
39
- */
40
- isClosed() {
41
- return this._closed;
17
+ push(query, params) {
18
+ const stackedQuery = new TransactionQuery(query, params, this._queries.length);
19
+ this._queries.push(stackedQuery);
20
+ return stackedQuery;
42
21
  }
43
- /**
44
- * This transaction unique identifier.
45
- */
46
- get uuid() {
47
- return this._uuid;
22
+ get queries() {
23
+ return this._queries;
48
24
  }
49
- /**
50
- * Begins a new transaction an retrieves the associated uuid.
51
- */
52
- begin() {
53
- return __awaiter(this, void 0, void 0, function* () {
54
- this._closed = false;
55
- this._uuid = yield this.api.transactionBegin();
56
- });
25
+ }
26
+ exports.Transaction = Transaction;
27
+ class TransactionResult {
28
+ constructor(resultSets) {
29
+ this.resultSets = resultSets;
57
30
  }
58
- /**
59
- * Executes the given query and waits for the response.
60
- * Make sure you called `begin` before using this method.
61
- * As this is a transaction changes will only be applied when you call the `commit` method.
62
- *
63
- * @param query The RQL query to send
64
- * @param params The additional parameters for the request
65
- * @returns A promise with the request's result set
66
- */
67
- execute(query, params = {}) {
68
- return __awaiter(this, void 0, void 0, function* () {
69
- this.preventUsageIfClosed();
70
- return yield this.api.transactionExecute(this.uuid, query, params);
71
- });
31
+ resolve(ref) {
32
+ return this.resultSets[ref.queryIndex][ref.row][ref.column];
72
33
  }
73
- /**
74
- * Commits the transaction to apply all the RQL queries sent with `execute`.
75
- */
76
- commit() {
77
- return __awaiter(this, void 0, void 0, function* () {
78
- this.preventUsageIfClosed();
79
- yield this.api.transactionCommit(this.uuid);
80
- this._closed = true;
81
- });
34
+ }
35
+ exports.TransactionResult = TransactionResult;
36
+ class TransactionQuery {
37
+ constructor(query, params, index) {
38
+ this.query = query;
39
+ this.params = params;
40
+ this.index = index;
41
+ }
42
+ ref(row = 0, column = 0) {
43
+ return {
44
+ queryIndex: this.index,
45
+ row,
46
+ column,
47
+ };
82
48
  }
83
- /**
84
- * Rollbacks all the queries sent with `execute` and closes the transaction.
85
- */
86
- rollback() {
49
+ }
50
+ exports.TransactionQuery = TransactionQuery;
51
+ function isTransactionQueryRef(value) {
52
+ return (typeof value === "object" &&
53
+ ["queryIndex", "row", "column"].every((key) => Object.keys(value).includes(key)));
54
+ }
55
+ exports.isTransactionQueryRef = isTransactionQueryRef;
56
+ class NativeTransactionBackend {
57
+ execute() {
87
58
  return __awaiter(this, void 0, void 0, function* () {
88
- this.preventUsageIfClosed();
89
- yield this.api.transactionRollback(this.uuid);
90
- this._closed = true;
59
+ throw new Error("NativeTransactionBackend.execute not yet implemented.");
91
60
  });
92
61
  }
93
- preventUsageIfClosed() {
94
- if (this.isClosed()) {
95
- throw new Errors_1.TransactionClosedError();
96
- }
97
- }
98
62
  }
99
- exports.default = Transaction;
63
+ exports.NativeTransactionBackend = NativeTransactionBackend;
100
64
  //# sourceMappingURL=Transaction.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"Transaction.js","sourceRoot":"","sources":["../../src/api/Transaction.ts"],"names":[],"mappings":";;;;;;;;;;;AAAA,sCAAmD;AAGnD;;;;;;;GAOG;AACH,MAAqB,WAAW;IAI9B;;;;;;;;;OASG;IACH,YAAoB,GAAQ;QAAR,QAAG,GAAH,GAAG,CAAK;QAbpB,UAAK,GAAkB,IAAI,CAAC;QAC5B,YAAO,GAAG,KAAK,CAAC;IAYO,CAAC;IAEhC;;OAEG;IACH,QAAQ;QACN,OAAO,IAAI,CAAC,OAAO,CAAC;IACtB,CAAC;IAED;;OAEG;IACH,IAAI,IAAI;QACN,OAAO,IAAI,CAAC,KAAK,CAAC;IACpB,CAAC;IAED;;OAEG;IACG,KAAK;;YACT,IAAI,CAAC,OAAO,GAAG,KAAK,CAAC;YACrB,IAAI,CAAC,KAAK,GAAG,MAAM,IAAI,CAAC,GAAG,CAAC,gBAAgB,EAAE,CAAC;QACjD,CAAC;KAAA;IAED;;;;;;;;OAQG;IACG,OAAO,CAAC,KAAa,EAAE,SAAoB,EAAE;;YACjD,IAAI,CAAC,oBAAoB,EAAE,CAAC;YAC5B,OAAO,MA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IAE5D,OAAO,CAAC,GAAwB;QAC9B,OAAO,IAAI,CAAC,UAAU,CAAC,GAAG,CAAC,UAAU,CAAC,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC;IAC9D,CAAC;CACF;AAND,8CAMC;AAQD,MAAa,gBAAgB;IAC3B,YACkB,KAAa,EACb,MAGf,EACe,KAAa;QALb,UAAK,GAAL,KAAK,CAAQ;QACb,WAAM,GAAN,MAAM,CAGrB;QACe,UAAK,GAAL,KAAK,CAAQ;IAC5B,CAAC;IAEJ,GAAG,CAAC,GAAG,GAAG,CAAC,EAAE,MAAM,GAAG,CAAC;QACrB,OAAO;YACL,UAAU,EAAE,IAAI,CAAC,KAAK;YACtB,GAAG;YACH,MAAM;SACP,CAAC;IACJ,CAAC;CACF;AAjBD,4CAiBC;AAED,SAAgB,qBAAqB,CACnC,KAA4C;IAE5C,OAAO,CACL,OAAO,KAAK,KAAK,QAAQ;QACzB,CAAC,YAAY,EAAE,KAAK,EAAE,QAAQ,CAAC,CAAC,KAAK,CAAC,CAAC,GAAG,EAAE,EAAE,CAC5C,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,QAAQ,CAAC,GAAG,CAAC,CACjC,CACF,CAAC;AACJ,CAAC;AATD,sDASC;AAMD,MAAa,wBAAwB;IAC7B,OAAO;;YACX,MAAM,IAAI,KAAK,CAAC,uDAAuD,CAAC,CAAC;QAC3E,CAAC;KAAA;CACF;AAJD,4DAIC"}
package/lib/index.d.ts CHANGED
@@ -9,6 +9,6 @@ export * from "./schema/classes/Schema";
9
9
  export * from "./schema/classes/Utils";
10
10
  export * from "./Errors";
11
11
  export { default as Client } from "./api/Client";
12
- export { default as Transaction } from "./api/Transaction";
12
+ export * from "./api/Transaction";
13
13
  export { RQLQuery, RQLParams, RqlRow, ResultSet, ApiErrorResponse, } from "./api/Api";
14
14
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,cAAc,0BAA0B,CAAC;AACzC,cAAc,uBAAuB,CAAC;AACtC,cAAc,kCAAkC,CAAC;AACjD,cAAc,qBAAqB,CAAC;AACpC,cAAc,oBAAoB,CAAC;AAGnC,cAAc,+BAA+B,CAAC;AAC9C,cAAc,2CAA2C,CAAC;AAC1D,cAAc,yBAAyB,CAAC;AACxC,cAAc,wBAAwB,CAAC;AAEvC,cAAc,UAAU,CAAC;AAGzB,OAAO,EAAE,OAAO,IAAI,MAAM,EAAE,MAAM,cAAc,CAAC;AACjD,OAAO,EAAE,OAAO,IAAI,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAC3D,OAAO,EACL,QAAQ,EACR,SAAS,EACT,MAAM,EACN,SAAS,EACT,gBAAgB,GACjB,MAAM,WAAW,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,cAAc,0BAA0B,CAAC;AACzC,cAAc,uBAAuB,CAAC;AACtC,cAAc,kCAAkC,CAAC;AACjD,cAAc,qBAAqB,CAAC;AACpC,cAAc,oBAAoB,CAAC;AAGnC,cAAc,+BAA+B,CAAC;AAC9C,cAAc,2CAA2C,CAAC;AAC1D,cAAc,yBAAyB,CAAC;AACxC,cAAc,wBAAwB,CAAC;AAEvC,cAAc,UAAU,CAAC;AAGzB,OAAO,EAAE,OAAO,IAAI,MAAM,EAAE,MAAM,cAAc,CAAC;AACjD,cAAc,mBAAmB,CAAC;AAClC,OAAO,EACL,QAAQ,EACR,SAAS,EACT,MAAM,EACN,SAAS,EACT,gBAAgB,GACjB,MAAM,WAAW,CAAC"}
package/lib/index.js CHANGED
@@ -17,7 +17,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
17
17
  return (mod && mod.__esModule) ? mod : { "default": mod };
18
18
  };
19
19
  Object.defineProperty(exports, "__esModule", { value: true });
20
- exports.Transaction = exports.Client = void 0;
20
+ exports.Client = void 0;
21
21
  // Raw Schema
22
22
  __exportStar(require("./schema/raw/Constraints"), exports);
23
23
  __exportStar(require("./schema/raw/Entities"), exports);
@@ -33,6 +33,5 @@ __exportStar(require("./Errors"), exports);
33
33
  // API
34
34
  var Client_1 = require("./api/Client");
35
35
  Object.defineProperty(exports, "Client", { enumerable: true, get: function () { return __importDefault(Client_1).default; } });
36
- var Transaction_1 = require("./api/Transaction");
37
- Object.defineProperty(exports, "Transaction", { enumerable: true, get: function () { return __importDefault(Transaction_1).default; } });
36
+ __exportStar(require("./api/Transaction"), exports);
38
37
  //# sourceMappingURL=index.js.map
package/lib/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA,aAAa;AACb,2DAAyC;AACzC,wDAAsC;AACtC,mEAAiD;AACjD,sDAAoC;AACpC,qDAAmC;AAEnC,2BAA2B;AAC3B,gEAA8C;AAC9C,4EAA0D;AAC1D,0DAAwC;AACxC,yDAAuC;AAEvC,2CAAyB;AAEzB,MAAM;AACN,uCAAiD;AAAxC,iHAAA,OAAO,OAAU;AAC1B,iDAA2D;AAAlD,2HAAA,OAAO,OAAe"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA,aAAa;AACb,2DAAyC;AACzC,wDAAsC;AACtC,mEAAiD;AACjD,sDAAoC;AACpC,qDAAmC;AAEnC,2BAA2B;AAC3B,gEAA8C;AAC9C,4EAA0D;AAC1D,0DAAwC;AACxC,yDAAuC;AAEvC,2CAAyB;AAEzB,MAAM;AACN,uCAAiD;AAAxC,iHAAA,OAAO,OAAU;AAC1B,oDAAkC"}
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "@cubicweb/client",
3
3
  "description": "Official JS client for CubicWeb API",
4
4
  "author": "Logilab",
5
- "version": "1.9.0",
5
+ "version": "2.0.0",
6
6
  "license": "LGPL-3.0-or-later",
7
7
  "main": "lib/index.js",
8
8
  "types": "lib/index.d.ts",
package/src/api/Client.ts CHANGED
@@ -1,12 +1,18 @@
1
1
  import { Schema } from "../schema/classes/Schema";
2
2
  import Api, { RQLParams, ResultSet, CurrentUser } from "./Api";
3
- import Transaction from "./Transaction";
3
+ import {
4
+ NativeTransactionBackend,
5
+ Transaction,
6
+ TransactionBackend,
7
+ TransactionResult,
8
+ } from "./Transaction";
4
9
 
5
10
  /**
6
11
  * Class used to communicate with a CubicWeb instance's API.
7
12
  */
8
13
  export default class Client {
9
14
  private api: Api;
15
+ private transactionBackend: TransactionBackend;
10
16
 
11
17
  /**
12
18
  * Class used to communicate with a CubicWeb instance's API.
@@ -15,8 +21,18 @@ export default class Client {
15
21
  * Example: if you can retrieve the schema with https://example.com/api/v1/schema,
16
22
  * use https://example.com/api as `apiUrl`.
17
23
  */
18
- constructor(apiUrl: string) {
24
+ constructor(
25
+ apiUrl: string,
26
+ {
27
+ transactionBackend: transactionBackend,
28
+ }: { transactionBackend?: TransactionBackend }
29
+ ) {
19
30
  this.api = new Api(apiUrl);
31
+ if (transactionBackend === undefined) {
32
+ this.transactionBackend = new NativeTransactionBackend();
33
+ } else {
34
+ this.transactionBackend = transactionBackend;
35
+ }
20
36
  }
21
37
 
22
38
  get apiUrl() {
@@ -65,31 +81,12 @@ export default class Client {
65
81
  }
66
82
 
67
83
  /**
68
- * Creates a new transaction and exposes the Transaction object in the given callback.
69
- * Use this callback to execute RQL requests as part of the transaction.
70
- * Once the callback's end is reached, the transaction is automatically committed.
71
- * Note that any exception thrown by the callback will lead to the
72
- * transaction being rolled back.
73
- *
74
- * @param callback Function to execute transaction requests in.
75
- * @returns The promise returned by the callback
84
+ * @param transaction Transaction to execute
85
+ * @returns A promise fulfilled when the transaction is completed
76
86
  */
77
- async transaction<T>(callback: (tr: Transaction) => Promise<T>): Promise<T> {
78
- const transaction = new Transaction(this.api);
79
- await transaction.begin();
80
-
81
- let result: T;
82
- try {
83
- result = await callback(transaction);
84
- } catch (error) {
85
- if (!transaction.isClosed()) {
86
- await transaction.rollback();
87
- }
88
- throw error;
89
- }
90
- if (!transaction.isClosed()) {
91
- await transaction.commit();
92
- }
93
- return result;
87
+ async executeTransaction(
88
+ transaction: Transaction
89
+ ): Promise<TransactionResult> {
90
+ return await this.transactionBackend.execute(transaction);
94
91
  }
95
92
  }
@@ -1,87 +1,76 @@
1
- import { TransactionClosedError } from "../Errors";
2
- import Api, { RQLParams } from "./Api";
1
+ import { ResultSet } from "./Api";
3
2
 
4
- /**
5
- * Class used to handle CubicWeb's transactions.
6
- * A transaction allows to execute several RQL requests without altering the database until all
7
- * changes are committed.
8
- *
9
- * You should never have to create this object yourself.
10
- * Instead, retrieve this object from the methods in the `Client` class.
11
- */
12
- export default class Transaction {
13
- private _uuid: string | null = null;
14
- private _closed = false;
3
+ export class Transaction {
4
+ private _queries: Array<TransactionQuery> = [];
15
5
 
16
- /**
17
- * Class used to handle CubicWeb's transactions.
18
- * A transaction allows to execute several RQL requests without altering the database until all
19
- * changes are committed.
20
- *
21
- * You should never have to create this object yourself.
22
- * Instead, retrieve this object from the methods in the `Client` class.
23
- *
24
- * @param api The API object used to communicate with the CubicWeb backend.
25
- */
26
- constructor(private api: Api) {}
27
-
28
- /**
29
- * @returns True if the connection is closed.
30
- */
31
- isClosed() {
32
- return this._closed;
6
+ push(
7
+ query: string,
8
+ params: Record<string, string | number | TransactionQueryRef>
9
+ ): TransactionQuery {
10
+ const stackedQuery = new TransactionQuery(
11
+ query,
12
+ params,
13
+ this._queries.length
14
+ );
15
+ this._queries.push(stackedQuery);
16
+ return stackedQuery;
33
17
  }
34
18
 
35
- /**
36
- * This transaction unique identifier.
37
- */
38
- get uuid() {
39
- return this._uuid;
19
+ get queries(): ReadonlyArray<TransactionQuery> {
20
+ return this._queries;
40
21
  }
22
+ }
41
23
 
42
- /**
43
- * Begins a new transaction an retrieves the associated uuid.
44
- */
45
- async begin() {
46
- this._closed = false;
47
- this._uuid = await this.api.transactionBegin();
48
- }
24
+ export class TransactionResult {
25
+ constructor(private resultSets: ReadonlyArray<ResultSet>) {}
49
26
 
50
- /**
51
- * Executes the given query and waits for the response.
52
- * Make sure you called `begin` before using this method.
53
- * As this is a transaction changes will only be applied when you call the `commit` method.
54
- *
55
- * @param query The RQL query to send
56
- * @param params The additional parameters for the request
57
- * @returns A promise with the request's result set
58
- */
59
- async execute(query: string, params: RQLParams = {}) {
60
- this.preventUsageIfClosed();
61
- return await this.api.transactionExecute(this.uuid, query, params);
27
+ resolve(ref: TransactionQueryRef) {
28
+ return this.resultSets[ref.queryIndex][ref.row][ref.column];
62
29
  }
30
+ }
63
31
 
64
- /**
65
- * Commits the transaction to apply all the RQL queries sent with `execute`.
66
- */
67
- async commit() {
68
- this.preventUsageIfClosed();
69
- await this.api.transactionCommit(this.uuid);
70
- this._closed = true;
71
- }
32
+ export interface TransactionQueryRef {
33
+ queryIndex: number;
34
+ row: number;
35
+ column: number;
36
+ }
72
37
 
73
- /**
74
- * Rollbacks all the queries sent with `execute` and closes the transaction.
75
- */
76
- async rollback() {
77
- this.preventUsageIfClosed();
78
- await this.api.transactionRollback(this.uuid);
79
- this._closed = true;
38
+ export class TransactionQuery {
39
+ constructor(
40
+ public readonly query: string,
41
+ public readonly params: Record<
42
+ string,
43
+ string | number | TransactionQueryRef
44
+ >,
45
+ public readonly index: number
46
+ ) {}
47
+
48
+ ref(row = 0, column = 0): TransactionQueryRef {
49
+ return {
50
+ queryIndex: this.index,
51
+ row,
52
+ column,
53
+ };
80
54
  }
55
+ }
56
+
57
+ export function isTransactionQueryRef(
58
+ value: string | number | TransactionQueryRef
59
+ ): value is TransactionQueryRef {
60
+ return (
61
+ typeof value === "object" &&
62
+ ["queryIndex", "row", "column"].every((key) =>
63
+ Object.keys(value).includes(key)
64
+ )
65
+ );
66
+ }
67
+
68
+ export interface TransactionBackend {
69
+ execute(transaction: Transaction): Promise<TransactionResult>;
70
+ }
81
71
 
82
- private preventUsageIfClosed(): asserts this is { uuid: string } {
83
- if (this.isClosed()) {
84
- throw new TransactionClosedError();
85
- }
72
+ export class NativeTransactionBackend {
73
+ async execute(): Promise<TransactionResult> {
74
+ throw new Error("NativeTransactionBackend.execute not yet implemented.");
86
75
  }
87
76
  }
package/src/index.ts CHANGED
@@ -15,7 +15,7 @@ export * from "./Errors";
15
15
 
16
16
  // API
17
17
  export { default as Client } from "./api/Client";
18
- export { default as Transaction } from "./api/Transaction";
18
+ export * from "./api/Transaction";
19
19
  export {
20
20
  RQLQuery,
21
21
  RQLParams,