Skip to content

Commit

Permalink
Formatted src files
Browse files Browse the repository at this point in the history
  • Loading branch information
thetutlage committed Sep 10, 2015
1 parent f74383b commit b7bf23e
Show file tree
Hide file tree
Showing 13 changed files with 194 additions and 62 deletions.
6 changes: 6 additions & 0 deletions providers/CollectionProvider.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
'use strict';

/**
* adonis-lucid
* Copyright(c) 2015-2015 Harminder Virk
* MIT Licensed
*/

const ServiceProvider = require('adonis-fold').ServiceProvider
const Collection = require('../src/Orm/Collection')

Expand Down
6 changes: 6 additions & 0 deletions providers/DatabaseProvider.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
'use strict';

/**
* adonis-lucid
* Copyright(c) 2015-2015 Harminder Virk
* MIT Licensed
*/

const ServiceProvider = require('adonis-fold').ServiceProvider
const Database = require('../src/Database')

Expand Down
6 changes: 6 additions & 0 deletions providers/LucidProvider.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
'use strict';

/**
* adonis-lucid
* Copyright(c) 2015-2015 Harminder Virk
* MIT Licensed
*/

const ServiceProvider = require('adonis-fold').ServiceProvider
const Lucid = require('../src/Orm/Proxy/Model')

Expand Down
7 changes: 6 additions & 1 deletion src/Database/index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
'use strict'

/**
* adonis-lucid
* Copyright(c) 2015-2015 Harminder Virk
* MIT Licensed
*/

const knex = require('knex')

