
Security News
Deno 2.2 Improves Dependency Management and Expands Node.js Compatibility
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
@brainhubeu/hadron-typeorm
Advanced tools
npm install @brainhubeu/hadron-typeorm --save
Pass package as an argument for hadron bootstrapping function:
const hadronTypeOrm = require('@brainhubeu/hadron-typeorm');
// ... importing and initializing other components
hadron(expressApp, [hadronTypeOrm], config).then(() => {
console.log('Hadron with typeORM initialized');
});
You can set up a new connection using connection object.
{
connectionName: string,
type: string,
host: string,
port: number,
username: string,
password: string,
database: string
entitySchemas: entities,
synchronize: bool,
}
connectionName
- string that identifies this connectiontype
- string that defines type of database, f.e. mysql, mariadb, postgres, sqlite, mongodb,host
- url to database,port
- port of database,username
- username of account to databse,password
- password to database,database
- name of databaseentities
- array of classes that defines modelsentitySchemas
- in case that You are describing models with schemas, put those in this parametersynchronize
- parameter that defines if database should be automatically synchronized with modelsAlso all other parameters available in typeOrm are available. Please take a look at TypeORM documentation
NOTE: Also events aren't included in this section so logging into the console is done using setTimeout.
Since we have our connection, we need to include it inside our hadron constructor's config object.
const hadronTypeOrm = require('@brainhubeu/hadron-typeorm');
const config = {
connection: {
/* connection object */
},
};
hadron(expressApp, [hadronTypeOrm], config).then((container) => {
console.log('Initialized connection:', container.take('connection'));
});
Let's assume we want to have a simple table user
Field | Type |
---|---|
🔑 id | int |
firstName | varchar |
lastName | varchar |
We have two options while defining our entities
.
import { Entity, Column, PrimaryColumn } from 'typeorm';
@Entity()
export class Photo {
@PrimaryGeneratedColumn()
@Column()
id: number;
@Column() firstName: string;
@Column() lastName: string;
}
When using this method, while creating connection to database, those classes should be in entities
parameter.
// entity/User.js
module.exports = {
name: 'User',
columns: {
id: {
primary: true,
type: 'int',
generated: true,
},
firstName: {
type: 'varchar',
},
lastName: {
type: 'varchar',
},
},
};
When using this method, while creating connection to database, those schemas should be in entitySchemas
parameter.
For more details about defining models, please take a look at TypeORM documentation. Especially section about available types for each database distribution
To include our entities in hadron, we simply need to include them in our config object. Let's modify the code that we were using to initialize hadron:
const hadronTypeOrm = require('@brainhubeu/hadron-typeorm');
const user = require('./entity/User');
const config = {
connection,
entities: [user],
};
hadron(expressApp, [hadronTypeOrm], config).then((container) => {
console.log(
'userRepository available:',
container.take('userRepository') !== null,
);
// User entity should be declared under userRepository key and
// will be available as a typeORM repository
});
Repository key in Container depends from name of schema/class and is builded in such way:
<schema/class name in camelCase>Repository
Examples:
User = userRepository
SuperUser = superUserRepository
loremIpsumDolor = loremIpsumDolorRepository
Generater repositories contain same methods as ones from TypeORM. Please check them out here:
http://typeorm.io/#/working-with-entity-manager
There are few possible reasons for that. Firstly check if parameter synchronize
in configuration is set to true.
Then make sure that connection configuration contains entities
/entitySchemas
fields.
Remember, if You are using class definition of models, You need to put them in entities
parameter, otherwise (schema method) in entitySchemas
FAQs
Hadron typeorm module
The npm package @brainhubeu/hadron-typeorm receives a total of 0 weekly downloads. As such, @brainhubeu/hadron-typeorm popularity was classified as not popular.
We found that @brainhubeu/hadron-typeorm demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 9 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
Security News
React's CRA deprecation announcement sparked community criticism over framework recommendations, leading to quick updates acknowledging build tools like Vite as valid alternatives.
Security News
Ransomware payment rates hit an all-time low in 2024 as law enforcement crackdowns, stronger defenses, and shifting policies make attacks riskier and less profitable.