@rushdb/javascript-sdk 0.9.4 → 0.10.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.
Files changed (100) hide show
  1. package/{types → dist}/api/create-api.d.ts +6 -6
  2. package/{types → dist}/api/types.d.ts +17 -17
  3. package/dist/index.cjs +1 -0
  4. package/dist/index.mjs +1 -0
  5. package/dist/index.node.d.ts +137 -0
  6. package/dist/index.worker.d.ts +137 -0
  7. package/{types → dist}/sdk/model.d.ts +19 -15
  8. package/{types → dist}/sdk/record.d.ts +15 -12
  9. package/{types → dist}/sdk/sdk.d.ts +27 -27
  10. package/{types → dist}/sdk/transaction.d.ts +2 -2
  11. package/{types → dist}/types/query.d.ts +15 -8
  12. package/package.json +36 -39
  13. package/CHANGELOG.md +0 -180
  14. package/cjs/api/api.js +0 -328
  15. package/cjs/api/constants.js +0 -7
  16. package/cjs/api/create-api.js +0 -323
  17. package/cjs/api/index.js +0 -8
  18. package/cjs/api/rest-api-proxy.js +0 -22
  19. package/cjs/api/types.js +0 -2
  20. package/cjs/api/utils.js +0 -155
  21. package/cjs/common/constants.js +0 -10
  22. package/cjs/common/utils.js +0 -46
  23. package/cjs/index.cjs.node.js +0 -24
  24. package/cjs/index.cjs.worker.js +0 -24
  25. package/cjs/network/FetchHttpClient.js +0 -138
  26. package/cjs/network/HttpClient.js +0 -59
  27. package/cjs/network/NodeHttpClient.js +0 -103
  28. package/cjs/network/index.js +0 -28
  29. package/cjs/network/types.js +0 -2
  30. package/cjs/package.json +0 -4
  31. package/cjs/sdk/constants.js +0 -13
  32. package/cjs/sdk/errors.js +0 -27
  33. package/cjs/sdk/index.js +0 -23
  34. package/cjs/sdk/model.js +0 -147
  35. package/cjs/sdk/record.js +0 -122
  36. package/cjs/sdk/sdk.js +0 -46
  37. package/cjs/sdk/transaction.js +0 -17
  38. package/cjs/sdk/types.js +0 -2
  39. package/cjs/sdk/utils.js +0 -103
  40. package/cjs/types/expressions.js +0 -2
  41. package/cjs/types/index.js +0 -21
  42. package/cjs/types/query.js +0 -2
  43. package/cjs/types/schema.js +0 -2
  44. package/cjs/types/utils.js +0 -2
  45. package/cjs/types/value.js +0 -2
  46. package/esm/api/api.js +0 -324
  47. package/esm/api/constants.js +0 -4
  48. package/esm/api/create-api.js +0 -319
  49. package/esm/api/index.js +0 -2
  50. package/esm/api/rest-api-proxy.js +0 -18
  51. package/esm/api/types.js +0 -1
  52. package/esm/api/utils.js +0 -139
  53. package/esm/common/constants.js +0 -7
  54. package/esm/common/utils.js +0 -37
  55. package/esm/index.esm.node.js +0 -6
  56. package/esm/index.esm.worker.js +0 -6
  57. package/esm/network/FetchHttpClient.js +0 -133
  58. package/esm/network/HttpClient.js +0 -53
  59. package/esm/network/NodeHttpClient.js +0 -98
  60. package/esm/network/index.js +0 -24
  61. package/esm/network/types.js +0 -1
  62. package/esm/package.json +0 -4
  63. package/esm/sdk/constants.js +0 -10
  64. package/esm/sdk/errors.js +0 -21
  65. package/esm/sdk/index.js +0 -7
  66. package/esm/sdk/model.js +0 -143
  67. package/esm/sdk/record.js +0 -115
  68. package/esm/sdk/sdk.js +0 -42
  69. package/esm/sdk/transaction.js +0 -13
  70. package/esm/sdk/types.js +0 -1
  71. package/esm/sdk/utils.js +0 -93
  72. package/esm/types/expressions.js +0 -1
  73. package/esm/types/index.js +0 -5
  74. package/esm/types/query.js +0 -1
  75. package/esm/types/schema.js +0 -1
  76. package/esm/types/utils.js +0 -1
  77. package/esm/types/value.js +0 -1
  78. package/types/index.d.ts +0 -60
  79. /package/{types → dist}/api/api.d.ts +0 -0
  80. /package/{types → dist}/api/constants.d.ts +0 -0
  81. /package/{types → dist}/api/index.d.ts +0 -0
  82. /package/{types → dist}/api/rest-api-proxy.d.ts +0 -0
  83. /package/{types → dist}/api/utils.d.ts +0 -0
  84. /package/{types → dist}/common/constants.d.ts +0 -0
  85. /package/{types → dist}/common/utils.d.ts +0 -0
  86. /package/{types → dist}/network/FetchHttpClient.d.ts +0 -0
  87. /package/{types → dist}/network/HttpClient.d.ts +0 -0
  88. /package/{types → dist}/network/NodeHttpClient.d.ts +0 -0
  89. /package/{types → dist}/network/index.d.ts +0 -0
  90. /package/{types → dist}/network/types.d.ts +0 -0
  91. /package/{types → dist}/sdk/constants.d.ts +0 -0
  92. /package/{types → dist}/sdk/errors.d.ts +0 -0
  93. /package/{types → dist}/sdk/index.d.ts +0 -0
  94. /package/{types → dist}/sdk/types.d.ts +0 -0
  95. /package/{types → dist}/sdk/utils.d.ts +0 -0
  96. /package/{types → dist}/types/expressions.d.ts +0 -0
  97. /package/{types → dist}/types/index.d.ts +0 -0
  98. /package/{types → dist}/types/schema.d.ts +0 -0
  99. /package/{types → dist}/types/utils.d.ts +0 -0
  100. /package/{types → dist}/types/value.d.ts +0 -0
