@syntropix/database 0.1.1 → 0.2.1

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/dist/index.cjs CHANGED
@@ -1,7 +1,8 @@
1
1
  'use strict';
2
2
 
3
- var axios = require('axios');
4
3
  require('reflect-metadata');
4
+ var axios = require('axios');
5
+ var bson = require('bson');
5
6
 
6
7
  function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
7
8
 
@@ -9,9 +10,87 @@ var axios__default = /*#__PURE__*/_interopDefault(axios);
9
10
 
10
11
  var __defProp = Object.defineProperty;
11
12
  var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
12
- var Client = class {
13
+
14
+ // src/types/model/db-index.ts
15
+ var IndexType = {
16
+ Gin: "Gin",
17
+ GinTrgmOps: "GinTrgmOps",
18
+ IvfflatL2Ops: "IvfflatL2Ops",
19
+ InfflatCosineOps: "InfflatCosineOps",
20
+ InfflatIpOps: "InfflatIpOps"
21
+ };
22
+
23
+ // src/types/model/filter.ts
24
+ var SortType = {
25
+ Descending: "Descending",
26
+ Ascending: "Ascending"
27
+ };
28
+ var AggregateFunction = /* @__PURE__ */ (function(AggregateFunction2) {
29
+ AggregateFunction2["Count"] = "Count";
30
+ AggregateFunction2["Sum"] = "Sum";
31
+ AggregateFunction2["Avg"] = "Avg";
32
+ AggregateFunction2["Min"] = "Min";
33
+ AggregateFunction2["Max"] = "Max";
34
+ AggregateFunction2["CountDistinct"] = "CountDistinct";
35
+ return AggregateFunction2;
36
+ })({});
37
+ var FilterOperation = /* @__PURE__ */ (function(FilterOperation2) {
38
+ FilterOperation2["LT"] = "LT";
39
+ FilterOperation2["LTE"] = "LTE";
40
+ FilterOperation2["GT"] = "GT";
41
+ FilterOperation2["GTE"] = "GTE";
42
+ FilterOperation2["EQ"] = "EQ";
43
+ FilterOperation2["NEQ"] = "NEQ";
44
+ FilterOperation2["Between"] = "Between";
45
+ FilterOperation2["In"] = "In";
46
+ FilterOperation2["Contains"] = "Contains";
47
+ FilterOperation2["Overlap"] = "Overlap";
48
+ FilterOperation2["NotIn"] = "NotIn";
49
+ FilterOperation2["Like"] = "Like";
50
+ FilterOperation2["NotLike"] = "NotLike";
51
+ FilterOperation2["ILike"] = "ILike";
52
+ FilterOperation2["NotILike"] = "NotILike";
53
+ FilterOperation2["IsNull"] = "IsNull";
54
+ FilterOperation2["IsNotNull"] = "IsNotNull";
55
+ FilterOperation2["Similarity"] = "Similarity";
56
+ FilterOperation2["SimilarityDistance"] = "SimilarityDistance";
57
+ FilterOperation2["WordSimilarity"] = "WordSimilarity";
58
+ FilterOperation2["WordSimilarityDistance"] = "WordSimilarityDistance";
59
+ FilterOperation2["StrictWordSimilarity"] = "StrictWordSimilarity";
60
+ FilterOperation2["StrictWordSimilarityDistance"] = "StrictWordSimilarityDistance";
61
+ FilterOperation2["EuclideanDistance"] = "EuclideanDistance";
62
+ FilterOperation2["NegativeInnerProduct"] = "NegativeInnerProduct";
63
+ FilterOperation2["CosineDistance"] = "CosineDistance";
64
+ return FilterOperation2;
65
+ })({});
66
+
67
+ // src/types/model/foreign-key.ts
68
+ var ForeignKeyAction = {
69
+ Cascade: "Cascade",
70
+ Restrict: "Restrict",
71
+ SetNull: "SetNull",
72
+ NoAction: "NoAction",
73
+ SetDefault: "SetDefault"
74
+ };
75
+
76
+ // src/types/model/shared.ts
77
+ var AccessType = {
78
+ None: "None",
79
+ Read: "Read",
80
+ Write: "Write",
81
+ Admin: "Admin"
82
+ };
83
+ function handleError(error) {
84
+ const axiosErr = error;
85
+ if (axiosErr.response?.data?.status === "error") {
86
+ throw new Error(axiosErr.response.data.message);
87
+ }
88
+ throw new Error(axiosErr.message);
89
+ }
90
+ __name(handleError, "handleError");
91
+ var BaseClient = class {
13
92
  static {
14
- __name(this, "Client");
93
+ __name(this, "BaseClient");
15
94
  }
16
95
  config;
17
96
  constructor(config) {
@@ -30,23 +109,41 @@ var Client = class {
30
109
  return `${this.config.baseUrl}${path}`;
31
110
  }
32
111
  async get(path, data = {}) {
33
- const response = await axios__default.default.get(this.url(path), {
34
- headers: this.headers(),
35
- params: data
36
- });
37
- if (response.status !== 200) {
38
- throw new Error(`Failed to get data: ${response.statusText}`);
112
+ try {
113
+ const response = await axios__default.default.get(this.url(path), {
114
+ headers: this.headers(),
115
+ params: data
116
+ });
117
+ const content = bson.EJSON.parse(JSON.stringify(response.data));
118
+ if (content.status !== "success") {
119
+ throw new Error(content.message);
120
+ }
121
+ return content.data;
122
+ } catch (error) {
123
+ if (axios__default.default.isAxiosError(error)) {
124
+ handleError(error);
125
+ }
126
+ console.error(error);
127
+ throw new Error("Failed to get data: Unknown error");
39
128
  }
40
- return response.data;
41
129
  }
42
130
  async post(path, data = {}) {
43
- const response = await axios__default.default.post(this.url(path), data, {
44
- headers: this.headers()
45
- });
46
- if (response.status !== 200) {
47
- throw new Error(`Failed to post data: ${response.statusText}`);
131
+ try {
132
+ const response = await axios__default.default.post(this.url(path), data, {
133
+ headers: this.headers()
134
+ });
135
+ const content = bson.EJSON.parse(JSON.stringify(response.data));
136
+ if (content.status !== "success") {
137
+ throw new Error(content.message);
138
+ }
139
+ return content.data;
140
+ } catch (error) {
141
+ if (axios__default.default.isAxiosError(error)) {
142
+ handleError(error);
143
+ }
144
+ console.error(error);
145
+ throw new Error("Failed to post data: Unknown error");
48
146
  }
49
- return response.data;
50
147
  }
51
148
  };
52
149
 
@@ -79,210 +176,176 @@ var ClientConfig = class {
79
176
  }
80
177
  };
81
178
 
82
- // src/types/filter.ts
83
- var SortType = /* @__PURE__ */ (function(SortType2) {
84
- SortType2["Descending"] = "Descending";
85
- SortType2["Ascending"] = "Ascending";
86
- return SortType2;
87
- })({});
88
- var FilterOperation = /* @__PURE__ */ (function(FilterOperation2) {
89
- FilterOperation2["LT"] = "LT";
90
- FilterOperation2["LTE"] = "LTE";
91
- FilterOperation2["GT"] = "GT";
92
- FilterOperation2["GTE"] = "GTE";
93
- FilterOperation2["EQ"] = "EQ";
94
- FilterOperation2["NEQ"] = "NEQ";
95
- FilterOperation2["Between"] = "Between";
96
- FilterOperation2["In"] = "In";
97
- FilterOperation2["Contains"] = "Contains";
98
- FilterOperation2["Overlap"] = "Overlap";
99
- FilterOperation2["NotIn"] = "NotIn";
100
- FilterOperation2["Like"] = "Like";
101
- FilterOperation2["NotLike"] = "NotLike";
102
- FilterOperation2["ILike"] = "ILike";
103
- FilterOperation2["NotILike"] = "NotILike";
104
- FilterOperation2["IsNull"] = "IsNull";
105
- FilterOperation2["IsNotNull"] = "IsNotNull";
106
- FilterOperation2["Similarity"] = "Similarity";
107
- FilterOperation2["SimilarityDistance"] = "SimilarityDistance";
108
- FilterOperation2["WordSimilarity"] = "WordSimilarity";
109
- FilterOperation2["WordSimilarityDistance"] = "WordSimilarityDistance";
110
- FilterOperation2["StrictWordSimilarity"] = "StrictWordSimilarity";
111
- FilterOperation2["StrictWordSimilarityDistance"] = "StrictWordSimilarityDistance";
112
- FilterOperation2["EuclideanDistance"] = "EuclideanDistance";
113
- FilterOperation2["NegativeInnerProduct"] = "NegativeInnerProduct";
114
- FilterOperation2["CosineDistance"] = "CosineDistance";
115
- return FilterOperation2;
116
- })({});
179
+ // src/core/filter-builder.ts
117
180
  var AND = /* @__PURE__ */ __name((...conditions) => conditions, "AND");
118
181
  var OR = /* @__PURE__ */ __name((...conditions) => conditions, "OR");
119
182
  var EQ = /* @__PURE__ */ __name((field, value) => ({
120
- column: field,
121
- operator: "EQ",
183
+ columnName: field,
184
+ operator: FilterOperation.EQ,
122
185
  staticValue: value
123
186
  }), "EQ");
124
187
  var NE = /* @__PURE__ */ __name((field, value) => ({
125
- column: field,
126
- operator: "NEQ",
188
+ columnName: field,
189
+ operator: FilterOperation.NEQ,
127
190
  staticValue: value
128
191
  }), "NE");
129
192
  var GT = /* @__PURE__ */ __name((field, value) => ({
130
- column: field,
131
- operator: "GT",
193
+ columnName: field,
194
+ operator: FilterOperation.GT,
132
195
  staticValue: value
133
196
  }), "GT");
134
197
  var GTE = /* @__PURE__ */ __name((field, value) => ({
135
- column: field,
136
- operator: "GTE",
198
+ columnName: field,
199
+ operator: FilterOperation.GTE,
137
200
  staticValue: value
138
201
  }), "GTE");
139
202
  var LT = /* @__PURE__ */ __name((field, value) => ({
140
- column: field,
141
- operator: "LT",
203
+ columnName: field,
204
+ operator: FilterOperation.LT,
142
205
  staticValue: value
143
206
  }), "LT");
144
207
  var LTE = /* @__PURE__ */ __name((field, value) => ({
145
- column: field,
146
- operator: "LTE",
208
+ columnName: field,
209
+ operator: FilterOperation.LTE,
147
210
  staticValue: value
148
211
  }), "LTE");
149
212
  var IN = /* @__PURE__ */ __name((field, values) => ({
150
- column: field,
151
- operator: "In",
213
+ columnName: field,
214
+ operator: FilterOperation.In,
152
215
  staticValue: values
153
216
  }), "IN");
154
217
  var CONTAINS = /* @__PURE__ */ __name((field, value) => ({
155
- column: field,
156
- operator: "Contains",
218
+ columnName: field,
219
+ operator: FilterOperation.Contains,
157
220
  staticValue: value
158
221
  }), "CONTAINS");
159
222
  var OVERLAP = /* @__PURE__ */ __name((field, value) => ({
160
- column: field,
161
- operator: "Overlap",
223
+ columnName: field,
224
+ operator: FilterOperation.Overlap,
162
225
  staticValue: value
163
226
  }), "OVERLAP");
164
227
  var I_LIKE = /* @__PURE__ */ __name((field, pattern) => ({
165
- column: field,
166
- operator: "ILike",
228
+ columnName: field,
229
+ operator: FilterOperation.ILike,
167
230
  staticValue: pattern
168
231
  }), "I_LIKE");
169
232
  var NOT_I_LIKE = /* @__PURE__ */ __name((field, pattern) => ({
170
- column: field,
171
- operator: "NotILike",
233
+ columnName: field,
234
+ operator: FilterOperation.NotILike,
172
235
  staticValue: pattern
173
236
  }), "NOT_I_LIKE");
174
237
  var NOT_IN = /* @__PURE__ */ __name((field, values) => ({
175
- column: field,
176
- operator: "NotIn",
238
+ columnName: field,
239
+ operator: FilterOperation.NotIn,
177
240
  staticValue: values
178
241
  }), "NOT_IN");
179
242
  var LIKE = /* @__PURE__ */ __name((field, pattern) => ({
180
- column: field,
181
- operator: "Like",
243
+ columnName: field,
244
+ operator: FilterOperation.Like,
182
245
  staticValue: pattern
183
246
  }), "LIKE");
184
247
  var NOT_LIKE = /* @__PURE__ */ __name((field, pattern) => ({
185
- column: field,
186
- operator: "NotLike",
248
+ columnName: field,
249
+ operator: FilterOperation.NotLike,
187
250
  staticValue: pattern
188
251
  }), "NOT_LIKE");
189
252
  var IS_NULL = /* @__PURE__ */ __name((field) => ({
190
- column: field,
191
- operator: "IsNull"
253
+ columnName: field,
254
+ operator: FilterOperation.IsNull
192
255
  }), "IS_NULL");
193
256
  var IS_NOT_NULL = /* @__PURE__ */ __name((field) => ({
194
- column: field,
195
- operator: "IsNotNull"
257
+ columnName: field,
258
+ operator: FilterOperation.IsNotNull
196
259
  }), "IS_NOT_NULL");
197
260
  var BETWEEN = /* @__PURE__ */ __name((field, min, max) => ({
198
- column: field,
199
- operator: "Between",
261
+ columnName: field,
262
+ operator: FilterOperation.Between,
200
263
  staticValue: [
201
264
  min,
202
265
  max
203
266
  ]
204
267
  }), "BETWEEN");
205
268
  var EQ_COL = /* @__PURE__ */ __name((field, otherField) => ({
206
- column: field,
207
- operator: "EQ",
269
+ columnName: field,
270
+ operator: FilterOperation.EQ,
208
271
  columnValue: otherField
209
272
  }), "EQ_COL");
210
273
  var NE_COL = /* @__PURE__ */ __name((field, otherField) => ({
211
- column: field,
212
- operator: "NEQ",
274
+ columnName: field,
275
+ operator: FilterOperation.NEQ,
213
276
  columnValue: otherField
214
277
  }), "NE_COL");
215
278
  var GT_COL = /* @__PURE__ */ __name((field, otherField) => ({
216
- column: field,
217
- operator: "GT",
279
+ columnName: field,
280
+ operator: FilterOperation.GT,
218
281
  columnValue: otherField
219
282
  }), "GT_COL");
220
283
  var GTE_COL = /* @__PURE__ */ __name((field, otherField) => ({
221
- column: field,
222
- operator: "GTE",
284
+ columnName: field,
285
+ operator: FilterOperation.GTE,
223
286
  columnValue: otherField
224
287
  }), "GTE_COL");
225
288
  var LT_COL = /* @__PURE__ */ __name((field, otherField) => ({
226
- column: field,
227
- operator: "LT",
289
+ columnName: field,
290
+ operator: FilterOperation.LT,
228
291
  columnValue: otherField
229
292
  }), "LT_COL");
230
293
  var LTE_COL = /* @__PURE__ */ __name((field, otherField) => ({
231
- column: field,
232
- operator: "LTE",
294
+ columnName: field,
295
+ operator: FilterOperation.LTE,
233
296
  columnValue: otherField
234
297
  }), "LTE_COL");
235
298
  var SIMILARITY = /* @__PURE__ */ __name((field, value, options) => ({
236
- column: field,
237
- operator: "Similarity",
299
+ columnName: field,
300
+ operator: FilterOperation.Similarity,
238
301
  staticValue: value,
239
302
  simiarityOptions: options
240
303
  }), "SIMILARITY");
241
304
  var SIMILARITY_DISTANCE = /* @__PURE__ */ __name((field, value, options) => ({
242
- column: field,
243
- operator: "SimilarityDistance",
305
+ columnName: field,
306
+ operator: FilterOperation.SimilarityDistance,
244
307
  staticValue: value,
245
308
  simiarityOptions: options
246
309
  }), "SIMILARITY_DISTANCE");
247
310
  var WORD_SIMILARITY = /* @__PURE__ */ __name((field, value, options) => ({
248
- column: field,
249
- operator: "WordSimilarity",
311
+ columnName: field,
312
+ operator: FilterOperation.WordSimilarity,
250
313
  staticValue: value,
251
314
  simiarityOptions: options
252
315
  }), "WORD_SIMILARITY");
253
316
  var WORD_SIMILARITY_DISTANCE = /* @__PURE__ */ __name((field, value, options) => ({
254
- column: field,
255
- operator: "WordSimilarityDistance",
317
+ columnName: field,
318
+ operator: FilterOperation.WordSimilarityDistance,
256
319
  staticValue: value,
257
320
  simiarityOptions: options
258
321
  }), "WORD_SIMILARITY_DISTANCE");
259
322
  var STRICT_WORD_SIMILARITY = /* @__PURE__ */ __name((field, value, options) => ({
260
- column: field,
261
- operator: "StrictWordSimilarity",
323
+ columnName: field,
324
+ operator: FilterOperation.StrictWordSimilarity,
262
325
  staticValue: value,
263
326
  simiarityOptions: options
264
327
  }), "STRICT_WORD_SIMILARITY");
265
328
  var STRICT_WORD_SIMILARITY_DISTANCE = /* @__PURE__ */ __name((field, value, options) => ({
266
- column: field,
267
- operator: "StrictWordSimilarityDistance",
329
+ columnName: field,
330
+ operator: FilterOperation.StrictWordSimilarityDistance,
268
331
  staticValue: value,
269
332
  simiarityOptions: options
270
333
  }), "STRICT_WORD_SIMILARITY_DISTANCE");
271
334
  var EUCLIDEAN_DISTANCE = /* @__PURE__ */ __name((field, value, options) => ({
272
- column: field,
273
- operator: "EuclideanDistance",
335
+ columnName: field,
336
+ operator: FilterOperation.EuclideanDistance,
274
337
  staticValue: value,
275
338
  simiarityOptions: options
276
339
  }), "EUCLIDEAN_DISTANCE");
277
340
  var NEGATIVE_INNER_PRODUCT = /* @__PURE__ */ __name((field, value, options) => ({
278
- column: field,
279
- operator: "NegativeInnerProduct",
341
+ columnName: field,
342
+ operator: FilterOperation.NegativeInnerProduct,
280
343
  staticValue: value,
281
344
  simiarityOptions: options
282
345
  }), "NEGATIVE_INNER_PRODUCT");
283
346
  var COSINE_DISTANCE = /* @__PURE__ */ __name((field, value, options) => ({
284
- column: field,
285
- operator: "CosineDistance",
347
+ columnName: field,
348
+ operator: FilterOperation.CosineDistance,
286
349
  staticValue: value,
287
350
  simiarityOptions: options
288
351
  }), "COSINE_DISTANCE");
@@ -301,155 +364,91 @@ function FilterWrapper(filter) {
301
364
  }
302
365
  __name(FilterWrapper, "FilterWrapper");
303
366
 
304
- // src/core/data-client.ts
367
+ // src/core/client/data-client.ts
305
368
  var DataClient = class {
306
369
  static {
307
370
  __name(this, "DataClient");
308
371
  }
309
372
  client;
310
373
  constructor(config = new ClientConfig()) {
311
- this.client = new Client(config);
374
+ this.client = new BaseClient(config);
312
375
  }
313
376
  async insertData(data) {
314
- const response = await this.client.post("/insert", data);
315
- if (response.status === "success") {
316
- return response.data;
317
- }
318
- throw new Error(response.message);
377
+ return await this.client.post("/insert", data);
319
378
  }
320
379
  async insertOne(data) {
321
- const response = await this.client.post("/insert/one", data);
322
- if (response.status === "success") {
323
- return response.data;
324
- }
325
- throw new Error(response.message);
380
+ return await this.client.post("/insert/one", data);
326
381
  }
327
382
  async updateByPrimaryKey(pk, data) {
328
- const response = await this.client.post(`/update/${pk}`, data);
329
- if (response.status === "success") {
330
- return response.data;
331
- }
332
- throw new Error(response.message);
383
+ return await this.client.post(`/update/${pk}`, data);
333
384
  }
334
385
  async queryOne(data, model) {
335
386
  data.query.filter = FilterWrapper(data.query.filter);
336
387
  const response = await this.client.post("/query", {
337
388
  One: data
338
389
  });
339
- if (response.status === "success") {
340
- const responseData = response.data;
341
- if (model !== void 0) {
342
- return new model(responseData);
343
- }
344
- return responseData;
390
+ if (model !== void 0) {
391
+ return new model(response);
345
392
  }
346
- throw new Error(response.message);
393
+ return response;
347
394
  }
348
395
  async queryMany(data, model) {
349
396
  data.query.filter = FilterWrapper(data.query.filter);
350
397
  const response = await this.client.post("/query", {
351
398
  Many: data
352
399
  });
353
- if (response.status === "success") {
354
- const responseData = response.data;
355
- if (model !== void 0) {
356
- return responseData.map((item) => new model(item));
357
- }
358
- return responseData;
400
+ if (model !== void 0) {
401
+ return response.map((item) => new model(item));
359
402
  }
360
- throw new Error(response.message);
403
+ return response;
361
404
  }
362
405
  async updateData(data) {
363
- const response = await this.client.post("/update", data);
364
- if (response.status === "success") {
365
- return response.data;
366
- }
367
- throw new Error(response.message);
406
+ return await this.client.post("/update", data);
368
407
  }
369
408
  async deleteData(data) {
370
- const response = await this.client.post("/delete", data);
371
- if (response.status === "success") {
372
- return response.data;
373
- }
374
- throw new Error(response.message);
409
+ return await this.client.post("/delete", data);
375
410
  }
376
411
  };
377
412
 
378
- // src/core/table-client.ts
413
+ // src/core/client/table-client.ts
379
414
  var TableClient = class {
380
415
  static {
381
416
  __name(this, "TableClient");
382
417
  }
383
418
  client;
384
419
  constructor(config = new ClientConfig()) {
385
- this.client = new Client(config);
420
+ this.client = new BaseClient(config);
386
421
  }
387
422
  async createTable(data) {
388
- const response = await this.client.post("/table/create", data);
389
- if (response.status === "success") {
390
- return response.data;
391
- }
392
- throw new Error(response.message);
423
+ return await this.client.post("/table/create", data);
393
424
  }
394
425
  async dropTable(data) {
395
- const response = await this.client.post("/table/drop", data);
396
- if (response.status === "success") {
397
- return response.data;
398
- }
399
- throw new Error(response.message);
426
+ return await this.client.post("/table/drop", data);
400
427
  }
401
428
  async renameTable(data) {
402
- const response = await this.client.post("/table/rename", data);
403
- if (response.status === "success") {
404
- return response.data;
405
- }
406
- throw new Error(response.message);
429
+ return await this.client.post("/table/rename", data);
407
430
  }
408
431
  async truncateTable(data) {
409
- const response = await this.client.post("/table/truncate", data);
410
- if (response.status === "success") {
411
- return response.data;
412
- }
413
- throw new Error(response.message);
432
+ return await this.client.post("/table/truncate", data);
414
433
  }
415
434
  async addColumn(data) {
416
- const response = await this.client.post("/table/column/add", data);
417
- if (response.status === "success") {
418
- return response.data;
419
- }
420
- throw new Error(response.message);
435
+ return await this.client.post("/table/column/add", data);
421
436
  }
422
437
  async dropColumn(data) {
423
- const response = await this.client.post("/table/column/drop", data);
424
- if (response.status === "success") {
425
- return response.data;
426
- }
427
- throw new Error(response.message);
438
+ return await this.client.post("/table/column/drop", data);
428
439
  }
429
440
  async modifyColumn(data) {
430
- const response = await this.client.post("/table/column/modify", data);
431
- if (response.status === "success") {
432
- return response.data;
433
- }
434
- throw new Error(response.message);
441
+ return await this.client.post("/table/column/modify", data);
435
442
  }
436
443
  async getTableSchema(data) {
437
- const response = await this.client.post("/table/schema", data);
438
- if (response.status === "success") {
439
- return response.data;
440
- }
441
- throw new Error(response.message);
444
+ return await this.client.get("/table/schema", data);
442
445
  }
443
446
  async getTableList(data) {
444
- const response = await this.client.get("/table/list", data);
445
- if (response.status === "success") {
446
- return response.data;
447
- }
448
- throw new Error(response.message);
447
+ return await this.client.get("/table/list", data);
449
448
  }
450
449
  };
451
450
 
452
- // src/core/syntropix.ts
451
+ // src/core/client/syntropix-client.ts
453
452
  var SyntropixClient = class {
454
453
  static {
455
454
  __name(this, "SyntropixClient");
@@ -474,26 +473,40 @@ var SyntropixClient = class {
474
473
  }
475
474
  };
476
475
 
477
- // src/types/common.ts
478
- var ForeignKeyAction = /* @__PURE__ */ (function(ForeignKeyAction2) {
479
- ForeignKeyAction2["Cascade"] = "Cascade";
480
- ForeignKeyAction2["Restrict"] = "Restrict";
481
- ForeignKeyAction2["SetNull"] = "SetNull";
482
- ForeignKeyAction2["NoAction"] = "NoAction";
483
- ForeignKeyAction2["SetDefault"] = "SetDefault";
484
- return ForeignKeyAction2;
485
- })({});
486
- var AggregateFunction = /* @__PURE__ */ (function(AggregateFunction2) {
487
- AggregateFunction2["Count"] = "Count";
488
- AggregateFunction2["Sum"] = "Sum";
489
- AggregateFunction2["Avg"] = "Avg";
490
- AggregateFunction2["Min"] = "Min";
491
- AggregateFunction2["Max"] = "Max";
492
- AggregateFunction2["CountDistinct"] = "CountDistinct";
493
- return AggregateFunction2;
494
- })({});
495
-
496
- // src/types/data-type.ts
476
+ // src/core/data-type.ts
477
+ var ArrayDataType = {
478
+ String: /* @__PURE__ */ __name((maxLength) => ({
479
+ String: maxLength
480
+ }), "String"),
481
+ Decimal: /* @__PURE__ */ __name((precision, scale) => ({
482
+ Decimal: precision && scale ? [
483
+ precision,
484
+ scale
485
+ ] : null
486
+ }), "Decimal"),
487
+ Text: "Text",
488
+ Integer: "Integer",
489
+ BigInteger: "BigInteger",
490
+ Double: "Double",
491
+ DateTime: "DateTime",
492
+ Timestamp: "Timestamp",
493
+ Date: "Date",
494
+ Boolean: "Boolean",
495
+ Money: /* @__PURE__ */ __name((precision, scale) => ({
496
+ Money: precision && scale ? [
497
+ precision,
498
+ scale
499
+ ] : null
500
+ }), "Money"),
501
+ Uuid: "Uuid",
502
+ Enum: /* @__PURE__ */ __name((name, variants) => ({
503
+ Enum: {
504
+ name,
505
+ variants
506
+ }
507
+ }), "Enum"),
508
+ Json: "Json"
509
+ };
497
510
  var ColumnDataType = {
498
511
  Integer: "Integer",
499
512
  String: /* @__PURE__ */ __name((maxLength) => ({
@@ -533,12 +546,13 @@ var ColumnDataType = {
533
546
  }), "Decimal")
534
547
  };
535
548
 
536
- // src/types/field.ts
549
+ // src/core/field.ts
537
550
  var Field = class {
538
551
  static {
539
552
  __name(this, "Field");
540
553
  }
541
554
  name = "";
555
+ displayName = "";
542
556
  description = "";
543
557
  columnType;
544
558
  isPrimaryKey = false;
@@ -549,6 +563,7 @@ var Field = class {
549
563
  constructor(columnType, options = {}) {
550
564
  this.columnType = columnType;
551
565
  this.name = options.name?.toLowerCase() || "";
566
+ this.displayName = options.displayName || "";
552
567
  this.description = options.description || "";
553
568
  this.isPrimaryKey = options.isPrimaryKey || false;
554
569
  this.isNullable = options.isNullable || false;
@@ -556,9 +571,10 @@ var Field = class {
556
571
  this.default = options.default;
557
572
  this.defaultAccess = options.defaultAccess;
558
573
  }
559
- into() {
574
+ intoColumnCreateDto() {
560
575
  return {
561
576
  name: this.name,
577
+ displayName: this.displayName,
562
578
  columnType: this.columnType,
563
579
  description: this.description,
564
580
  isPrimaryKey: this.isPrimaryKey,
@@ -705,8 +721,11 @@ var DoubleField = class extends Field {
705
721
  super(ColumnDataType.Double, options);
706
722
  }
707
723
  };
724
+
725
+ // src/core/base-model.ts
708
726
  var FIELDS_KEY = Symbol("fields");
709
727
  var TABLE_NAME_KEY = Symbol("tableName");
728
+ var TABLE_DISPLAY_NAME_KEY = Symbol("displayTableName");
710
729
  var DESCRIPTION_KEY = Symbol("description");
711
730
  var INDEXES_KEY = Symbol("indexes");
712
731
  function Column(options = {}) {
@@ -718,11 +737,13 @@ function Column(options = {}) {
718
737
  constructor() {
719
738
  super(columnType, {
720
739
  name: fieldName,
740
+ displayName: options.displayName,
721
741
  description: options.description,
722
742
  isPrimaryKey: options.primary,
723
743
  isNullable: options.nullable,
724
744
  autoIncrement: options.autoIncrement,
725
- default: options.default
745
+ default: options.default,
746
+ defaultAccess: options.defaultAccess
726
747
  });
727
748
  }
728
749
  }();
@@ -731,15 +752,22 @@ function Column(options = {}) {
731
752
  };
732
753
  }
733
754
  __name(Column, "Column");
755
+ function Description(description) {
756
+ return function(target) {
757
+ Reflect.defineMetadata(DESCRIPTION_KEY, description, target);
758
+ };
759
+ }
760
+ __name(Description, "Description");
734
761
  function ForeignKey(tableName, columnName, options = {}) {
735
762
  return function(target, propertyKey) {
736
763
  const fields = Reflect.getMetadata(FIELDS_KEY, target.constructor) || {};
737
764
  const field = new ForeignKeyField(options.type || ColumnDataType.Integer, tableName, columnName, {
738
- onDelete: options.onDelete,
739
- onUpdate: options.onUpdate,
765
+ displayName: options.displayName,
740
766
  description: options.description,
741
767
  isNullable: options.nullable,
742
- default: options.default
768
+ default: options.default,
769
+ onDelete: options.onDelete,
770
+ onUpdate: options.onUpdate
743
771
  });
744
772
  field.name = options.name || propertyKey.toLowerCase();
745
773
  fields[propertyKey] = field;
@@ -778,6 +806,14 @@ var BaseModel = class {
778
806
  }
779
807
  return this.name.toLowerCase();
780
808
  }
809
+ static getDisplayName() {
810
+ const displayName = Reflect.getMetadata(TABLE_DISPLAY_NAME_KEY, this);
811
+ if (displayName) return displayName;
812
+ if ("displayName" in this && typeof this.displayName === "string") {
813
+ return this.displayName;
814
+ }
815
+ return this.name;
816
+ }
781
817
  static getDescription() {
782
818
  const metadataDescription = Reflect.getMetadata(DESCRIPTION_KEY, this);
783
819
  if (metadataDescription) return metadataDescription;
@@ -830,6 +866,9 @@ var BaseModel = class {
830
866
  getTableName() {
831
867
  return this.constructor.getTableName();
832
868
  }
869
+ getDisplayName() {
870
+ return this.constructor.getDisplayName();
871
+ }
833
872
  getPrimaryKeyName() {
834
873
  return this.constructor.getPrimaryKeyName();
835
874
  }
@@ -845,14 +884,14 @@ var BaseModel = class {
845
884
  // Client getter
846
885
  get client() {
847
886
  if (!this._client) {
848
- this._client = new SyntropixClient(new ClientConfig());
887
+ this._client = new SyntropixClient();
849
888
  }
850
889
  return this._client;
851
890
  }
852
891
  // Table operations
853
- static async createTable(_client) {
892
+ static async createTable(client = new SyntropixClient()) {
854
893
  const fields = this.getFields();
855
- const columns = Object.values(fields).map((f) => f.into());
894
+ const columns = Object.values(fields).map((f) => f.intoColumnCreateDto());
856
895
  const foreignKeys = this.getAssociations().map((f) => ({
857
896
  fromTableName: this.getTableName(),
858
897
  fromColumnName: f.name,
@@ -862,36 +901,38 @@ var BaseModel = class {
862
901
  onUpdate: f.onUpdate
863
902
  }));
864
903
  const indexes = this.getIndexes();
865
- const client = _client || new SyntropixClient(new ClientConfig());
866
- return client.table.createTable({
904
+ return await client.table.createTable({
867
905
  name: this.getTableName(),
906
+ displayName: this.getDisplayName(),
868
907
  description: this.getDescription() || "No description",
869
908
  columns,
870
909
  foreignKeys,
871
910
  indexes
872
911
  });
873
912
  }
874
- static async dropTable(_client) {
875
- const client = _client || new SyntropixClient(new ClientConfig());
876
- return client.table.dropTable({
913
+ static async dropTable(client = new SyntropixClient()) {
914
+ return await client.table.dropTable({
877
915
  tableName: this.getTableName()
878
916
  });
879
917
  }
880
- static async renameTable(newName, _client) {
881
- const client = _client || new SyntropixClient(new ClientConfig());
882
- return client.table.renameTable({
918
+ static async renameTable(newName, client = new SyntropixClient()) {
919
+ return await client.table.renameTable({
883
920
  tableName: this.getTableName(),
884
921
  newTableName: newName
885
922
  });
886
923
  }
887
- static async truncateTable(_client) {
888
- const client = _client || new SyntropixClient(new ClientConfig());
889
- return client.table.truncateTable({
924
+ static async truncateTable(client = new SyntropixClient()) {
925
+ return await client.table.truncateTable({
926
+ tableName: this.getTableName()
927
+ });
928
+ }
929
+ static async getTableSchema(client = new SyntropixClient()) {
930
+ return await client.table.getTableSchema({
890
931
  tableName: this.getTableName()
891
932
  });
892
933
  }
893
934
  // Data operations
894
- static async create(data, _client) {
935
+ static async create(data, client = new SyntropixClient()) {
895
936
  const model = this;
896
937
  const fields = model.getFields();
897
938
  const columns = [];
@@ -904,7 +945,6 @@ var BaseModel = class {
904
945
  throw new Error(`Invalid field: ${key}`);
905
946
  }
906
947
  }
907
- const client = _client || new SyntropixClient(new ClientConfig());
908
948
  return client.data.insertOne({
909
949
  tableName: model.getTableName(),
910
950
  data: {
@@ -915,7 +955,7 @@ var BaseModel = class {
915
955
  }
916
956
  });
917
957
  }
918
- async save(_client) {
958
+ async save(client = new SyntropixClient()) {
919
959
  const pk = this.getPrimaryKey();
920
960
  const pkValue = this[this.getPrimaryKeyName()];
921
961
  const fields = this.getFields();
@@ -924,7 +964,7 @@ var BaseModel = class {
924
964
  for (const [key, field] of Object.entries(fields)) {
925
965
  data[key] = this[key];
926
966
  }
927
- return this.constructor.create(data, _client);
967
+ return this.constructor.create(data, client);
928
968
  }
929
969
  const columns = [];
930
970
  const values = [];
@@ -934,7 +974,7 @@ var BaseModel = class {
934
974
  values.push(this[key]);
935
975
  }
936
976
  }
937
- return _client || this.client.data.updateByPrimaryKey(pkValue, {
977
+ return client || this.client.data.updateByPrimaryKey(pkValue, {
938
978
  tableName: this.getTableName(),
939
979
  payload: {
940
980
  filter: [
@@ -971,7 +1011,7 @@ var BaseModel = class {
971
1011
  }
972
1012
  }
973
1013
  let values = [];
974
- const client = _client || new SyntropixClient(new ClientConfig());
1014
+ const client = _client || new SyntropixClient();
975
1015
  for (const data of datas) {
976
1016
  if (columnsSet.size !== new Set(Object.keys(data)).size || !columns.every((c) => c in data)) {
977
1017
  throw new Error("All data must have the same columns");
@@ -1012,7 +1052,7 @@ var BaseModel = class {
1012
1052
  }
1013
1053
  }
1014
1054
  const values = columns.map((c) => data[c]);
1015
- const client = _client || new SyntropixClient(new ClientConfig());
1055
+ const client = _client || new SyntropixClient();
1016
1056
  return client.data.updateData({
1017
1057
  tableName: model.getTableName(),
1018
1058
  payload: {
@@ -1024,7 +1064,7 @@ var BaseModel = class {
1024
1064
  }
1025
1065
  static async delete(filter, _client) {
1026
1066
  const model = this;
1027
- const client = _client || new SyntropixClient(new ClientConfig());
1067
+ const client = _client || new SyntropixClient();
1028
1068
  return client.data.deleteData({
1029
1069
  tableName: model.getTableName(),
1030
1070
  payload: {
@@ -1035,7 +1075,7 @@ var BaseModel = class {
1035
1075
  static async get(filter, select, _client) {
1036
1076
  const model = this;
1037
1077
  const fields = model.getFields();
1038
- const client = _client || new SyntropixClient(new ClientConfig());
1078
+ const client = _client || new SyntropixClient();
1039
1079
  const data = await client.data.queryOne({
1040
1080
  tableName: model.getTableName(),
1041
1081
  query: {
@@ -1048,7 +1088,7 @@ var BaseModel = class {
1048
1088
  }
1049
1089
  static async filter(options) {
1050
1090
  const model = this;
1051
- const client = options._client || new SyntropixClient(new ClientConfig());
1091
+ const client = options._client || new SyntropixClient();
1052
1092
  const data = await client.data.queryMany({
1053
1093
  tableName: model.getTableName(),
1054
1094
  query: {
@@ -1068,7 +1108,7 @@ var BaseModel = class {
1068
1108
  }
1069
1109
  static async count(options = {}) {
1070
1110
  const model = this;
1071
- const client = options._client || new SyntropixClient(new ClientConfig());
1111
+ const client = options._client || new SyntropixClient();
1072
1112
  const data = await client.data.queryMany({
1073
1113
  tableName: model.getTableName(),
1074
1114
  query: {
@@ -1122,29 +1162,25 @@ var BaseModel = class {
1122
1162
  }
1123
1163
  };
1124
1164
 
1125
- // src/types/requests.ts
1126
- var SyntropixDBAccessType = {
1127
- None: "None",
1128
- Read: "Read",
1129
- Write: "Write",
1130
- Admin: "Admin"
1131
- };
1132
-
1133
1165
  exports.AND = AND;
1166
+ exports.AccessType = AccessType;
1134
1167
  exports.AggregateFunction = AggregateFunction;
1168
+ exports.ArrayDataType = ArrayDataType;
1135
1169
  exports.ArrayField = ArrayField;
1136
1170
  exports.BETWEEN = BETWEEN;
1171
+ exports.BaseClient = BaseClient;
1137
1172
  exports.BaseModel = BaseModel;
1138
1173
  exports.BooleanField = BooleanField;
1139
1174
  exports.CONTAINS = CONTAINS;
1140
1175
  exports.COSINE_DISTANCE = COSINE_DISTANCE;
1141
- exports.Client = Client;
1142
1176
  exports.ClientConfig = ClientConfig;
1143
1177
  exports.Column = Column;
1144
1178
  exports.ColumnDataType = ColumnDataType;
1179
+ exports.DataClient = DataClient;
1145
1180
  exports.DateField = DateField;
1146
1181
  exports.DateTimeField = DateTimeField;
1147
1182
  exports.DecimalField = DecimalField;
1183
+ exports.Description = Description;
1148
1184
  exports.DoubleField = DoubleField;
1149
1185
  exports.EQ = EQ;
1150
1186
  exports.EQ_COL = EQ_COL;
@@ -1164,6 +1200,7 @@ exports.IN = IN;
1164
1200
  exports.IS_NOT_NULL = IS_NOT_NULL;
1165
1201
  exports.IS_NULL = IS_NULL;
1166
1202
  exports.I_LIKE = I_LIKE;
1203
+ exports.IndexType = IndexType;
1167
1204
  exports.IntegerField = IntegerField;
1168
1205
  exports.JsonField = JsonField;
1169
1206
  exports.LIKE = LIKE;
@@ -1187,7 +1224,7 @@ exports.STRICT_WORD_SIMILARITY_DISTANCE = STRICT_WORD_SIMILARITY_DISTANCE;
1187
1224
  exports.SortType = SortType;
1188
1225
  exports.StringField = StringField;
1189
1226
  exports.SyntropixClient = SyntropixClient;
1190
- exports.SyntropixDBAccessType = SyntropixDBAccessType;
1227
+ exports.TableClient = TableClient;
1191
1228
  exports.TextField = TextField;
1192
1229
  exports.TimestampField = TimestampField;
1193
1230
  exports.UuidField = UuidField;