@promakeai/orm 1.0.5 → 1.0.6
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.d.ts +2 -1
- package/dist/index.js +69 -5
- package/dist/utils/jsonConverter.d.ts +5 -0
- package/package.json +1 -1
- package/src/index.ts +6 -2
- package/src/utils/jsonConverter.ts +115 -14
package/dist/index.d.ts
CHANGED
|
@@ -46,7 +46,8 @@ export { buildTranslationQuery, buildTranslationQueryById, buildTranslationInser
|
|
|
46
46
|
export type { TranslationQueryOptions, TranslationQueryResult, } from "./utils/translationQuery";
|
|
47
47
|
export { resolvePopulate, getPopulatableFields, validatePopulate, } from "./utils/populateResolver";
|
|
48
48
|
export type { PopulateAdapter } from "./utils/populateResolver";
|
|
49
|
-
export { parseJSONSchema } from "./utils/jsonConverter";
|
|
49
|
+
export { parseJSONSchema, parseJSONSchemaWithWarnings, } from "./utils/jsonConverter";
|
|
50
|
+
export type { ParseJSONSchemaResult } from "./utils/jsonConverter";
|
|
50
51
|
export { isJsonType } from "./types";
|
|
51
52
|
export { deserializeRow, serializeRow } from "./utils/deserializer";
|
|
52
53
|
export type { FieldType, FieldDefinition, FieldReference, FieldBuilderLike, TableDefinition, LanguageConfig, SchemaDefinition, SchemaInput, JSONFieldType, JSONFieldDefinition, JSONTableDefinition, JSONSchemaDefinition, WhereCondition, OrderByOption, PopulateOption, PopulateNested, QueryOptions, PaginatedResult, ORMConfig, } from "./types";
|
package/dist/index.js
CHANGED
|
@@ -1008,10 +1008,25 @@ function extractTranslatableData(data, tableSchema) {
|
|
|
1008
1008
|
return { mainData, translatableData };
|
|
1009
1009
|
}
|
|
1010
1010
|
// src/utils/jsonConverter.ts
|
|
1011
|
+
var LANGUAGE_NAME_TO_CODE = {
|
|
1012
|
+
english: "en",
|
|
1013
|
+
turkish: "tr",
|
|
1014
|
+
german: "de",
|
|
1015
|
+
french: "fr",
|
|
1016
|
+
spanish: "es",
|
|
1017
|
+
russian: "ru",
|
|
1018
|
+
arabic: "ar"
|
|
1019
|
+
};
|
|
1011
1020
|
function parseJSONSchema(json) {
|
|
1021
|
+
return parseJSONSchemaWithWarnings(json).schema;
|
|
1022
|
+
}
|
|
1023
|
+
function parseJSONSchemaWithWarnings(json) {
|
|
1024
|
+
const warnings = [];
|
|
1025
|
+
const supported = (json.languages || ["en"]).map((lang) => normalizeLangToken(lang));
|
|
1026
|
+
const defaultLanguage = resolveDefaultLanguage(json.defaultLanguage, supported, warnings);
|
|
1012
1027
|
const languages = {
|
|
1013
|
-
default:
|
|
1014
|
-
supported
|
|
1028
|
+
default: defaultLanguage,
|
|
1029
|
+
supported
|
|
1015
1030
|
};
|
|
1016
1031
|
const tables = {};
|
|
1017
1032
|
for (const [tableName, jsonTable] of Object.entries(json.tables)) {
|
|
@@ -1025,11 +1040,59 @@ function parseJSONSchema(json) {
|
|
|
1025
1040
|
};
|
|
1026
1041
|
}
|
|
1027
1042
|
return {
|
|
1028
|
-
|
|
1029
|
-
|
|
1030
|
-
|
|
1043
|
+
schema: {
|
|
1044
|
+
name: json.name,
|
|
1045
|
+
languages,
|
|
1046
|
+
tables
|
|
1047
|
+
},
|
|
1048
|
+
warnings
|
|
1031
1049
|
};
|
|
1032
1050
|
}
|
|
1051
|
+
function normalizeLangToken(lang) {
|
|
1052
|
+
return String(lang).trim().toLowerCase();
|
|
1053
|
+
}
|
|
1054
|
+
function resolveDefaultLanguage(input, supported, warnings) {
|
|
1055
|
+
const fallback = supported[0] || "en";
|
|
1056
|
+
if (!input)
|
|
1057
|
+
return fallback;
|
|
1058
|
+
const normalizedInput = normalizeLangToken(input);
|
|
1059
|
+
const directMatch = supported.find((lang) => normalizeLangToken(lang) === normalizedInput);
|
|
1060
|
+
if (directMatch)
|
|
1061
|
+
return directMatch;
|
|
1062
|
+
const languageNameMatch = resolveLanguageNameToSupportedCode(normalizedInput, supported);
|
|
1063
|
+
if (languageNameMatch) {
|
|
1064
|
+
warnings.push(`Normalized defaultLanguage "${input}" to "${languageNameMatch}".`);
|
|
1065
|
+
return languageNameMatch;
|
|
1066
|
+
}
|
|
1067
|
+
throw new Error(`Invalid defaultLanguage "${input}". Expected one of: ${supported.join(", ")}`);
|
|
1068
|
+
}
|
|
1069
|
+
function resolveLanguageNameToSupportedCode(normalizedInput, supported) {
|
|
1070
|
+
const mapped = LANGUAGE_NAME_TO_CODE[normalizedInput];
|
|
1071
|
+
if (mapped) {
|
|
1072
|
+
const supportedMatch = supported.find((lang) => normalizeLangToken(lang) === mapped);
|
|
1073
|
+
if (supportedMatch)
|
|
1074
|
+
return supportedMatch;
|
|
1075
|
+
}
|
|
1076
|
+
for (const code of supported) {
|
|
1077
|
+
const languageName = getLanguageDisplayName(code);
|
|
1078
|
+
if (languageName && normalizeLangToken(languageName) === normalizedInput) {
|
|
1079
|
+
return code;
|
|
1080
|
+
}
|
|
1081
|
+
}
|
|
1082
|
+
return null;
|
|
1083
|
+
}
|
|
1084
|
+
function getLanguageDisplayName(code) {
|
|
1085
|
+
try {
|
|
1086
|
+
if (typeof Intl === "undefined" || !("DisplayNames" in Intl)) {
|
|
1087
|
+
return null;
|
|
1088
|
+
}
|
|
1089
|
+
const displayNames = new Intl.DisplayNames(["en"], { type: "language" });
|
|
1090
|
+
const label = displayNames.of(code);
|
|
1091
|
+
return label || null;
|
|
1092
|
+
} catch {
|
|
1093
|
+
return null;
|
|
1094
|
+
}
|
|
1095
|
+
}
|
|
1033
1096
|
function normalizeJsonType(type) {
|
|
1034
1097
|
if (Array.isArray(type)) {
|
|
1035
1098
|
const first = type[0];
|
|
@@ -1133,6 +1196,7 @@ export {
|
|
|
1133
1196
|
serializeRow,
|
|
1134
1197
|
resolvePopulate,
|
|
1135
1198
|
pluralize,
|
|
1199
|
+
parseJSONSchemaWithWarnings,
|
|
1136
1200
|
parseJSONSchema,
|
|
1137
1201
|
mergeSchemas,
|
|
1138
1202
|
isValidSchema,
|
|
@@ -5,6 +5,10 @@
|
|
|
5
5
|
* Enables AI agents to work with JSON while maintaining type-safe internal representation.
|
|
6
6
|
*/
|
|
7
7
|
import type { JSONSchemaDefinition, SchemaDefinition } from "../types";
|
|
8
|
+
export interface ParseJSONSchemaResult {
|
|
9
|
+
schema: SchemaDefinition;
|
|
10
|
+
warnings: string[];
|
|
11
|
+
}
|
|
8
12
|
/**
|
|
9
13
|
* Convert JSON schema to internal SchemaDefinition
|
|
10
14
|
*
|
|
@@ -27,3 +31,4 @@ import type { JSONSchemaDefinition, SchemaDefinition } from "../types";
|
|
|
27
31
|
* ```
|
|
28
32
|
*/
|
|
29
33
|
export declare function parseJSONSchema(json: JSONSchemaDefinition): SchemaDefinition;
|
|
34
|
+
export declare function parseJSONSchemaWithWarnings(json: JSONSchemaDefinition): ParseJSONSchemaResult;
|
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -113,8 +113,12 @@ export {
|
|
|
113
113
|
} from "./utils/populateResolver";
|
|
114
114
|
export type { PopulateAdapter } from "./utils/populateResolver";
|
|
115
115
|
|
|
116
|
-
// JSON Schema Converter
|
|
117
|
-
export {
|
|
116
|
+
// JSON Schema Converter
|
|
117
|
+
export {
|
|
118
|
+
parseJSONSchema,
|
|
119
|
+
parseJSONSchemaWithWarnings,
|
|
120
|
+
} from "./utils/jsonConverter";
|
|
121
|
+
export type { ParseJSONSchemaResult } from "./utils/jsonConverter";
|
|
118
122
|
|
|
119
123
|
// Type Helpers
|
|
120
124
|
export { isJsonType } from "./types";
|
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
* Enables AI agents to work with JSON while maintaining type-safe internal representation.
|
|
6
6
|
*/
|
|
7
7
|
|
|
8
|
-
import type {
|
|
8
|
+
import type {
|
|
9
9
|
JSONSchemaDefinition,
|
|
10
10
|
JSONFieldDefinition,
|
|
11
11
|
JSONFieldType,
|
|
@@ -13,8 +13,23 @@ import type {
|
|
|
13
13
|
FieldDefinition,
|
|
14
14
|
FieldType,
|
|
15
15
|
TableDefinition,
|
|
16
|
-
LanguageConfig,
|
|
17
|
-
} from "../types";
|
|
16
|
+
LanguageConfig,
|
|
17
|
+
} from "../types";
|
|
18
|
+
|
|
19
|
+
const LANGUAGE_NAME_TO_CODE: Record<string, string> = {
|
|
20
|
+
english: "en",
|
|
21
|
+
turkish: "tr",
|
|
22
|
+
german: "de",
|
|
23
|
+
french: "fr",
|
|
24
|
+
spanish: "es",
|
|
25
|
+
russian: "ru",
|
|
26
|
+
arabic: "ar",
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
export interface ParseJSONSchemaResult {
|
|
30
|
+
schema: SchemaDefinition;
|
|
31
|
+
warnings: string[];
|
|
32
|
+
}
|
|
18
33
|
|
|
19
34
|
/**
|
|
20
35
|
* Convert JSON schema to internal SchemaDefinition
|
|
@@ -37,11 +52,25 @@ import type {
|
|
|
37
52
|
* const schema = parseJSONSchema(jsonSchema);
|
|
38
53
|
* ```
|
|
39
54
|
*/
|
|
40
|
-
export function parseJSONSchema(json: JSONSchemaDefinition): SchemaDefinition {
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
55
|
+
export function parseJSONSchema(json: JSONSchemaDefinition): SchemaDefinition {
|
|
56
|
+
return parseJSONSchemaWithWarnings(json).schema;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export function parseJSONSchemaWithWarnings(
|
|
60
|
+
json: JSONSchemaDefinition
|
|
61
|
+
): ParseJSONSchemaResult {
|
|
62
|
+
const warnings: string[] = [];
|
|
63
|
+
const supported = (json.languages || ["en"]).map((lang) => normalizeLangToken(lang));
|
|
64
|
+
const defaultLanguage = resolveDefaultLanguage(
|
|
65
|
+
json.defaultLanguage,
|
|
66
|
+
supported,
|
|
67
|
+
warnings
|
|
68
|
+
);
|
|
69
|
+
|
|
70
|
+
const languages: LanguageConfig = {
|
|
71
|
+
default: defaultLanguage,
|
|
72
|
+
supported,
|
|
73
|
+
};
|
|
45
74
|
|
|
46
75
|
const tables: Record<string, TableDefinition> = {};
|
|
47
76
|
|
|
@@ -58,12 +87,84 @@ export function parseJSONSchema(json: JSONSchemaDefinition): SchemaDefinition {
|
|
|
58
87
|
};
|
|
59
88
|
}
|
|
60
89
|
|
|
61
|
-
return {
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
}
|
|
90
|
+
return {
|
|
91
|
+
schema: {
|
|
92
|
+
name: json.name,
|
|
93
|
+
languages,
|
|
94
|
+
tables,
|
|
95
|
+
},
|
|
96
|
+
warnings,
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
function normalizeLangToken(lang: string): string {
|
|
101
|
+
return String(lang).trim().toLowerCase();
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
function resolveDefaultLanguage(
|
|
105
|
+
input: string | undefined,
|
|
106
|
+
supported: string[],
|
|
107
|
+
warnings: string[]
|
|
108
|
+
): string {
|
|
109
|
+
const fallback = supported[0] || "en";
|
|
110
|
+
if (!input) return fallback;
|
|
111
|
+
|
|
112
|
+
const normalizedInput = normalizeLangToken(input);
|
|
113
|
+
const directMatch = supported.find(
|
|
114
|
+
(lang) => normalizeLangToken(lang) === normalizedInput
|
|
115
|
+
);
|
|
116
|
+
if (directMatch) return directMatch;
|
|
117
|
+
|
|
118
|
+
const languageNameMatch = resolveLanguageNameToSupportedCode(
|
|
119
|
+
normalizedInput,
|
|
120
|
+
supported
|
|
121
|
+
);
|
|
122
|
+
if (languageNameMatch) {
|
|
123
|
+
warnings.push(
|
|
124
|
+
`Normalized defaultLanguage "${input}" to "${languageNameMatch}".`
|
|
125
|
+
);
|
|
126
|
+
return languageNameMatch;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
throw new Error(
|
|
130
|
+
`Invalid defaultLanguage "${input}". Expected one of: ${supported.join(", ")}`
|
|
131
|
+
);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
function resolveLanguageNameToSupportedCode(
|
|
135
|
+
normalizedInput: string,
|
|
136
|
+
supported: string[]
|
|
137
|
+
): string | null {
|
|
138
|
+
const mapped = LANGUAGE_NAME_TO_CODE[normalizedInput];
|
|
139
|
+
if (mapped) {
|
|
140
|
+
const supportedMatch = supported.find(
|
|
141
|
+
(lang) => normalizeLangToken(lang) === mapped
|
|
142
|
+
);
|
|
143
|
+
if (supportedMatch) return supportedMatch;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
for (const code of supported) {
|
|
147
|
+
const languageName = getLanguageDisplayName(code);
|
|
148
|
+
if (languageName && normalizeLangToken(languageName) === normalizedInput) {
|
|
149
|
+
return code;
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
return null;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
function getLanguageDisplayName(code: string): string | null {
|
|
157
|
+
try {
|
|
158
|
+
if (typeof Intl === "undefined" || !("DisplayNames" in Intl)) {
|
|
159
|
+
return null;
|
|
160
|
+
}
|
|
161
|
+
const displayNames = new Intl.DisplayNames(["en"], { type: "language" });
|
|
162
|
+
const label = displayNames.of(code);
|
|
163
|
+
return label || null;
|
|
164
|
+
} catch {
|
|
165
|
+
return null;
|
|
166
|
+
}
|
|
167
|
+
}
|
|
67
168
|
|
|
68
169
|
/**
|
|
69
170
|
* Normalize JSON-native type syntax to internal FieldType + properties
|