package/esm/sdk/model.js DELETED
@@ -1,143 +0,0 @@
1
- import { RestApiProxy } from '../api/rest-api-proxy.js';
2
- import { isEmptyObject } from '../common/utils.js';
3
- import { EmptyTargetError, UniquenessError } from './errors.js';
4
- import { mergeDefaultsWithPayload, pickUniqFieldsFromRecord, pickUniqFieldsFromRecords } from './utils.js';
5
- export class Model extends RestApiProxy {
6
- constructor(modelName, schema, RushDBInstance) {
7
- super();
8
- this.label = modelName;
9
- this.schema = schema;
10
- RushDBInstance === null || RushDBInstance === void 0 ? void 0 : RushDBInstance.registerModel(this);
11
- }
12
- getLabel() {
13
- return this.label;
14
- }
15
- async find(params = {}, transaction) {
16
- var _a;
17
- return (_a = this.apiProxy) === null || _a === void 0 ? void 0 : _a.records.find(this.label, params, transaction);
18
- }
19
- async findOne(params = {}, transaction) {
20
- var _a;
21
- return (_a = this.apiProxy) === null || _a === void 0 ? void 0 : _a.records.findOne(this.label, Object.assign({}, params), transaction);
22
- }
23
- async findById(id, transaction) {
24
- return this.findOne({ where: { $id: id } }, transaction);
25
- }
26
- async findUniq(params = {}, transaction) {
27
- var _a;
28
- return (_a = this.apiProxy) === null || _a === void 0 ? void 0 : _a.records.findUniq(this.label, Object.assign({}, params), transaction);
29
- }
30
- async create(record, transaction) {
31
- var _a;
32
- const data = await mergeDefaultsWithPayload(this.schema, record);
33
- const uniqFields = pickUniqFieldsFromRecord(this.schema, data);
34
- if (!isEmptyObject(uniqFields)) {
35
- const tx = transaction !== null && transaction !== void 0 ? transaction : (await this.apiProxy.tx.begin());
36
- const matchingRecords = await this.find({ where: uniqFields }, tx);
37
- const hasOwnTransaction = typeof transaction !== 'undefined';
38
- const canCreate = !((_a = matchingRecords === null || matchingRecords === void 0 ? void 0 : matchingRecords.data) === null || _a === void 0 ? void 0 : _a.length);
39
- if (canCreate) {
40
- const result = await this.apiProxy.records.create(this.label, data, tx);
41
- if (!hasOwnTransaction) {
42
- await tx.commit();
43
- }
44
- return result;
45
- }
46
- else {
47
- if (!hasOwnTransaction) {
48
- await tx.commit();
49
- }
50
- // @TODO: Make it optional
51
- throw new UniquenessError(this.label, uniqFields);
52
- }
53
- }
54
- return await this.apiProxy.records.create(this.label, data, transaction);
55
- }
56
- attach(sourceId, target, options, transaction) {
57
- return this.apiProxy.records.attach(sourceId, target, options, transaction);
58
- }
59
- detach(sourceId, target, options, transaction) {
60
- return this.apiProxy.records.detach(sourceId, target, options, transaction);
61
- }
62
- async handleSetOrUpdate(id, record, method, transaction) {
63
- var _a;
64
- const data = await mergeDefaultsWithPayload(this.schema, record);
65
- const uniqFields = pickUniqFieldsFromRecord(this.schema, data);
66
- if (!isEmptyObject(uniqFields)) {
67
- const tx = transaction !== null && transaction !== void 0 ? transaction : (await this.apiProxy.tx.begin());
68
- const matchingRecords = await this.find({ where: uniqFields }, tx);
69
- const hasOwnTransaction = typeof transaction !== 'undefined';
70
- const canUpdate = !((_a = matchingRecords === null || matchingRecords === void 0 ? void 0 : matchingRecords.data) === null || _a === void 0 ? void 0 : _a.length) ||
71
- (matchingRecords.data.length === 1 && matchingRecords.data[0].__id === id);
72
- if (canUpdate) {
73
- const result = await this.apiProxy.records[method](id, data, tx);
74
- if (!hasOwnTransaction) {
75
- await tx.commit();
76
- }
77
- return result;
78
- }
79
- else {
80
- if (!hasOwnTransaction) {
81
- await tx.commit();
82
- }
83
- throw new UniquenessError(this.label, uniqFields);
84
- }
85
- }
86
- return await this.apiProxy.records[method](id, data, transaction);
87
- }
88
- async set(id, record, transaction) {
89
- return await this.handleSetOrUpdate(id, record, 'set', transaction);
90
- }
91
- async update(id, record, transaction) {
92
- return await this.handleSetOrUpdate(id, record, 'update', transaction);
93
- }
94
- async createMany(records, transaction) {
95
- var _a;
96
- // Begin a transaction if one isn't provided.
97
- const hasOwnTransaction = typeof transaction !== 'undefined';
98
- const tx = transaction !== null && transaction !== void 0 ? transaction : (await this.apiProxy.tx.begin());
99
- try {
100
- // Apply defaults in parallel
101
- const recordsToStore = await Promise.all(records.map(async (record) => {
102
- return await mergeDefaultsWithPayload(this.schema, record);
103
- }));
104
- // Check uniqueness
105
- const uniqProperties = pickUniqFieldsFromRecords(recordsToStore, this.schema, this.label);
106
- if (!isEmptyObject(uniqProperties)) {
107
- const criteria = Object.entries(uniqProperties).map(([key, values]) => ({
108
- [key]: { $in: values }
109
- }));
110
- const matchingRecords = await this.find({
111
- where: {
112
- $or: criteria
113
- }
114
- }, tx);
115
- if ((_a = matchingRecords === null || matchingRecords === void 0 ? void 0 : matchingRecords.data) === null || _a === void 0 ? void 0 : _a.length) {
116
- throw new UniquenessError(this.label, Object.keys(uniqProperties));
117
- }
118
- }
119
- // Create records in the database
120
- const createdRecords = await this.apiProxy.records.createMany(this.label, recordsToStore, tx);
121
- // Commit the transaction if it was created internally
122
- if (!hasOwnTransaction) {
123
- await tx.commit();
124
- }
125
- return createdRecords;
126
- }
127
- catch (error) {
128
- if (!hasOwnTransaction) {
129
- await tx.rollback();
130
- }
131
- throw error;
132
- }
133
- }
134
- async delete(searchParams, transaction) {
135
- if (isEmptyObject(searchParams.where)) {
136
- throw new EmptyTargetError(`You must specify criteria to delete records of type '${this.label}'. Empty criteria are not allowed. If this was intentional, use the Dashboard instead.`);
137
- }
138
- return await this.apiProxy.records.delete(Object.assign(Object.assign({}, searchParams), { labels: [this.label] }), transaction);
139
- }
140
- async deleteById(idOrIds, transaction) {
141
- return await this.apiProxy.records.deleteById(idOrIds, transaction);
142
- }
143
- }
package/esm/sdk/record.js DELETED
@@ -1,115 +0,0 @@
1
- import { RestApiProxy } from '../api/rest-api-proxy.js';
2
- import { idToDate, idToTimestamp } from './utils.js';
3
- export class DBRecordsBatchDraft {
4
- constructor({ label, options = {
5
- returnResult: true,
6
- suggestTypes: true
7
- }, payload }) {
8
- this.label = label;
9
- this.options = options;
10
- this.payload = payload;
11
- }
12
- toJson() {
13
- return {
14
- label: this.label,
15
- options: this.options,
16
- payload: this.payload
17
- };
18
- }
19
- }
20
- export class DBRecordDraft {
21
- constructor({ label, properties = [] }) {
22
- this.label = label;
23
- this.properties = properties;
24
- }
25
- toJson() {
26
- return {
27
- label: this.label,
28
- properties: this.properties
29
- };
30
- }
31
- }
32
- export class DBRecordInstance extends RestApiProxy {
33
- constructor(data) {
34
- super();
35
- this.data = data;
36
- }
37
- get id() {
38
- try {
39
- return this.data.__id;
40
- }
41
- catch (_a) {
42
- throw new Error(`DBRecordInstance: Unable to access 'id'. The Record data is undefined.`);
43
- }
44
- }
45
- get label() {
46
- try {
47
- return this.data.__label;
48
- }
49
- catch (_a) {
50
- throw new Error(`DBRecordInstance: Unable to access 'label'. The Record data is undefined.`);
51
- }
52
- }
53
- get proptypes() {
54
- try {
55
- return this.data.__proptypes;
56
- }
57
- catch (_a) {
58
- throw new Error(`DBRecordInstance: Unable to access 'proptypes'. The Record data' is undefined.`);
59
- }
60
- }
61
- get date() {
62
- try {
63
- return idToDate(this.data.__id);
64
- }
65
- catch (_a) {
66
- throw new Error(`DBRecordInstance: Unable to access 'date'. The Record data is undefined.`);
67
- }
68
- }
69
- get timestamp() {
70
- try {
71
- return idToTimestamp(this.data.__id);
72
- }
73
- catch (_a) {
74
- throw new Error(`DBRecordInstance: Unable to access 'timestamp'. The Record data is undefined.`);
75
- }
76
- }
77
- async delete(transaction) {
78
- if (!this.data) {
79
- throw new Error('DBRecordInstance: Unable to delete Record. The Record data is undefined.');
80
- }
81
- return await this.apiProxy.records.deleteById(this.data.__id, transaction);
82
- }
83
- async update(partialData, transaction) {
84
- if (!this.data) {
85
- throw new Error('DBRecordInstance: Unable to update Record. The Record data is undefined.');
86
- }
87
- return this.apiProxy.records.update(this.data.__id, partialData, transaction);
88
- }
89
- async set(data, transaction) {
90
- if (!this.data) {
91
- throw new Error('DBRecordInstance: Unable to set. The Record data is undefined.');
92
- }
93
- return this.apiProxy.records.set(this.data.__id, data, transaction);
94
- }
95
- async attach(target, options, transaction) {
96
- if (!this.data) {
97
- throw new Error('DBRecordInstance: Unable to attach Record. The Record data is undefined.');
98
- }
99
- return this.apiProxy.records.attach(this.data.__id, target, options, transaction);
100
- }
101
- async detach(target, options, transaction) {
102
- if (!this.data) {
103
- throw new Error('DBRecordInstance: Unable to detach Record. The Record data is undefined.');
104
- }
105
- return this.apiProxy.records.detach(this.data.__id, target, options, transaction);
106
- }
107
- }
108
- export class DBRecordsArrayInstance extends RestApiProxy {
109
- constructor(data, total, searchParams) {
110
- super();
111
- this.data = data;
112
- this.total = total;
113
- this.searchParams = searchParams;
114
- }
115
- }
package/esm/sdk/sdk.js DELETED
@@ -1,42 +0,0 @@
1
- import { RestAPI } from '../api/api.js';
2
- import { DEFAULT_TIMEOUT } from '../common/constants.js';
3
- import { DBRecordInstance } from './record.js';
4
- import { parseConfig, validateInteger } from './utils.js';
5
- export const initSDK = (httpClient) => {
6
- class RushDB extends RestAPI {
7
- constructor(token, config) {
8
- var _a;
9
- const props = parseConfig(config);
10
- super(token, Object.assign(Object.assign({}, props), { httpClient: (_a = props.httpClient) !== null && _a !== void 0 ? _a : httpClient }));
11
- this.state = {
12
- debug: false,
13
- timeout: validateInteger('timeout', props.timeout, DEFAULT_TIMEOUT),
14
- token
15
- };
16
- this.models = new Map();
17
- RushDB.instance = this;
18
- }
19
- static getInstance() {
20
- return RushDB.instance;
21
- }
22
- registerModel(model) {
23
- const label = model.getLabel();
24
- // Inject the API into the model
25
- model.init(this);
26
- this.models.set(label, model);
27
- return model; //as unknown as Model<T['schema']> & Omit<RestAPI, 'find'>
28
- }
29
- getModels() {
30
- return this.models;
31
- }
32
- getModel(label) {
33
- return this.models.get(label);
34
- }
35
- toInstance(record) {
36
- const result = new DBRecordInstance(record);
37
- result.init(this);
38
- return result;
39
- }
40
- }
41
- return RushDB;
42
- };
@@ -1,13 +0,0 @@
1
- import { RestApiProxy } from '../api/rest-api-proxy.js';
2
- export class Transaction extends RestApiProxy {
3
- constructor(id) {
4
- super();
5
- this.id = id;
6
- }
7
- async rollback() {
8
- return await this.apiProxy.tx.rollback(this.id);
9
- }
10
- async commit() {
11
- return await this.apiProxy.tx.commit(this.id);
12
- }
13
- }
package/esm/sdk/types.js DELETED
@@ -1 +0,0 @@
1
- export {};
package/esm/sdk/utils.js DELETED
@@ -1,93 +0,0 @@
1
- import { isObject } from '../common/utils.js';
2
- import { ALLOWED_CONFIG_PROPERTIES } from './constants.js';
3
- import { UniquenessError } from './errors.js';
4
- export const mergeDefaultsWithPayload = async (schema, payload) => {
5
- const defaultPromises = Object.entries(schema).map(async ([key, prop]) => {
6
- if (prop.default &&
7
- typeof prop.default !== 'undefined' &&
8
- typeof payload[key] === 'undefined') {
9
- return {
10
- key,
11
- value: typeof prop.default === 'function' ? await prop.default() : prop.default
12
- };
13
- }
14
- else {
15
- return { key, value: undefined };
16
- }
17
- });
18
- const resolvedDefaults = await Promise.all(defaultPromises);
19
- const defaults = resolvedDefaults.reduce((acc, { key, value }) => {
20
- if (value !== undefined) {
21
- acc[key] = value;
22
- }
23
- return acc;
24
- }, {});
25
- return Object.assign(Object.assign({}, defaults), payload);
26
- };
27
- export const pickUniqFieldsFromRecord = (schema, data) => {
28
- return Object.entries(data)
29
- .filter(([key]) => { var _a; return (_a = schema[key]) === null || _a === void 0 ? void 0 : _a.uniq; })
30
- .reduce((acc, [key, value]) => {
31
- if (key in schema) {
32
- acc[key] = value;
33
- }
34
- return acc;
35
- }, {});
36
- };
37
- export const pickUniqFieldsFromRecords = (data, schema, label) => {
38
- const properties = {};
39
- const uniqFields = Object.entries(schema)
40
- .filter(([_, config]) => config.uniq)
41
- .reduce((acc, [key]) => {
42
- acc[key] = true;
43
- return acc;
44
- }, {});
45
- data.forEach((record) => {
46
- Object.entries(record).forEach(([key, value]) => {
47
- if (key in uniqFields) {
48
- if (properties[key]) {
49
- if (properties[key].includes(value)) {
50
- throw new UniquenessError(label, { [key]: value });
51
- }
52
- properties[key] = [...properties[key], value];
53
- }
54
- else {
55
- properties[key] = [value];
56
- }
57
- }
58
- });
59
- });
60
- return properties;
61
- };
62
- export const parseConfig = (config) => {
63
- if (!config) {
64
- return {};
65
- }
66
- if (!isObject(config)) {
67
- throw new Error('Config must be an object');
68
- }
69
- const values = Object.keys(config).filter((value) => !ALLOWED_CONFIG_PROPERTIES.includes(value));
70
- if (values.length > 0) {
71
- throw new Error(`Config object may only contain the following: ${ALLOWED_CONFIG_PROPERTIES.join(', ')}`);
72
- }
73
- return config;
74
- };
75
- export function validateInteger(name, n, defaultVal) {
76
- if (!Number.isInteger(n)) {
77
- if (defaultVal !== undefined) {
78
- return defaultVal;
79
- }
80
- else {
81
- throw new Error(`${name} must be an integer`);
82
- }
83
- }
84
- return n;
85
- }
86
- export function idToTimestamp(id) {
87
- const parts = id.split('-');
88
- const highBitsHex = parts[0] + parts[1].slice(0, 4);
89
- return parseInt(highBitsHex, 16);
90
- }
91
- export function idToDate(id) {
92
- return new Date(idToTimestamp(id));
93
- }
@@ -1 +0,0 @@
1
- export {};
@@ -1,5 +0,0 @@
1
- export * from './value.js';
2
- export * from './query.js';
3
- export * from './utils.js';
4
- export * from './schema.js';
5
- export * from './expressions.js';
@@ -1 +0,0 @@
1
- export {};
@@ -1 +0,0 @@
1
- export {};
@@ -1 +0,0 @@
1
- export {};
@@ -1 +0,0 @@
1
- export {};
package/types/index.d.ts DELETED
@@ -1,60 +0,0 @@
1
- import type { InferType, DBRecord, UserProvidedConfig, Relation } from './sdk'
2
-
3
- import { DBRecordsBatchDraft, DBRecordDraft, RestAPI, ApiResponse } from './api'
4
- import { HttpClient, HttpClientResponse } from './network/HttpClient'
5
- import {
6
- Model,
7
- DBRecordInstance,
8
- DBRecordsArrayInstance,
9
- Transaction,
10
- idToDate,
11
- idToTimestamp,
12
- EmptyTargetError,
13
- UniquenessError,
14
- NonUniqueResultError
15
- } from './sdk'
16
- import { Models, Schema } from './types'
17
-
18
- declare module '@rushdb/javascript-sdk' {
19
- export namespace RushDB {}
20
-
21
- export class RushDB extends RestAPI {
22
- static RushDB: typeof RushDB
23
-
24
- constructor(token?: string, config?: UserProvidedConfig)
25
- public api: RestAPI
26
-
27
- public registerModel<Model extends Model = Model>(model: Model): Model //Model<T['schema']>
28
- public getModel(label: string): Model
29
- public getModels(): Map<string, Model>
30
- public getInstance(token: string, config?: UserProvidedConfig): RushDB
31
- public toInstance<Schema extends Schema = Schema>(record: DBRecord<Schema>): DBRecordInstance
32
- }
33
-
34
- export {
35
- DBRecordsBatchDraft,
36
- Relation,
37
- ApiResponse,
38
- InferType,
39
- Model,
40
- Models,
41
- DBRecord,
42
- DBRecordDraft,
43
- DBRecordInstance,
44
- DBRecordsArrayInstance,
45
- RestAPI,
46
- Schema,
47
- Transaction,
48
- HttpClient,
49
- HttpClientResponse,
50
- EmptyTargetError,
51
- NonUniqueResultError,
52
- UniquenessError,
53
- idToDate,
54
- idToTimestamp
55
- }
56
-
57
- export * from './types'
58
-
59
- export default RushDB
60
- }
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes