|
|
import { MongoClient, ObjectId, BSON, BSONType, Decimal128, Binary } from 'npm:mongodb';
|
|
|
|
|
|
interface Schema {
|
|
|
[key: string]: {
|
|
|
type: string;
|
|
|
required: boolean;
|
|
|
};
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
interface MongoTypeMap {
|
|
|
String: string;
|
|
|
Integer: number;
|
|
|
Double: number;
|
|
|
Decimal128: number;
|
|
|
Object: object;
|
|
|
Array: any[];
|
|
|
Binary: Uint8Array;
|
|
|
Timestamp: Date;
|
|
|
Date: Date;
|
|
|
ObjectId: string;
|
|
|
Boolean: boolean;
|
|
|
}
|
|
|
|
|
|
type MongoTypeKeys = keyof MongoTypeMap;
|
|
|
function getFieldTypeFromValue(value: any): any {
|
|
|
console.log(value instanceof Date)
|
|
|
if (value instanceof ObjectId) {
|
|
|
return ObjectId;
|
|
|
}
|
|
|
if (value instanceof Date) {
|
|
|
return Date;
|
|
|
}
|
|
|
if (value instanceof Decimal128) {
|
|
|
return Number;
|
|
|
}
|
|
|
if (value instanceof Binary) {
|
|
|
return 'Binary';
|
|
|
}
|
|
|
if (Array.isArray(value)) {
|
|
|
const [firstElem] = value;
|
|
|
const innerType = getFieldTypeFromValue(firstElem);
|
|
|
return `${innerType}[]`;
|
|
|
}
|
|
|
if (typeof value === 'object' && value !== null) {
|
|
|
const keys = Object.keys(value);
|
|
|
const innerTypes = keys.map((key) => {
|
|
|
const innerValue = value[key];
|
|
|
const innerType = getFieldTypeFromValue(innerValue);
|
|
|
return `${key}: ${innerType}`;
|
|
|
});
|
|
|
return `{ ${innerTypes.join(', ')} }`;
|
|
|
}
|
|
|
return typeof value;
|
|
|
}
|
|
|
|
|
|
function mongoSchemaToType(schema: any): any {
|
|
|
const fields: any = {};
|
|
|
|
|
|
for (const [key, value] of Object.entries(schema)) {
|
|
|
console.log("🚀 ~ file: convert_types.ts:105 ~ mongoSchemaToType ~ value:", value)
|
|
|
if (Array.isArray(value)) {
|
|
|
fields[key] = "Array";
|
|
|
|
|
|
if (value.length > 0) {
|
|
|
const subType = getFieldTypeFromValue(value[0]);
|
|
|
if (subType === "Object") {
|
|
|
fields[key] = [mongoSchemaToType(value[0])];
|
|
|
} else {
|
|
|
fields[key] = [subType];
|
|
|
}
|
|
|
}
|
|
|
// } else if (typeof value === "object" && value !== null) {
|
|
|
// fields[key] = "Object";
|
|
|
// fields[key] = mongoSchemaToType(value);
|
|
|
} else {
|
|
|
const fieldType = getFieldTypeFromValue(value);
|
|
|
fields[key] = fieldType;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
return fields;
|
|
|
}
|
|
|
|
|
|
|
|
|
async function getSchemaFromCollection(uri: string, dbName: string, collectionName: string, sampleSize: number = 1000): Promise<Schema> {
|
|
|
const client = new MongoClient(uri);
|
|
|
await client.connect();
|
|
|
const db = client.db(dbName);
|
|
|
const collection = db.collection(collectionName);
|
|
|
const stats = await db.command({ collStats: collectionName });
|
|
|
console.log("🚀 ~ file: convert_types.ts:56 ~ getSchemaFromCollection ~ stats:", stats)
|
|
|
// Alanların tiplerini elde et
|
|
|
const fieldTypes: { [key: string]: string } = {};
|
|
|
for (const key in stats['fields']) {
|
|
|
const fieldStats = stats['fields'][key];
|
|
|
fieldTypes[key] = fieldStats['type'];
|
|
|
}
|
|
|
|
|
|
const sampleDocs = await collection.aggregate([{ $sample: { size: sampleSize } }]).toArray();
|
|
|
// console.log("🚀 ~ file: convert_types.ts:16 ~ getSchemaFromCollection ~ sampleDocs:", sampleDocs)
|
|
|
await client.close();
|
|
|
const schema: any = {};
|
|
|
sampleDocs.forEach((doc) => {
|
|
|
Object.entries(doc).forEach(([key, value]) => {
|
|
|
schema[key] = value
|
|
|
});
|
|
|
});
|
|
|
return schema;
|
|
|
}
|
|
|
|
|
|
// Usage example
|
|
|
getSchemaFromCollection('mongodb://127.0.0.1:27017', 'mestgps', 'takip_gps_yeni').then(async (schema) => {
|
|
|
const data = mongoSchemaToType(schema)
|
|
|
for (let prop in data) {
|
|
|
console.log("ccc", data[prop], typeof data[prop])
|
|
|
if (typeof data[prop] === 'string') {
|
|
|
data[prop] = data[prop].replace(/^"(.*)"$/, '$1').replace(/^'(.*)'$/, '$1');
|
|
|
}
|
|
|
}
|
|
|
console.log(data);
|
|
|
|
|
|
// const schema = {};
|
|
|
|
|
|
// console.log(schema);
|
|
|
// console.log(interfaceCode);
|
|
|
});
|