@rushdb/javascript-sdk 0.1.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.
- package/CHANGELOG.md +7 -0
- package/LICENSE +21 -0
- package/README.md +119 -0
- package/cjs/api/api.js +328 -0
- package/cjs/api/constants.js +7 -0
- package/cjs/api/create-api.js +258 -0
- package/cjs/api/index.js +8 -0
- package/cjs/api/rest-api-proxy.js +22 -0
- package/cjs/api/types.js +2 -0
- package/cjs/api/utils.js +154 -0
- package/cjs/common/constants.js +10 -0
- package/cjs/common/utils.js +46 -0
- package/cjs/index.cjs.node.js +24 -0
- package/cjs/index.cjs.worker.js +24 -0
- package/cjs/network/FetchHttpClient.js +138 -0
- package/cjs/network/HttpClient.js +59 -0
- package/cjs/network/NodeHttpClient.js +103 -0
- package/cjs/network/index.js +28 -0
- package/cjs/network/types.js +2 -0
- package/cjs/package.json +4 -0
- package/cjs/sdk/constants.js +12 -0
- package/cjs/sdk/errors.js +27 -0
- package/cjs/sdk/index.js +23 -0
- package/cjs/sdk/model.js +147 -0
- package/cjs/sdk/record.js +77 -0
- package/cjs/sdk/sdk.js +46 -0
- package/cjs/sdk/transaction.js +17 -0
- package/cjs/sdk/types.js +2 -0
- package/cjs/sdk/utils.js +103 -0
- package/cjs/types/expressions.js +2 -0
- package/cjs/types/index.js +21 -0
- package/cjs/types/query.js +2 -0
- package/cjs/types/schema.js +2 -0
- package/cjs/types/utils.js +2 -0
- package/cjs/types/value.js +2 -0
- package/esm/api/api.js +324 -0
- package/esm/api/constants.js +4 -0
- package/esm/api/create-api.js +254 -0
- package/esm/api/index.js +2 -0
- package/esm/api/rest-api-proxy.js +18 -0
- package/esm/api/types.js +1 -0
- package/esm/api/utils.js +138 -0
- package/esm/common/constants.js +7 -0
- package/esm/common/utils.js +37 -0
- package/esm/index.esm.node.js +6 -0
- package/esm/index.esm.worker.js +6 -0
- package/esm/network/FetchHttpClient.js +133 -0
- package/esm/network/HttpClient.js +53 -0
- package/esm/network/NodeHttpClient.js +98 -0
- package/esm/network/index.js +24 -0
- package/esm/network/types.js +1 -0
- package/esm/package.json +4 -0
- package/esm/sdk/constants.js +9 -0
- package/esm/sdk/errors.js +21 -0
- package/esm/sdk/index.js +7 -0
- package/esm/sdk/model.js +143 -0
- package/esm/sdk/record.js +70 -0
- package/esm/sdk/sdk.js +42 -0
- package/esm/sdk/transaction.js +13 -0
- package/esm/sdk/types.js +1 -0
- package/esm/sdk/utils.js +93 -0
- package/esm/types/expressions.js +1 -0
- package/esm/types/index.js +5 -0
- package/esm/types/query.js +1 -0
- package/esm/types/schema.js +1 -0
- package/esm/types/utils.js +1 -0
- package/esm/types/value.js +1 -0
- package/package.json +59 -0
- package/types/api/api.d.ts +43 -0
- package/types/api/constants.d.ts +4 -0
- package/types/api/create-api.d.ts +67 -0
- package/types/api/index.d.ts +3 -0
- package/types/api/rest-api-proxy.d.ts +6 -0
- package/types/api/types.d.ts +65 -0
- package/types/api/utils.d.ts +43 -0
- package/types/common/constants.d.ts +7 -0
- package/types/common/utils.d.ts +7 -0
- package/types/index.d.ts +72 -0
- package/types/network/FetchHttpClient.d.ts +26 -0
- package/types/network/HttpClient.d.ts +48 -0
- package/types/network/NodeHttpClient.d.ts +16 -0
- package/types/network/index.d.ts +6 -0
- package/types/network/types.d.ts +4 -0
- package/types/sdk/constants.d.ts +1 -0
- package/types/sdk/errors.d.ts +9 -0
- package/types/sdk/index.d.ts +7 -0
- package/types/sdk/model.d.ts +72 -0
- package/types/sdk/record.d.ts +109 -0
- package/types/sdk/sdk.d.ts +136 -0
- package/types/sdk/transaction.d.ts +11 -0
- package/types/sdk/types.d.ts +19 -0
- package/types/sdk/utils.d.ts +9 -0
- package/types/types/expressions.d.ts +23 -0
- package/types/types/index.d.ts +5 -0
- package/types/types/query.d.ts +102 -0
- package/types/types/schema.d.ts +48 -0
- package/types/types/utils.d.ts +9 -0
- package/types/types/value.d.ts +35 -0
package/CHANGELOG.md
ADDED
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 RushDB
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
<div align="center">
|
|
2
|
+
|
|
3
|
+

|
|
4
|
+
|
|
5
|
+
# RushDB SDK
|
|
6
|
+
|
|
7
|
+
> The RushDB.com SDK for JavaScript and TypeScript
|
|
8
|
+
|
|
9
|
+
[](https://www.npmjs.com/package/@rushdb/javascript-sdk)
|
|
10
|
+
[](#license "Go to license section")
|
|
11
|
+
|
|
12
|
+

|
|
13
|
+

|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
[](https://nodejs.org "Go to Node.js homepage")
|
|
17
|
+
[](https://www.npmjs.com/package/typescript "Go to TypeScript on NPM")
|
|
18
|
+
|
|
19
|
+
[Homepage](https://rushdb.com) — [Blog](https://rushdb.com/blog) — [Platform](https://app.rushdb.com) — [Docs](https://docs.rushdb.com) — [Examples](https://github.com/rush-db/rushdb/examples)
|
|
20
|
+
</div>
|
|
21
|
+
|
|
22
|
+
## Features
|
|
23
|
+
|
|
24
|
+
---
|
|
25
|
+
- **Automatic Type Inference**: Enjoy seamless type safety with automatic TypeScript inference.
|
|
26
|
+
- **Isomorphic Architecture**: Fully compatible with both server and browser environments.
|
|
27
|
+
- **Zero Dependencies**: Lightweight and efficient with no external dependencies.
|
|
28
|
+
- **No Configuration Needed**: Plug-and-play design requires no setup or configuration.
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
## Installation
|
|
32
|
+
|
|
33
|
+
---
|
|
34
|
+
NPM:
|
|
35
|
+
```bash
|
|
36
|
+
npm install @rushdb/javascript-sdk
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
YARN:
|
|
40
|
+
```bash
|
|
41
|
+
yarn add @rushdb/javascript-sdk
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
PNPM:
|
|
45
|
+
```bash
|
|
46
|
+
pnmp add @rushdb/javascript-sdk
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
## Usage
|
|
51
|
+
|
|
52
|
+
---
|
|
53
|
+
|
|
54
|
+
1. **Obtain RushDB API Token**: Grab your API token from the [Dashboard](https://app.rushdb.com).
|
|
55
|
+
2. **Setup RushDB Instance**: Initialize your RushDB instance with obtained token.
|
|
56
|
+
3. **(Optional) Define Data Models**: Tailor your data models to fit your needs.
|
|
57
|
+
4. **Manage Your Data**: Push, link, fetch, and manage your data effortlessly.
|
|
58
|
+
|
|
59
|
+
### TLDR;
|
|
60
|
+
```ts
|
|
61
|
+
/* ./your-app/src/db.ts */
|
|
62
|
+
|
|
63
|
+
import RushDB, { Model } from '@rushdb/javascript-sdk'
|
|
64
|
+
|
|
65
|
+
// Setup RushDB instance
|
|
66
|
+
const db = new RushDB("API_TOKEN")
|
|
67
|
+
|
|
68
|
+
// Optionaly define Model
|
|
69
|
+
export const UserRepo = new Model(
|
|
70
|
+
'USER',
|
|
71
|
+
{
|
|
72
|
+
name: { type: 'string' },
|
|
73
|
+
email: { type: 'string', uniq: true },
|
|
74
|
+
verified: { type: 'boolean', default: false },
|
|
75
|
+
hobbies: { type: 'string', multiple: true, requiered: false },
|
|
76
|
+
rating: { type: 'number', default: 1 },
|
|
77
|
+
created: { type: 'datetime', default: () => new Date().toISOString() },
|
|
78
|
+
password: { type: 'string' }
|
|
79
|
+
},
|
|
80
|
+
db
|
|
81
|
+
)
|
|
82
|
+
|
|
83
|
+
// Create new Record
|
|
84
|
+
const newUser = await UserRepo.create({
|
|
85
|
+
name: "John Galt",
|
|
86
|
+
email: 'john.g@example.com',
|
|
87
|
+
hobbies: ['Programming', 'Hiking'],
|
|
88
|
+
password: '********'
|
|
89
|
+
})
|
|
90
|
+
|
|
91
|
+
// Find Records by specific criteria
|
|
92
|
+
const matchedUsers = await UserRepo.find({
|
|
93
|
+
where: {
|
|
94
|
+
email: { $ne: 'john.g@example.com' },
|
|
95
|
+
hobbies: { $in: ['Hiking'] },
|
|
96
|
+
rating: { $gte: 1.5 }
|
|
97
|
+
}
|
|
98
|
+
})
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
<div align="center">
|
|
102
|
+
<b>You're Awesome!</b> 🚀
|
|
103
|
+
</div>
|
|
104
|
+
|
|
105
|
+
---
|
|
106
|
+
|
|
107
|
+
<div align="center" style="margin-top: 20px">
|
|
108
|
+
|
|
109
|
+
> Check the [Docs](https://docs.rushdb.com) and [Examples Repository](https://github.com/collect-so/examples) to learn more 🤓
|
|
110
|
+
|
|
111
|
+
|
|
112
|
+
</div>
|
|
113
|
+
|
|
114
|
+
|
|
115
|
+
## Contributing
|
|
116
|
+
|
|
117
|
+
---
|
|
118
|
+
See [CONTRIBUTING.md](CONTRIBUTING.md).
|
|
119
|
+
|
package/cjs/api/api.js
ADDED
|
@@ -0,0 +1,328 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.RestAPI = void 0;
|
|
4
|
+
const utils_js_1 = require("../common/utils.js");
|
|
5
|
+
const index_js_1 = require("../network/index.js");
|
|
6
|
+
const errors_js_1 = require("../sdk/errors.js");
|
|
7
|
+
const record_js_1 = require("../sdk/record.js");
|
|
8
|
+
const transaction_js_1 = require("../sdk/transaction.js");
|
|
9
|
+
const create_api_js_1 = require("./create-api.js");
|
|
10
|
+
const utils_js_2 = require("./utils.js");
|
|
11
|
+
class RestAPI {
|
|
12
|
+
constructor(token, config) {
|
|
13
|
+
this.relations = {
|
|
14
|
+
find: async ({ pagination, search, transaction }) => {
|
|
15
|
+
const { searchParams } = (0, utils_js_2.createSearchParams)(search);
|
|
16
|
+
const tx = (0, utils_js_2.pickTransaction)(transaction);
|
|
17
|
+
return await this.api.relations.find(searchParams, pagination, tx);
|
|
18
|
+
}
|
|
19
|
+
};
|
|
20
|
+
this.properties = {
|
|
21
|
+
delete: async (id, transaction) => {
|
|
22
|
+
var _a;
|
|
23
|
+
return (_a = this.api) === null || _a === void 0 ? void 0 : _a.properties.delete(id, transaction);
|
|
24
|
+
},
|
|
25
|
+
find: async (searchParams, transaction) => {
|
|
26
|
+
var _a;
|
|
27
|
+
return (_a = this.api) === null || _a === void 0 ? void 0 : _a.properties.find(searchParams, transaction);
|
|
28
|
+
},
|
|
29
|
+
findById: async (id, transaction) => {
|
|
30
|
+
var _a;
|
|
31
|
+
return (_a = this.api) === null || _a === void 0 ? void 0 : _a.properties.findById(id, transaction);
|
|
32
|
+
},
|
|
33
|
+
values: async (id, transaction) => {
|
|
34
|
+
var _a;
|
|
35
|
+
return (_a = this.api) === null || _a === void 0 ? void 0 : _a.properties.values(id, transaction);
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
this.labels = {
|
|
39
|
+
find: async (searchParams, transaction) => {
|
|
40
|
+
return this.api.labels.find(searchParams, transaction);
|
|
41
|
+
}
|
|
42
|
+
};
|
|
43
|
+
this.tx = {
|
|
44
|
+
begin: async (config) => {
|
|
45
|
+
var _a;
|
|
46
|
+
const transaction = await ((_a = this.api) === null || _a === void 0 ? void 0 : _a.tx.begin(config));
|
|
47
|
+
const result = new transaction_js_1.Transaction(transaction.data.id);
|
|
48
|
+
result.init(this);
|
|
49
|
+
return result;
|
|
50
|
+
},
|
|
51
|
+
commit: async (id) => { var _a; return (_a = this.api) === null || _a === void 0 ? void 0 : _a.tx.commit(id); },
|
|
52
|
+
get: async (id) => {
|
|
53
|
+
var _a;
|
|
54
|
+
const transaction = await ((_a = this.api) === null || _a === void 0 ? void 0 : _a.tx.get(id));
|
|
55
|
+
const result = new transaction_js_1.Transaction(transaction.data.id);
|
|
56
|
+
result.init(this);
|
|
57
|
+
return result;
|
|
58
|
+
},
|
|
59
|
+
rollback: async (id) => { var _a; return (_a = this.api) === null || _a === void 0 ? void 0 : _a.tx.commit(id); }
|
|
60
|
+
};
|
|
61
|
+
this.fetcher = null;
|
|
62
|
+
if (config === null || config === void 0 ? void 0 : config.httpClient) {
|
|
63
|
+
const url = (0, utils_js_2.buildUrl)(config);
|
|
64
|
+
this.fetcher = (0, index_js_1.createFetcher)({
|
|
65
|
+
httpClient: config.httpClient,
|
|
66
|
+
token,
|
|
67
|
+
url
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
this.api = (0, create_api_js_1.createApi)(this.fetcher);
|
|
71
|
+
this.records = {
|
|
72
|
+
attach: async (sourceId, target, options, transaction) => {
|
|
73
|
+
var _a, _b;
|
|
74
|
+
// target is MaybeArray<DBRecordInstance>
|
|
75
|
+
if (target instanceof record_js_1.DBRecordInstance) {
|
|
76
|
+
const id = (_a = target.data) === null || _a === void 0 ? void 0 : _a.__id;
|
|
77
|
+
if (id) {
|
|
78
|
+
return await this.api.records.attach(sourceId, id, options, transaction);
|
|
79
|
+
}
|
|
80
|
+
else {
|
|
81
|
+
throw new errors_js_1.EmptyTargetError('Attach error: Target id is empty');
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
else if ((0, utils_js_1.isArray)(target) && target.every((r) => r instanceof record_js_1.DBRecordInstance)) {
|
|
85
|
+
const ids = target.map((r) => { var _a; return (_a = r.data) === null || _a === void 0 ? void 0 : _a.__id; }).filter(utils_js_1.toBoolean);
|
|
86
|
+
if (ids.length) {
|
|
87
|
+
return await this.api.records.attach(sourceId, ids, options, transaction);
|
|
88
|
+
}
|
|
89
|
+
else {
|
|
90
|
+
throw new errors_js_1.EmptyTargetError('Attach error: Target ids are empty');
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
// target is DBRecordsArrayInstance
|
|
94
|
+
else if (target instanceof record_js_1.DBRecordsArrayInstance) {
|
|
95
|
+
const ids = (_b = target.data) === null || _b === void 0 ? void 0 : _b.map((r) => r.__id).filter(Boolean);
|
|
96
|
+
if (ids === null || ids === void 0 ? void 0 : ids.length) {
|
|
97
|
+
return await this.api.records.attach(sourceId, ids, options, transaction);
|
|
98
|
+
}
|
|
99
|
+
else {
|
|
100
|
+
throw new errors_js_1.EmptyTargetError('Attach error: Target ids are empty');
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
// target is MaybeArray<DBRecord>
|
|
104
|
+
else if ((0, utils_js_1.isObject)(target) && '__id' in target) {
|
|
105
|
+
return await this.api.records.attach(sourceId, target.__id, options, transaction);
|
|
106
|
+
}
|
|
107
|
+
else if ((0, utils_js_1.isArray)(target) && target.every((r) => (0, utils_js_1.isObject)(r) && '__id' in r)) {
|
|
108
|
+
const ids = target === null || target === void 0 ? void 0 : target.map((r) => r.__id).filter(Boolean);
|
|
109
|
+
if (ids === null || ids === void 0 ? void 0 : ids.length) {
|
|
110
|
+
return await this.api.records.attach(sourceId, ids, options, transaction);
|
|
111
|
+
}
|
|
112
|
+
else {
|
|
113
|
+
throw new errors_js_1.EmptyTargetError('Attach error: Target ids are empty');
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
// target is MaybeArray<string>
|
|
117
|
+
else {
|
|
118
|
+
return await this.api.records.attach(sourceId, target, options, transaction);
|
|
119
|
+
}
|
|
120
|
+
},
|
|
121
|
+
create: async (labelOrData, maybeDataOrTransaction, transaction) => {
|
|
122
|
+
var _a, _b;
|
|
123
|
+
let response;
|
|
124
|
+
if (labelOrData instanceof record_js_1.DBRecordDraft) {
|
|
125
|
+
response = await ((_a = this.api) === null || _a === void 0 ? void 0 : _a.records.create(labelOrData, (0, utils_js_2.pickTransaction)(maybeDataOrTransaction)));
|
|
126
|
+
}
|
|
127
|
+
if (!response && (0, utils_js_1.isString)(labelOrData)) {
|
|
128
|
+
if ((0, utils_js_1.isObjectFlat)(maybeDataOrTransaction)) {
|
|
129
|
+
const normalizedRecord = (0, utils_js_2.normalizeRecord)({
|
|
130
|
+
label: labelOrData,
|
|
131
|
+
payload: maybeDataOrTransaction
|
|
132
|
+
});
|
|
133
|
+
response = await ((_b = this.api) === null || _b === void 0 ? void 0 : _b.records.create(new record_js_1.DBRecordDraft(normalizedRecord), transaction));
|
|
134
|
+
}
|
|
135
|
+
else if ((0, utils_js_1.isObject)(maybeDataOrTransaction)) {
|
|
136
|
+
throw Error('Provided data is not a flat object. Consider to use `createMany` method.');
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
if ((response === null || response === void 0 ? void 0 : response.success) && (response === null || response === void 0 ? void 0 : response.data)) {
|
|
140
|
+
const result = new record_js_1.DBRecordInstance(response.data);
|
|
141
|
+
result.init(this);
|
|
142
|
+
return result;
|
|
143
|
+
}
|
|
144
|
+
return new record_js_1.DBRecordInstance();
|
|
145
|
+
},
|
|
146
|
+
createMany: async (labelOrData, maybeDataOrTransaction, transaction) => {
|
|
147
|
+
var _a, _b;
|
|
148
|
+
let response;
|
|
149
|
+
if (labelOrData instanceof record_js_1.DBRecordsBatchDraft) {
|
|
150
|
+
response = await ((_a = this.api) === null || _a === void 0 ? void 0 : _a.records.createMany(labelOrData, (0, utils_js_2.pickTransaction)(maybeDataOrTransaction)));
|
|
151
|
+
}
|
|
152
|
+
if (!response &&
|
|
153
|
+
(0, utils_js_1.isString)(labelOrData) &&
|
|
154
|
+
((0, utils_js_1.isArray)(maybeDataOrTransaction) || (0, utils_js_1.isObject)(maybeDataOrTransaction))) {
|
|
155
|
+
const data = new record_js_1.DBRecordsBatchDraft({
|
|
156
|
+
label: labelOrData,
|
|
157
|
+
payload: maybeDataOrTransaction
|
|
158
|
+
});
|
|
159
|
+
response = await ((_b = this.api) === null || _b === void 0 ? void 0 : _b.records.createMany(data, transaction));
|
|
160
|
+
}
|
|
161
|
+
if ((response === null || response === void 0 ? void 0 : response.success) && (response === null || response === void 0 ? void 0 : response.data)) {
|
|
162
|
+
const result = new record_js_1.DBRecordsArrayInstance(response.data, response.total);
|
|
163
|
+
result.init(this);
|
|
164
|
+
return result;
|
|
165
|
+
}
|
|
166
|
+
return new record_js_1.DBRecordsArrayInstance([]);
|
|
167
|
+
},
|
|
168
|
+
delete: async (searchParams, transaction) => {
|
|
169
|
+
var _a;
|
|
170
|
+
if ((0, utils_js_1.isEmptyObject)(searchParams.where)) {
|
|
171
|
+
throw new errors_js_1.EmptyTargetError(`You must specify criteria to delete records. Empty criteria are not allowed. If this was intentional, use the Dashboard instead.`);
|
|
172
|
+
}
|
|
173
|
+
return (_a = this.api) === null || _a === void 0 ? void 0 : _a.records.delete(searchParams, transaction);
|
|
174
|
+
},
|
|
175
|
+
deleteById: async (idOrIds, transaction) => {
|
|
176
|
+
var _a;
|
|
177
|
+
return (_a = this.api) === null || _a === void 0 ? void 0 : _a.records.deleteById(idOrIds, transaction);
|
|
178
|
+
},
|
|
179
|
+
detach: async (sourceId, target, options, transaction) => {
|
|
180
|
+
var _a, _b;
|
|
181
|
+
// target is MaybeArray<DBRecordInstance>
|
|
182
|
+
if (target instanceof record_js_1.DBRecordInstance) {
|
|
183
|
+
const id = (_a = target.data) === null || _a === void 0 ? void 0 : _a.__id;
|
|
184
|
+
if (id) {
|
|
185
|
+
return await this.api.records.detach(sourceId, id, options, transaction);
|
|
186
|
+
}
|
|
187
|
+
else {
|
|
188
|
+
throw new errors_js_1.EmptyTargetError('Detach error: Target id is empty');
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
else if ((0, utils_js_1.isArray)(target) && target.every((r) => r instanceof record_js_1.DBRecordInstance)) {
|
|
192
|
+
const ids = target.map((r) => { var _a; return (_a = r.data) === null || _a === void 0 ? void 0 : _a.__id; }).filter(Boolean);
|
|
193
|
+
if (ids.length) {
|
|
194
|
+
return await this.api.records.detach(sourceId, ids, options, transaction);
|
|
195
|
+
}
|
|
196
|
+
else {
|
|
197
|
+
throw new errors_js_1.EmptyTargetError('Detach error: Target ids are empty');
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
// target is DBRecordsArrayInstance
|
|
201
|
+
else if (target instanceof record_js_1.DBRecordsArrayInstance) {
|
|
202
|
+
const ids = (_b = target.data) === null || _b === void 0 ? void 0 : _b.map((r) => r.__id).filter(Boolean);
|
|
203
|
+
if (ids === null || ids === void 0 ? void 0 : ids.length) {
|
|
204
|
+
return await this.api.records.detach(sourceId, ids, options, transaction);
|
|
205
|
+
}
|
|
206
|
+
else {
|
|
207
|
+
throw new errors_js_1.EmptyTargetError('Detach error: Target ids are empty');
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
// target is MaybeArray<DBRecord>
|
|
211
|
+
else if ((0, utils_js_1.isObject)(target) && '__id' in target) {
|
|
212
|
+
return await this.api.records.detach(sourceId, target.__id, options, transaction);
|
|
213
|
+
}
|
|
214
|
+
else if ((0, utils_js_1.isArray)(target) && target.every((r) => (0, utils_js_1.isObject)(r) && '__id' in r)) {
|
|
215
|
+
const ids = target === null || target === void 0 ? void 0 : target.map((r) => r.__id).filter(Boolean);
|
|
216
|
+
if (ids === null || ids === void 0 ? void 0 : ids.length) {
|
|
217
|
+
return await this.api.records.detach(sourceId, ids, options, transaction);
|
|
218
|
+
}
|
|
219
|
+
else {
|
|
220
|
+
throw new errors_js_1.EmptyTargetError('Detach error: Target ids are empty');
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
// target is MaybeArray<string>
|
|
224
|
+
else {
|
|
225
|
+
return await this.api.records.detach(sourceId, target, options, transaction);
|
|
226
|
+
}
|
|
227
|
+
},
|
|
228
|
+
export: async (searchParams, transaction) => {
|
|
229
|
+
var _a;
|
|
230
|
+
return (_a = this.api) === null || _a === void 0 ? void 0 : _a.records.export(searchParams, transaction);
|
|
231
|
+
},
|
|
232
|
+
find: async (labelOrSearchParams, searchParamsOrTransaction, transaction) => {
|
|
233
|
+
var _a;
|
|
234
|
+
const isTransactionParam = (0, utils_js_2.isTransaction)(searchParamsOrTransaction);
|
|
235
|
+
const { id, searchParams } = (0, utils_js_2.createSearchParams)(labelOrSearchParams, searchParamsOrTransaction);
|
|
236
|
+
const tx = isTransactionParam ? searchParamsOrTransaction : transaction;
|
|
237
|
+
const response = await ((_a = this.api) === null || _a === void 0 ? void 0 : _a.records.find({ id, searchParams }, tx));
|
|
238
|
+
const result = new record_js_1.DBRecordsArrayInstance(response.data, response.total, searchParamsOrTransaction);
|
|
239
|
+
result.init(this);
|
|
240
|
+
return result;
|
|
241
|
+
},
|
|
242
|
+
findById: async (idOrIds, transaction) => {
|
|
243
|
+
var _a, _b;
|
|
244
|
+
if ((0, utils_js_1.isArray)(idOrIds)) {
|
|
245
|
+
const response = (await ((_a = this.api) === null || _a === void 0 ? void 0 : _a.records.findById(idOrIds, transaction)));
|
|
246
|
+
const result = new record_js_1.DBRecordsArrayInstance(response.data, response.total);
|
|
247
|
+
result.init(this);
|
|
248
|
+
return result;
|
|
249
|
+
}
|
|
250
|
+
else {
|
|
251
|
+
const response = (await ((_b = this.api) === null || _b === void 0 ? void 0 : _b.records.findById(idOrIds, transaction)));
|
|
252
|
+
const result = new record_js_1.DBRecordInstance(response.data);
|
|
253
|
+
result.init(this);
|
|
254
|
+
return result;
|
|
255
|
+
}
|
|
256
|
+
},
|
|
257
|
+
findOne: async (labelOrSearchParams, searchParamsOrTransaction, transaction) => {
|
|
258
|
+
var _a;
|
|
259
|
+
const isTransactionParam = (0, utils_js_2.isTransaction)(searchParamsOrTransaction);
|
|
260
|
+
const { searchParams } = (0, utils_js_2.createSearchParams)(labelOrSearchParams, searchParamsOrTransaction);
|
|
261
|
+
const tx = isTransactionParam ? searchParamsOrTransaction : transaction;
|
|
262
|
+
const response = await ((_a = this.api) === null || _a === void 0 ? void 0 : _a.records.findOne(searchParams, tx));
|
|
263
|
+
const result = new record_js_1.DBRecordInstance(response.data, searchParamsOrTransaction);
|
|
264
|
+
result.init(this);
|
|
265
|
+
return result;
|
|
266
|
+
},
|
|
267
|
+
findUniq: async (labelOrSearchParams, searchParamsOrTransaction, transaction) => {
|
|
268
|
+
var _a;
|
|
269
|
+
const isTransactionParam = (0, utils_js_2.isTransaction)(searchParamsOrTransaction);
|
|
270
|
+
const { searchParams } = (0, utils_js_2.createSearchParams)(labelOrSearchParams, searchParamsOrTransaction);
|
|
271
|
+
const tx = isTransactionParam ? searchParamsOrTransaction : transaction;
|
|
272
|
+
const response = await ((_a = this.api) === null || _a === void 0 ? void 0 : _a.records.findUniq(searchParams, tx));
|
|
273
|
+
const result = new record_js_1.DBRecordInstance(response.data, searchParamsOrTransaction);
|
|
274
|
+
result.init(this);
|
|
275
|
+
return result;
|
|
276
|
+
},
|
|
277
|
+
properties: async (id, transaction) => {
|
|
278
|
+
var _a;
|
|
279
|
+
return (_a = this.api) === null || _a === void 0 ? void 0 : _a.records.properties(id, transaction);
|
|
280
|
+
},
|
|
281
|
+
relations: async (id, transaction) => {
|
|
282
|
+
return await this.api.records.relations(id, transaction);
|
|
283
|
+
},
|
|
284
|
+
set: async (id, data, transaction) => {
|
|
285
|
+
var _a, _b;
|
|
286
|
+
let response;
|
|
287
|
+
if (data instanceof record_js_1.DBRecordDraft) {
|
|
288
|
+
response = await ((_a = this.api) === null || _a === void 0 ? void 0 : _a.records.set(id, data, transaction));
|
|
289
|
+
}
|
|
290
|
+
else if ((0, utils_js_1.isObjectFlat)(data)) {
|
|
291
|
+
const properties = (0, utils_js_2.prepareProperties)(data);
|
|
292
|
+
response = await ((_b = this.api) === null || _b === void 0 ? void 0 : _b.records.set(id, new record_js_1.DBRecordDraft({ properties }), transaction));
|
|
293
|
+
}
|
|
294
|
+
else if ((0, utils_js_1.isObject)(data)) {
|
|
295
|
+
throw Error('Provided data is not a flat object. Consider to use `createMany` method.');
|
|
296
|
+
}
|
|
297
|
+
if ((response === null || response === void 0 ? void 0 : response.success) && (response === null || response === void 0 ? void 0 : response.data)) {
|
|
298
|
+
const result = new record_js_1.DBRecordInstance(response.data);
|
|
299
|
+
result.init(this);
|
|
300
|
+
return result;
|
|
301
|
+
}
|
|
302
|
+
return new record_js_1.DBRecordInstance();
|
|
303
|
+
},
|
|
304
|
+
update: async (id, data, transaction) => {
|
|
305
|
+
var _a, _b;
|
|
306
|
+
let response;
|
|
307
|
+
if (data instanceof record_js_1.DBRecordDraft) {
|
|
308
|
+
response = await ((_a = this.api) === null || _a === void 0 ? void 0 : _a.records.update(id, data, transaction));
|
|
309
|
+
}
|
|
310
|
+
else if ((0, utils_js_1.isObjectFlat)(data)) {
|
|
311
|
+
const properties = (0, utils_js_2.prepareProperties)(data);
|
|
312
|
+
const recordDraft = new record_js_1.DBRecordDraft({ properties });
|
|
313
|
+
response = await ((_b = this.api) === null || _b === void 0 ? void 0 : _b.records.update(id, recordDraft, transaction));
|
|
314
|
+
}
|
|
315
|
+
else if ((0, utils_js_1.isObject)(data)) {
|
|
316
|
+
throw Error('Provided data is not a flat object. Consider to use `createMany` method.');
|
|
317
|
+
}
|
|
318
|
+
if ((response === null || response === void 0 ? void 0 : response.success) && (response === null || response === void 0 ? void 0 : response.data)) {
|
|
319
|
+
const result = new record_js_1.DBRecordInstance(response.data);
|
|
320
|
+
result.init(this);
|
|
321
|
+
return result;
|
|
322
|
+
}
|
|
323
|
+
return new record_js_1.DBRecordInstance();
|
|
324
|
+
}
|
|
325
|
+
};
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
exports.RestAPI = RestAPI;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.DEFAULT_BASE_PATH = exports.DEFAULT_PROTOCOL = exports.DEFAULT_PORT = exports.DEFAULT_HOST = void 0;
|
|
4
|
+
exports.DEFAULT_HOST = 'api.rushdb.com';
|
|
5
|
+
exports.DEFAULT_PORT = 443;
|
|
6
|
+
exports.DEFAULT_PROTOCOL = 'https';
|
|
7
|
+
exports.DEFAULT_BASE_PATH = '/api/v1';
|