• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

typeorm / typeorm / 14796576772

02 May 2025 01:52PM UTC coverage: 45.367% (-30.9%) from 76.309%
14796576772

Pull #11434

github

web-flow
Merge ec4ce2d00 into fadad1a74
Pull Request #11434: feat: release PR releases using pkg.pr.new

5216 of 12761 branches covered (40.87%)

Branch coverage included in aggregate %.

11439 of 23951 relevant lines covered (47.76%)

15712.55 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

12.5
/src/schema-builder/MongoSchemaBuilder.ts
1
import { DataSource } from "../data-source/DataSource"
2
import { SchemaBuilder } from "./SchemaBuilder"
3
import { MongoQueryRunner } from "../driver/mongodb/MongoQueryRunner"
4
import { SqlInMemory } from "../driver/SqlInMemory"
4✔
5
import { CreateIndexesOptions } from "../driver/mongodb/typings"
6

7
/**
8
 * Creates complete tables schemas in the database based on the entity metadatas.
9
 *
10
 * Steps how schema is being built:
11
 * 1. load list of all tables with complete column and keys information from the db
12
 * 2. drop all (old) foreign keys that exist in the table, but does not exist in the metadata
13
 * 3. create new tables that does not exist in the db, but exist in the metadata
14
 * 4. drop all columns exist (left old) in the db table, but does not exist in the metadata
15
 * 5. add columns from metadata which does not exist in the table
16
 * 6. update all exist columns which metadata has changed
17
 * 7. update primary keys - update old and create new primary key from changed columns
18
 * 8. create foreign keys which does not exist in the table yet
19
 * 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
20
 */
21
export class MongoSchemaBuilder implements SchemaBuilder {
4✔
22
    // -------------------------------------------------------------------------
23
    // Constructor
24
    // -------------------------------------------------------------------------
25

26
    constructor(protected connection: DataSource) {}
×
27

28
    // -------------------------------------------------------------------------
29
    // Public Methods
30
    // -------------------------------------------------------------------------
31

32
    /**
33
     * Creates complete schemas for the given entity metadatas.
34
     */
35
    async build(): Promise<void> {
36
        const queryRunner =
37
            this.connection.createQueryRunner() as MongoQueryRunner
×
38
        const promises: Promise<any>[] = []
×
39
        this.connection.entityMetadatas.forEach((metadata) => {
×
40
            metadata.indices.forEach((index) => {
×
41
                const options: CreateIndexesOptions = Object.assign(
×
42
                    {},
43
                    {
44
                        name: index.name,
45
                        unique: index.isUnique,
46
                        sparse: index.isSparse,
47
                        background: index.isBackground,
48
                    },
49
                    index.expireAfterSeconds === undefined
×
50
                        ? {}
51
                        : { expireAfterSeconds: index.expireAfterSeconds },
52
                )
53
                promises.push(
×
54
                    queryRunner.createCollectionIndex(
55
                        metadata.tableName,
56
                        index.columnNamesWithOrderingMap,
57
                        options,
58
                    ),
59
                )
60
            })
61
            metadata.uniques.forEach((unique) => {
×
62
                const options = <CreateIndexesOptions>{
×
63
                    name: unique.name,
64
                    unique: true,
65
                }
66
                promises.push(
×
67
                    queryRunner.createCollectionIndex(
68
                        metadata.tableName,
69
                        unique.columnNamesWithOrderingMap,
70
                        options,
71
                    ),
72
                )
73
            })
74
        })
75
        await Promise.all(promises)
×
76
    }
77

78
    /**
79
     * Returns query to be executed by schema builder.
80
     */
81
    log(): Promise<SqlInMemory> {
82
        return Promise.resolve(new SqlInMemory())
×
83
    }
84
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc