@leansdk/leanrc
Version:
LeanRC is a MVC framework for creating graceful applications
231 lines (194 loc) • 7.87 kB
text/coffeescript
# This file is part of LeanRC.
#
# LeanRC is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# LeanRC is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with LeanRC. If not, see <https://www.gnu.org/licenses/>.
###
```coffee
# in application when its need
Module = require 'Module'
ArangoExtension = require 'leanrc-arango-extension'
# example of concrete application collection for instantuate it in PrepareModelCommand
module.exports = (App)->
class App::ArangoCollection extends Module::Collection
ArangoExtension::ArangoCollectionMixin
#... some other definitions
return App::ArangoCollection.initialize()
```
```coffee
module.exports = (App)->
App::PrepareModelCommand extends Module::SimpleCommand
execute: Function,
default: ->
#...
.registerProxy App::ArangoCollection.new 'CucumbersCollection',
# какие-то конфиги
#...
```
###
module.exports = (Module)->
{
AnyT, NilT
FuncG, SubsetG, MaybeG, UnionG, ListG, InterfaceG
CollectionInterface, RecordInterface, CursorInterface
SerializerInterface, ObjectizerInterface
ConfigurableMixin
Serializer, Objectizer
Utils: { _, inflect }
} = Module::
class Collection extends Module::Proxy
ConfigurableMixin
CollectionInterface
Module
delegate: SubsetG(RecordInterface),
get: ->
delegate = ?.delegate
UnionG(String, Function, SubsetG RecordInterface) delegate
if _.isString delegate
delegate = (.NS ? ::)[delegate]
else unless /Migration$|Record$/.test delegate.name
delegate = delegate?()
delegate
serializer: MaybeG SerializerInterface
objectizer: MaybeG ObjectizerInterface
collectionName: FuncG([], String),
default: ->
firstClassName = _.first _.remove .parentClassNames(), (name)->
not /Mixin$|Interface$|^CoreObject$|^Record$/.test name
inflect.pluralize inflect.underscore firstClassName.replace /Record$/, ''
collectionPrefix: FuncG([], String),
default: ->
"#{inflect.underscore @Module.name}_"
collectionFullName: FuncG([MaybeG String], String),
default: (asName = null)->
"#{@collectionPrefix()}#{asName ? @collectionName()}"
recordHasBeenChanged: FuncG([String, Object]),
default: (asType, aoData)->
Module::RECORD_CHANGED, aoData, asType
return
generateId: FuncG([RecordInterface], UnionG String, Number, NilT),
default: -> yield return
build: FuncG(Object, RecordInterface),
default: (properties)->
return yield .recoverize , properties
create: FuncG(Object, RecordInterface),
default: (properties)->
voRecord = yield properties
return yield voRecord.save()
push: FuncG(RecordInterface, RecordInterface),
default: ->
throw new Error 'Not implemented specific method'
yield return
delete: FuncG([UnionG String, Number]),
default: (id)->
voRecord = yield id
yield voRecord.delete()
yield return
destroy: FuncG([UnionG String, Number]),
default: (id)->
voRecord = yield id
yield voRecord.destroy()
yield return
remove: FuncG([UnionG String, Number]),
default: ->
throw new Error 'Not implemented specific method'
yield return
find: FuncG([UnionG String, Number], MaybeG RecordInterface),
default: (id)-> return yield id
findMany: FuncG([ListG UnionG String, Number], CursorInterface),
default: (ids)-> return yield ids
take: FuncG([UnionG String, Number], MaybeG RecordInterface),
default: ->
throw new Error 'Not implemented specific method'
yield return
takeMany: FuncG([ListG UnionG String, Number], CursorInterface),
default: ->
throw new Error 'Not implemented specific method'
yield return
takeAll: FuncG([], CursorInterface),
default: ->
throw new Error 'Not implemented specific method'
yield return
update: FuncG([UnionG(String, Number), Object], RecordInterface),
default: (id, properties)->
properties.id = id
existedRecord = yield id
receivedRecord = yield .recoverize , properties
for own key of properties
existedRecord[key] = receivedRecord[key]
return yield existedRecord.save()
override: FuncG([UnionG(String, Number), RecordInterface], RecordInterface),
default: ->
throw new Error 'Not implemented specific method'
yield return
clone: FuncG(RecordInterface, RecordInterface),
default: (aoRecord)->
vhAttributes = {}
vlAttributes = Object.keys .attributes
for key in vlAttributes
vhAttributes[key] = aoRecord[key]
voRecord = .new vhAttributes, @
voRecord.id = yield
yield return voRecord
copy: FuncG(RecordInterface, RecordInterface),
default: (aoRecord)->
voRecord = yield aoRecord
yield voRecord.save()
yield return voRecord
includes: FuncG([UnionG String, Number], Boolean),
default: ->
throw new Error 'Not implemented specific method'
yield return
length: FuncG([], Number),
default: ->
throw new Error 'Not implemented specific method'
yield return
normalize: FuncG(AnyT, RecordInterface),
default: (ahData)->
return yield .normalize , ahData
serialize: FuncG(RecordInterface, AnyT),
default: (aoRecord, ahOptions)->
return yield .serialize aoRecord, ahOptions
init: FuncG([String, MaybeG InterfaceG {
delegate: UnionG String, Function, SubsetG RecordInterface
serializer: MaybeG UnionG String, Function, SubsetG Serializer
objectizer: MaybeG UnionG String, Function, SubsetG Objectizer
}]),
default: (args...)->
args...
serializer = ?.serializer
objectizer = ?.objectizer
vcSerializer = unless serializer?
Serializer
else if _.isString serializer
(
.NS ? ::
)[serializer]
else unless /Serializer$/.test serializer.name
serializer?()
else
serializer
vcObjectizer = unless objectizer?
Objectizer
else if _.isString objectizer
(
.NS ? ::
)[objectizer]
else unless /Objectizer$/.test objectizer.name
objectizer?()
else
objectizer
= vcSerializer.new @
= vcObjectizer.new @
return