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AwB;oCACjC,IAAI,EAAE,MAAM,CAAC,IAAI;oCACjB,MAAM,EAAE,IAAI;iCACf,CAAC;gCACF,QAAQ,CAAC,IAAI,CAAC,WAAW,CAAC,qBAAqB,CAAC,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAC,0BAA0B,EAAE,OAAO,CAAC,CAAC,CAAC;4BACrH,CAAC,CAAC,CAAC;wBACP,CAAC,CAAC,CAAC;wBACH,qBAAM,OAAO,CAAC,GAAG,CAAC,QAAQ,CAAC,EAAA;;wBAA3B,SAA2B,CAAC;;;;;KAC/B;IAED;;OAEG;IACH,gCAAG,GAAH;QACI,OAAO,OAAO,CAAC,OAAO,CAAC,IAAI,WAAW,EAAE,CAAC,CAAC;IAC9C,CAAC;IAEL,yBAAC;AAAD,CAjDA,AAiDC,IAAA","file":"MongoSchemaBuilder.js","sourcesContent":["import { Connection } from \"../connection/Connection\";\nimport { SchemaBuilder } from \"./SchemaBuilder\";\nimport { MongoDriver } from \"../driver/mongodb/MongoDriver\";\nimport { SqlInMemory } from \"../driver/SqlInMemory\";\nimport { MongodbIndexOptions } from \"../driver/mongodb/typings\";\n\n/**\n * Creates complete tables schemas in the database based on the entity metadatas.\n *\n * Steps how schema is being built:\n * 1. load list of all tables with complete column and keys information from the db\n * 2. drop all (old) foreign keys that exist in the table, but does not exist in the metadata\n * 3. create new tables that does not exist in the db, but exist in the metadata\n * 4. drop all columns exist (left old) in the db table, but does not exist in the metadata\n * 5. add columns from metadata which does not exist in the table\n * 6. update all exist columns which metadata has changed\n * 7. update primary keys - update old and create new primary key from changed columns\n * 8. create foreign keys which does not exist in the table yet\n * 9. create indices which are missing in db yet, and drops indices which exist in the db, but does not exist in the metadata anymore\n */\nexport class MongoSchemaBuilder implements SchemaBuilder {\n\n    // -------------------------------------------------------------------------\n    // Constructor\n    // -------------------------------------------------------------------------\n\n    constructor(protected connection: Connection) {\n    }\n\n    // -------------------------------------------------------------------------\n    // Public Methods\n    // -------------------------------------------------------------------------\n\n    /**\n     * Creates complete schemas for the given entity metadatas.\n     */\n    async build(): Promise<void> {\n        const queryRunner = (this.connection.driver as MongoDriver).createQueryRunner();\n        const promises: Promise<any>[] = [];\n        this.connection.entityMetadatas.forEach(metadata => {\n            metadata.indices.forEach(index => {\n                const options: MongodbIndexOptions = Object.assign({}, {\n                    name: index.name,\n                    unique: index.isUnique,\n                    sparse: index.isSparse,\n                    background: index.isBackground\n                }, index.expireAfterSeconds === undefined\n                    ? {}\n                    : { expireAfterSeconds: index.expireAfterSeconds });\n                promises.push(queryRunner.createCollectionIndex(metadata.tableName, index.columnNamesWithOrderingMap, options));\n            });\n            metadata.uniques.forEach(unique => {\n                const options = <MongodbIndexOptions>{\n                    name: unique.name,\n                    unique: true,\n                };\n                promises.push(queryRunner.createCollectionIndex(metadata.tableName, unique.columnNamesWithOrderingMap, options));\n            });\n        });\n        await Promise.all(promises);\n    }\n\n    /**\n     * Returns query to be executed by schema builder.\n     */\n    log(): Promise<SqlInMemory> {\n        return Promise.resolve(new SqlInMemory());\n    }\n\n}\n"],"sourceRoot":".."}