Skip to content

Commit 645dde2

Browse files
committed
get rid of deprecated createConnection
1 parent bf381d0 commit 645dde2

File tree

6 files changed

+63
-57
lines changed

6 files changed

+63
-57
lines changed

plugins/node/instrumentation-typeorm/test/Connection.test.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,10 @@ describe('Connection', () => {
4848
describe('single connection', () => {
4949
it('raw query', async () => {
5050
const options = rawQueryOptions;
51-
const connection = await typeorm.createConnection(rawQueryOptions);
51+
const ds = new typeorm.DataSource(options);
52+
await ds.initialize();
5253
const query = 'select * from user';
53-
await connection.query(query);
54+
await ds.query(query);
5455
const typeOrmSpans = getTestSpans();
5556

5657
assert.strictEqual(typeOrmSpans.length, 1);
@@ -60,7 +61,7 @@ describe('Connection', () => {
6061
assert.strictEqual(attributes[ATTR_DB_NAMESPACE], options.database);
6162
assert.strictEqual(attributes[ATTR_DB_OPERATION_NAME], 'raw query');
6263
assert.strictEqual(attributes[ATTR_DB_QUERY_TEXT], query);
63-
await connection.destroy();
64+
await ds.destroy();
6465
});
6566
});
6667
});

plugins/node/instrumentation-typeorm/test/EntityManager.test.ts

Lines changed: 30 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,11 @@ describe('EntityManager', () => {
4848
describe('single connection', () => {
4949
it('save using connection.manager', async () => {
5050
const options = defaultOptions;
51-
const connection = await typeorm.createConnection(defaultOptions);
51+
const ds = new typeorm.DataSource(options);
52+
await ds.initialize();
53+
5254
const user = new User(1, 'opentelemetry', 'io');
53-
await connection.manager.save(user);
55+
await ds.manager.save(user);
5456
const typeOrmSpans = getTestSpans();
5557

5658
assert.strictEqual(typeOrmSpans.length, 1);
@@ -64,13 +66,14 @@ describe('EntityManager', () => {
6466
attributes[ATTR_DB_QUERY_TEXT],
6567
JSON.stringify({ targetOrEntity: user })
6668
);
67-
await connection.destroy();
69+
await ds.destroy();
6870
});
6971

7072
it('save', async () => {
7173
const options = defaultOptions;
72-
const connection = await typeorm.createConnection(defaultOptions);
73-
const manager = connection.createEntityManager();
74+
const ds = new typeorm.DataSource(options);
75+
await ds.initialize();
76+
const manager = ds.manager;
7477
const user = new User(1, 'opentelemetry', 'io');
7578
await manager.save(user);
7679
const typeOrmSpans = getTestSpans();
@@ -86,13 +89,14 @@ describe('EntityManager', () => {
8689
attributes[ATTR_DB_QUERY_TEXT],
8790
JSON.stringify({ targetOrEntity: user })
8891
);
89-
await connection.destroy();
92+
await ds.destroy();
9093
});
9194

9295
it('remove', async () => {
9396
const options = defaultOptions;
94-
const connection = await typeorm.createConnection(defaultOptions);
95-
const manager = connection.createEntityManager();
97+
const ds = new typeorm.DataSource(options);
98+
await ds.initialize();
99+
const manager = ds.manager;
96100

97101
const user = new User(56, 'opentelemetry', 'io');
98102
await manager.save(user);
@@ -116,13 +120,14 @@ describe('EntityManager', () => {
116120
},
117121
})
118122
);
119-
await connection.destroy();
123+
await ds.destroy();
120124
});
121125

122126
it('update', async () => {
123127
const options = defaultOptions;
124-
const connection = await typeorm.createConnection(defaultOptions);
125-
const manager = connection.createEntityManager();
128+
const ds = new typeorm.DataSource(options);
129+
await ds.initialize();
130+
const manager = ds.manager;
126131
const user = new User(56, 'opentelemetry', 'io');
127132
await manager.save(user);
128133
const partialEntity = { lastName: '.io' };
@@ -140,12 +145,13 @@ describe('EntityManager', () => {
140145
attributes[ATTR_DB_QUERY_TEXT],
141146
JSON.stringify({ target: 'User', criteria: 56, partialEntity })
142147
);
143-
await connection.destroy();
148+
await ds.destroy();
144149
});
145150

146151
it('Sets failure status when function throws', async () => {
147-
const connection = await typeorm.createConnection(defaultOptions);
148-
const manager = connection.createEntityManager();
152+
const ds = new typeorm.DataSource(defaultOptions);
153+
await ds.initialize();
154+
const manager = ds.manager;
149155
try {
150156
await manager.find({} as any);
151157
} catch (err) {}
@@ -157,7 +163,7 @@ describe('EntityManager', () => {
157163
typeOrmSpans[0].status.message,
158164
'No metadata for "[object Object]" was found.'
159165
);
160-
await connection.destroy();
166+
await ds.destroy();
161167
});
162168
});
163169

@@ -171,12 +177,13 @@ describe('EntityManager', () => {
171177
};
172178

173179
it('appends matching connection details to span', async () => {
174-
const [sqlite1, sqlite2] = await typeorm.createConnections([
175-
defaultOptions,
176-
options2,
177-
]);
178-
const manager1 = sqlite1.createEntityManager();
179-
const manager2 = sqlite2.createEntityManager();
180+
const ds1 = new typeorm.DataSource(defaultOptions);
181+
await ds1.initialize();
182+
const ds2 = new typeorm.DataSource(options2);
183+
await ds2.initialize();
184+
185+
const manager1 = ds1.manager;
186+
const manager2 = ds2.manager;
180187

181188
const user = new User(1, 'opentelemetry', 'io');
182189
await manager1.save(user);
@@ -220,8 +227,8 @@ describe('EntityManager', () => {
220227
sqlite2Span.attributes[ATTR_DB_COLLECTION_NAME],
221228
'user'
222229
);
223-
await sqlite1.destroy();
224-
await sqlite2.destroy();
230+
await ds1.destroy();
231+
await ds2.destroy();
225232
});
226233
});
227234
});

plugins/node/instrumentation-typeorm/test/QueryBuilder.test.ts

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,9 @@ describe('QueryBuilder', () => {
4646
});
4747

4848
it('getManyAndCount', async () => {
49-
const connectionOptions = defaultOptions as any;
50-
const connection = await typeorm.createConnection(connectionOptions);
51-
const queryBuilder = connection
52-
.getRepository(User)
53-
.createQueryBuilder('user');
49+
const ds = new typeorm.DataSource(defaultOptions);
50+
await ds.initialize();
51+
const queryBuilder = ds.getRepository(User).createQueryBuilder('user');
5452
const users = await queryBuilder
5553
.where('user.id = :userId', { userId: '1' })
5654
.getManyAndCount();
@@ -59,18 +57,15 @@ describe('QueryBuilder', () => {
5957
assert.strictEqual(typeOrmSpans.length, 1);
6058
assert.strictEqual(typeOrmSpans[0].status.code, SpanStatusCode.UNSET);
6159
const attributes = typeOrmSpans[0].attributes;
62-
assert.strictEqual(attributes[ATTR_DB_SYSTEM_NAME], connectionOptions.type);
63-
assert.strictEqual(attributes[ATTR_SERVER_ADDRESS], connectionOptions.host);
64-
assert.strictEqual(attributes[ATTR_SERVER_PORT], connectionOptions.port);
65-
assert.strictEqual(
66-
attributes[ATTR_DB_NAMESPACE],
67-
connectionOptions.database
68-
);
60+
assert.strictEqual(attributes[ATTR_DB_SYSTEM_NAME], defaultOptions.type);
61+
assert.strictEqual(attributes[ATTR_SERVER_ADDRESS], defaultOptions.host);
62+
assert.strictEqual(attributes[ATTR_SERVER_PORT], defaultOptions.port);
63+
assert.strictEqual(attributes[ATTR_DB_NAMESPACE], defaultOptions.database);
6964
assert.strictEqual(attributes[ATTR_DB_COLLECTION_NAME], 'user');
7065
assert.strictEqual(
7166
attributes[ATTR_DB_QUERY_TEXT],
7267
'SELECT "user"."id" AS "user_id", "user"."firstName" AS "user_firstName", "user"."lastName" AS "user_lastName" FROM "user" "user" WHERE "user"."id" = :userId'
7368
);
74-
await connection.destroy();
69+
await ds.destroy();
7570
});
7671
});

plugins/node/instrumentation-typeorm/test/Repository.test.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,16 @@ describe('Repository', () => {
3838
});
3939

4040
it('findAndCount', async () => {
41-
const connection = await typeorm.createConnection(defaultOptions);
42-
const repo = connection.getRepository(User);
41+
const ds = new typeorm.DataSource(defaultOptions);
42+
await ds.initialize();
43+
const repo = ds.getRepository(User);
4344
const [_users, count] = await repo.findAndCount();
4445
assert(count === 0);
4546
const spans = getTestSpans();
4647
assert.strictEqual(spans.length, 1);
4748
const span = spans[0];
4849
const attributes = span.attributes;
4950
assert.strictEqual(attributes[ATTR_DB_COLLECTION_NAME], 'user');
50-
await connection.destroy();
51+
await ds.destroy();
5152
});
5253
});

plugins/node/instrumentation-typeorm/test/config.test.ts

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -54,26 +54,28 @@ describe('TypeormInstrumentationConfig', () => {
5454
instrumentation.setConfig(config);
5555
instrumentation.enable();
5656

57-
const connection = await typeorm.createConnection(defaultOptions);
57+
const ds = new typeorm.DataSource(defaultOptions);
58+
await ds.initialize();
5859
const user = new User(1, 'opentelemetry', 'io');
59-
await connection.manager.save(user);
60+
await ds.manager.save(user);
6061
const typeOrmSpans = getTestSpans();
6162
assert.strictEqual(typeOrmSpans.length, 1);
6263
const attributes = typeOrmSpans[0].attributes;
6364

6465
assert.strictEqual(attributes['test'], JSON.stringify(user));
6566
assert.strictEqual(attributes[ATTR_DB_OPERATION_NAME], 'save');
6667
assert.strictEqual(attributes[ATTR_DB_SYSTEM_NAME], defaultOptions.type);
67-
await connection.destroy();
68+
await ds.destroy();
6869
});
6970

7071
it('enableInternalInstrumentation:true', async () => {
7172
const config: TypeormInstrumentationConfig = {
7273
enableInternalInstrumentation: true,
7374
};
7475
instrumentation.setConfig(config);
75-
const connection = await typeorm.createConnection(defaultOptions);
76-
await connection.manager.findAndCount(User);
76+
const ds = new typeorm.DataSource(defaultOptions);
77+
await ds.initialize();
78+
await ds.manager.findAndCount(User);
7779
const spans = getTestSpans();
7880
assert.strictEqual(spans.length, 2);
7981

@@ -97,33 +99,35 @@ describe('TypeormInstrumentationConfig', () => {
9799
'select'
98100
);
99101
assert.strictEqual(selectSpan?.attributes[ATTR_DB_COLLECTION_NAME], 'user');
100-
await connection.destroy();
102+
await ds.destroy();
101103
});
102104

103105
it('enableInternalInstrumentation:false', async () => {
104106
const config: TypeormInstrumentationConfig = {
105107
enableInternalInstrumentation: false,
106108
};
107109
instrumentation.setConfig(config);
108-
const connection = await typeorm.createConnection(defaultOptions);
109-
await connection.manager.findAndCount(User);
110+
const ds = new typeorm.DataSource(defaultOptions);
111+
await ds.initialize();
112+
await ds.manager.findAndCount(User);
110113
const spans = getTestSpans();
111114
assert.strictEqual(spans.length, 1);
112115
const attributes = spans[0].attributes;
113116
assert.strictEqual(attributes[ATTR_DB_OPERATION_NAME], 'findAndCount');
114117
assert.strictEqual(attributes[ATTR_DB_SYSTEM_NAME], defaultOptions.type);
115118
assert.strictEqual(attributes[ATTR_DB_COLLECTION_NAME], 'user');
116-
await connection.destroy();
119+
await ds.destroy();
117120
});
118121

119122
it('enhancedDatabaseReporting:true', async () => {
120123
const config: TypeormInstrumentationConfig = {
121124
enhancedDatabaseReporting: true,
122125
};
123126
instrumentation.setConfig(config);
124-
const connectionOptions = defaultOptions as any;
125-
const connection = await typeorm.createConnection(connectionOptions);
126-
await connection
127+
const connectionOptions = defaultOptions;
128+
const ds = new typeorm.DataSource(connectionOptions);
129+
await ds.initialize();
130+
await ds
127131
.getRepository(User)
128132
.createQueryBuilder('user')
129133
.where('user.id = :userId', { userId: '1' })
@@ -150,6 +154,6 @@ describe('TypeormInstrumentationConfig', () => {
150154
attributes[ExtendedDatabaseAttribute.DB_STATEMENT_PARAMETERS],
151155
JSON.stringify({ userId: '1', firstName: 'bob', lastName: 'dow' })
152156
);
153-
await connection.destroy();
157+
await ds.destroy();
154158
});
155159
});

plugins/node/instrumentation-typeorm/test/utils.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,6 @@ export class User {
3333
}
3434
}
3535

36-
// type is typeorm.ConnectionOptions for <0.3.0
37-
// and typeorm.DataSourceOptions for >=0.3.0
3836
export const defaultOptions: any = {
3937
type: 'sqlite',
4038
database: ':memory:',

0 commit comments

Comments
 (0)