This repository has been archived by the owner on Apr 29, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathactive-record.coffee
132 lines (102 loc) · 2.93 KB
/
active-record.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
_ = require 'lodash'
# camelToSnake('camelCase') returns 'camel_case'
camelToSnake = (string) ->
string.replace /([a-z][A-Z])/g, (m) -> m[0] + '_' + m[1].toLowerCase()
# snakeToCamel('snake_case') returns 'snakeCase'
snakeToCamel = (string) ->
string.replace /_([a-z])/g, (m) -> m[1].toUpperCase()
mapKeys = (object, f) ->
result = {}
Object.keys(object).forEach (key) ->
result[f key] = object[key]
return result
snakeToCamelObject = (snake) -> mapKeys snake, snakeToCamel
camelToSnakeObject = (camel) -> mapKeys camel, camelToSnake
################################################################################
# record
# constructor
Record = (data) ->
_.assign(@, data)
# instance methods
Record.prototype =
save: ->
that = @
if @id?
@_table
.where(id: @id)
.returnFirst()
.update(@)
.then (data) ->
_.assign that, data
return that
else
@_table
.insert(@)
.then (data) ->
_.assign that, data
return that
delete: ->
that = @
@_table
.where(id: @id)
.delete()
.then ->
delete that.id
load: ->
that = @
@_table
.where(id: @id)
.first()
.then (data) ->
_.assign that, data
################################################################################
# movie
Movie = (data) ->
Record.call this, data
Movie.prototype = Object.create Record.prototype
_.assign Movie.prototype,
name: ->
################################################################################
# person
Person = (data) ->
Record.call this, data
Person.prototype = Object.create Record.prototype
_.assign Person.prototype,
name: ->
################################################################################
# export factory
module.exports = (mesa) ->
result = {}
result.Movie = (data) -> Movie.call this, data
result.Movie.prototype = Object.create Movie.prototype
mesaForActiveRecord = mesa
.queueBeforeEach(camelToSnakeObject)
.queueAfterEach(snakeToCamelObject)
movieTable = mesaForActiveRecord
.table('movie')
.allow('name')
.queueAfterEach (data) ->
new result.Movie data
personTable = mesaForActiveRecord
.table('person')
.allow('name')
.queueAfterEach (data) ->
new result.Person data
result.Person = (data) -> Person.call this, data
result.Person.prototype = Object.create Person.prototype
# make all instances share the same table property
result.Movie.prototype._table = movieTable
result.Person.prototype._table = personTable
# static methods
_.assign result.Movie,
getWhereName: (name) ->
movieTable.where(name: name).first()
getWhereNameMatches: (name) ->
movieTable.where(name: name).first()
getWhereId: (id) ->
movieTable.where(id: id).first()
all: ->
movieTable.find()
starring: (nameOrId) ->
Person.getWhereName(actorName) ->
return result