-
Notifications
You must be signed in to change notification settings - Fork 2
[WIP] Refactor #16
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
base: master
Are you sure you want to change the base?
[WIP] Refactor #16
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,11 +6,16 @@ export default { | |
| before: 'define-orm-models', | ||
| async initialize(application) { | ||
| let container = application.container; | ||
| let adapter = container.lookup('orm-adapter:node-orm2'); | ||
| let config = application.config.database; | ||
| let config = application.config; | ||
|
|
||
| if (!config.database || !config.database.orm2) { | ||
| // Config is not specified | ||
| return; | ||
| } | ||
|
|
||
| try { | ||
| adapter.db = await fromNode((cb) => orm.connect(config.url, cb)); | ||
| let connection = await fromNode((cb) => orm.connect(config.database.orm2, cb)); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm. Not sure I like scoping the config inside What if we did something like |
||
| container.register('database:orm2', connection, { singleton: true }); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Two things:
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agreed that we need some conventions around this. |
||
| } catch (error) { | ||
| application.logger.error('Error initializing the node-orm2 adapter or database connection:'); | ||
| application.logger.error(error.stack); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,10 +5,10 @@ export default { | |
| after: 'define-orm-models', | ||
| async initialize(application) { | ||
| let container = application.container; | ||
| let adapter = container.lookup('orm-adapter:node-orm2'); | ||
| let db = container.lookup('database:orm2', { loose: true }); | ||
|
|
||
| if (application.config.database.syncSchema) { | ||
| await fromNode((cb) => adapter.db.sync(cb)); | ||
| if (db && application.config.database.sync) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is a null check for |
||
| await fromNode((cb) => db.sync(cb)); | ||
| } | ||
| } | ||
| }; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,6 +11,11 @@ export default class NodeORM2Adapter extends ORMAdapter { | |
| return fromNode((cb) => OrmModel.get(id, cb)); | ||
| } | ||
|
|
||
| queryOne(type, query) { | ||
| let OrmModel = this.ormModels[type]; | ||
| return fromNode((cb) => OrmModel.one(query, cb)); | ||
| } | ||
|
|
||
| all(type, options = {}) { | ||
| let OrmModel = this.ormModels[type]; | ||
| return fromNode((cb) => OrmModel.all(null, options, cb)); | ||
|
|
@@ -21,25 +26,25 @@ export default class NodeORM2Adapter extends ORMAdapter { | |
| return fromNode((cb) => OrmModel.find(query, options, cb)); | ||
| } | ||
|
|
||
| findOne(type, query) { | ||
| let OrmModel = this.ormModels[type]; | ||
| return fromNode((cb) => OrmModel.one(query, cb)); | ||
| } | ||
|
|
||
| createRecord(type, data) { | ||
| let OrmModel = this.ormModels[type]; | ||
| return fromNode((cb) => OrmModel.create(data, cb)); | ||
| } | ||
|
|
||
| idFor(model) { | ||
| return model.record.id; | ||
| } | ||
|
|
||
| setId(model, id) { | ||
| model.record.id = id; | ||
| return true; | ||
| } | ||
|
|
||
| buildRecord(type, data) { | ||
| let OrmModel = this.ormModels[type]; | ||
| return new OrmModel(data); | ||
| } | ||
|
|
||
| idFor(model) { | ||
| return model.record.id; | ||
| } | ||
|
|
||
| getAttribute(model, property) { | ||
| return model.record[property]; | ||
| } | ||
|
|
@@ -87,35 +92,35 @@ export default class NodeORM2Adapter extends ORMAdapter { | |
| } | ||
|
|
||
| saveRecord(model) { | ||
| return fromNode(model.record.save.bind(model.record)); | ||
| return fromNode((cb) => model.record.save(cb)); | ||
| } | ||
|
|
||
| deleteRecord(model) { | ||
| return fromNode(model.record.remove.bind(model.record)); | ||
| return fromNode((cb) => model.record.remove(cb)); | ||
| } | ||
|
|
||
| defineModels(models) { | ||
| // Don't define abstract base classes | ||
| models = models.filter((Model) => !(Model.hasOwnProperty('abstract') && Model.abstract)); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don't we need this here?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's done instead in the |
||
|
|
||
| let db = this.container.lookup('database:orm2'); | ||
| this.ormModels = {}; | ||
|
|
||
| // Define models | ||
| models.forEach((Model) => { | ||
| let attributes = {}; | ||
| Model.eachAttribute((key, attribute) => { | ||
| let modelType = this.container.metaFor(Model).containerName; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Latest denali version introduces |
||
| Model.mapAttributeDescriptors((attribute, key) => { | ||
| attributes[key] = assign({ | ||
| mapsTo: this.keyToColumn(key), | ||
| type: this.denaliTypeToORMType(attribute.type) | ||
| }, attribute.options); | ||
| }); | ||
| this.ormModels[Model.type] = this.db.define(Model.type, attributes); | ||
| this.ormModels[modelType] = db.define(modelType, attributes); | ||
| }); | ||
|
|
||
| // Define relationships between models | ||
| models.forEach((Model) => { | ||
| Model.eachRelationship((key, relationship) => { | ||
| let OrmModel = this.ormModels[Model.type]; | ||
| let modelType = this.container.metaFor(Model).containerName; | ||
| Model.mapRelationshipDescriptors((relationship, key) => { | ||
| let OrmModel = this.ormModels[modelType]; | ||
| let Related = this.ormModels[relationship.type]; | ||
| if (relationship.mode === 'hasOne') { | ||
| OrmModel.hasOne(key, Related); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't we probably throw here? If you've included this addon but haven't configured database settings, something must be wrong, right?