Get Remove them ORMs from your Projects.

Ernesto Jara Olveda
2 min readJan 4, 2021

--

I mean our connector will be kind of basic, do not over hope or you will end up disappointed xDDD.

Our project will have the following structure.

src/
-- mysql
---- mysql.ts
---- pool-connector.ts
package.json
tsconfig.json

b4 we start we need to install some dependencies.

npm i -D @types/mysql @types/node tslib typescript && npm i mysql

our mysql functions will be at the mysql.ts file, which has the following structure.

export abstract class MySQL {
private _pool: mysql.Pool;

protected constructor (config);
private getConnection ();
private runQuery (connection,sql,opts);
private beginTransaction (connection);
private commitTransaction (connection);
protected read (sql, opts);
protected write (sql, opts);
}

yes it is right we are going to use an abstract class </3

the private attribute _pool will be the one dispatching them connections.

Private methods

  • getConnection — gets a connection from the pool.
  • runQuery — executes an sql statement.
  • beginTransaction — as the name says.
  • commitTransaction — write data into them tables.

Those methods are for internal use only it is why they are “private” and

Protected Methods

  • read — runs none transactional sql statements such as selects.
  • write — runs transactional sql statements such as inserts.

read and write are only accessible by the sons of our class it is why they are “protected”

with that been said.

Next the son of our connector. The son class does not do sh*t or at least it shouldn’t implement anything it only defines the unfamous what the how will be implemented on great sons of our mysql class xDD xDD xDD. I mean yow bros I did not invent POO

OKi just like in every other orm we have the save, findby … … bla bla bla.

now let’s create the accounts class and to do so we are going to start with the interface for this example is going to be some kind of simple

export interface AccountsProps {
id: number;
name: string;
last: string;
}

now the class

export class Accounts extends PoolConnector<AccountsProps> {}

if we do so youll see that vscode or any other code editor will complain bcuz you need to implement them methods

once you implement them the error will be gone

I Hope it can help by any means.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

No responses yet

Write a response