Comments (5)
Hi guys. I'm the author of InversifyJS. Inversify allows you to inject an asynchronous factory (AKA provider). Providers are designed to inject objects with an asynchronous nature (e.g db clients).
A Provider is just a function that returns a promise:
() => Promise<T>
In the next release (InversifyJS 3.0) we are going to improve the way providers work: inversify/InversifyJS#418
I will try my best to document the integration with TypeORM 😉
from typeorm.
its actually better to ask inversify what to do in cases when you integrate with other libraries which does not have their annotation set. Maybe you do something wrong? May because you are setting kernel.bind<ConnectionManager>(ConnectionManager).to(ConnectionManager);
it causes such error and you should not set it? As I mentioned if you are missing some feature in typedi you can propose it there and lets discuss if this can be implemented there.
from typeorm.
At WonderBits we made a skeleton TS backend( with users, roles, permissions and OAuth tokens) using typeorm and Inversify and ended up using the singleton of a DB provider class that checks if a local db connection on itself is set or not to create a new connection or just return the previously set one. This is a kind of Factory/Provider pattern into an IoC injection pattern.
We were messing up with createConnection() returning a Promise and not being sure how to handle an unique connection with Inversify's Provider pattern so we ended up doing the aforementioned.
import ConfigProvider from './ConfigProvider';
import { injectable, inject } from 'inversify';
import { Connection, createConnection } from 'typeorm';
@injectable()
export default class DatabaseProvider {
public db: Connection;
constructor(@inject(ConfigProvider) private configProvider: ConfigProvider) {
}
getDBConnection(): Promise<Connection> {
return Promise.resolve()
.then(()=> {
if (!this.db) {
let config: any = this.configProvider.config;
return createConnection({
driver: {
type: config.dialect,
host: config.host,
port: config.port,
database: config.database,
username: config.username,
password: config.password
},
entities: [
__dirname + '/../modules/**/*.model.ts'
],
autoSchemaSync: true,
});
}
})
.then((db) => {
if (!this.db && db) {
this.db = db;
}
return this.db;
})
.catch((e) => {
console.log('ERROR: ' + JSON.stringify(e));
throw e; // Propagate the error
})
}
}
And then in the main server class we wait for getDBConnection() before instantiating the context class that depends on the DB provider. It isn't the cleaner way but we were forced by the asynchronous nature of createConnection() and our still incomplete control of Inversify and wanted to get a fast glimpse of typeorm working with our structure.
Anyway I agree with @pleerock that this shall be a question to make in Inversify context.
from typeorm.
@dreamersp Thanks for sharing. Interestingly, I have basically ended up with the same approach.
I think there are two issues here then:
-
The async nature of
createConnection()
doesn't appear to play nice with DI in general, is it possible for a synchronous version? -
Asking the Inversify people how to hook up TypeOrm correctly to their container implementation.
from typeorm.
here @remojansen gave answer how to use typeorm with inversify
from typeorm.
Related Issues (20)
- Default values in a jsonb column do not generate migrations correctly
- how to make a baseController in typeorm 0.3.17 and extends to other classes like mangoose
- Both CURRENT_TIMESTAMP and CURRENT_TIMESTAMP(0) as default values result in "Invalid default value" error
- Cannot find record in table if it has relation to another table
- Parameter binding fails for Oracle query when parameter name is reused HOT 5
- Predefined table name not being used when using Entity Manager API HOT 1
- Rotate Credentials
- SQL Error from Trailing Line Break in VirtualColumn Query String HOT 2
- Tree Entities can query the tree, but the result is unordered and a sorting parameter should be added
- Add support for ParadeDB Postgres
- The new typeorm version is not compatible with the mongodb driver package version 6.x.x HOT 3
- TreeRepository does not update mpath if parentId was soft-deleted
- Entity instance is not correctly initialized inside .save() method
- Updating a parent of a record with children to a new parent closure table
- Repository.prototype.save results for geography types are not consistent with find methods
- typeorm query spesific selecion HOT 1
- typeorm TypeError: Cannot read properties of undefined (reading 'type')
- TypeORM Migration Bug: Duplicate Index Names in Generated Migration
- The getPendingMigrations creates migrations table
- Bug: ORDER BY NULLS (FIRST | LAST) for SQLServer
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from typeorm.