-
-
Notifications
You must be signed in to change notification settings - Fork 197
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(lucid): implement model basic functionality
- Loading branch information
1 parent
00f091d
commit 1bb531f
Showing
16 changed files
with
1,727 additions
and
4 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
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) |
This file contains 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
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') |
This file contains 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
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() ] | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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
This file contains 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
This file contains 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
This file contains 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
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 |
Oops, something went wrong.