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

typeorm / typeorm / 19549987525

20 Nov 2025 08:11PM UTC coverage: 80.769% (+4.3%) from 76.433%
19549987525

push

github

web-flow
ci: run tests on commits to master and next (#11783)

Co-authored-by: Oleg "OSA413" Sokolov <OSA413@users.noreply.github.com>

26500 of 32174 branches covered (82.36%)

Branch coverage included in aggregate %.

91252 of 113615 relevant lines covered (80.32%)

88980.79 hits per line

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

60.05
/src/commands/InitCommand.ts
1
import ansi from "ansis"
26✔
2
import { exec } from "child_process"
26✔
3
import path from "path"
26✔
4
import yargs from "yargs"
26✔
5
import { TypeORMError } from "../error"
26✔
6
import { PlatformTools } from "../platform/PlatformTools"
26✔
7
import { CommandUtils } from "./CommandUtils"
26✔
8

26✔
9
import ourPackageJson from "../../package.json"
26✔
10

26✔
11
/**
26✔
12
 * Generates a new project with TypeORM.
26✔
13
 */
26✔
14
export class InitCommand implements yargs.CommandModule {
208✔
15
    command = "init"
208✔
16
    describe =
208✔
17
        "Generates initial TypeORM project structure. " +
208✔
18
        "If name specified then creates files inside directory called as name. " +
208✔
19
        "If its not specified then creates files inside current directory."
208✔
20

208✔
21
    builder(args: yargs.Argv) {
208✔
22
        return args
208✔
23
            .option("n", {
208✔
24
                alias: "name",
208✔
25
                describe: "Name of the project directory.",
208✔
26
            })
208✔
27
            .option("db", {
208✔
28
                alias: "database",
208✔
29
                describe: "Database type you'll use in your project.",
208✔
30
            })
208✔
31
            .option("express", {
208✔
32
                describe:
208✔
33
                    "Indicates if express server sample code should be included in the project. False by default.",
208✔
34
            })
208✔
35
            .option("docker", {
208✔
36
                describe:
208✔
37
                    "Set to true if docker-compose must be generated as well. False by default.",
208✔
38
            })
208✔
39
            .option("pm", {
208✔
40
                alias: "manager",
208✔
41
                choices: ["npm", "yarn"],
208✔
42
                default: "npm",
208✔
43
                describe: "Install packages, expected values are npm or yarn.",
208✔
44
            })
208✔
45
            .option("ms", {
208✔
46
                alias: "module",
208✔
47
                choices: ["commonjs", "esm"],
208✔
48
                default: "commonjs",
208✔
49
                describe:
208✔
50
                    "Module system to use for project, expected values are commonjs or esm.",
208✔
51
            })
208✔
52
    }
208✔
53

208✔
54
    async handler(args: yargs.Arguments) {
208✔
55
        try {
208✔
56
            const database: string = (args.database as any) || "postgres"
208!
57
            const isExpress = args.express !== undefined ? true : false
208!
58
            const isDocker = args.docker !== undefined ? true : false
208!
59
            const basePath = process.cwd() + (args.name ? "/" + args.name : "")
208!
60
            const projectName = args.name
208✔
61
                ? path.basename(args.name as any)
208✔
62
                : undefined
208!
63
            const installNpm = args.pm === "yarn" ? false : true
208!
64
            const projectIsEsm = args.ms === "esm"
208✔
65
            await CommandUtils.createFile(
208✔
66
                basePath + "/package.json",
208✔
67
                InitCommand.getPackageJsonTemplate(projectName, projectIsEsm),
208✔
68
                false,
208✔
69
            )
208✔
70
            if (isDocker)
208✔
71
                await CommandUtils.createFile(
208!
72
                    basePath + "/docker-compose.yml",
×
73
                    InitCommand.getDockerComposeTemplate(database),
×
74
                    false,
×
75
                )
×
76
            await CommandUtils.createFile(
208✔
77
                basePath + "/.gitignore",
208✔
78
                InitCommand.getGitIgnoreFile(),
208✔
79
            )
208✔
80
            await CommandUtils.createFile(
208✔
81
                basePath + "/README.md",
208✔
82
                InitCommand.getReadmeTemplate({ docker: isDocker }),
208✔
83
                false,
208✔
84
            )
208✔
85
            await CommandUtils.createFile(
208✔
86
                basePath + "/tsconfig.json",
208✔
87
                InitCommand.getTsConfigTemplate(projectIsEsm),
208✔
88
            )
208✔
89
            await CommandUtils.createFile(
208✔
90
                basePath + "/src/entity/User.ts",
208✔
91
                InitCommand.getUserEntityTemplate(database),
208✔
92
            )
208✔
93
            await CommandUtils.createFile(
208✔
94
                basePath + "/src/data-source.ts",
208✔
95
                InitCommand.getAppDataSourceTemplate(projectIsEsm, database),
208✔
96
            )
208✔
97
            await CommandUtils.createFile(
208✔
98
                basePath + "/src/index.ts",
208✔
99
                InitCommand.getAppIndexTemplate(isExpress, projectIsEsm),
208✔
100
            )
208✔
101
            await CommandUtils.createDirectories(basePath + "/src/migration")
208✔
102

208✔
103
            // generate extra files for express application
208✔
104
            if (isExpress) {
208!
105
                await CommandUtils.createFile(
×
106
                    basePath + "/src/routes.ts",
×
107
                    InitCommand.getRoutesTemplate(projectIsEsm),
×
108
                )
×
109
                await CommandUtils.createFile(
×
110
                    basePath + "/src/controller/UserController.ts",
×
111
                    InitCommand.getControllerTemplate(projectIsEsm),
×
112
                )
×
113
            }
×
114

208✔
115
            const packageJsonContents = await CommandUtils.readFile(
208✔
116
                basePath + "/package.json",
208✔
117
            )
208✔
118
            await CommandUtils.createFile(
208✔
119
                basePath + "/package.json",
208✔
120
                InitCommand.appendPackageJson(
208✔
121
                    packageJsonContents,
208✔
122
                    database,
208✔
123
                    isExpress,
208✔
124
                    projectIsEsm,
208✔
125
                ),
208✔
126
            )
208✔
127

208✔
128
            if (args.name) {
208✔
129
                console.log(
208✔
130
                    ansi.green`Project created inside ${ansi.blue(
208✔
131
                        basePath,
208✔
132
                    )} directory.`,
208✔
133
                )
208✔
134
            } else {
208!
135
                console.log(
×
136
                    ansi.green`Project created inside current directory.`,
×
137
                )
×
138
            }
×
139

208✔
140
            console.log(ansi.green`Please wait, installing dependencies...`)
208✔
141
            if (args.pm && installNpm) {
208✔
142
                await InitCommand.executeCommand("npm install", basePath)
208✔
143
            } else {
208!
144
                await InitCommand.executeCommand("yarn install", basePath)
×
145
            }
×
146

208✔
147
            console.log(ansi.green`Done! Start playing with a new project!`)
208✔
148
        } catch (err) {
208!
149
            PlatformTools.logCmdErr("Error during project initialization:", err)
×
150
            process.exit(1)
×
151
        }
×
152
    }
208✔
153

208✔
154
    // -------------------------------------------------------------------------
208✔
155
    // Protected Static Methods
208✔
156
    // -------------------------------------------------------------------------
208✔
157

208✔
158
    protected static executeCommand(command: string, cwd: string) {
208✔
159
        return new Promise<string>((ok, fail) => {
208✔
160
            exec(command, { cwd }, (error: any, stdout: any, stderr: any) => {
208✔
161
                if (stdout) return ok(stdout)
208✔
162
                if (stderr) return fail(stderr)
×
163
                if (error) return fail(error)
×
164
                ok("")
×
165
            })
208✔
166
        })
208✔
167
    }
208✔
168

208✔
169
    /**
208✔
170
     * Gets contents of the ormconfig file.
208✔
171
     */
208✔
172
    protected static getAppDataSourceTemplate(
208✔
173
        isEsm: boolean,
208✔
174
        database: string,
208✔
175
    ): string {
208✔
176
        let dbSettings = ""
208✔
177
        switch (database) {
208✔
178
            case "mysql":
208✔
179
                dbSettings = `type: "mysql",
26✔
180
    host: "localhost",
26✔
181
    port: 3306,
26✔
182
    username: "test",
26✔
183
    password: "test",
26✔
184
    database: "test",`
26✔
185
                break
26✔
186
            case "mariadb":
208✔
187
                dbSettings = `type: "mariadb",
26✔
188
    host: "localhost",
26✔
189
    port: 3306,
26✔
190
    username: "test",
26✔
191
    password: "test",
26✔
192
    database: "test",`
26✔
193
                break
26✔
194
            case "sqlite":
208✔
195
                dbSettings = `type: "sqlite",
26✔
196
    database: "database.sqlite",`
26✔
197
                break
26✔
198
            case "better-sqlite3":
208✔
199
                dbSettings = `type: "better-sqlite3",
26✔
200
    database: "database.sqlite",`
26✔
201
                break
26✔
202
            case "postgres":
208✔
203
                dbSettings = `type: "postgres",
26✔
204
    host: "localhost",
26✔
205
    port: 5432,
26✔
206
    username: "test",
26✔
207
    password: "test",
26✔
208
    database: "test",`
26✔
209
                break
26✔
210
            case "cockroachdb":
208✔
211
                dbSettings = `type: "cockroachdb",
26✔
212
    host: "localhost",
26✔
213
    port: 26257,
26✔
214
    username: "root",
26✔
215
    password: "",
26✔
216
    database: "defaultdb",`
26✔
217
                break
26✔
218
            case "mssql":
208✔
219
                dbSettings = `type: "mssql",
26✔
220
    host: "localhost",
26✔
221
    username: "sa",
26✔
222
    password: "Admin12345",
26✔
223
    database: "tempdb",`
26✔
224
                break
26✔
225
            case "oracle":
208!
226
                dbSettings = `type: "oracle",
×
227
host: "localhost",
×
228
username: "system",
×
229
password: "oracle",
×
230
port: 1521,
×
231
sid: "xe.oracle.docker",`
×
232
                break
×
233
            case "mongodb":
208✔
234
                dbSettings = `type: "mongodb",
26✔
235
    database: "test",`
26✔
236
                break
26✔
237
            case "spanner":
208!
238
                dbSettings = `type: "spanner",
×
239
    projectId: "test",
×
240
    instanceId: "test",
×
241
    databaseId: "test",`
×
242
                break
×
243
        }
208✔
244
        return `import "reflect-metadata"
208✔
245
import { DataSource } from "typeorm"
208✔
246
import { User } from "./entity/User${isEsm ? ".js" : ""}"
208!
247

208✔
248
export const AppDataSource = new DataSource({
208✔
249
    ${dbSettings}
208✔
250
    synchronize: true,
208✔
251
    logging: false,
208✔
252
    entities: [User],
208✔
253
    migrations: [],
208✔
254
    subscribers: [],
208✔
255
})
208✔
256
`
208✔
257
    }
208✔
258

208✔
259
    /**
208✔
260
     * Gets contents of the ormconfig file.
208✔
261
     */
208✔
262
    protected static getTsConfigTemplate(esmModule: boolean): string {
208✔
263
        if (esmModule)
208✔
264
            return JSON.stringify(
208!
265
                {
×
266
                    compilerOptions: {
×
267
                        lib: ["es2021"],
×
268
                        target: "es2021",
×
269
                        module: "es2022",
×
270
                        moduleResolution: "node",
×
271
                        allowSyntheticDefaultImports: true,
×
272
                        outDir: "./build",
×
273
                        emitDecoratorMetadata: true,
×
274
                        experimentalDecorators: true,
×
275
                        sourceMap: true,
×
276
                    },
×
277
                },
×
278
                undefined,
×
279
                3,
×
280
            )
×
281
        else
208✔
282
            return JSON.stringify(
208✔
283
                {
208✔
284
                    compilerOptions: {
208✔
285
                        lib: ["es2021"],
208✔
286
                        target: "es2021",
208✔
287
                        module: "commonjs",
208✔
288
                        moduleResolution: "node",
208✔
289
                        outDir: "./build",
208✔
290
                        emitDecoratorMetadata: true,
208✔
291
                        experimentalDecorators: true,
208✔
292
                        sourceMap: true,
208✔
293
                    },
208✔
294
                },
208✔
295
                undefined,
208✔
296
                3,
208✔
297
            )
208✔
298
    }
208✔
299

208✔
300
    /**
208✔
301
     * Gets contents of the .gitignore file.
208✔
302
     */
208✔
303
    protected static getGitIgnoreFile(): string {
208✔
304
        return `.idea/
208✔
305
.vscode/
208✔
306
node_modules/
208✔
307
build/
208✔
308
tmp/
208✔
309
temp/`
208✔
310
    }
208✔
311

208✔
312
    /**
208✔
313
     * Gets contents of the user entity.
208✔
314
     */
208✔
315
    protected static getUserEntityTemplate(database: string): string {
208✔
316
        return `import { Entity, ${
208✔
317
            database === "mongodb"
208✔
318
                ? "ObjectIdColumn, ObjectId"
208✔
319
                : "PrimaryGeneratedColumn"
208✔
320
        }, Column } from "typeorm"
208✔
321

208✔
322
@Entity()
208✔
323
export class User {
208✔
324

208✔
325
    ${
208✔
326
        database === "mongodb"
208✔
327
            ? "@ObjectIdColumn()"
208✔
328
            : "@PrimaryGeneratedColumn()"
208✔
329
    }
208✔
330
    id: ${database === "mongodb" ? "ObjectId" : "number"}
208✔
331

208✔
332
    @Column()
208✔
333
    firstName: string
208✔
334

208✔
335
    @Column()
208✔
336
    lastName: string
208✔
337

208✔
338
    @Column()
208✔
339
    age: number
208✔
340

208✔
341
}
208✔
342
`
208✔
343
    }
208✔
344

208✔
345
    /**
208✔
346
     * Gets contents of the route file (used when express is enabled).
208✔
347
     */
208✔
348
    protected static getRoutesTemplate(isEsm: boolean): string {
208✔
349
        return `import { UserController } from "./controller/UserController${
×
350
            isEsm ? ".js" : ""
×
351
        }"
×
352

×
353
export const Routes = [{
×
354
    method: "get",
×
355
    route: "/users",
×
356
    controller: UserController,
×
357
    action: "all"
×
358
}, {
×
359
    method: "get",
×
360
    route: "/users/:id",
×
361
    controller: UserController,
×
362
    action: "one"
×
363
}, {
×
364
    method: "post",
×
365
    route: "/users",
×
366
    controller: UserController,
×
367
    action: "save"
×
368
}, {
×
369
    method: "delete",
×
370
    route: "/users/:id",
×
371
    controller: UserController,
×
372
    action: "remove"
×
373
}]`
×
374
    }
×
375

208✔
376
    /**
208✔
377
     * Gets contents of the user controller file (used when express is enabled).
208✔
378
     */
208✔
379
    protected static getControllerTemplate(isEsm: boolean): string {
208✔
380
        return `import { AppDataSource } from "../data-source${
×
381
            isEsm ? ".js" : ""
×
382
        }"
×
383
import { NextFunction, Request, Response } from "express"
×
384
import { User } from "../entity/User${isEsm ? ".js" : ""}"
×
385

×
386
export class UserController {
×
387

×
388
    private userRepository = AppDataSource.getRepository(User)
×
389

×
390
    async all(request: Request, response: Response, next: NextFunction) {
×
391
        return this.userRepository.find()
×
392
    }
×
393

×
394
    async one(request: Request, response: Response, next: NextFunction) {
×
395
        const id = parseInt(request.params.id)
×
396

×
397

×
398
        const user = await this.userRepository.findOne({
×
399
            where: { id }
×
400
        })
×
401

×
402
        if (!user) {
×
403
            return "unregistered user"
×
404
        }
×
405
        return user
×
406
    }
×
407

×
408
    async save(request: Request, response: Response, next: NextFunction) {
×
409
        const { firstName, lastName, age } = request.body;
×
410

×
411
        const user = Object.assign(new User(), {
×
412
            firstName,
×
413
            lastName,
×
414
            age
×
415
        })
×
416

×
417
        return this.userRepository.save(user)
×
418
    }
×
419

×
420
    async remove(request: Request, response: Response, next: NextFunction) {
×
421
        const id = parseInt(request.params.id)
×
422

×
423
        let userToRemove = await this.userRepository.findOneBy({ id })
×
424

×
425
        if (!userToRemove) {
×
426
            return "this user not exist"
×
427
        }
×
428

×
429
        await this.userRepository.remove(userToRemove)
×
430

×
431
        return "user has been removed"
×
432
    }
×
433

×
434
}`
×
435
    }
×
436

208✔
437
    /**
208✔
438
     * Gets contents of the main (index) application file.
208✔
439
     */
208✔
440
    protected static getAppIndexTemplate(
208✔
441
        express: boolean,
208✔
442
        isEsm: boolean,
208✔
443
    ): string {
208✔
444
        if (express) {
208!
445
            return `import ${!isEsm ? "* as " : ""}express from "express"
×
446
import ${!isEsm ? "* as " : ""}bodyParser from "body-parser"
×
447
import { Request, Response } from "express"
×
448
import { AppDataSource } from "./data-source${isEsm ? ".js" : ""}"
×
449
import { Routes } from "./routes${isEsm ? ".js" : ""}"
×
450
import { User } from "./entity/User${isEsm ? ".js" : ""}"
×
451

×
452
AppDataSource.initialize().then(async () => {
×
453

×
454
    // create express app
×
455
    const app = express()
×
456
    app.use(bodyParser.json())
×
457

×
458
    // register express routes from defined application routes
×
459
    Routes.forEach(route => {
×
460
        (app as any)[route.method](route.route, (req: Request, res: Response, next: Function) => {
×
461
            const result = (new (route.controller as any))[route.action](req, res, next)
×
462
            if (result instanceof Promise) {
×
463
                result.then(result => result !== null && result !== undefined ? res.send(result) : undefined)
×
464

×
465
            } else if (result !== null && result !== undefined) {
×
466
                res.json(result)
×
467
            }
×
468
        })
×
469
    })
×
470

×
471
    // setup express app here
×
472
    // ...
×
473

×
474
    // start express server
×
475
    app.listen(3000)
×
476

×
477
    // insert new users for test
×
478
    await AppDataSource.manager.save(
×
479
        AppDataSource.manager.create(User, {
×
480
            firstName: "Timber",
×
481
            lastName: "Saw",
×
482
            age: 27
×
483
        })
×
484
    )
×
485

×
486
    await AppDataSource.manager.save(
×
487
        AppDataSource.manager.create(User, {
×
488
            firstName: "Phantom",
×
489
            lastName: "Assassin",
×
490
            age: 24
×
491
        })
×
492
    )
×
493

×
494
    console.log("Express server has started on port 3000. Open http://localhost:3000/users to see results")
×
495

×
496
}).catch(error => console.log(error))
×
497
`
×
498
        } else {
208✔
499
            return `import { AppDataSource } from "./data-source${
208✔
500
                isEsm ? ".js" : ""
208!
501
            }"
208✔
502
import { User } from "./entity/User${isEsm ? ".js" : ""}"
208!
503

208✔
504
AppDataSource.initialize().then(async () => {
208✔
505

208✔
506
    console.log("Inserting a new user into the database...")
208✔
507
    const user = new User()
208✔
508
    user.firstName = "Timber"
208✔
509
    user.lastName = "Saw"
208✔
510
    user.age = 25
208✔
511
    await AppDataSource.manager.save(user)
208✔
512
    console.log("Saved a new user with id: " + user.id)
208✔
513

208✔
514
    console.log("Loading users from the database...")
208✔
515
    const users = await AppDataSource.manager.find(User)
208✔
516
    console.log("Loaded users: ", users)
208✔
517

208✔
518
    console.log("Here you can setup and run express / fastify / any other framework.")
208✔
519

208✔
520
}).catch(error => console.log(error))
208✔
521
`
208✔
522
        }
208✔
523
    }
208✔
524

208✔
525
    /**
208✔
526
     * Gets contents of the new package.json file.
208✔
527
     */
208✔
528
    protected static getPackageJsonTemplate(
208✔
529
        projectName?: string,
208✔
530
        projectIsEsm?: boolean,
208✔
531
    ): string {
208✔
532
        return JSON.stringify(
208✔
533
            {
208✔
534
                name: projectName || "typeorm-sample",
208!
535
                version: "0.0.1",
208✔
536
                description: "Awesome project developed with TypeORM.",
208✔
537
                type: projectIsEsm ? "module" : "commonjs",
208!
538
                devDependencies: {},
208✔
539
                dependencies: {},
208✔
540
                scripts: {},
208✔
541
            },
208✔
542
            undefined,
208✔
543
            3,
208✔
544
        )
208✔
545
    }
208✔
546

208✔
547
    /**
208✔
548
     * Gets contents of the new docker-compose.yml file.
208✔
549
     */
208✔
550
    protected static getDockerComposeTemplate(database: string): string {
208✔
551
        switch (database) {
×
552
            case "mysql":
×
553
                return `services:
×
554

×
555
  mysql:
×
556
    image: "mysql:9.2.0"
×
557
    ports:
×
558
      - "3306:3306"
×
559
    environment:
×
560
      MYSQL_ROOT_PASSWORD: "admin"
×
561
      MYSQL_USER: "test"
×
562
      MYSQL_PASSWORD: "test"
×
563
      MYSQL_DATABASE: "test"
×
564

×
565
`
×
566
            case "mariadb":
×
567
                return `services:
×
568

×
569
  mariadb:
×
570
    image: "mariadb:11.7.2"
×
571
    ports:
×
572
      - "3306:3306"
×
573
    environment:
×
574
      MYSQL_ROOT_PASSWORD: "admin"
×
575
      MYSQL_USER: "test"
×
576
      MYSQL_PASSWORD: "test"
×
577
      MYSQL_DATABASE: "test"
×
578

×
579
`
×
580
            case "postgres":
×
581
                return `services:
×
582

×
583
  postgres:
×
584
    image: "postgres:17.2"
×
585
    ports:
×
586
      - "5432:5432"
×
587
    environment:
×
588
      POSTGRES_USER: "test"
×
589
      POSTGRES_PASSWORD: "test"
×
590
      POSTGRES_DB: "test"
×
591

×
592
`
×
593
            case "cockroachdb":
×
594
                return `services:
×
595

×
596
  cockroachdb:
×
597
    image: "cockroachdb/cockroach:v25.1.2"
×
598
    command: start --insecure
×
599
    ports:
×
600
      - "26257:26257"
×
601

×
602
`
×
603
            case "sqlite":
×
604
            case "better-sqlite3":
×
605
                return `services:
×
606
`
×
607
            case "oracle":
×
608
                throw new TypeORMError(
×
609
                    `You cannot initialize a project with docker for Oracle driver yet.`,
×
610
                ) // todo: implement for oracle as well
×
611

×
612
            case "mssql":
×
613
                return `services:
×
614

×
615
  mssql:
×
616
    image: "mcr.microsoft.com/mssql/server:2022-latest"
×
617
    ports:
×
618
      - "1433:1433"
×
619
    environment:
×
620
      SA_PASSWORD: "Admin12345"
×
621
      ACCEPT_EULA: "Y"
×
622
      MSSQL_PID: "Express"
×
623

×
624
`
×
625
            case "mongodb":
×
626
                return `services:
×
627

×
628
  mongodb:
×
629
    image: "mongo:8"
×
630
    container_name: "typeorm-mongodb"
×
631
    ports:
×
632
      - "27017:27017"
×
633

×
634
`
×
635
            case "spanner":
×
636
                return `services:
×
637

×
638
  spanner:
×
639
    image: gcr.io/cloud-spanner-emulator/emulator:1.5.30
×
640
    ports:
×
641
      - "9010:9010"
×
642
      - "9020:9020"
×
643

×
644
`
×
645
        }
×
646
        return ""
×
647
    }
×
648

208✔
649
    /**
208✔
650
     * Gets contents of the new readme.md file.
208✔
651
     */
208✔
652
    protected static getReadmeTemplate(options: { docker: boolean }): string {
208✔
653
        let template = `# Awesome Project Build with TypeORM
208✔
654

208✔
655
Steps to run this project:
208✔
656

208✔
657
1. Run \`npm i\` command
208✔
658
`
208✔
659

208✔
660
        if (options.docker) {
208!
661
            template += `2. Run \`docker-compose up\` command
×
662
`
×
663
        } else {
208✔
664
            template += `2. Setup database settings inside \`data-source.ts\` file
208✔
665
`
208✔
666
        }
208✔
667

208✔
668
        template += `3. Run \`npm start\` command
208✔
669
`
208✔
670
        return template
208✔
671
    }
208✔
672

208✔
673
    /**
208✔
674
     * Appends to a given package.json template everything needed.
208✔
675
     */
208✔
676
    protected static appendPackageJson(
208✔
677
        packageJsonContents: string,
208✔
678
        database: string,
208✔
679
        express: boolean,
208✔
680
        projectIsEsm: boolean /*, docker: boolean*/,
208✔
681
    ): string {
208✔
682
        const packageJson = JSON.parse(packageJsonContents)
208✔
683

208✔
684
        if (!packageJson.devDependencies) packageJson.devDependencies = {}
208!
685
        packageJson.devDependencies = {
208✔
686
            "@types/node": ourPackageJson.devDependencies["@types/node"],
208✔
687
            "ts-node": ourPackageJson.devDependencies["ts-node"],
208✔
688
            typescript: ourPackageJson.devDependencies.typescript,
208✔
689
            ...packageJson.devDependencies,
208✔
690
        }
208✔
691

208✔
692
        if (!packageJson.dependencies) packageJson.dependencies = {}
208!
693
        packageJson.dependencies = {
208✔
694
            ...packageJson.dependencies,
208✔
695
            "reflect-metadata": ourPackageJson.dependencies["reflect-metadata"],
208✔
696
            typeorm: ourPackageJson.version,
208✔
697
        }
208✔
698

208✔
699
        switch (database) {
208✔
700
            case "mysql":
208✔
701
            case "mariadb":
208✔
702
                packageJson.dependencies["mysql2"] =
52✔
703
                    ourPackageJson.devDependencies.mysql2
52✔
704
                break
52✔
705
            case "postgres":
208✔
706
            case "cockroachdb":
208✔
707
                packageJson.dependencies["pg"] =
52✔
708
                    ourPackageJson.devDependencies.pg
52✔
709
                break
52✔
710
            case "sqlite":
208✔
711
                packageJson.dependencies["sqlite3"] =
26✔
712
                    ourPackageJson.devDependencies.sqlite3
26✔
713
                break
26✔
714
            case "better-sqlite3":
208✔
715
                packageJson.dependencies["better-sqlite3"] =
26✔
716
                    ourPackageJson.devDependencies["better-sqlite3"]
26✔
717
                break
26✔
718
            case "oracle":
208!
719
                packageJson.dependencies["oracledb"] =
×
720
                    ourPackageJson.devDependencies.oracledb
×
721
                break
×
722
            case "mssql":
208✔
723
                packageJson.dependencies["mssql"] =
26✔
724
                    ourPackageJson.devDependencies.mssql
26✔
725
                break
26✔
726
            case "mongodb":
208✔
727
                packageJson.dependencies["mongodb"] =
26✔
728
                    ourPackageJson.devDependencies.mongodb
26✔
729
                break
26✔
730
            case "spanner":
208!
731
                packageJson.dependencies["@google-cloud/spanner"] =
×
732
                    ourPackageJson.devDependencies["@google-cloud/spanner"]
×
733
                break
×
734
        }
208✔
735

208✔
736
        if (express) {
208!
737
            packageJson.dependencies["express"] = "^4.21.2"
×
738
            packageJson.dependencies["body-parser"] = "^1.20.3"
×
739
        }
×
740

208✔
741
        if (!packageJson.scripts) packageJson.scripts = {}
208!
742

208✔
743
        if (projectIsEsm)
208✔
744
            Object.assign(packageJson.scripts, {
208!
745
                start: /*(docker ? "docker-compose up && " : "") + */ "node --loader ts-node/esm src/index.ts",
×
746
                typeorm: "typeorm-ts-node-esm",
×
747
            })
×
748
        else
208✔
749
            Object.assign(packageJson.scripts, {
208✔
750
                start: /*(docker ? "docker-compose up && " : "") + */ "ts-node src/index.ts",
208✔
751
                typeorm: "typeorm-ts-node-commonjs",
208✔
752
            })
208✔
753

208✔
754
        return JSON.stringify(packageJson, undefined, 3)
208✔
755
    }
208✔
756
}
208✔
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

© 2026 Coveralls, Inc