@objectql/core 1.8.1 → 1.8.3
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/CHANGELOG.md +19 -0
- package/dist/app.d.ts +14 -0
- package/dist/app.js +33 -0
- package/dist/app.js.map +1 -1
- package/dist/util.d.ts +17 -0
- package/dist/util.js +114 -0
- package/dist/util.js.map +1 -1
- package/package.json +15 -2
- package/src/app.ts +48 -0
- package/src/util.ts +146 -0
- package/test/introspection.test.ts +289 -0
- package/tsconfig.tsbuildinfo +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,24 @@
|
|
|
1
1
|
# @objectql/core
|
|
2
2
|
|
|
3
|
+
## 1.8.3
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- Release patch version 1.8.3
|
|
8
|
+
|
|
9
|
+
Small version update with latest improvements and bug fixes.
|
|
10
|
+
|
|
11
|
+
- Updated dependencies
|
|
12
|
+
- @objectql/types@1.8.3
|
|
13
|
+
|
|
14
|
+
## 1.8.2
|
|
15
|
+
|
|
16
|
+
### Patch Changes
|
|
17
|
+
|
|
18
|
+
- Patch release v1.8.2 - Small version update with latest improvements
|
|
19
|
+
- Updated dependencies
|
|
20
|
+
- @objectql/types@1.8.2
|
|
21
|
+
|
|
3
22
|
## 1.8.1
|
|
4
23
|
|
|
5
24
|
### Patch Changes
|
package/dist/app.d.ts
CHANGED
|
@@ -20,6 +20,20 @@ export declare class ObjectQL implements IObjectQL {
|
|
|
20
20
|
getObject(name: string): ObjectConfig | undefined;
|
|
21
21
|
getConfigs(): Record<string, ObjectConfig>;
|
|
22
22
|
datasource(name: string): Driver;
|
|
23
|
+
/**
|
|
24
|
+
* Introspect the database schema and automatically register objects.
|
|
25
|
+
* This allows connecting to an existing database without defining metadata.
|
|
26
|
+
*
|
|
27
|
+
* @param datasourceName - The name of the datasource to introspect (default: 'default')
|
|
28
|
+
* @param options - Optional configuration for schema conversion
|
|
29
|
+
* @returns Array of registered ObjectConfig
|
|
30
|
+
*/
|
|
31
|
+
introspectAndRegister(datasourceName?: string, options?: {
|
|
32
|
+
excludeTables?: string[];
|
|
33
|
+
includeTables?: string[];
|
|
34
|
+
skipSystemColumns?: boolean;
|
|
35
|
+
}): Promise<ObjectConfig[]>;
|
|
36
|
+
close(): Promise<void>;
|
|
23
37
|
init(): Promise<void>;
|
|
24
38
|
private processInitialData;
|
|
25
39
|
}
|
package/dist/app.js
CHANGED
|
@@ -8,6 +8,7 @@ const repository_1 = require("./repository");
|
|
|
8
8
|
const action_1 = require("./action");
|
|
9
9
|
const hook_1 = require("./hook");
|
|
10
10
|
const object_1 = require("./object");
|
|
11
|
+
const util_1 = require("./util");
|
|
11
12
|
class ObjectQL {
|
|
12
13
|
constructor(config) {
|
|
13
14
|
this.datasources = {};
|
|
@@ -125,6 +126,38 @@ class ObjectQL {
|
|
|
125
126
|
}
|
|
126
127
|
return driver;
|
|
127
128
|
}
|
|
129
|
+
/**
|
|
130
|
+
* Introspect the database schema and automatically register objects.
|
|
131
|
+
* This allows connecting to an existing database without defining metadata.
|
|
132
|
+
*
|
|
133
|
+
* @param datasourceName - The name of the datasource to introspect (default: 'default')
|
|
134
|
+
* @param options - Optional configuration for schema conversion
|
|
135
|
+
* @returns Array of registered ObjectConfig
|
|
136
|
+
*/
|
|
137
|
+
async introspectAndRegister(datasourceName = 'default', options) {
|
|
138
|
+
const driver = this.datasource(datasourceName);
|
|
139
|
+
if (!driver.introspectSchema) {
|
|
140
|
+
throw new Error(`Driver for datasource '${datasourceName}' does not support schema introspection`);
|
|
141
|
+
}
|
|
142
|
+
console.log(`Introspecting datasource '${datasourceName}'...`);
|
|
143
|
+
const introspectedSchema = await driver.introspectSchema();
|
|
144
|
+
// Convert introspected schema to ObjectQL objects
|
|
145
|
+
const objects = (0, util_1.convertIntrospectedSchemaToObjects)(introspectedSchema, options);
|
|
146
|
+
console.log(`Discovered ${objects.length} table(s), registering as objects...`);
|
|
147
|
+
// Register each discovered object
|
|
148
|
+
for (const obj of objects) {
|
|
149
|
+
this.registerObject(obj);
|
|
150
|
+
}
|
|
151
|
+
return objects;
|
|
152
|
+
}
|
|
153
|
+
async close() {
|
|
154
|
+
for (const [name, driver] of Object.entries(this.datasources)) {
|
|
155
|
+
if (driver.disconnect) {
|
|
156
|
+
console.log(`Closing driver '${name}'...`);
|
|
157
|
+
await driver.disconnect();
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
}
|
|
128
161
|
async init() {
|
|
129
162
|
// 0. Init Plugins (This allows plugins to register custom loaders)
|
|
130
163
|
for (const plugin of this.pluginsList) {
|
package/dist/app.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"app.js","sourceRoot":"","sources":["../src/app.ts"],"names":[],"mappings":";;;AAAA,2CAeyB;AACzB,6CAAgD;AAChD,wEAAwE;AAExE,gDAAgD;AAChD,qCAAkF;AAClF,iCAA0E;AAC1E,qCAAkE;
|
|
1
|
+
{"version":3,"file":"app.js","sourceRoot":"","sources":["../src/app.ts"],"names":[],"mappings":";;;AAAA,2CAeyB;AACzB,6CAAgD;AAChD,wEAAwE;AAExE,gDAAgD;AAChD,qCAAkF;AAClF,iCAA0E;AAC1E,qCAAkE;AAClE,iCAA4D;AAE5D,MAAa,QAAQ;IAWjB,YAAY,MAAsB;QAT1B,gBAAW,GAA2B,EAAE,CAAC;QACzC,YAAO,GAAa,EAAE,CAAC;QACvB,UAAK,GAAgC,EAAE,CAAC;QACxC,YAAO,GAAgC,EAAE,CAAC;QAC1C,gBAAW,GAAqB,EAAE,CAAC;QAMvC,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;QACrB,IAAI,CAAC,QAAQ,GAAG,MAAM,CAAC,QAAQ,IAAI,IAAI,wBAAgB,EAAE,CAAC;QAC1D,IAAI,CAAC,WAAW,GAAG,MAAM,CAAC,WAAW,IAAI,EAAE,CAAC;QAC5C,uCAAuC;QAEvC,IAAI,MAAM,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|
|
1
2
|
export declare function toTitleCase(str: string): string;
|
|
3
|
+
/**
|
|
4
|
+
* Convert an introspected database schema to ObjectQL object configurations.
|
|
5
|
+
* This allows using existing database tables without manually defining metadata.
|
|
6
|
+
*
|
|
7
|
+
* @param introspectedSchema - The schema returned from driver.introspectSchema()
|
|
8
|
+
* @param options - Optional configuration for the conversion
|
|
9
|
+
* @returns Array of ObjectConfig that can be registered with ObjectQL
|
|
10
|
+
*/
|
|
11
|
+
export declare function convertIntrospectedSchemaToObjects(introspectedSchema: IntrospectedSchema, options?: {
|
|
12
|
+
/** Tables to exclude from conversion */
|
|
13
|
+
excludeTables?: string[];
|
|
14
|
+
/** Tables to include (if specified, only these will be converted) */
|
|
15
|
+
includeTables?: string[];
|
|
16
|
+
/** Whether to skip system columns like id, created_at, updated_at */
|
|
17
|
+
skipSystemColumns?: boolean;
|
|
18
|
+
}): ObjectConfig[];
|
package/dist/util.js
CHANGED
|
@@ -1,9 +1,123 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.toTitleCase = toTitleCase;
|
|
4
|
+
exports.convertIntrospectedSchemaToObjects = convertIntrospectedSchemaToObjects;
|
|
4
5
|
function toTitleCase(str) {
|
|
5
6
|
return str
|
|
6
7
|
.replace(/_/g, ' ')
|
|
7
8
|
.replace(/\b\w/g, (char) => char.toUpperCase());
|
|
8
9
|
}
|
|
10
|
+
/**
|
|
11
|
+
* Convert database type to ObjectQL field type.
|
|
12
|
+
*/
|
|
13
|
+
function mapDatabaseTypeToFieldType(dbType) {
|
|
14
|
+
const type = dbType.toLowerCase();
|
|
15
|
+
// Text types
|
|
16
|
+
if (type.includes('char') || type.includes('varchar') || type.includes('text')) {
|
|
17
|
+
if (type.includes('text'))
|
|
18
|
+
return 'textarea';
|
|
19
|
+
return 'text';
|
|
20
|
+
}
|
|
21
|
+
// Numeric types
|
|
22
|
+
if (type.includes('int') || type === 'integer' || type === 'bigint' || type === 'smallint') {
|
|
23
|
+
return 'number';
|
|
24
|
+
}
|
|
25
|
+
if (type.includes('float') || type.includes('double') || type.includes('decimal') || type.includes('numeric') || type === 'real') {
|
|
26
|
+
return 'number';
|
|
27
|
+
}
|
|
28
|
+
// Boolean
|
|
29
|
+
if (type.includes('bool')) {
|
|
30
|
+
return 'boolean';
|
|
31
|
+
}
|
|
32
|
+
// Date/Time types
|
|
33
|
+
if (type.includes('timestamp') || type === 'datetime') {
|
|
34
|
+
return 'datetime';
|
|
35
|
+
}
|
|
36
|
+
if (type === 'date') {
|
|
37
|
+
return 'date';
|
|
38
|
+
}
|
|
39
|
+
if (type === 'time') {
|
|
40
|
+
return 'time';
|
|
41
|
+
}
|
|
42
|
+
// JSON types
|
|
43
|
+
if (type === 'json' || type === 'jsonb') {
|
|
44
|
+
return 'object';
|
|
45
|
+
}
|
|
46
|
+
// Default to text
|
|
47
|
+
return 'text';
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Convert an introspected database schema to ObjectQL object configurations.
|
|
51
|
+
* This allows using existing database tables without manually defining metadata.
|
|
52
|
+
*
|
|
53
|
+
* @param introspectedSchema - The schema returned from driver.introspectSchema()
|
|
54
|
+
* @param options - Optional configuration for the conversion
|
|
55
|
+
* @returns Array of ObjectConfig that can be registered with ObjectQL
|
|
56
|
+
*/
|
|
57
|
+
function convertIntrospectedSchemaToObjects(introspectedSchema, options) {
|
|
58
|
+
const objects = [];
|
|
59
|
+
const excludeTables = (options === null || options === void 0 ? void 0 : options.excludeTables) || [];
|
|
60
|
+
const includeTables = options === null || options === void 0 ? void 0 : options.includeTables;
|
|
61
|
+
const skipSystemColumns = (options === null || options === void 0 ? void 0 : options.skipSystemColumns) !== false;
|
|
62
|
+
for (const [tableName, table] of Object.entries(introspectedSchema.tables)) {
|
|
63
|
+
// Skip excluded tables
|
|
64
|
+
if (excludeTables.includes(tableName)) {
|
|
65
|
+
continue;
|
|
66
|
+
}
|
|
67
|
+
// If includeTables is specified, skip tables not in the list
|
|
68
|
+
if (includeTables && !includeTables.includes(tableName)) {
|
|
69
|
+
continue;
|
|
70
|
+
}
|
|
71
|
+
const fields = {};
|
|
72
|
+
// Convert columns to fields
|
|
73
|
+
for (const column of table.columns) {
|
|
74
|
+
// Skip system columns if requested
|
|
75
|
+
if (skipSystemColumns && ['id', 'created_at', 'updated_at'].includes(column.name)) {
|
|
76
|
+
continue;
|
|
77
|
+
}
|
|
78
|
+
// Check if this column is a foreign key
|
|
79
|
+
const foreignKey = table.foreignKeys.find(fk => fk.columnName === column.name);
|
|
80
|
+
let fieldConfig;
|
|
81
|
+
if (foreignKey) {
|
|
82
|
+
// This is a lookup field
|
|
83
|
+
fieldConfig = {
|
|
84
|
+
type: 'lookup',
|
|
85
|
+
reference_to: foreignKey.referencedTable,
|
|
86
|
+
label: toTitleCase(column.name),
|
|
87
|
+
required: !column.nullable
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
else {
|
|
91
|
+
// Regular field
|
|
92
|
+
const fieldType = mapDatabaseTypeToFieldType(column.type);
|
|
93
|
+
fieldConfig = {
|
|
94
|
+
type: fieldType,
|
|
95
|
+
label: toTitleCase(column.name),
|
|
96
|
+
required: !column.nullable
|
|
97
|
+
};
|
|
98
|
+
// Add unique constraint
|
|
99
|
+
if (column.isUnique) {
|
|
100
|
+
fieldConfig.unique = true;
|
|
101
|
+
}
|
|
102
|
+
// Add max length for text fields
|
|
103
|
+
if (column.maxLength && (fieldType === 'text' || fieldType === 'textarea')) {
|
|
104
|
+
fieldConfig.max_length = column.maxLength;
|
|
105
|
+
}
|
|
106
|
+
// Add default value
|
|
107
|
+
if (column.defaultValue != null) {
|
|
108
|
+
fieldConfig.defaultValue = column.defaultValue;
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
fields[column.name] = fieldConfig;
|
|
112
|
+
}
|
|
113
|
+
// Create the object configuration
|
|
114
|
+
const objectConfig = {
|
|
115
|
+
name: tableName,
|
|
116
|
+
label: toTitleCase(tableName),
|
|
117
|
+
fields
|
|
118
|
+
};
|
|
119
|
+
objects.push(objectConfig);
|
|
120
|
+
}
|
|
121
|
+
return objects;
|
|
122
|
+
}
|
|
9
123
|
//# sourceMappingURL=util.js.map
|
package/dist/util.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"util.js","sourceRoot":"","sources":["../src/util.ts"],"names":[],"mappings":";;
|
|
1
|
+
{"version":3,"file":"util.js","sourceRoot":"","sources":["../src/util.ts"],"names":[],"mappings":";;AAEA,kCAIC;AAuDD,gFAyFC;AApJD,SAAgB,WAAW,CAAC,GAAW;IACnC,OAAO,GAAG;SACL,OAAO,CAAC,IAAI,EAAE,GAAG,CAAC;SAClB,OAAO,CAAC,OAAO,EAAE,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,CAAC,WAAW,EAAE,CAAC,CAAC;AACxD,CAAC;AAED;;GAEG;AACH,SAAS,0BAA0B,CAAC,MAAc;IAC9C,MAAM,IAAI,GAAG,MAAM,CAAC,WAAW,EAAE,CAAC;IAElC,aAAa;IACb,IAAI,IAAI,CAAC,QAAQ,CAAC,MAAM,CAAC,IAAI,IAAI,CAAC,QAAQ,CAAC,SAAS,CAAC,IAAI,IAAI,CAAC,QAAQ,CAAC,MAAM,CAAC,EAAE,CAAC;QAC7E,IAAI,IAAI,CAAC,QAAQ,CAAC,MAAM,CAAC;YAAE,OAAO,UAAU,CAAC;QAC7C,OAAO,MAAM,CAAC;IAClB,CAAC;IAED,gBAAgB;IAChB,IAAI,IAAI,CAAC,QAAQ,CAAC,KAAK,CAAC,IAAI,IAAI,KAAK,SAAS,IAAI,IAAI,KAAK,QAAQ,IAAI,IAAI,KAAK,UAAU,EAAE,CAAC;QACzF,OAAO,QAAQ,CAAC;IACpB,CAAC;IACD,IAAI,IAAI,CAAC,QAAQ,CAAC,OAAO,CAAC,IAAI,IAAI,CAAC,QAAQ,CAAC,QAAQ,CAAC,IAAI,IAAI,CAAC,QAAQ,CAAC,SA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AC;gBAED,iCAAiC;gBACjC,IAAI,MAAM,CAAC,SAAS,IAAI,CAAC,SAAS,KAAK,MAAM,IAAI,SAAS,KAAK,UAAU,CAAC,EAAE,CAAC;oBACzE,WAAW,CAAC,UAAU,GAAG,MAAM,CAAC,SAAS,CAAC;gBAC9C,CAAC;gBAED,oBAAoB;gBACpB,IAAI,MAAM,CAAC,YAAY,IAAI,IAAI,EAAE,CAAC;oBAC9B,WAAW,CAAC,YAAY,GAAG,MAAM,CAAC,YAAY,CAAC;gBACnD,CAAC;YACL,CAAC;YAED,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,GAAG,WAAW,CAAC;QACtC,CAAC;QAED,kCAAkC;QAClC,MAAM,YAAY,GAAiB;YAC/B,IAAI,EAAE,SAAS;YACf,KAAK,EAAE,WAAW,CAAC,SAAS,CAAC;YAC7B,MAAM;SACT,CAAC;QAEF,OAAO,CAAC,IAAI,CAAC,YAAY,CAAC,CAAC;IAC/B,CAAC;IAED,OAAO,OAAO,CAAC;AACnB,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,13 +1,26 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@objectql/core",
|
|
3
|
-
"version": "1.8.
|
|
3
|
+
"version": "1.8.3",
|
|
4
|
+
"description": "Universal runtime engine for ObjectQL - AI-native metadata-driven ORM with validation, repository pattern, and driver orchestration",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"objectql",
|
|
7
|
+
"orm",
|
|
8
|
+
"database",
|
|
9
|
+
"ai-native",
|
|
10
|
+
"metadata",
|
|
11
|
+
"validation",
|
|
12
|
+
"repository",
|
|
13
|
+
"universal",
|
|
14
|
+
"typescript",
|
|
15
|
+
"query-builder"
|
|
16
|
+
],
|
|
4
17
|
"license": "MIT",
|
|
5
18
|
"main": "dist/index.js",
|
|
6
19
|
"types": "dist/index.d.ts",
|
|
7
20
|
"dependencies": {
|
|
8
21
|
"openai": "^4.28.0",
|
|
9
22
|
"js-yaml": "^4.1.0",
|
|
10
|
-
"@objectql/types": "1.8.
|
|
23
|
+
"@objectql/types": "1.8.3"
|
|
11
24
|
},
|
|
12
25
|
"devDependencies": {
|
|
13
26
|
"typescript": "^5.3.0",
|
package/src/app.ts
CHANGED
|
@@ -21,6 +21,7 @@ import { ObjectRepository } from './repository';
|
|
|
21
21
|
import { executeActionHelper, registerActionHelper, ActionEntry } from './action';
|
|
22
22
|
import { registerHookHelper, triggerHookHelper, HookEntry } from './hook';
|
|
23
23
|
import { registerObjectHelper, getConfigsHelper } from './object';
|
|
24
|
+
import { convertIntrospectedSchemaToObjects } from './util';
|
|
24
25
|
|
|
25
26
|
export class ObjectQL implements IObjectQL {
|
|
26
27
|
public metadata: MetadataRegistry;
|
|
@@ -158,6 +159,53 @@ export class ObjectQL implements IObjectQL {
|
|
|
158
159
|
return driver;
|
|
159
160
|
}
|
|
160
161
|
|
|
162
|
+
/**
|
|
163
|
+
* Introspect the database schema and automatically register objects.
|
|
164
|
+
* This allows connecting to an existing database without defining metadata.
|
|
165
|
+
*
|
|
166
|
+
* @param datasourceName - The name of the datasource to introspect (default: 'default')
|
|
167
|
+
* @param options - Optional configuration for schema conversion
|
|
168
|
+
* @returns Array of registered ObjectConfig
|
|
169
|
+
*/
|
|
170
|
+
async introspectAndRegister(
|
|
171
|
+
datasourceName: string = 'default',
|
|
172
|
+
options?: {
|
|
173
|
+
excludeTables?: string[];
|
|
174
|
+
includeTables?: string[];
|
|
175
|
+
skipSystemColumns?: boolean;
|
|
176
|
+
}
|
|
177
|
+
): Promise<ObjectConfig[]> {
|
|
178
|
+
const driver = this.datasource(datasourceName);
|
|
179
|
+
|
|
180
|
+
if (!driver.introspectSchema) {
|
|
181
|
+
throw new Error(`Driver for datasource '${datasourceName}' does not support schema introspection`);
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
console.log(`Introspecting datasource '${datasourceName}'...`);
|
|
185
|
+
const introspectedSchema = await driver.introspectSchema();
|
|
186
|
+
|
|
187
|
+
// Convert introspected schema to ObjectQL objects
|
|
188
|
+
const objects = convertIntrospectedSchemaToObjects(introspectedSchema, options);
|
|
189
|
+
|
|
190
|
+
console.log(`Discovered ${objects.length} table(s), registering as objects...`);
|
|
191
|
+
|
|
192
|
+
// Register each discovered object
|
|
193
|
+
for (const obj of objects) {
|
|
194
|
+
this.registerObject(obj);
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
return objects;
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
async close() {
|
|
201
|
+
for (const [name, driver] of Object.entries(this.datasources)) {
|
|
202
|
+
if (driver.disconnect) {
|
|
203
|
+
console.log(`Closing driver '${name}'...`);
|
|
204
|
+
await driver.disconnect();
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
|
|
161
209
|
async init() {
|
|
162
210
|
// 0. Init Plugins (This allows plugins to register custom loaders)
|
|
163
211
|
for (const plugin of this.pluginsList) {
|
package/src/util.ts
CHANGED
|
@@ -1,5 +1,151 @@
|
|
|
1
|
+
import { ObjectConfig, FieldConfig, FieldType, IntrospectedSchema, IntrospectedColumn, IntrospectedTable } from '@objectql/types';
|
|
2
|
+
|
|
1
3
|
export function toTitleCase(str: string): string {
|
|
2
4
|
return str
|
|
3
5
|
.replace(/_/g, ' ')
|
|
4
6
|
.replace(/\b\w/g, (char) => char.toUpperCase());
|
|
5
7
|
}
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Convert database type to ObjectQL field type.
|
|
11
|
+
*/
|
|
12
|
+
function mapDatabaseTypeToFieldType(dbType: string): FieldType {
|
|
13
|
+
const type = dbType.toLowerCase();
|
|
14
|
+
|
|
15
|
+
// Text types
|
|
16
|
+
if (type.includes('char') || type.includes('varchar') || type.includes('text')) {
|
|
17
|
+
if (type.includes('text')) return 'textarea';
|
|
18
|
+
return 'text';
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
// Numeric types
|
|
22
|
+
if (type.includes('int') || type === 'integer' || type === 'bigint' || type === 'smallint') {
|
|
23
|
+
return 'number';
|
|
24
|
+
}
|
|
25
|
+
if (type.includes('float') || type.includes('double') || type.includes('decimal') || type.includes('numeric') || type === 'real') {
|
|
26
|
+
return 'number';
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
// Boolean
|
|
30
|
+
if (type.includes('bool')) {
|
|
31
|
+
return 'boolean';
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
// Date/Time types
|
|
35
|
+
if (type.includes('timestamp') || type === 'datetime') {
|
|
36
|
+
return 'datetime';
|
|
37
|
+
}
|
|
38
|
+
if (type === 'date') {
|
|
39
|
+
return 'date';
|
|
40
|
+
}
|
|
41
|
+
if (type === 'time') {
|
|
42
|
+
return 'time';
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
// JSON types
|
|
46
|
+
if (type === 'json' || type === 'jsonb') {
|
|
47
|
+
return 'object';
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
// Default to text
|
|
51
|
+
return 'text';
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Convert an introspected database schema to ObjectQL object configurations.
|
|
56
|
+
* This allows using existing database tables without manually defining metadata.
|
|
57
|
+
*
|
|
58
|
+
* @param introspectedSchema - The schema returned from driver.introspectSchema()
|
|
59
|
+
* @param options - Optional configuration for the conversion
|
|
60
|
+
* @returns Array of ObjectConfig that can be registered with ObjectQL
|
|
61
|
+
*/
|
|
62
|
+
export function convertIntrospectedSchemaToObjects(
|
|
63
|
+
introspectedSchema: IntrospectedSchema,
|
|
64
|
+
options?: {
|
|
65
|
+
/** Tables to exclude from conversion */
|
|
66
|
+
excludeTables?: string[];
|
|
67
|
+
/** Tables to include (if specified, only these will be converted) */
|
|
68
|
+
includeTables?: string[];
|
|
69
|
+
/** Whether to skip system columns like id, created_at, updated_at */
|
|
70
|
+
skipSystemColumns?: boolean;
|
|
71
|
+
}
|
|
72
|
+
): ObjectConfig[] {
|
|
73
|
+
const objects: ObjectConfig[] = [];
|
|
74
|
+
const excludeTables = options?.excludeTables || [];
|
|
75
|
+
const includeTables = options?.includeTables;
|
|
76
|
+
const skipSystemColumns = options?.skipSystemColumns !== false;
|
|
77
|
+
|
|
78
|
+
for (const [tableName, table] of Object.entries(introspectedSchema.tables)) {
|
|
79
|
+
// Skip excluded tables
|
|
80
|
+
if (excludeTables.includes(tableName)) {
|
|
81
|
+
continue;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
// If includeTables is specified, skip tables not in the list
|
|
85
|
+
if (includeTables && !includeTables.includes(tableName)) {
|
|
86
|
+
continue;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
const fields: Record<string, FieldConfig> = {};
|
|
90
|
+
|
|
91
|
+
// Convert columns to fields
|
|
92
|
+
for (const column of table.columns) {
|
|
93
|
+
// Skip system columns if requested
|
|
94
|
+
if (skipSystemColumns && ['id', 'created_at', 'updated_at'].includes(column.name)) {
|
|
95
|
+
continue;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
// Check if this column is a foreign key
|
|
99
|
+
const foreignKey = table.foreignKeys.find(fk => fk.columnName === column.name);
|
|
100
|
+
|
|
101
|
+
let fieldConfig: FieldConfig;
|
|
102
|
+
|
|
103
|
+
if (foreignKey) {
|
|
104
|
+
// This is a lookup field
|
|
105
|
+
fieldConfig = {
|
|
106
|
+
type: 'lookup',
|
|
107
|
+
reference_to: foreignKey.referencedTable,
|
|
108
|
+
label: toTitleCase(column.name),
|
|
109
|
+
required: !column.nullable
|
|
110
|
+
};
|
|
111
|
+
} else {
|
|
112
|
+
// Regular field
|
|
113
|
+
const fieldType = mapDatabaseTypeToFieldType(column.type);
|
|
114
|
+
|
|
115
|
+
fieldConfig = {
|
|
116
|
+
type: fieldType,
|
|
117
|
+
label: toTitleCase(column.name),
|
|
118
|
+
required: !column.nullable
|
|
119
|
+
};
|
|
120
|
+
|
|
121
|
+
// Add unique constraint
|
|
122
|
+
if (column.isUnique) {
|
|
123
|
+
fieldConfig.unique = true;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
// Add max length for text fields
|
|
127
|
+
if (column.maxLength && (fieldType === 'text' || fieldType === 'textarea')) {
|
|
128
|
+
fieldConfig.max_length = column.maxLength;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
// Add default value
|
|
132
|
+
if (column.defaultValue != null) {
|
|
133
|
+
fieldConfig.defaultValue = column.defaultValue;
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
fields[column.name] = fieldConfig;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
// Create the object configuration
|
|
141
|
+
const objectConfig: ObjectConfig = {
|
|
142
|
+
name: tableName,
|
|
143
|
+
label: toTitleCase(tableName),
|
|
144
|
+
fields
|
|
145
|
+
};
|
|
146
|
+
|
|
147
|
+
objects.push(objectConfig);
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
return objects;
|
|
151
|
+
}
|