After installation, we can now run a simply TypeORM example. Create the relevant tsconfig.json file for your application:

{

 "compilerOptions": {

 "target": "ES2020",

 "module": "commonjs",

 "strict": true,

 "esModuleInterop": true,

 "experimentalDecorators": true,

 "strictPropertyInitialization": false,

 "emitDecoratorMetadata": true,

 "outDir": "./dist",

 "lib": ["ES2020", "DOM"],

 "typeRoots": ["./node_modules/@types"]

 },

 "include": ["typeorm_example.ts"]

}

Below is a simple code example, namely, typeorm_example.ts, accessing a Regatta Database.

Note that this example is also available as part of the downloaded driver.

Following is the output of the application:

Figure 54 TypeORM example of an application using the tool

import { DataSource, Entity, PrimaryColumn, Column } from 'typeorm-regatta';

import { v4 as uuidv4 } from 'uuid'; // Use uuid to generate unique ids

// Define an entity

@Entity()

class User {

 @PrimaryColumn()

 id: string;

 @Column()

 firstName: string;

 @Column()

 lastName: string;

 @Column()

 age: number;

}

// Create a new DataSource instance

const dataSource = new DataSource({

 type: 'regatta',

 host: 'aaa.bbb.ccc.ddd:9999',

 username: 'MyUsername',

 password: 'SomeSophisticatedPassword',

 entities: [User],

 synchronize: true,

 logging: true,

 extra: {

 deviceNames: ['m10d1']

 }

});

async function run() {

 try {

 // Initialize the DataSource

 await dataSource.initialize();

 console.log('Data Source has been initialized!');

 // Create a new User

 const user = new User();

 user.id = uuidv4(); // Use UUID for the ID

 user.firstName = 'John';

 user.lastName = 'Doe';

 user.age = 25;

 // Save the User

 const userRepository = dataSource.getRepository(User);

 await userRepository.save(user);

 console.log('User has been saved!');

 // Query the User table

 const users = await userRepository.find();

 console.log('Users:', users);

 // Update the user

 user.age = 26;

 await userRepository.save(user);

 console.log('User has been updated:', user);

 // Delete the user

 await userRepository.remove(user);

 console.log('User has been removed:', user);

 } catch (error) {

 console.error('Error:', error);

 } finally {

 // Drop the User table

 await dropUserTable();

 // Close the DataSource connection

 await dataSource.destroy();

 }

}

async function dropUserTable() {

 let queryRunner;

 try {

 // Create a query runner from a new manager

 queryRunner = dataSource.createQueryRunner();

 await queryRunner.connect();

 await queryRunner.startTransaction();

 // Run the query to drop the table

 await queryRunner.query('DROP TABLE "user"');

 console.log('User table has been dropped!');

 await queryRunner.commitTransaction();

 } catch (error) {

 console.error('Error dropping table:', error);

 } finally {

 // Release the query runner if it was created

 if (queryRunner) await queryRunner.release();

 }

}

// Run the main function

run();