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