-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate-config.ts
169 lines (159 loc) · 4.52 KB
/
generate-config.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import { getQdrantClient } from "./src/qdrant";
import fs from "fs";
import { promisify } from "util";
import { insertion } from "./src/utilities";
import { RESTRICTED_OBJECTS, BASE_FIELDS, BASE_TYPES, INSERT_FIELDS, INSERT_FIELDS_VECTOR } from "./src/constants";
const readFile = promisify(fs.readFile);
const writeFile = promisify(fs.writeFile);
let HASURA_CONFIGURATION_DIRECTORY = process.env["HASURA_CONFIGURATION_DIRECTORY"] as string | undefined;
if (HASURA_CONFIGURATION_DIRECTORY === undefined || HASURA_CONFIGURATION_DIRECTORY.length === 0){
HASURA_CONFIGURATION_DIRECTORY = ".";
}
const QDRANT_URL = process.env["QDRANT_URL"] as string;
let QDRANT_API_KEY = process.env["QDRANT_API_KEY"] as string | undefined;
if (QDRANT_API_KEY?.length === 0){
QDRANT_API_KEY = undefined;
}
let client = getQdrantClient(QDRANT_URL, QDRANT_API_KEY);
async function main() {
const collections = await client.getCollections();
const collectionNames = collections.collections.map((c) => c.name);
let collectionVectors: Record<string, boolean> = {};
let objectTypes: Record<string, any> = {
...BASE_TYPES,
};
for (const cn of collectionNames) {
if (RESTRICTED_OBJECTS.includes(cn)) {
throw new Error(`${cn} is a restricted name!`);
}
const { points: records } = await client.scroll(cn, {
limit: 1,
with_payload: true,
with_vector: true
});
let fieldDict = {};
let baseFields = {};
let insertFields = {};
if (records.length > 0) {
const recordPayload = records[0].payload;
fieldDict = insertion(cn, recordPayload!, objectTypes);
if (typeof records[0].id === "number"){
baseFields = {
id: {
description: null,
type: {
type: "named",
name: "Int",
},
},
...BASE_FIELDS
};
if (Array.isArray(records[0].vector)){
insertFields = {
id: {
description: null,
type: {
type: "named",
name: "Int",
},
},
...INSERT_FIELDS
};
} else {
insertFields = {
id: {
description: null,
type: {
type: "named",
name: "Int",
},
},
...INSERT_FIELDS_VECTOR
};
}
} else {
baseFields = {
id: {
description: null,
type: {
type: "named",
name: "String",
},
},
...BASE_FIELDS
};
if (Array.isArray(records[0].vector)){
insertFields = {
id: {
description: null,
type: {
type: "named",
name: "String",
},
},
...INSERT_FIELDS
};
} else {
insertFields = {
id: {
description: null,
type: {
type: "named",
name: "String",
},
},
...INSERT_FIELDS_VECTOR
};
}
}
if (!Array.isArray(records[0].vector)){
collectionVectors[cn] = true;
} else {
collectionVectors[cn] = false;
}
}
objectTypes[cn] = {
description: null,
fields: {
...fieldDict,
...baseFields,
},
};
// Need to handle that here as well for insert fields.
objectTypes[`${cn}_InsertType`] = {
description: null,
fields: {
...fieldDict,
...insertFields
}
}
}
const objectFields: Record<string, string[]> = {};
for (const [cn, objectType] of Object.entries(objectTypes)) {
objectFields[cn] = Object.keys(objectType.fields);
}
let res: any = {};
res["config"] = {
collection_names: collectionNames,
object_fields: objectFields,
object_types: objectTypes,
collection_vectors: collectionVectors,
functions: [],
procedures: [],
}
const jsonString = JSON.stringify(res, null, 4);
let filePath = `${HASURA_CONFIGURATION_DIRECTORY}/config.json`;
try {
const existingData = await readFile(filePath, 'utf8');
if (existingData !== jsonString) {
await writeFile(filePath, jsonString);
console.log('File updated.');
} else {
console.log('No changes detected. File not updated.');
}
} catch (error) {
await writeFile(filePath, jsonString);
console.log('New file written.');
}
}
main();