Skip to content

Commit

Permalink
feat(lucid): implement model basic functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
thetutlage committed Jun 18, 2017
1 parent 00f091d commit 1bb531f
Show file tree
Hide file tree
Showing 16 changed files with 1,727 additions and 4 deletions.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,19 @@
- [x] withPrefix
- [x] transactions
- [x] global transactions



## Model

- [ ] hooks
- [ ] getters
- [ ] setters
- [ ] helper static methods
- [ ] boot method ( ability to extend via BaseModel )
- [ ] refresh model
- [ ] fill model with json data
- [ ] use traits
- [ ] computed properties
- [ ] visible/hidden attributes
- [ ] timestamps
22 changes: 22 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -1 +1,23 @@
'use strict'

const path = require('path')
const knex = require('knex')({
client: 'sqlite',
connection: ':memory:',
debug: true,
useNullAsDefault: true
})

knex
.schema
.createTableIfNotExists('users', function (table) {
table.integer('uuid')
table.string('username')
})
.then(() => {
return knex
.table('users')
.insert({ uuid: 1100, username: 'virk' })
})
.then(console.log)
.catch(console.error)
4 changes: 4 additions & 0 deletions japaFile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
'use strict'

const cli = require('japa/cli')
cli.run('test/**/*.spec.js')
37 changes: 37 additions & 0 deletions lib/util.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
'use strict'

/*
* adonis-lucid
*
* (c) Harminder Virk <virk@adonisjs.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/


const _ = require('lodash')
const pluralize = require('pluralize')

const util = exports = module.exports = {}

util.makeTableName = function (name) {
return _.snakeCase(pluralize(name))
}

util.getSetterName = function (name) {
return `set${_.upperFirst(_.camelCase(name))}`
}

util.getGetterName = function (name) {
return `get${_.upperFirst(_.camelCase(name))}`
}

util.getCycleAndEvent = function (name) {
const tokens = name.match(/^(before|after)(\w+)/)
if (!tokens) {
return []
}

return [ tokens[1], tokens[2].toLowerCase() ]
}
43 changes: 43 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,16 @@
},
"homepage": "https://github.com/adonisjs/adonis-lucid#readme",
"dependencies": {
"date-fns": "^1.28.5",
"debug": "^2.6.8",
"knex": "^0.13.0",
"lodash": "^4.17.4",
"node-exceptions": "^2.0.2"
"moment": "^2.18.1",
"node-exceptions": "^2.0.2",
"pluralize": "^5.0.0"
},
"devDependencies": {
"@adonisjs/fold": "git+https://github.com/poppinss/adonis-fold.git#dawn",
"@adonisjs/sink": "^1.0.8",
"chance": "^1.0.9",
"coveralls": "^2.13.1",
Expand Down
4 changes: 3 additions & 1 deletion src/Database/Manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,12 @@ class DatabaseManager {
*
* @return {Database}
*/
connection (name = this.Config.get('database.connection')) {
connection (name) {
name = name || this.Config.get('database.connection')
if (this._connectionPools[name]) {
return this._connectionPools[name]
}

const connectionSettings = this.Config.get(`database.${name}`)
if (!connectionSettings) {
throw CE.RuntimeException.missingDatabaseConnection(name)
Expand Down
12 changes: 11 additions & 1 deletion src/Exceptions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,14 @@ class RuntimeException extends NE.RuntimeException {
}
}

module.exports = { RuntimeException }
class InvalidArgumentException extends NE.InvalidArgumentException {
static missingParameter (message) {
return new this(message, 500, 'E_MISSING_PARAMETER')
}

static invalidParamter (message) {
return new this(message, 500, 'E_INVALID_PARAMETER')
}
}

module.exports = { RuntimeException, InvalidArgumentException }
152 changes: 152 additions & 0 deletions src/Lucid/Hooks/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
'use strict'

/*
* adonis-lucid
*
* (c) Harminder Virk <virk@adonisjs.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

const _ = require('lodash')
const CE = require('../../Exceptions')

class Hooks {
constructor () {
this._events = ['create', 'update', 'delete', 'restore', 'find']

/**
* The event aliases. Whenever a handler is saved for a alias,
* it will called when those events occurs.
*
* @type {Object}
*/
this._aliases = {
create: 'save',
update: 'save'
}

/**
* A map of handlers to be called for each event
*
* @type {Object}
*/
this._handlers = {}
}

/**
* Adds a new handler for an event. Make sure to give
* handler a unique name if planning to remove it
* later at runtime
*
* @method addHandler
*
* @param {String} event
* @param {Function|String} handler
* @param {String} [name]
*
* @return {void}
*
* @example
* ```
* this.addHandler('create', async function () {
* })
* ```
*/
addHandler (event, handler, name) {
if (!this._events[event]) {
// error
}
this._handlers[event] = this._handlers[event] || []
this._handlers[event].push({ handler, name })
}

/**
* Removes handler using it's name. This methods returns
* void when successfully executed, otherwise an
* exception is thrown.
*
* @method removeHandler
*
* @param {String} event
* @param {String} name
*
* @return {void}
*
* @example
* ```js
* this.removeHandler('create', 'updatePassword')
* ```
*
* @throws {InvalidArgumentException} If `name` is missing
*/
removeHandler (event, name) {
if (!name) {
throw CE.InvalidArgumentException.missingParameter('Cannot remove hook without a name')
}
_.remove(this._handlers[event], (handler) => handler.name === name)
}

/**
* Removes all handlers for a given event. This method
* returns void when successfully executed, otherwise
* an exception is thrown.
*
* @method removeAllHandlers
*
* @param {String} event
*
* @return {void}
*
* @example
* ```
* this.removeAllHandlers('create')
* ```
*/
removeAllHandlers (event) {
/**
* Don't create an empty array of events when there was
* not one.
*/
if (!this._handlers[event]) {
return
}
this._handlers[event] = []
}

/**
* Execute hooks in sequence. If this method doesn't
* throws an exception, means everything went fine.
*
* @method exec
* @async
*
* @param {String} event
* @param {Object} ctx
*
* @return {void}
*/
async exec (event, ctx) {
const handlers = this._handlers[event] || []
const aliasesHandlers = this._aliases[event] ? this._handlers[this._aliases[event]] || [] : []
const allHandlers = handlers.concat(aliasesHandlers)

/**
* Return if there are no handlers for a given
* event
*/
if (!allHandlers.length) {
return
}

/**
* Execute all handlers in sequence
*/
for (let handler of allHandlers) {
await handler.handler(ctx)
}
}
}

module.exports = Hooks
Loading

0 comments on commit 1bb531f

Please sign in to comment.