Last active
April 21, 2018 16:28
-
-
Save uyu423/c69156ea3bc28e0c2848a1b716570157 to your computer and use it in GitHub Desktop.
Node.js 에서 사용할 수 있는 효율적인 Repository Pattern 에 대해 생각해보자
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import config from 'getconfig'; | |
import knex from 'knex'; | |
import mongoose from 'mongoose'; | |
import { User, Log } from './domainObjects'; | |
import { Log as LogSchema } from './mongodbSchemas'; | |
class MySQL { | |
constructor() { | |
this.driver = knex(config.DATABASE.MYSQL); | |
} | |
__findByIdx(table, idx) { | |
return this.driver.select().table(table).where({ idx }); | |
} | |
__findByValue(table, params) { | |
return this.driver.select().table(table).where(params); | |
} | |
} | |
class MongoDB { | |
constructor() { | |
mongoose.connect(config.DATABASE.MONGODB); | |
this.driver = mongoose.connection; | |
} | |
__findByIdx(model, idx) { | |
return model.find({ idx }); | |
} | |
} | |
class Repository { | |
constructor(engine) { | |
this.engine = engine; | |
this.connector = this.setEngine(engine); | |
} | |
setEngine(engine) { | |
switch (engine) { | |
case 'mysql': { | |
return new MySQL(); | |
break; | |
} | |
case 'mongodb': { | |
return new MongoDB(); | |
break; | |
} | |
default: { | |
throw new Error('Unsupported Database Engine Type'); | |
} | |
} | |
return this; | |
} | |
__findByIdx(target, idx) { | |
return this.connector.__findByIdx(target, idx) | |
} | |
} | |
class UserRepository extends Repository { | |
constructor() { | |
super('mysql'); | |
this.TABLE_NAME = 'user'; | |
} | |
async find(idx) { | |
const result = await this.__findByIdx(this.TABLE_NAME, idx) | |
.catch(error => Promise.reject(new Error(String(error)))); // using Super Class Method | |
return new User(result[0]); | |
} | |
async findByEmail(email) { | |
const result = await this.connector.__findByValue(this.TABLE_NAME, { email }) | |
.catch(error => Promise.reject(new Error(String(error)))); // using Super Class's connector object | |
return new User(result[0]); | |
} | |
} | |
class LogRepository extends Repository { | |
constructor() { | |
super('mongodb'); | |
this.MODEL = mongoose.model('logs', LogSchema); | |
} | |
async find(idx) { | |
const log = await this.__findByIdx(this.MODEL, idx); | |
return new Log(log); | |
} | |
} | |
const repository = { | |
user: new UserRepository(), | |
log: new LogRepository(), | |
}; | |
const findedUser = repository.user.find(49850).then(result => result); | |
const findedLog = repository.log.find(19910423).then(result => result); | |
const finedUser2 = repository.user.findByEmail('[email protected]').then(result => result); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.