|
| 1 | +import { analyzeDocuments, StandardJSONSchema, toTypescriptTypeDefinition } from '.'; |
| 2 | + |
| 3 | +import assert from 'assert/strict'; |
| 4 | + |
| 5 | +import { |
| 6 | + BSONRegExp, |
| 7 | + Binary, |
| 8 | + Code, |
| 9 | + DBRef, |
| 10 | + Decimal128, |
| 11 | + Double, |
| 12 | + Int32, |
| 13 | + Long, |
| 14 | + MaxKey, |
| 15 | + MinKey, |
| 16 | + ObjectId, |
| 17 | + Timestamp, |
| 18 | + UUID, |
| 19 | + BSONSymbol |
| 20 | +} from 'bson'; |
| 21 | + |
| 22 | +import { inspect } from 'util'; |
| 23 | + |
| 24 | +const bsonDocuments = [ |
| 25 | + { |
| 26 | + _id: new ObjectId('642d766b7300158b1f22e972'), |
| 27 | + double: new Double(1.2), // Double, 1, double |
| 28 | + doubleThatIsAlsoAnInteger: new Double(1), // Double, 1, double |
| 29 | + string: 'Hello, world!', // String, 2, string |
| 30 | + object: { key: 'value' }, // Object, 3, object |
| 31 | + array: [1, 2, 3], // Array, 4, array |
| 32 | + binData: new Binary(Buffer.from([1, 2, 3])), // Binary data, 5, binData |
| 33 | + // Undefined, 6, undefined (deprecated) |
| 34 | + objectId: new ObjectId('642d766c7300158b1f22e975'), // ObjectId, 7, objectId |
| 35 | + boolean: true, // Boolean, 8, boolean |
| 36 | + date: new Date('2023-04-05T13:25:08.445Z'), // Date, 9, date |
| 37 | + null: null, // Null, 10, null |
| 38 | + regex: new BSONRegExp('pattern', 'i'), // Regular Expression, 11, regex |
| 39 | + // DBPointer, 12, dbPointer (deprecated) |
| 40 | + javascript: new Code('function() {}'), // JavaScript, 13, javascript |
| 41 | + symbol: new BSONSymbol('symbol'), // Symbol, 14, symbol (deprecated) |
| 42 | + javascriptWithScope: new Code('function() {}', { foo: 1, bar: 'a' }), // JavaScript code with scope 15 "javascriptWithScope" Deprecated in MongoDB 4.4. |
| 43 | + int: new Int32(12345), // 32-bit integer, 16, "int" |
| 44 | + timestamp: new Timestamp(new Long('7218556297505931265')), // Timestamp, 17, timestamp |
| 45 | + long: new Long('123456789123456789'), // 64-bit integer, 18, long |
| 46 | + decimal: new Decimal128( |
| 47 | + Buffer.from([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]) |
| 48 | + ), // Decimal128, 19, decimal |
| 49 | + minKey: new MinKey(), // Min key, -1, minKey |
| 50 | + maxKey: new MaxKey(), // Max key, 127, maxKey |
| 51 | + |
| 52 | + binaries: { |
| 53 | + generic: new Binary(Buffer.from([1, 2, 3]), 0), // 0 |
| 54 | + functionData: new Binary(Buffer.from('//8='), 1), // 1 |
| 55 | + binaryOld: new Binary(Buffer.from('//8='), 2), // 2 |
| 56 | + uuidOld: new Binary(Buffer.from('c//SZESzTGmQ6OfR38A11A=='), 3), // 3 |
| 57 | + uuid: new UUID('AAAAAAAA-AAAA-4AAA-AAAA-AAAAAAAAAAAA'), // 4 |
| 58 | + md5: new Binary(Buffer.from('c//SZESzTGmQ6OfR38A11A=='), 5), // 5 |
| 59 | + encrypted: new Binary(Buffer.from('c//SZESzTGmQ6OfR38A11A=='), 6), // 6 |
| 60 | + compressedTimeSeries: new Binary( |
| 61 | + Buffer.from( |
| 62 | + 'CQCKW/8XjAEAAIfx//////////H/////////AQAAAAAAAABfAAAAAAAAAAEAAAAAAAAAAgAAAAAAAAAHAAAAAAAAAA4AAAAAAAAAAA==', |
| 63 | + 'base64' |
| 64 | + ), |
| 65 | + 7 |
| 66 | + ), // 7 |
| 67 | + custom: new Binary(Buffer.from('//8='), 128) // 128 |
| 68 | + }, |
| 69 | + |
| 70 | + dbRef: new DBRef('namespace', new ObjectId('642d76b4b7ebfab15d3c4a78')) // not actually a separate type, just a convention |
| 71 | + |
| 72 | + // TODO: what about arrays of objects or arrays of arrays or heterogynous types in general |
| 73 | + } |
| 74 | +]; |
| 75 | + |
| 76 | +// from https://json-schema.org/learn/miscellaneous-examples#complex-object-with-nested-properties |
| 77 | +const jsonSchema: StandardJSONSchema = { |
| 78 | + $id: 'https://example.com/complex-object.schema.json', |
| 79 | + $schema: 'https://json-schema.org/draft/2020-12/schema', |
| 80 | + title: 'Complex Object', |
| 81 | + type: 'object', |
| 82 | + properties: { |
| 83 | + name: { |
| 84 | + type: 'string' |
| 85 | + }, |
| 86 | + age: { |
| 87 | + type: 'integer', |
| 88 | + minimum: 0 |
| 89 | + }, |
| 90 | + address: { |
| 91 | + type: 'object', |
| 92 | + properties: { |
| 93 | + street: { |
| 94 | + type: 'string' |
| 95 | + }, |
| 96 | + city: { |
| 97 | + type: 'string' |
| 98 | + }, |
| 99 | + state: { |
| 100 | + type: 'string' |
| 101 | + }, |
| 102 | + postalCode: { |
| 103 | + type: 'string', |
| 104 | + pattern: '\\d{5}' |
| 105 | + } |
| 106 | + }, |
| 107 | + required: ['street', 'city', 'state', 'postalCode'] |
| 108 | + }, |
| 109 | + hobbies: { |
| 110 | + type: 'array', |
| 111 | + items: { |
| 112 | + type: 'string' |
| 113 | + } |
| 114 | + } |
| 115 | + }, |
| 116 | + required: ['name', 'age'] |
| 117 | +}; |
| 118 | + |
| 119 | +describe.only('toTypescriptTypeDefinition', function() { |
| 120 | + it('converts a MongoDB JSON schema to TypeScript', async function() { |
| 121 | + const databaseName = 'myDb'; |
| 122 | + const collectionName = 'myCollection'; |
| 123 | + const analyzedDocuments = await analyzeDocuments(bsonDocuments); |
| 124 | + const schema = await analyzedDocuments.getMongoDBJsonSchema(); |
| 125 | + |
| 126 | + console.log(inspect(schema, { depth: null })); |
| 127 | + |
| 128 | + assert.equal(toTypescriptTypeDefinition(databaseName, collectionName, schema), ''); |
| 129 | + }); |
| 130 | + |
| 131 | + it('converts a standard JSON schema to TypeScript', function() { |
| 132 | + const databaseName = 'myDb'; |
| 133 | + const collectionName = 'myCollection'; |
| 134 | + |
| 135 | + console.log(inspect(jsonSchema, { depth: null })); |
| 136 | + |
| 137 | + assert.equal(toTypescriptTypeDefinition(databaseName, collectionName, jsonSchema), ''); |
| 138 | + }); |
| 139 | +}); |
0 commit comments