@syntropix/database 0.1.0 → 0.1.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 ADDED
@@ -0,0 +1,1199 @@
1
+ 'use strict';
2
+
3
+ var axios = require('axios');
4
+ require('reflect-metadata');
5
+
6
+ function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
7
+
8
+ var axios__default = /*#__PURE__*/_interopDefault(axios);
9
+
10
+ var __defProp = Object.defineProperty;
11
+ var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
12
+ var Client = class {
13
+ static {
14
+ __name(this, "Client");
15
+ }
16
+ config;
17
+ constructor(config) {
18
+ this.config = config;
19
+ }
20
+ headers() {
21
+ return {
22
+ "Content-Type": "application/json",
23
+ Authorization: this.config.apiKey || ""
24
+ };
25
+ }
26
+ url(path) {
27
+ if (path.startsWith("/")) {
28
+ path = path.slice(1);
29
+ }
30
+ return `${this.config.baseUrl}${path}`;
31
+ }
32
+ 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}`);
39
+ }
40
+ return response.data;
41
+ }
42
+ 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}`);
48
+ }
49
+ return response.data;
50
+ }
51
+ };
52
+
53
+ // src/core/config.ts
54
+ var ClientConfig = class {
55
+ static {
56
+ __name(this, "ClientConfig");
57
+ }
58
+ apiKey;
59
+ baseUrl;
60
+ timeout;
61
+ retries;
62
+ constructor(config) {
63
+ this.apiKey = config?.apiKey || process.env.SYNTROPIX_API_KEY;
64
+ this.baseUrl = config?.baseUrl || process.env.SYNTROPIX_API_URL;
65
+ this.timeout = config?.timeout || process.env.SYNTROPIX_API_TIMEOUT;
66
+ this.retries = config?.retries || process.env.SYNTROPIX_API_RETRIES;
67
+ if (!this.apiKey || !this.baseUrl) {
68
+ throw new Error("API key and base URL are required");
69
+ }
70
+ if (!this.timeout) {
71
+ this.timeout = 1e4;
72
+ }
73
+ if (!this.retries) {
74
+ this.retries = 3;
75
+ }
76
+ if (!this.baseUrl.endsWith("/")) {
77
+ this.baseUrl += "/";
78
+ }
79
+ }
80
+ };
81
+
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
+ })({});
117
+ var AND = /* @__PURE__ */ __name((...conditions) => conditions, "AND");
118
+ var OR = /* @__PURE__ */ __name((...conditions) => conditions, "OR");
119
+ var EQ = /* @__PURE__ */ __name((field, value) => ({
120
+ column: field,
121
+ operator: "EQ",
122
+ staticValue: value
123
+ }), "EQ");
124
+ var NE = /* @__PURE__ */ __name((field, value) => ({
125
+ column: field,
126
+ operator: "NEQ",
127
+ staticValue: value
128
+ }), "NE");
129
+ var GT = /* @__PURE__ */ __name((field, value) => ({
130
+ column: field,
131
+ operator: "GT",
132
+ staticValue: value
133
+ }), "GT");
134
+ var GTE = /* @__PURE__ */ __name((field, value) => ({
135
+ column: field,
136
+ operator: "GTE",
137
+ staticValue: value
138
+ }), "GTE");
139
+ var LT = /* @__PURE__ */ __name((field, value) => ({
140
+ column: field,
141
+ operator: "LT",
142
+ staticValue: value
143
+ }), "LT");
144
+ var LTE = /* @__PURE__ */ __name((field, value) => ({
145
+ column: field,
146
+ operator: "LTE",
147
+ staticValue: value
148
+ }), "LTE");
149
+ var IN = /* @__PURE__ */ __name((field, values) => ({
150
+ column: field,
151
+ operator: "In",
152
+ staticValue: values
153
+ }), "IN");
154
+ var CONTAINS = /* @__PURE__ */ __name((field, value) => ({
155
+ column: field,
156
+ operator: "Contains",
157
+ staticValue: value
158
+ }), "CONTAINS");
159
+ var OVERLAP = /* @__PURE__ */ __name((field, value) => ({
160
+ column: field,
161
+ operator: "Overlap",
162
+ staticValue: value
163
+ }), "OVERLAP");
164
+ var I_LIKE = /* @__PURE__ */ __name((field, pattern) => ({
165
+ column: field,
166
+ operator: "ILike",
167
+ staticValue: pattern
168
+ }), "I_LIKE");
169
+ var NOT_I_LIKE = /* @__PURE__ */ __name((field, pattern) => ({
170
+ column: field,
171
+ operator: "NotILike",
172
+ staticValue: pattern
173
+ }), "NOT_I_LIKE");
174
+ var NOT_IN = /* @__PURE__ */ __name((field, values) => ({
175
+ column: field,
176
+ operator: "NotIn",
177
+ staticValue: values
178
+ }), "NOT_IN");
179
+ var LIKE = /* @__PURE__ */ __name((field, pattern) => ({
180
+ column: field,
181
+ operator: "Like",
182
+ staticValue: pattern
183
+ }), "LIKE");
184
+ var NOT_LIKE = /* @__PURE__ */ __name((field, pattern) => ({
185
+ column: field,
186
+ operator: "NotLike",
187
+ staticValue: pattern
188
+ }), "NOT_LIKE");
189
+ var IS_NULL = /* @__PURE__ */ __name((field) => ({
190
+ column: field,
191
+ operator: "IsNull"
192
+ }), "IS_NULL");
193
+ var IS_NOT_NULL = /* @__PURE__ */ __name((field) => ({
194
+ column: field,
195
+ operator: "IsNotNull"
196
+ }), "IS_NOT_NULL");
197
+ var BETWEEN = /* @__PURE__ */ __name((field, min, max) => ({
198
+ column: field,
199
+ operator: "Between",
200
+ staticValue: [
201
+ min,
202
+ max
203
+ ]
204
+ }), "BETWEEN");
205
+ var EQ_COL = /* @__PURE__ */ __name((field, otherField) => ({
206
+ column: field,
207
+ operator: "EQ",
208
+ columnValue: otherField
209
+ }), "EQ_COL");
210
+ var NE_COL = /* @__PURE__ */ __name((field, otherField) => ({
211
+ column: field,
212
+ operator: "NEQ",
213
+ columnValue: otherField
214
+ }), "NE_COL");
215
+ var GT_COL = /* @__PURE__ */ __name((field, otherField) => ({
216
+ column: field,
217
+ operator: "GT",
218
+ columnValue: otherField
219
+ }), "GT_COL");
220
+ var GTE_COL = /* @__PURE__ */ __name((field, otherField) => ({
221
+ column: field,
222
+ operator: "GTE",
223
+ columnValue: otherField
224
+ }), "GTE_COL");
225
+ var LT_COL = /* @__PURE__ */ __name((field, otherField) => ({
226
+ column: field,
227
+ operator: "LT",
228
+ columnValue: otherField
229
+ }), "LT_COL");
230
+ var LTE_COL = /* @__PURE__ */ __name((field, otherField) => ({
231
+ column: field,
232
+ operator: "LTE",
233
+ columnValue: otherField
234
+ }), "LTE_COL");
235
+ var SIMILARITY = /* @__PURE__ */ __name((field, value, options) => ({
236
+ column: field,
237
+ operator: "Similarity",
238
+ staticValue: value,
239
+ simiarityOptions: options
240
+ }), "SIMILARITY");
241
+ var SIMILARITY_DISTANCE = /* @__PURE__ */ __name((field, value, options) => ({
242
+ column: field,
243
+ operator: "SimilarityDistance",
244
+ staticValue: value,
245
+ simiarityOptions: options
246
+ }), "SIMILARITY_DISTANCE");
247
+ var WORD_SIMILARITY = /* @__PURE__ */ __name((field, value, options) => ({
248
+ column: field,
249
+ operator: "WordSimilarity",
250
+ staticValue: value,
251
+ simiarityOptions: options
252
+ }), "WORD_SIMILARITY");
253
+ var WORD_SIMILARITY_DISTANCE = /* @__PURE__ */ __name((field, value, options) => ({
254
+ column: field,
255
+ operator: "WordSimilarityDistance",
256
+ staticValue: value,
257
+ simiarityOptions: options
258
+ }), "WORD_SIMILARITY_DISTANCE");
259
+ var STRICT_WORD_SIMILARITY = /* @__PURE__ */ __name((field, value, options) => ({
260
+ column: field,
261
+ operator: "StrictWordSimilarity",
262
+ staticValue: value,
263
+ simiarityOptions: options
264
+ }), "STRICT_WORD_SIMILARITY");
265
+ var STRICT_WORD_SIMILARITY_DISTANCE = /* @__PURE__ */ __name((field, value, options) => ({
266
+ column: field,
267
+ operator: "StrictWordSimilarityDistance",
268
+ staticValue: value,
269
+ simiarityOptions: options
270
+ }), "STRICT_WORD_SIMILARITY_DISTANCE");
271
+ var EUCLIDEAN_DISTANCE = /* @__PURE__ */ __name((field, value, options) => ({
272
+ column: field,
273
+ operator: "EuclideanDistance",
274
+ staticValue: value,
275
+ simiarityOptions: options
276
+ }), "EUCLIDEAN_DISTANCE");
277
+ var NEGATIVE_INNER_PRODUCT = /* @__PURE__ */ __name((field, value, options) => ({
278
+ column: field,
279
+ operator: "NegativeInnerProduct",
280
+ staticValue: value,
281
+ simiarityOptions: options
282
+ }), "NEGATIVE_INNER_PRODUCT");
283
+ var COSINE_DISTANCE = /* @__PURE__ */ __name((field, value, options) => ({
284
+ column: field,
285
+ operator: "CosineDistance",
286
+ staticValue: value,
287
+ simiarityOptions: options
288
+ }), "COSINE_DISTANCE");
289
+ var Value = /* @__PURE__ */ __name((value) => value, "Value");
290
+ function FilterWrapper(filter) {
291
+ if (filter === void 0) {
292
+ return void 0;
293
+ }
294
+ if (!Array.isArray(filter)) {
295
+ return OR(AND(filter));
296
+ } else if (!filter.every((item) => Array.isArray(item))) {
297
+ return OR(filter);
298
+ } else {
299
+ return filter;
300
+ }
301
+ }
302
+ __name(FilterWrapper, "FilterWrapper");
303
+
304
+ // src/core/data-client.ts
305
+ var DataClient = class {
306
+ static {
307
+ __name(this, "DataClient");
308
+ }
309
+ client;
310
+ constructor(config = new ClientConfig()) {
311
+ this.client = new Client(config);
312
+ }
313
+ 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);
319
+ }
320
+ 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);
326
+ }
327
+ 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);
333
+ }
334
+ async queryOne(data, model) {
335
+ data.query.filter = FilterWrapper(data.query.filter);
336
+ const response = await this.client.post("/query", {
337
+ One: data
338
+ });
339
+ if (response.status === "success") {
340
+ const responseData = response.data;
341
+ if (model !== void 0) {
342
+ return new model(responseData);
343
+ }
344
+ return responseData;
345
+ }
346
+ throw new Error(response.message);
347
+ }
348
+ async queryMany(data, model) {
349
+ data.query.filter = FilterWrapper(data.query.filter);
350
+ const response = await this.client.post("/query", {
351
+ Many: data
352
+ });
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;
359
+ }
360
+ throw new Error(response.message);
361
+ }
362
+ 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);
368
+ }
369
+ 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);
375
+ }
376
+ };
377
+
378
+ // src/core/table-client.ts
379
+ var TableClient = class {
380
+ static {
381
+ __name(this, "TableClient");
382
+ }
383
+ client;
384
+ constructor(config = new ClientConfig()) {
385
+ this.client = new Client(config);
386
+ }
387
+ 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);
393
+ }
394
+ 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);
400
+ }
401
+ 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);
407
+ }
408
+ 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);
414
+ }
415
+ 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);
421
+ }
422
+ 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);
428
+ }
429
+ 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);
435
+ }
436
+ 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);
442
+ }
443
+ 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);
449
+ }
450
+ };
451
+
452
+ // src/core/syntropix.ts
453
+ var SyntropixClient = class {
454
+ static {
455
+ __name(this, "SyntropixClient");
456
+ }
457
+ config;
458
+ _tableClient;
459
+ _dataClient;
460
+ constructor(config = new ClientConfig()) {
461
+ this.config = config;
462
+ }
463
+ get table() {
464
+ if (!this._tableClient) {
465
+ this._tableClient = new TableClient(this.config);
466
+ }
467
+ return this._tableClient;
468
+ }
469
+ get data() {
470
+ if (!this._dataClient) {
471
+ this._dataClient = new DataClient(this.config);
472
+ }
473
+ return this._dataClient;
474
+ }
475
+ };
476
+
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
497
+ var ColumnDataType = {
498
+ Integer: "Integer",
499
+ String: /* @__PURE__ */ __name((maxLength) => ({
500
+ String: maxLength
501
+ }), "String"),
502
+ Text: "Text",
503
+ Boolean: "Boolean",
504
+ DateTime: "DateTime",
505
+ Timestamp: "Timestamp",
506
+ Date: "Date",
507
+ Json: "Json",
508
+ Uuid: "Uuid",
509
+ Double: "Double",
510
+ Vector: /* @__PURE__ */ __name((dimensions) => ({
511
+ Vector: dimensions
512
+ }), "Vector"),
513
+ Array: /* @__PURE__ */ __name((dataType) => ({
514
+ Array: dataType
515
+ }), "Array"),
516
+ Enum: /* @__PURE__ */ __name((name, variants) => ({
517
+ Enum: {
518
+ name,
519
+ variants
520
+ }
521
+ }), "Enum"),
522
+ Money: /* @__PURE__ */ __name((precision, scale) => ({
523
+ Money: precision && scale ? [
524
+ precision,
525
+ scale
526
+ ] : null
527
+ }), "Money"),
528
+ Decimal: /* @__PURE__ */ __name((precision, scale) => ({
529
+ Decimal: precision && scale ? [
530
+ precision,
531
+ scale
532
+ ] : null
533
+ }), "Decimal")
534
+ };
535
+
536
+ // src/types/field.ts
537
+ var Field = class {
538
+ static {
539
+ __name(this, "Field");
540
+ }
541
+ name = "";
542
+ description = "";
543
+ columnType;
544
+ isPrimaryKey = false;
545
+ isNullable = false;
546
+ autoIncrement = false;
547
+ default;
548
+ defaultAccess;
549
+ constructor(columnType, options = {}) {
550
+ this.columnType = columnType;
551
+ this.name = options.name?.toLowerCase() || "";
552
+ this.description = options.description || "";
553
+ this.isPrimaryKey = options.isPrimaryKey || false;
554
+ this.isNullable = options.isNullable || false;
555
+ this.autoIncrement = options.autoIncrement || false;
556
+ this.default = options.default;
557
+ this.defaultAccess = options.defaultAccess;
558
+ }
559
+ into() {
560
+ return {
561
+ name: this.name,
562
+ columnType: this.columnType,
563
+ description: this.description,
564
+ isPrimaryKey: this.isPrimaryKey,
565
+ isNullable: this.isNullable,
566
+ autoIncrement: this.autoIncrement,
567
+ default: this.default,
568
+ defaultAccess: this.defaultAccess
569
+ };
570
+ }
571
+ };
572
+ var ForeignKeyField = class extends Field {
573
+ static {
574
+ __name(this, "ForeignKeyField");
575
+ }
576
+ tableName;
577
+ columnName;
578
+ onDelete;
579
+ onUpdate;
580
+ constructor(columnType, tableName, columnName, options = {}) {
581
+ super(columnType, options);
582
+ this.tableName = tableName;
583
+ this.columnName = columnName;
584
+ this.onDelete = options.onDelete || ForeignKeyAction.Cascade;
585
+ this.onUpdate = options.onUpdate || ForeignKeyAction.Cascade;
586
+ }
587
+ };
588
+ var StringField = class extends Field {
589
+ static {
590
+ __name(this, "StringField");
591
+ }
592
+ constructor(maxLength, options = {}) {
593
+ super(ColumnDataType.String(maxLength), options);
594
+ }
595
+ };
596
+ var TextField = class extends Field {
597
+ static {
598
+ __name(this, "TextField");
599
+ }
600
+ constructor(options = {}) {
601
+ super(ColumnDataType.Text, options);
602
+ }
603
+ };
604
+ var IntegerField = class extends Field {
605
+ static {
606
+ __name(this, "IntegerField");
607
+ }
608
+ constructor(options = {}) {
609
+ super(ColumnDataType.Integer, options);
610
+ }
611
+ };
612
+ var BooleanField = class extends Field {
613
+ static {
614
+ __name(this, "BooleanField");
615
+ }
616
+ constructor(options = {}) {
617
+ super(ColumnDataType.Boolean, options);
618
+ }
619
+ };
620
+ var DateTimeField = class extends Field {
621
+ static {
622
+ __name(this, "DateTimeField");
623
+ }
624
+ constructor(options = {}) {
625
+ super(ColumnDataType.DateTime, options);
626
+ }
627
+ };
628
+ var TimestampField = class extends Field {
629
+ static {
630
+ __name(this, "TimestampField");
631
+ }
632
+ constructor(options = {}) {
633
+ super(ColumnDataType.Timestamp, options);
634
+ }
635
+ };
636
+ var DateField = class extends Field {
637
+ static {
638
+ __name(this, "DateField");
639
+ }
640
+ constructor(options = {}) {
641
+ super(ColumnDataType.Date, options);
642
+ }
643
+ };
644
+ var JsonField = class extends Field {
645
+ static {
646
+ __name(this, "JsonField");
647
+ }
648
+ constructor(options = {}) {
649
+ super(ColumnDataType.Json, options);
650
+ }
651
+ };
652
+ var UuidField = class extends Field {
653
+ static {
654
+ __name(this, "UuidField");
655
+ }
656
+ constructor(options = {}) {
657
+ super(ColumnDataType.Uuid, options);
658
+ }
659
+ };
660
+ var VectorField = class extends Field {
661
+ static {
662
+ __name(this, "VectorField");
663
+ }
664
+ constructor(dimensions, options = {}) {
665
+ super(ColumnDataType.Vector(dimensions), options);
666
+ }
667
+ };
668
+ var ArrayField = class extends Field {
669
+ static {
670
+ __name(this, "ArrayField");
671
+ }
672
+ constructor(dataType, options = {}) {
673
+ super(ColumnDataType.Array(dataType), options);
674
+ }
675
+ };
676
+ var EnumField = class extends Field {
677
+ static {
678
+ __name(this, "EnumField");
679
+ }
680
+ constructor(name, variants, options = {}) {
681
+ super(ColumnDataType.Enum(name, variants), options);
682
+ }
683
+ };
684
+ var MoneyField = class extends Field {
685
+ static {
686
+ __name(this, "MoneyField");
687
+ }
688
+ constructor(precision, scale, options = {}) {
689
+ super(ColumnDataType.Money(precision, scale), options);
690
+ }
691
+ };
692
+ var DecimalField = class extends Field {
693
+ static {
694
+ __name(this, "DecimalField");
695
+ }
696
+ constructor(precision, scale, options = {}) {
697
+ super(ColumnDataType.Decimal(precision, scale), options);
698
+ }
699
+ };
700
+ var DoubleField = class extends Field {
701
+ static {
702
+ __name(this, "DoubleField");
703
+ }
704
+ constructor(options = {}) {
705
+ super(ColumnDataType.Double, options);
706
+ }
707
+ };
708
+ var FIELDS_KEY = Symbol("fields");
709
+ var TABLE_NAME_KEY = Symbol("tableName");
710
+ var DESCRIPTION_KEY = Symbol("description");
711
+ var INDEXES_KEY = Symbol("indexes");
712
+ function Column(options = {}) {
713
+ return function(target, propertyKey) {
714
+ const fields = Reflect.getMetadata(FIELDS_KEY, target.constructor) || {};
715
+ const fieldName = options.name || propertyKey.toLowerCase();
716
+ const columnType = options.type || ColumnDataType.Text;
717
+ const field = new class extends Field {
718
+ constructor() {
719
+ super(columnType, {
720
+ name: fieldName,
721
+ description: options.description,
722
+ isPrimaryKey: options.primary,
723
+ isNullable: options.nullable,
724
+ autoIncrement: options.autoIncrement,
725
+ default: options.default
726
+ });
727
+ }
728
+ }();
729
+ fields[propertyKey] = field;
730
+ Reflect.defineMetadata(FIELDS_KEY, fields, target.constructor);
731
+ };
732
+ }
733
+ __name(Column, "Column");
734
+ function ForeignKey(tableName, columnName, options = {}) {
735
+ return function(target, propertyKey) {
736
+ const fields = Reflect.getMetadata(FIELDS_KEY, target.constructor) || {};
737
+ const field = new ForeignKeyField(options.type || ColumnDataType.Integer, tableName, columnName, {
738
+ onDelete: options.onDelete,
739
+ onUpdate: options.onUpdate,
740
+ description: options.description,
741
+ isNullable: options.nullable,
742
+ default: options.default
743
+ });
744
+ field.name = options.name || propertyKey.toLowerCase();
745
+ fields[propertyKey] = field;
746
+ Reflect.defineMetadata(FIELDS_KEY, fields, target.constructor);
747
+ };
748
+ }
749
+ __name(ForeignKey, "ForeignKey");
750
+ var BaseModel = class {
751
+ static {
752
+ __name(this, "BaseModel");
753
+ }
754
+ _client;
755
+ _extra = {};
756
+ constructor(data = {}) {
757
+ const fields = this.getFields();
758
+ const columnToPropertyMap = {};
759
+ for (const [propertyName, field] of Object.entries(fields)) {
760
+ columnToPropertyMap[field.name] = propertyName;
761
+ }
762
+ for (const [key, value] of Object.entries(data)) {
763
+ if (key in fields) {
764
+ this[key] = value;
765
+ } else if (key in columnToPropertyMap) {
766
+ this[columnToPropertyMap[key]] = value;
767
+ } else {
768
+ this._extra[key] = value;
769
+ }
770
+ }
771
+ }
772
+ // Static metadata getters
773
+ static getTableName() {
774
+ const tableName = Reflect.getMetadata(TABLE_NAME_KEY, this);
775
+ if (tableName) return tableName;
776
+ if ("tableName" in this && typeof this.tableName === "string") {
777
+ return this.tableName;
778
+ }
779
+ return this.name.toLowerCase();
780
+ }
781
+ static getDescription() {
782
+ const metadataDescription = Reflect.getMetadata(DESCRIPTION_KEY, this);
783
+ if (metadataDescription) return metadataDescription;
784
+ if ("description" in this && typeof this.description === "string") {
785
+ return this.description;
786
+ }
787
+ return "";
788
+ }
789
+ static getIndexes() {
790
+ return Reflect.getMetadata(INDEXES_KEY, this) || [];
791
+ }
792
+ static getFields() {
793
+ const fields = Reflect.getMetadata(FIELDS_KEY, this) || {};
794
+ let hasPrimaryKey = false;
795
+ for (const field of Object.values(fields)) {
796
+ if (field.isPrimaryKey) {
797
+ hasPrimaryKey = true;
798
+ field.isNullable = false;
799
+ break;
800
+ }
801
+ }
802
+ if (!hasPrimaryKey && !("id" in fields)) {
803
+ fields["id"] = new IntegerField({
804
+ isPrimaryKey: true,
805
+ isNullable: false,
806
+ autoIncrement: true,
807
+ description: "Primary key"
808
+ });
809
+ fields["id"].name = "id";
810
+ }
811
+ return fields;
812
+ }
813
+ static getPrimaryKeyName() {
814
+ const fields = this.getFields();
815
+ for (const [key, field] of Object.entries(fields)) {
816
+ if (field.isPrimaryKey) {
817
+ return field.name;
818
+ }
819
+ }
820
+ return "id";
821
+ }
822
+ static getAssociations() {
823
+ const fields = this.getFields();
824
+ return Object.values(fields).filter((f) => f instanceof ForeignKeyField);
825
+ }
826
+ // Instance metadata getters
827
+ getFields() {
828
+ return this.constructor.getFields();
829
+ }
830
+ getTableName() {
831
+ return this.constructor.getTableName();
832
+ }
833
+ getPrimaryKeyName() {
834
+ return this.constructor.getPrimaryKeyName();
835
+ }
836
+ getPrimaryKey() {
837
+ const fields = this.getFields();
838
+ for (const field of Object.values(fields)) {
839
+ if (field.isPrimaryKey) {
840
+ return field;
841
+ }
842
+ }
843
+ return void 0;
844
+ }
845
+ // Client getter
846
+ get client() {
847
+ if (!this._client) {
848
+ this._client = new SyntropixClient(new ClientConfig());
849
+ }
850
+ return this._client;
851
+ }
852
+ // Table operations
853
+ static async createTable(_client) {
854
+ const fields = this.getFields();
855
+ const columns = Object.values(fields).map((f) => f.into());
856
+ const foreignKeys = this.getAssociations().map((f) => ({
857
+ fromTableName: this.getTableName(),
858
+ fromColumnName: f.name,
859
+ toTableName: f.tableName,
860
+ toColumnName: f.columnName,
861
+ onDelete: f.onDelete,
862
+ onUpdate: f.onUpdate
863
+ }));
864
+ const indexes = this.getIndexes();
865
+ const client = _client || new SyntropixClient(new ClientConfig());
866
+ return client.table.createTable({
867
+ name: this.getTableName(),
868
+ description: this.getDescription() || "No description",
869
+ columns,
870
+ foreignKeys,
871
+ indexes
872
+ });
873
+ }
874
+ static async dropTable(_client) {
875
+ const client = _client || new SyntropixClient(new ClientConfig());
876
+ return client.table.dropTable({
877
+ tableName: this.getTableName()
878
+ });
879
+ }
880
+ static async renameTable(newName, _client) {
881
+ const client = _client || new SyntropixClient(new ClientConfig());
882
+ return client.table.renameTable({
883
+ tableName: this.getTableName(),
884
+ newTableName: newName
885
+ });
886
+ }
887
+ static async truncateTable(_client) {
888
+ const client = _client || new SyntropixClient(new ClientConfig());
889
+ return client.table.truncateTable({
890
+ tableName: this.getTableName()
891
+ });
892
+ }
893
+ // Data operations
894
+ static async create(data, _client) {
895
+ const model = this;
896
+ const fields = model.getFields();
897
+ const columns = [];
898
+ const values = [];
899
+ for (const [key, value] of Object.entries(data)) {
900
+ if (key in fields) {
901
+ columns.push(fields[key].name);
902
+ values.push(value);
903
+ } else {
904
+ throw new Error(`Invalid field: ${key}`);
905
+ }
906
+ }
907
+ const client = _client || new SyntropixClient(new ClientConfig());
908
+ return client.data.insertOne({
909
+ tableName: model.getTableName(),
910
+ data: {
911
+ columns,
912
+ values: [
913
+ values
914
+ ]
915
+ }
916
+ });
917
+ }
918
+ async save(_client) {
919
+ const pk = this.getPrimaryKey();
920
+ const pkValue = this[this.getPrimaryKeyName()];
921
+ const fields = this.getFields();
922
+ if (pkValue === null || pkValue === void 0) {
923
+ const data = {};
924
+ for (const [key, field] of Object.entries(fields)) {
925
+ data[key] = this[key];
926
+ }
927
+ return this.constructor.create(data, _client);
928
+ }
929
+ const columns = [];
930
+ const values = [];
931
+ for (const [key, field] of Object.entries(fields)) {
932
+ if (field.name !== pk?.name) {
933
+ columns.push(field.name);
934
+ values.push(this[key]);
935
+ }
936
+ }
937
+ return _client || this.client.data.updateByPrimaryKey(pkValue, {
938
+ tableName: this.getTableName(),
939
+ payload: {
940
+ filter: [
941
+ []
942
+ ],
943
+ columns,
944
+ values
945
+ }
946
+ });
947
+ }
948
+ async remove(filter, _client) {
949
+ const pk = this.getPrimaryKey();
950
+ const pkValue = this[this.getPrimaryKeyName()];
951
+ const finalFilter = filter || OR(AND(EQ(pk?.name || "id", pkValue)));
952
+ return _client || this.client.data.deleteData({
953
+ tableName: this.getTableName(),
954
+ payload: {
955
+ filter: finalFilter
956
+ }
957
+ });
958
+ }
959
+ static async bulkCreate(datas, batchSize = 32, _client) {
960
+ if (!datas.length) return 0;
961
+ if (batchSize <= 0) throw new Error("Batch size must be greater than 0");
962
+ const model = this;
963
+ const fields = model.getFields();
964
+ let cnt = 0;
965
+ batchSize = Math.min(batchSize, 128);
966
+ const columns = Object.keys(datas[0]);
967
+ const columnsSet = new Set(columns);
968
+ for (const col of columns) {
969
+ if (!(col in fields)) {
970
+ throw new Error(`Invalid field: ${col}`);
971
+ }
972
+ }
973
+ let values = [];
974
+ const client = _client || new SyntropixClient(new ClientConfig());
975
+ for (const data of datas) {
976
+ if (columnsSet.size !== new Set(Object.keys(data)).size || !columns.every((c) => c in data)) {
977
+ throw new Error("All data must have the same columns");
978
+ }
979
+ values.push(columns.map((c) => data[c]));
980
+ if (values.length === batchSize) {
981
+ cnt += await client.data.insertData({
982
+ tableName: model.getTableName(),
983
+ data: {
984
+ columns: columns.map((c) => fields[c].name),
985
+ values
986
+ }
987
+ });
988
+ values = [];
989
+ }
990
+ }
991
+ if (values.length > 0) {
992
+ cnt += await client.data.insertData({
993
+ tableName: model.getTableName(),
994
+ data: {
995
+ columns: columns.map((c) => fields[c].name),
996
+ values
997
+ }
998
+ });
999
+ }
1000
+ return cnt;
1001
+ }
1002
+ static async update(filter, data, _client) {
1003
+ const model = this;
1004
+ const fields = model.getFields();
1005
+ const columns = Object.keys(data);
1006
+ if (!columns.length) {
1007
+ throw new Error("No columns to update");
1008
+ }
1009
+ for (const col of columns) {
1010
+ if (!(col in fields)) {
1011
+ throw new Error(`Invalid field: ${col}`);
1012
+ }
1013
+ }
1014
+ const values = columns.map((c) => data[c]);
1015
+ const client = _client || new SyntropixClient(new ClientConfig());
1016
+ return client.data.updateData({
1017
+ tableName: model.getTableName(),
1018
+ payload: {
1019
+ filter,
1020
+ columns: columns.map((c) => fields[c].name),
1021
+ values
1022
+ }
1023
+ });
1024
+ }
1025
+ static async delete(filter, _client) {
1026
+ const model = this;
1027
+ const client = _client || new SyntropixClient(new ClientConfig());
1028
+ return client.data.deleteData({
1029
+ tableName: model.getTableName(),
1030
+ payload: {
1031
+ filter
1032
+ }
1033
+ });
1034
+ }
1035
+ static async get(filter, select, _client) {
1036
+ const model = this;
1037
+ const fields = model.getFields();
1038
+ const client = _client || new SyntropixClient(new ClientConfig());
1039
+ const data = await client.data.queryOne({
1040
+ tableName: model.getTableName(),
1041
+ query: {
1042
+ filter,
1043
+ select: select || Object.values(fields).map((f) => f.name),
1044
+ limit: 1
1045
+ }
1046
+ });
1047
+ return new this(data);
1048
+ }
1049
+ static async filter(options) {
1050
+ const model = this;
1051
+ const client = options._client || new SyntropixClient(new ClientConfig());
1052
+ const data = await client.data.queryMany({
1053
+ tableName: model.getTableName(),
1054
+ query: {
1055
+ filter: options.filter,
1056
+ sort: options.sort,
1057
+ aggregate: options.aggregate,
1058
+ join: options.join ? [
1059
+ options.join
1060
+ ] : void 0,
1061
+ limit: options.limit,
1062
+ offset: options.offset,
1063
+ groupBy: options.groupBy,
1064
+ select: options.select
1065
+ }
1066
+ });
1067
+ return data.map((item) => new this(item));
1068
+ }
1069
+ static async count(options = {}) {
1070
+ const model = this;
1071
+ const client = options._client || new SyntropixClient(new ClientConfig());
1072
+ const data = await client.data.queryMany({
1073
+ tableName: model.getTableName(),
1074
+ query: {
1075
+ filter: options.filter || [
1076
+ [
1077
+ GTE("id", Value(0))
1078
+ ]
1079
+ ],
1080
+ aggregate: [
1081
+ {
1082
+ column: "id",
1083
+ function: AggregateFunction.Count,
1084
+ alias: "count"
1085
+ }
1086
+ ],
1087
+ join: options.join ? [
1088
+ options.join
1089
+ ] : void 0,
1090
+ limit: 1,
1091
+ offset: 0,
1092
+ groupBy: options.groupBy,
1093
+ select: [
1094
+ "count"
1095
+ ]
1096
+ }
1097
+ });
1098
+ return data.length > 0 ? data[0].count : 0;
1099
+ }
1100
+ toString() {
1101
+ const fields = this.getFields();
1102
+ const data = {};
1103
+ for (const key of Object.keys(fields)) {
1104
+ const value = this[key];
1105
+ if (!(value instanceof Field)) {
1106
+ data[key] = value;
1107
+ }
1108
+ }
1109
+ data._extra = this._extra;
1110
+ return `${this.constructor.name}(${JSON.stringify(data).slice(1, -1)})`;
1111
+ }
1112
+ toJSON() {
1113
+ const fields = this.getFields();
1114
+ const data = {};
1115
+ for (const key of Object.keys(fields)) {
1116
+ data[key] = this[key];
1117
+ }
1118
+ return {
1119
+ ...data,
1120
+ ...this._extra
1121
+ };
1122
+ }
1123
+ };
1124
+
1125
+ // src/types/requests.ts
1126
+ var SyntropixDBAccessType = {
1127
+ None: "None",
1128
+ Read: "Read",
1129
+ Write: "Write",
1130
+ Admin: "Admin"
1131
+ };
1132
+
1133
+ exports.AND = AND;
1134
+ exports.AggregateFunction = AggregateFunction;
1135
+ exports.ArrayField = ArrayField;
1136
+ exports.BETWEEN = BETWEEN;
1137
+ exports.BaseModel = BaseModel;
1138
+ exports.BooleanField = BooleanField;
1139
+ exports.CONTAINS = CONTAINS;
1140
+ exports.COSINE_DISTANCE = COSINE_DISTANCE;
1141
+ exports.Client = Client;
1142
+ exports.ClientConfig = ClientConfig;
1143
+ exports.Column = Column;
1144
+ exports.ColumnDataType = ColumnDataType;
1145
+ exports.DateField = DateField;
1146
+ exports.DateTimeField = DateTimeField;
1147
+ exports.DecimalField = DecimalField;
1148
+ exports.DoubleField = DoubleField;
1149
+ exports.EQ = EQ;
1150
+ exports.EQ_COL = EQ_COL;
1151
+ exports.EUCLIDEAN_DISTANCE = EUCLIDEAN_DISTANCE;
1152
+ exports.EnumField = EnumField;
1153
+ exports.Field = Field;
1154
+ exports.FilterOperation = FilterOperation;
1155
+ exports.FilterWrapper = FilterWrapper;
1156
+ exports.ForeignKey = ForeignKey;
1157
+ exports.ForeignKeyAction = ForeignKeyAction;
1158
+ exports.ForeignKeyField = ForeignKeyField;
1159
+ exports.GT = GT;
1160
+ exports.GTE = GTE;
1161
+ exports.GTE_COL = GTE_COL;
1162
+ exports.GT_COL = GT_COL;
1163
+ exports.IN = IN;
1164
+ exports.IS_NOT_NULL = IS_NOT_NULL;
1165
+ exports.IS_NULL = IS_NULL;
1166
+ exports.I_LIKE = I_LIKE;
1167
+ exports.IntegerField = IntegerField;
1168
+ exports.JsonField = JsonField;
1169
+ exports.LIKE = LIKE;
1170
+ exports.LT = LT;
1171
+ exports.LTE = LTE;
1172
+ exports.LTE_COL = LTE_COL;
1173
+ exports.LT_COL = LT_COL;
1174
+ exports.MoneyField = MoneyField;
1175
+ exports.NE = NE;
1176
+ exports.NEGATIVE_INNER_PRODUCT = NEGATIVE_INNER_PRODUCT;
1177
+ exports.NE_COL = NE_COL;
1178
+ exports.NOT_IN = NOT_IN;
1179
+ exports.NOT_I_LIKE = NOT_I_LIKE;
1180
+ exports.NOT_LIKE = NOT_LIKE;
1181
+ exports.OR = OR;
1182
+ exports.OVERLAP = OVERLAP;
1183
+ exports.SIMILARITY = SIMILARITY;
1184
+ exports.SIMILARITY_DISTANCE = SIMILARITY_DISTANCE;
1185
+ exports.STRICT_WORD_SIMILARITY = STRICT_WORD_SIMILARITY;
1186
+ exports.STRICT_WORD_SIMILARITY_DISTANCE = STRICT_WORD_SIMILARITY_DISTANCE;
1187
+ exports.SortType = SortType;
1188
+ exports.StringField = StringField;
1189
+ exports.SyntropixClient = SyntropixClient;
1190
+ exports.SyntropixDBAccessType = SyntropixDBAccessType;
1191
+ exports.TextField = TextField;
1192
+ exports.TimestampField = TimestampField;
1193
+ exports.UuidField = UuidField;
1194
+ exports.Value = Value;
1195
+ exports.VectorField = VectorField;
1196
+ exports.WORD_SIMILARITY = WORD_SIMILARITY;
1197
+ exports.WORD_SIMILARITY_DISTANCE = WORD_SIMILARITY_DISTANCE;
1198
+ //# sourceMappingURL=index.cjs.map
1199
+ //# sourceMappingURL=index.cjs.map