/**
* @module Database
* @namespace Adonis/Src/Database
* @description Fluent query builder for adonis framework
*/
class Database {
Expand Down
14 changes: 6 additions & 8 deletions src/Orm/Collection/index.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
'use strict'

/**
* adonis-lucid
* Copyright(c) 2015-2015 Harminder Virk
* MIT Licensed
*/

const _ = require('lodash')

/**
* @module Collection
* @namespace Adonis/Src/Collection
* @description Convert an array of object of data into
* lodash collection
*/
class Collection {

constructor (values) {
return _(values)
}

}

module.exports = Collection
22 changes: 21 additions & 1 deletion src/Orm/Proxy/Model/helpers.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
'use strict'

/**
* adonis-lucid
* Copyright(c) 2015-2015 Harminder Virk
* MIT Licensed
*/

const changeCase = require('change-case')
const _ = require('lodash')

Expand All @@ -16,6 +22,7 @@ let helpers = exports = module.exports = {}
* @param {Object} target
* @param {String} field
* @return {Function|Null}
* @public
*/
helpers.mutateField = function (target, field) {
const setter = `set${changeCase.pascalCase(field)}`
Expand All @@ -29,6 +36,7 @@ helpers.mutateField = function (target, field) {
* @param {Object} target
* @param {Object} row
* @return {Object}
* @public
*/
helpers.mutateRow = function (target, row) {
return _.object(_.map(row, function (item, index) {
Expand All @@ -45,6 +53,7 @@ helpers.mutateRow = function (target, row) {
* @param {Object} target
* @param {Array} values
* @return {Array}
* @public
*/
helpers.mutateSetters = function (target, values) {
if (_.isArray(values)) {
Expand All @@ -62,6 +71,7 @@ helpers.mutateSetters = function (target, values) {
* @param {Array|Object} rows
* @param {Array} keys
* @return {Array|Object}
* @public
*/
helpers.addTimeStamps = function (rows, keys) {
if (_.isArray(rows)) {
Expand All @@ -81,6 +91,7 @@ helpers.addTimeStamps = function (rows, keys) {
* @param {Object} row
* @param {Array} keys
* @return {Object}
* @public
*/
helpers.rowTimeStamp = function (row, keys) {
const currentTimeStamp = new Date()
Expand All @@ -90,7 +101,16 @@ helpers.rowTimeStamp = function (row, keys) {
return row
}


/**
* @function isFetched
* @description determines whether there are any where
* statements available on query chain. Required
* to make sure we are updating models whose
* instance belongs to a user via find
* @param {Object} target
* @return {Boolean}
* @public
*/
helpers.isFetched = function (target) {
return _.size(target.connection._statements) > 0
}
65 changes: 46 additions & 19 deletions src/Orm/Proxy/Model/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
'use strict'

/**
* adonis-lucid
* Copyright(c) 2015-2015 Harminder Virk
* MIT Licensed
*/

require('harmony-reflect')
const mapper = require('./mapper')
const helpers = require('./helpers')
Expand All @@ -15,7 +21,6 @@ const _ = require('lodash')
class Model {

constructor (attributes) {

/**
* initiating model with array of data is not allowed , as it will
* be considered as bulk inserts
Expand Down Expand Up @@ -48,6 +53,7 @@ class Model {
* static create method.
* @param {Array|Object} values
* @return {Promise}
* @public
*/
create (values) {
let isMutated = !values
Expand All @@ -61,9 +67,10 @@ class Model {
* or passing new attributes
* @param {Array|Object} values
* @return {Promise}
* @public
*/
update (values) {
if(!helpers.isFetched(this)){
if (!helpers.isFetched(this)) {
throw new Error(`You cannot update a fresh model instance , trying fetching one using find method`)
}
let isMutated = !values
Expand All @@ -76,9 +83,10 @@ class Model {
* @description soft deleting or deleting rows based upon
* model settings
* @return {Promise}
* @public
*/
delete () {
if(!helpers.isFetched(this)){
if (!helpers.isFetched(this)) {
throw new Error(`You cannot delete a fresh model instance , trying fetching one using find method`)
}
return this.constructor.delete(this.connection)
Expand All @@ -89,29 +97,31 @@ class Model {
* @description force deleting rows even if soft deletes
* are enabled
* @return {Promise}
* @public
*/
forceDelete () {
let self = this
if(!helpers.isFetched(this)){
if (!helpers.isFetched(this)) {
throw new Error(`You cannot delete a fresh model instance , trying fetching one using find method`)
}
return new Promise(function (resolve,reject) {
return new Promise(function (resolve, reject) {
self
.constructor
.forceDelete(self.connection)
.then (function (response) {
self.attributes = {}
self.connection = self.constructor.database.table(self.constructor.table)
resolve(response)
})
.catch(reject)
.constructor
.forceDelete(self.connection)
.then(function (response) {
self.attributes = {}
self.connection = self.constructor.database.table(self.constructor.table)
resolve(response)
})
.catch(reject)
})
}

/**
* @function isTrashed
* @description finding whether row has been soft deleted or not
* @return {Boolean}
* @public
*/
isTrashed () {
const softDeleteKey = this.constructor.softDeletes
Expand All @@ -129,6 +139,7 @@ class Model {
* @static true
* @description default field name for soft deletes
* @return {String}
* @public
*/
static get softDeletes () {
return 'deleted_at'
Expand All @@ -140,6 +151,7 @@ class Model {
* @description by default timestamps are enabled
* on models
* @return {Boolean}
* @public
*/
static get timestamps () {
return true
Expand All @@ -150,6 +162,7 @@ class Model {
* @static true
* @description default table name for a given model
* @return {String}
* @public
*/
static get table () {
return staticHelpers.getTableName(this)
Expand All @@ -161,33 +174,47 @@ class Model {
* @description by default id is considered to be the primary key on
* a model
* @return {String}
* @public
*/
static get primaryKey(){
static get primaryKey () {
return 'id'
}

/**
* hooks are used by ioc container to transform return
* value and here we want to return proxied model
* @return {Array}
* @public
*/
static get hooks () {
return ['extend']
}

/**
* @function extend
* @description extending static interface of class via StaticProxy
* @return {Object}
* @public
*/
static extend(){
return new StaticProxy(this,this.database)
static extend () {
return new StaticProxy(this, this.database)
}

/**
* @getter
* database instance for this model
* @public
*/
static get database(){
return this._database;
static get database () {
return this._database
}

/**
* @setter
* database instance for this model
* @public
*/
static set database(database){
static set database (database) {
this._database = database
}

Expand Down
10 changes: 9 additions & 1 deletion src/Orm/Proxy/Model/mapper.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
'use strict'

/**
* adonis-lucid
* Copyright(c) 2015-2015 Harminder Virk
* MIT Licensed
*/

const helpers = require('./helpers')

/**
Expand All @@ -14,6 +20,7 @@ let mapper = exports = module.exports = {}
* @param {Object} target
* @param {String} name
* @return {*}
* @public
*/
mapper.get = function (target, name) {
if (target[name]) {
Expand All @@ -32,9 +39,10 @@ mapper.get = function (target, name) {
* @param {Object} target
* @param {String} name
* @param {*} value
* @public
*/
mapper.set = function (target, name, value) {
if(name === 'attributes'){
if (name === 'attributes') {
target[name] = value
return true
}
Expand Down
Loading

0 comments on commit b7bf23e

Please sign in to comment.