Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implementa SQLite básico en vez de JSON #6

Open
wants to merge 3 commits into
base: example/json-instead-of-sqlite
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
801 changes: 302 additions & 499 deletions package-lock.json

Large diffs are not rendered by default.

5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,12 @@
},
"dependencies": {
"axios": "^0.19.2",
"better-sqlite3": "^7.1.0",
"dotenv": "^8.2.0",
"express": "^4.17.1",
"express-session": "^1.17.1",
"multer": "^1.4.2",
"nunjucks": "^3.2.1",
"rsdi": "^1.0.5",
"sequelize": "^6.3.3",
"uuid": "^8.3.0"
"rsdi": "^1.0.5"
}
}
19 changes: 7 additions & 12 deletions src/config/di.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
// configure DI container
const path = require('path');
const uuid = require('uuid');
const fs = require('fs');
const { default: DIContainer, object, get, factory } = require('rsdi');
const multer = require('multer');
const Sqlite3Database = require('better-sqlite3');

const session = require('express-session');
const { ClubController, ClubService, ClubRepository } = require('../module/club/module');

function configureMainJSONDatabase() {
return process.env.JSON_DB_PATH;
function configureMainDatabaseAdapter() {
return new Sqlite3Database(process.env.DB_PATH, {
verbose: console.log,
});
}

function configureSession() {
Expand Down Expand Up @@ -39,18 +40,12 @@ function configureMulter() {
return multer({ storage });
}

function configureUuid() {
return uuid.v4;
}

/**
* @param {DIContainer} container
*/
function addCommonDefinitions(container) {
container.addDefinitions({
fs,
uuid: factory(configureUuid),
JSONDatabase: factory(configureMainJSONDatabase),
MainDatabaseAdapter: factory(configureMainDatabaseAdapter),
Session: factory(configureSession),
Multer: factory(configureMulter),
});
Expand All @@ -63,7 +58,7 @@ function addClubModuleDefinitions(container) {
container.addDefinitions({
ClubController: object(ClubController).construct(get('Multer'), get('ClubService')),
ClubService: object(ClubService).construct(get('ClubRepository')),
ClubRepository: object(ClubRepository).construct(get('uuid'), get('fs'), get('JSONDatabase')),
ClubRepository: object(ClubRepository).construct(get('MainDatabaseAdapter')),
});
}

Expand Down
17 changes: 17 additions & 0 deletions src/config/setup.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
DROP TABLE IF EXISTS clubes;
CREATE TABLE clubes (
id INTEGER PRIMARY KEY AUTOINCREMENT,
`name` TEXT NOT NULL,
short_name TEXT NOT NULL,
tla TEXT NOT NULL,
crest_url TEXT NOT NULL,
`address` TEXT NOT NULL,
phone TEXT NOT NULL,
website TEXT NOT NULL,
email TEXT NOT NULL,
founded TEXT NOT NULL,
club_colors TEXT NOT NULL,
venue TEXT NOT NULL,
created_at DATE DEFAULT (datetime('now', 'localtime')) NOT NULL,
updated_at DATE DEFAULT (datetime('now', 'localtime')) NOT NULL
);
36 changes: 36 additions & 0 deletions src/module/club/mapper/clubMapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,42 @@ function fromDataToEntity({
});
}

/**
*
* @param {Object} formData
* @returns Club
*/
function fromDbToEntity({
id,
name,
short_name: shortName,
tla,
crest_url: crestUrl,
address,
phone,
website,
email,
founded,
club_colors: clubColors,
venue,
}) {
return new Club({
id,
name,
shortName,
tla,
crestUrl,
address,
phone,
website,
email,
founded,
clubColors,
venue,
});
}

module.exports = {
fromDataToEntity,
fromDbToEntity,
};
2 changes: 1 addition & 1 deletion src/module/club/module.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const ClubController = require('./controller/clubController');
const ClubRepository = require('./repository/json/clubRepository');
const ClubRepository = require('./repository/sqlite/clubRepository');
const ClubService = require('./service/clubService');

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
/* eslint max-classes-per-file: "off" */

const AbstractClubRepository = require('../abstractClubRepository');
const AbstractClubRepositoryError = require('../error/abstractClubRepositoryError');
const MethodNotImplementedError = require('../error/methodNotImplementedError');

test('No se puede instanciar un repositorio abstracto', () => {
let repoInstance;
Expand All @@ -18,3 +21,13 @@ test('Se puede instanciar un repositorio concreto que herede del repositorio abs
expect(respositoryInstance).toBeInstanceOf(ConcreteRepository);
expect(respositoryInstance).toBeInstanceOf(AbstractClubRepository);
});

test('Llamar a métodos base sin implementación concreta da error', () => {
const ConcreteRepository = class extends AbstractClubRepository {};
const respositoryInstance = new ConcreteRepository();

expect(() => respositoryInstance.save()).rejects.toThrowError(MethodNotImplementedError);
expect(() => respositoryInstance.delete()).rejects.toThrowError(MethodNotImplementedError);
expect(() => respositoryInstance.getAll()).rejects.toThrowError(MethodNotImplementedError);
expect(() => respositoryInstance.getById()).rejects.toThrowError(MethodNotImplementedError);
});
17 changes: 13 additions & 4 deletions src/module/club/repository/abstractClubRepository.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* eslint-disable no-empty-function */
/* eslint-disable no-unused-vars */
const AbstractClubRepositoryError = require('./error/abstractClubRepositoryError');
const MethodNotImplementedError = require('./error/methodNotImplementedError');

module.exports = class AbstractClubRepository {
constructor() {
Expand All @@ -15,21 +16,29 @@ module.exports = class AbstractClubRepository {
* @param {import('../entity/club')} club
* @returns {import('../entity/club')}
*/
async save(club) {}
async save(club) {
throw new MethodNotImplementedError();
}

/**
* @param {Number} id
*/
async delete(id) {}
async delete(id) {
throw new MethodNotImplementedError();
}

/**
* @param {Number} id
* @returns {import('../entity/club')}
*/
async getById(id) {}
async getById(id) {
throw new MethodNotImplementedError();
}

/**
* @returns {Array<import('../entity/club')>}
*/
async getAll() {}
async getAll() {
throw new MethodNotImplementedError();
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = class MethodNotImplementedError extends Error {};
108 changes: 0 additions & 108 deletions src/module/club/repository/json/clubRepository.js

This file was deleted.

Loading