@syntropix/database 0.0.2 → 0.0.4
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/.env +1 -1
- package/.vscode/settings.json +1 -8
- package/dist/core/dataClient.d.ts +1 -1
- package/dist/core/dataClient.js +2 -6
- package/dist/core/tableClient.d.ts +3 -2
- package/dist/core/tableClient.js +2 -1
- package/dist/types/basemodel.d.ts +3 -2
- package/dist/types/dto/base.d.ts +4 -0
- package/dist/types/dto/base.js +1 -0
- package/dist/types/dto/table.d.ts +22 -0
- package/dist/types/dto/table.js +1 -0
- package/dist/types/filter.d.ts +8 -0
- package/dist/types/filter.js +24 -0
- package/dist/types/requests.d.ts +2 -41
- package/eslint.config.mjs +23 -23
- package/examples/tsconfig.json +13 -0
- package/examples/usage.ts +73 -52
- package/jest.config.ts +9 -10
- package/package.json +4 -4
- package/src/core/dataClient.ts +1 -6
- package/src/core/tableClient.ts +3 -2
- package/src/types/basemodel.ts +3 -2
- package/src/types/common.ts +83 -83
- package/src/types/dto/base.ts +4 -0
- package/src/types/dto/table.ts +21 -0
- package/src/types/filter.ts +28 -1
- package/src/types/requests.ts +2 -52
- package/tests/basic.test.ts +52 -34
- package/tests/tsconfig.json +8 -0
- package/tsconfig.json +18 -21
- package/examples/advanced-usage.d.ts +0 -42
- package/examples/advanced-usage.d.ts.map +0 -1
- package/examples/advanced-usage.js +0 -257
- package/examples/advanced-usage.js.map +0 -1
- package/examples/usage.d.ts +0 -11
- package/examples/usage.d.ts.map +0 -1
- package/examples/usage.js +0 -91
- package/examples/usage.js.map +0 -1
- package/tsconfig.base.json +0 -17
- package/tsconfig.test.json +0 -13
package/.env
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
SYNTROPIX_API_KEY = "
|
|
1
|
+
SYNTROPIX_API_KEY = "MrxxQ9nzk9LNSHMTBLGP1YpTVr+sT+sldRYASP0r/v7qPB16Qaksv3GU3ns/viLTyHDVgh1dN1ihtns+GXPu8pR4CjUuqpjXaowI5glQt2VbDXdScRQOkBs4YfJ9HBCts+Ri/32QYeOIjtWYWh52+349zi34rA91btmDw/0yzSv4mG9esygDm2zCSSvSEh/XZY1VhC+97/gxXwKOEVwINI2Twd/DOV3zvmQxfY733Ono1+2qWbLMLY/0rEKDx16eyRlud1PwauZXKzA1VdVpxC6O1RLwVXkfS7LNkr5QxhaWPdQWcbeJ9fPboFFbGNzICs5hBTEBDGnqbSnqwD6CfsqqgVpl5YM1DGzQyfvjb88="
|
|
2
2
|
SYNTROPIX_API_URL = "http://149.248.14.64:8080"
|
package/.vscode/settings.json
CHANGED
|
@@ -2,7 +2,7 @@ import { DeleteData, Insert, Query, Update } from '../types/requests';
|
|
|
2
2
|
import { ClientConfig } from './config';
|
|
3
3
|
export declare class DataClient {
|
|
4
4
|
private client;
|
|
5
|
-
constructor(config
|
|
5
|
+
constructor(config?: ClientConfig);
|
|
6
6
|
insertData(data: Insert): Promise<any>;
|
|
7
7
|
insertOne(data: Insert): Promise<any>;
|
|
8
8
|
updateByPrimaryKey(pk: string, data: Update): Promise<any>;
|
package/dist/core/dataClient.js
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import { Client } from './client';
|
|
2
|
+
import { ClientConfig } from './config';
|
|
2
3
|
export class DataClient {
|
|
3
4
|
client;
|
|
4
|
-
constructor(config) {
|
|
5
|
+
constructor(config = new ClientConfig()) {
|
|
5
6
|
this.client = new Client(config);
|
|
6
7
|
}
|
|
7
8
|
async insertData(data) {
|
|
@@ -19,8 +20,6 @@ export class DataClient {
|
|
|
19
20
|
throw new Error(response.message);
|
|
20
21
|
}
|
|
21
22
|
async updateByPrimaryKey(pk, data) {
|
|
22
|
-
console.log(pk);
|
|
23
|
-
console.log(data);
|
|
24
23
|
const response = await this.client.post(`/update/${pk}`, data);
|
|
25
24
|
if (response.status === 'success') {
|
|
26
25
|
return response.data;
|
|
@@ -32,9 +31,6 @@ export class DataClient {
|
|
|
32
31
|
if (response.status === 'success') {
|
|
33
32
|
const responseData = response.data;
|
|
34
33
|
if (model !== undefined) {
|
|
35
|
-
// In TypeScript, we would typically use a validation library or manual validation
|
|
36
|
-
// For now, we'll return the data as-is, but in a real implementation you might want to use
|
|
37
|
-
// a library like class-validator or zod for validation
|
|
38
34
|
return new model(responseData);
|
|
39
35
|
}
|
|
40
36
|
return responseData;
|
|
@@ -1,10 +1,11 @@
|
|
|
1
|
+
import { TableCreateResponse } from '@/types/dto/table';
|
|
1
2
|
import { SyntropixDBTable } from '../types/common';
|
|
2
3
|
import { TableAddColumn, TableCreate, TableDrop, TableDropColumn, TableGetSchema, TableModifyColumn, TableRename, TableTruncate } from '../types/requests';
|
|
3
4
|
import { ClientConfig } from './config';
|
|
4
5
|
export declare class TableClient {
|
|
5
6
|
private client;
|
|
6
|
-
constructor(config
|
|
7
|
-
createTable(data: TableCreate): Promise<
|
|
7
|
+
constructor(config?: ClientConfig);
|
|
8
|
+
createTable(data: TableCreate): Promise<TableCreateResponse>;
|
|
8
9
|
dropTable(data: TableDrop): Promise<any>;
|
|
9
10
|
renameTable(data: TableRename): Promise<any>;
|
|
10
11
|
truncateTable(data: TableTruncate): Promise<any>;
|
package/dist/core/tableClient.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import 'reflect-metadata';
|
|
2
2
|
import { SyntropixClient } from '../core/syntropix';
|
|
3
3
|
import { Aggregate, GroupBy, Index, Join, Sort } from './common';
|
|
4
|
+
import { TableCreateResponse } from './dto/table';
|
|
4
5
|
import { Field, ForeignKeyField } from './field';
|
|
5
6
|
import { Filter } from './filter';
|
|
6
7
|
export declare function Column(options?: {
|
|
@@ -37,7 +38,7 @@ export declare class BaseModel {
|
|
|
37
38
|
protected getPrimaryKeyName(): string;
|
|
38
39
|
protected getPrimaryKey(): Field | undefined;
|
|
39
40
|
protected get client(): SyntropixClient;
|
|
40
|
-
static createTable(_client?: SyntropixClient | null): Promise<
|
|
41
|
+
static createTable(_client?: SyntropixClient | null): Promise<TableCreateResponse>;
|
|
41
42
|
static dropTable(_client?: SyntropixClient | null): Promise<any>;
|
|
42
43
|
static renameTable(newName: string, _client?: SyntropixClient | null): Promise<any>;
|
|
43
44
|
static truncateTable(_client?: SyntropixClient | null): Promise<any>;
|
|
@@ -50,7 +51,7 @@ export declare class BaseModel {
|
|
|
50
51
|
static get<T extends BaseModel>(this: new (data?: any) => T, filter: Filter, select?: string[], _client?: SyntropixClient | null): Promise<T>;
|
|
51
52
|
static filter<T extends BaseModel>(this: new (data?: any) => T, options: {
|
|
52
53
|
filter: Filter;
|
|
53
|
-
sort?: Sort;
|
|
54
|
+
sort?: Sort[];
|
|
54
55
|
aggregate?: Aggregate[];
|
|
55
56
|
join?: Join;
|
|
56
57
|
limit?: number;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { Column, ForeignKey, Index } from '../common';
|
|
2
|
+
export interface TableCreateResponse {
|
|
3
|
+
_id: {
|
|
4
|
+
$oid: string;
|
|
5
|
+
};
|
|
6
|
+
table: {
|
|
7
|
+
id: string;
|
|
8
|
+
name: string;
|
|
9
|
+
description: string;
|
|
10
|
+
created_at: any;
|
|
11
|
+
columns: Column[];
|
|
12
|
+
foreign_keys: ForeignKey[];
|
|
13
|
+
indexes: Index[];
|
|
14
|
+
schema: string;
|
|
15
|
+
default_access: string | null;
|
|
16
|
+
};
|
|
17
|
+
created_at: any;
|
|
18
|
+
updated_at: any;
|
|
19
|
+
created_by: string;
|
|
20
|
+
metadata: Record<string, any>;
|
|
21
|
+
triggers: any[];
|
|
22
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/dist/types/filter.d.ts
CHANGED
|
@@ -16,9 +16,13 @@ export declare enum FilterOperation {
|
|
|
16
16
|
NEQ = "NEQ",
|
|
17
17
|
Between = "Between",
|
|
18
18
|
In = "In",
|
|
19
|
+
Contains = "Contains",
|
|
20
|
+
Overlap = "Overlap",
|
|
19
21
|
NotIn = "NotIn",
|
|
20
22
|
Like = "Like",
|
|
21
23
|
NotLike = "NotLike",
|
|
24
|
+
ILike = "ILike",
|
|
25
|
+
NotILike = "NotILike",
|
|
22
26
|
IsNull = "IsNull",
|
|
23
27
|
IsNotNull = "IsNotNull",
|
|
24
28
|
Similarity = "Similarity",
|
|
@@ -51,6 +55,10 @@ export declare const GTE: (field: string, value: any) => SyntropixDBFilterItem;
|
|
|
51
55
|
export declare const LT: (field: string, value: any) => SyntropixDBFilterItem;
|
|
52
56
|
export declare const LTE: (field: string, value: any) => SyntropixDBFilterItem;
|
|
53
57
|
export declare const IN: (field: string, values: any[]) => SyntropixDBFilterItem;
|
|
58
|
+
export declare const CONTAINS: (field: string, value: any) => SyntropixDBFilterItem;
|
|
59
|
+
export declare const OVERLAP: (field: string, value: any) => SyntropixDBFilterItem;
|
|
60
|
+
export declare const I_LIKE: (field: string, pattern: string) => SyntropixDBFilterItem;
|
|
61
|
+
export declare const NOT_I_LIKE: (field: string, pattern: string) => SyntropixDBFilterItem;
|
|
54
62
|
export declare const NOT_IN: (field: string, values: any[]) => SyntropixDBFilterItem;
|
|
55
63
|
export declare const LIKE: (field: string, pattern: string) => SyntropixDBFilterItem;
|
|
56
64
|
export declare const NOT_LIKE: (field: string, pattern: string) => SyntropixDBFilterItem;
|
package/dist/types/filter.js
CHANGED
|
@@ -14,9 +14,13 @@ export var FilterOperation;
|
|
|
14
14
|
FilterOperation["NEQ"] = "NEQ";
|
|
15
15
|
FilterOperation["Between"] = "Between";
|
|
16
16
|
FilterOperation["In"] = "In";
|
|
17
|
+
FilterOperation["Contains"] = "Contains";
|
|
18
|
+
FilterOperation["Overlap"] = "Overlap";
|
|
17
19
|
FilterOperation["NotIn"] = "NotIn";
|
|
18
20
|
FilterOperation["Like"] = "Like";
|
|
19
21
|
FilterOperation["NotLike"] = "NotLike";
|
|
22
|
+
FilterOperation["ILike"] = "ILike";
|
|
23
|
+
FilterOperation["NotILike"] = "NotILike";
|
|
20
24
|
FilterOperation["IsNull"] = "IsNull";
|
|
21
25
|
FilterOperation["IsNotNull"] = "IsNotNull";
|
|
22
26
|
FilterOperation["Similarity"] = "Similarity";
|
|
@@ -67,6 +71,26 @@ export const IN = (field, values) => ({
|
|
|
67
71
|
operator: FilterOperation.In,
|
|
68
72
|
static_value: values,
|
|
69
73
|
});
|
|
74
|
+
export const CONTAINS = (field, value) => ({
|
|
75
|
+
column: field,
|
|
76
|
+
operator: FilterOperation.Contains,
|
|
77
|
+
static_value: value,
|
|
78
|
+
});
|
|
79
|
+
export const OVERLAP = (field, value) => ({
|
|
80
|
+
column: field,
|
|
81
|
+
operator: FilterOperation.Overlap,
|
|
82
|
+
static_value: value,
|
|
83
|
+
});
|
|
84
|
+
export const I_LIKE = (field, pattern) => ({
|
|
85
|
+
column: field,
|
|
86
|
+
operator: FilterOperation.ILike,
|
|
87
|
+
static_value: pattern,
|
|
88
|
+
});
|
|
89
|
+
export const NOT_I_LIKE = (field, pattern) => ({
|
|
90
|
+
column: field,
|
|
91
|
+
operator: FilterOperation.NotILike,
|
|
92
|
+
static_value: pattern,
|
|
93
|
+
});
|
|
70
94
|
export const NOT_IN = (field, values) => ({
|
|
71
95
|
column: field,
|
|
72
96
|
operator: FilterOperation.NotIn,
|
package/dist/types/requests.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Aggregate, Column, ForeignKey, GroupBy, Index, Join, Sort } from './common';
|
|
2
|
-
import { Filter
|
|
2
|
+
import { Filter } from './filter';
|
|
3
3
|
export interface TableCreate {
|
|
4
4
|
name: string;
|
|
5
5
|
description?: string;
|
|
@@ -58,7 +58,7 @@ export interface DeleteData {
|
|
|
58
58
|
}
|
|
59
59
|
export interface QueryPayload {
|
|
60
60
|
filter?: Filter;
|
|
61
|
-
sort?: Sort;
|
|
61
|
+
sort?: Sort[];
|
|
62
62
|
aggregate?: Aggregate[];
|
|
63
63
|
join?: Join[];
|
|
64
64
|
limit?: number;
|
|
@@ -70,42 +70,3 @@ export interface Query {
|
|
|
70
70
|
table_name: string;
|
|
71
71
|
query: QueryPayload;
|
|
72
72
|
}
|
|
73
|
-
export interface SyntropixDBSortItem {
|
|
74
|
-
column: string;
|
|
75
|
-
direction: SortType;
|
|
76
|
-
}
|
|
77
|
-
export type SyntropixDBSort = SyntropixDBSortItem[];
|
|
78
|
-
export interface SyntropixDBJoin {
|
|
79
|
-
table: string;
|
|
80
|
-
on: string;
|
|
81
|
-
}
|
|
82
|
-
export interface SyntropixDBAggregate {
|
|
83
|
-
function: string;
|
|
84
|
-
column: string;
|
|
85
|
-
alias: string;
|
|
86
|
-
}
|
|
87
|
-
export type SyntropixDBGroupBy = string[];
|
|
88
|
-
export type SyntropixDBDistinct = string[];
|
|
89
|
-
export interface SyntropixDBQuery {
|
|
90
|
-
filter: SyntropixDBFilter;
|
|
91
|
-
sort?: SyntropixDBSort;
|
|
92
|
-
limit?: number;
|
|
93
|
-
offset?: number;
|
|
94
|
-
select?: string[];
|
|
95
|
-
join?: SyntropixDBJoin[];
|
|
96
|
-
aggregate?: SyntropixDBAggregate[];
|
|
97
|
-
group_by?: SyntropixDBGroupBy;
|
|
98
|
-
distinct?: SyntropixDBDistinct;
|
|
99
|
-
}
|
|
100
|
-
export interface SyntropixDBInsert {
|
|
101
|
-
columns: string[];
|
|
102
|
-
values: any[][];
|
|
103
|
-
}
|
|
104
|
-
export interface SyntropixDBUpdate {
|
|
105
|
-
filter: SyntropixDBFilter;
|
|
106
|
-
columns: string[];
|
|
107
|
-
values: any[];
|
|
108
|
-
}
|
|
109
|
-
export interface SyntropixDBDelete {
|
|
110
|
-
filter: SyntropixDBFilter;
|
|
111
|
-
}
|
package/eslint.config.mjs
CHANGED
|
@@ -1,23 +1,23 @@
|
|
|
1
|
-
import eslint from '@eslint/js';
|
|
2
|
-
import { defineConfig, globalIgnores } from 'eslint/config';
|
|
3
|
-
import tseslint from 'typescript-eslint';
|
|
4
|
-
|
|
5
|
-
export default defineConfig([
|
|
6
|
-
globalIgnores(['dist/**', 'node_modules/**', 'examples/**']),
|
|
7
|
-
eslint.configs.recommended,
|
|
8
|
-
...tseslint.configs.recommended,
|
|
9
|
-
{
|
|
10
|
-
files: ['**/*.ts', '**/*.tsx'],
|
|
11
|
-
languageOptions: {
|
|
12
|
-
parser: tseslint.parser,
|
|
13
|
-
parserOptions: {
|
|
14
|
-
project: './tsconfig.json',
|
|
15
|
-
},
|
|
16
|
-
},
|
|
17
|
-
rules: {
|
|
18
|
-
'no-unused-vars': 'off',
|
|
19
|
-
'@typescript-eslint/no-unused-vars': 'off',
|
|
20
|
-
'@typescript-eslint/no-explicit-any': 'off',
|
|
21
|
-
},
|
|
22
|
-
},
|
|
23
|
-
]);
|
|
1
|
+
import eslint from '@eslint/js';
|
|
2
|
+
import { defineConfig, globalIgnores } from 'eslint/config';
|
|
3
|
+
import tseslint from 'typescript-eslint';
|
|
4
|
+
|
|
5
|
+
export default defineConfig([
|
|
6
|
+
globalIgnores(['dist/**', 'node_modules/**', 'examples/**', 'tests/**', 'jest.config.ts']),
|
|
7
|
+
eslint.configs.recommended,
|
|
8
|
+
...tseslint.configs.recommended,
|
|
9
|
+
{
|
|
10
|
+
files: ['**/*.ts', '**/*.tsx'],
|
|
11
|
+
languageOptions: {
|
|
12
|
+
parser: tseslint.parser,
|
|
13
|
+
parserOptions: {
|
|
14
|
+
project: './tsconfig.json',
|
|
15
|
+
},
|
|
16
|
+
},
|
|
17
|
+
rules: {
|
|
18
|
+
'no-unused-vars': 'off',
|
|
19
|
+
'@typescript-eslint/no-unused-vars': 'off',
|
|
20
|
+
'@typescript-eslint/no-explicit-any': 'off',
|
|
21
|
+
},
|
|
22
|
+
},
|
|
23
|
+
]);
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
// This file is used to let VSCode respect the tsconfig.json for this test folder
|
|
2
|
+
{
|
|
3
|
+
"extends": "../tsconfig.json",
|
|
4
|
+
"include": [".", "../src"],
|
|
5
|
+
"compilerOptions": {
|
|
6
|
+
"noEmit": true,
|
|
7
|
+
"baseUrl": ".",
|
|
8
|
+
"paths": {
|
|
9
|
+
"@syntropix/database": ["../src/index.ts"],
|
|
10
|
+
"@syntropix/database/*": ["../src/*"]
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
}
|
package/examples/usage.ts
CHANGED
|
@@ -1,73 +1,94 @@
|
|
|
1
|
-
|
|
1
|
+
import { AND, BaseModel, Column, EQ, IN, OR } from '@syntropix/database';
|
|
2
|
+
import { DataClient } from '@syntropix/database/core/dataClient';
|
|
3
|
+
import { TableClient } from '@syntropix/database/core/tableClient';
|
|
2
4
|
import 'dotenv/config';
|
|
3
|
-
import { BaseModel, Column } from '../dist/types/basemodel.js';
|
|
4
|
-
import { AND, EQ, OR } from '../dist/types/filter.js';
|
|
5
5
|
|
|
6
|
-
|
|
7
|
-
export class User extends BaseModel {
|
|
6
|
+
class User extends BaseModel {
|
|
8
7
|
static tableName = 'users';
|
|
9
8
|
|
|
10
|
-
@Column({ type: 'Integer', primary: true, auto_increment: true })
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
@Column(
|
|
14
|
-
|
|
9
|
+
@Column({ type: 'Integer', primary: true, auto_increment: true })
|
|
10
|
+
declare id: number;
|
|
11
|
+
|
|
12
|
+
@Column()
|
|
13
|
+
declare email: string;
|
|
14
|
+
|
|
15
|
+
@Column({ name: 'full_name' })
|
|
16
|
+
declare fullName: string;
|
|
17
|
+
|
|
18
|
+
@Column({ type: 'Json', nullable: true })
|
|
19
|
+
declare profile: any;
|
|
20
|
+
|
|
21
|
+
@Column({ type: 'Boolean', name: 'is_active' })
|
|
22
|
+
declare isActive: boolean;
|
|
15
23
|
}
|
|
16
24
|
|
|
17
|
-
|
|
18
|
-
async function examples() {
|
|
25
|
+
async function ORMExample() {
|
|
19
26
|
// Create table
|
|
20
|
-
await User.createTable();
|
|
27
|
+
// await User.createTable();
|
|
21
28
|
|
|
22
29
|
// Create a new user
|
|
23
|
-
const newUser = await User.create({
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
});
|
|
30
|
+
// const newUser = await User.create({
|
|
31
|
+
// id: 1,
|
|
32
|
+
// email: 'user@example.com',
|
|
33
|
+
// fullName: 'John Doe',
|
|
34
|
+
// isActive: true,
|
|
35
|
+
// });
|
|
36
|
+
// console.log(newUser);
|
|
29
37
|
|
|
30
|
-
// Get a user
|
|
31
|
-
const user = await User.get(OR(AND(EQ('email', 'user@example.com'))));
|
|
32
|
-
console.log(user);
|
|
38
|
+
// // Get a user
|
|
39
|
+
// const user = await User.get(OR(AND(EQ('email', 'user@example.com'))));
|
|
40
|
+
// console.log(user);
|
|
33
41
|
|
|
34
|
-
// Update user
|
|
35
|
-
user.fullName = 'Jane Doe1';
|
|
36
|
-
await user.save();
|
|
42
|
+
// // Update user
|
|
43
|
+
// user.fullName = 'Jane Doe1';
|
|
44
|
+
// await user.save();
|
|
37
45
|
|
|
38
|
-
//
|
|
46
|
+
// Filter users
|
|
39
47
|
const activeUsers = await User.filter({
|
|
40
|
-
filter: OR(AND(
|
|
48
|
+
filter: OR(AND(IN('email', ['user@example.com', 'user3@example.com']))),
|
|
41
49
|
limit: 10,
|
|
42
50
|
});
|
|
43
51
|
|
|
44
|
-
// // Count users
|
|
45
|
-
const userCount = await User.count({
|
|
46
|
-
filter: OR(AND(EQ('is_active', true))),
|
|
47
|
-
});
|
|
48
52
|
console.log(activeUsers);
|
|
49
|
-
console.log(userCount);
|
|
50
|
-
// // Delete user
|
|
51
|
-
await user.remove();
|
|
52
|
-
|
|
53
|
-
// // Bulk create
|
|
54
|
-
await User.bulkCreate([
|
|
55
|
-
{
|
|
56
|
-
id: 2,
|
|
57
|
-
email: 'user2@example.com',
|
|
58
|
-
fullName: 'User 2',
|
|
59
|
-
isActive: true,
|
|
60
|
-
},
|
|
61
|
-
{
|
|
62
|
-
id: 3,
|
|
63
|
-
email: 'user3@example.com',
|
|
64
|
-
fullName: 'User 3',
|
|
65
|
-
isActive: false,
|
|
66
|
-
},
|
|
67
|
-
]);
|
|
68
53
|
|
|
69
|
-
//
|
|
54
|
+
// // // Count users
|
|
55
|
+
// const userCount = await User.count({
|
|
56
|
+
// filter: OR(AND(EQ('is_active', true))),
|
|
57
|
+
// });
|
|
58
|
+
// console.log(activeUsers);
|
|
59
|
+
// console.log(userCount);
|
|
60
|
+
// // // Delete user
|
|
61
|
+
// await user.remove();
|
|
62
|
+
|
|
63
|
+
// // // Bulk create
|
|
64
|
+
// await User.bulkCreate([
|
|
65
|
+
// {
|
|
66
|
+
// id: 2,
|
|
67
|
+
// email: 'user2@example.com',
|
|
68
|
+
// fullName: 'User 2',
|
|
69
|
+
// isActive: true,
|
|
70
|
+
// },
|
|
71
|
+
// {
|
|
72
|
+
// id: 3,
|
|
73
|
+
// email: 'user3@example.com',
|
|
74
|
+
// fullName: 'User 3',
|
|
75
|
+
// isActive: false,
|
|
76
|
+
// },
|
|
77
|
+
// ]);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
async function ClientExample() {
|
|
81
|
+
const tableClient = new TableClient();
|
|
82
|
+
const dataClient = new DataClient();
|
|
83
|
+
const data = await dataClient.queryMany({
|
|
84
|
+
table_name: 'users',
|
|
85
|
+
query: {
|
|
86
|
+
filter: OR(AND(EQ('is_active', true))),
|
|
87
|
+
},
|
|
88
|
+
});
|
|
89
|
+
console.log(data);
|
|
70
90
|
}
|
|
71
91
|
|
|
72
92
|
// Run examples
|
|
73
|
-
|
|
93
|
+
ORMExample().catch(console.error);
|
|
94
|
+
// ClientExample().catch(console.error);
|
package/jest.config.ts
CHANGED
|
@@ -1,10 +1,9 @@
|
|
|
1
|
-
import type { Config } from 'jest';
|
|
2
|
-
import { createDefaultEsmPreset } from 'ts-jest';
|
|
3
|
-
|
|
4
|
-
const presetConfig = createDefaultEsmPreset({
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
} satisfies Config;
|
|
1
|
+
import type { Config } from 'jest';
|
|
2
|
+
import { createDefaultEsmPreset, pathsToModuleNameMapper } from 'ts-jest';
|
|
3
|
+
|
|
4
|
+
const presetConfig = createDefaultEsmPreset({});
|
|
5
|
+
|
|
6
|
+
export default {
|
|
7
|
+
...presetConfig,
|
|
8
|
+
moduleNameMapper: pathsToModuleNameMapper({ '@/*': ['src/*'] }, { prefix: '<rootDir>/' }),
|
|
9
|
+
} satisfies Config;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@syntropix/database",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.4",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "TypeScript SDK for database operations with ORM support",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -14,8 +14,8 @@
|
|
|
14
14
|
"build": "tsc",
|
|
15
15
|
"dev": "tsc --watch",
|
|
16
16
|
"test": "node --experimental-vm-modules $(yarn bin jest)",
|
|
17
|
-
"
|
|
18
|
-
"
|
|
17
|
+
"publish": "npm publish --access public",
|
|
18
|
+
"example": "tsx --tsconfig examples/tsconfig.json examples/usage.ts",
|
|
19
19
|
"lint": "eslint",
|
|
20
20
|
"lint:fix": "eslint --fix",
|
|
21
21
|
"format": "prettier --write .",
|
|
@@ -27,7 +27,7 @@
|
|
|
27
27
|
"typescript",
|
|
28
28
|
"sdk"
|
|
29
29
|
],
|
|
30
|
-
"author": "Syntropix",
|
|
30
|
+
"author": "Syntropix, Inc.",
|
|
31
31
|
"license": "MIT",
|
|
32
32
|
"dependencies": {
|
|
33
33
|
"dotenv": "^17.2.3",
|
package/src/core/dataClient.ts
CHANGED
|
@@ -5,7 +5,7 @@ import { ClientConfig } from './config';
|
|
|
5
5
|
export class DataClient {
|
|
6
6
|
private client: Client;
|
|
7
7
|
|
|
8
|
-
constructor(config: ClientConfig) {
|
|
8
|
+
constructor(config: ClientConfig = new ClientConfig()) {
|
|
9
9
|
this.client = new Client(config);
|
|
10
10
|
}
|
|
11
11
|
|
|
@@ -26,8 +26,6 @@ export class DataClient {
|
|
|
26
26
|
}
|
|
27
27
|
|
|
28
28
|
async updateByPrimaryKey(pk: string, data: Update): Promise<any> {
|
|
29
|
-
console.log(pk);
|
|
30
|
-
console.log(data);
|
|
31
29
|
const response = await this.client.post(`/update/${pk}`, data);
|
|
32
30
|
if (response.status === 'success') {
|
|
33
31
|
return response.data;
|
|
@@ -40,9 +38,6 @@ export class DataClient {
|
|
|
40
38
|
if (response.status === 'success') {
|
|
41
39
|
const responseData = response.data;
|
|
42
40
|
if (model !== undefined) {
|
|
43
|
-
// In TypeScript, we would typically use a validation library or manual validation
|
|
44
|
-
// For now, we'll return the data as-is, but in a real implementation you might want to use
|
|
45
|
-
// a library like class-validator or zod for validation
|
|
46
41
|
return new model(responseData);
|
|
47
42
|
}
|
|
48
43
|
return responseData;
|
package/src/core/tableClient.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { TableCreateResponse } from '@/types/dto/table';
|
|
1
2
|
import { SyntropixDBTable } from '../types/common';
|
|
2
3
|
import {
|
|
3
4
|
TableAddColumn,
|
|
@@ -15,11 +16,11 @@ import { ClientConfig } from './config';
|
|
|
15
16
|
export class TableClient {
|
|
16
17
|
private client: Client;
|
|
17
18
|
|
|
18
|
-
constructor(config: ClientConfig) {
|
|
19
|
+
constructor(config: ClientConfig = new ClientConfig()) {
|
|
19
20
|
this.client = new Client(config);
|
|
20
21
|
}
|
|
21
22
|
|
|
22
|
-
async createTable(data: TableCreate): Promise<
|
|
23
|
+
async createTable(data: TableCreate): Promise<TableCreateResponse> {
|
|
23
24
|
const response = await this.client.post('/table/create', data);
|
|
24
25
|
if (response.status === 'success') {
|
|
25
26
|
return response.data;
|
package/src/types/basemodel.ts
CHANGED
|
@@ -3,6 +3,7 @@ import 'reflect-metadata';
|
|
|
3
3
|
import { ClientConfig } from '../core/config';
|
|
4
4
|
import { SyntropixClient } from '../core/syntropix';
|
|
5
5
|
import { Aggregate, AggregateFunction, ForeignKey, GroupBy, Index, Join, Sort } from './common';
|
|
6
|
+
import { TableCreateResponse } from './dto/table';
|
|
6
7
|
import { Field, ForeignKeyField, IntegerField } from './field';
|
|
7
8
|
import { AND, EQ, Filter, GTE, OR, Value } from './filter';
|
|
8
9
|
|
|
@@ -218,7 +219,7 @@ export class BaseModel {
|
|
|
218
219
|
}
|
|
219
220
|
|
|
220
221
|
// Table operations
|
|
221
|
-
static async createTable(_client?: SyntropixClient | null): Promise<
|
|
222
|
+
static async createTable(_client?: SyntropixClient | null): Promise<TableCreateResponse> {
|
|
222
223
|
const fields = this.getFields();
|
|
223
224
|
const columns = Object.values(fields).map((f) => f.into());
|
|
224
225
|
const foreignKeys: ForeignKey[] = this.getAssociations().map((f) => ({
|
|
@@ -465,7 +466,7 @@ export class BaseModel {
|
|
|
465
466
|
this: new (data?: any) => T,
|
|
466
467
|
options: {
|
|
467
468
|
filter: Filter;
|
|
468
|
-
sort?: Sort;
|
|
469
|
+
sort?: Sort[];
|
|
469
470
|
aggregate?: Aggregate[];
|
|
470
471
|
join?: Join;
|
|
471
472
|
limit?: number;
|