This repository was archived by the owner on Aug 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 604
Expand file tree
/
Copy pathtest.coffee
More file actions
executable file
·125 lines (111 loc) · 2.7 KB
/
test.coffee
File metadata and controls
executable file
·125 lines (111 loc) · 2.7 KB
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
{ Model, ObjectId, mongodb } = require '../lib'
{ Db, Server } = mongodb
{ log } = console
Model.setClient 'mongodb://localhost:27017/bongo_test'
class Foo extends Model
@set
# client : # credentials
# server : 'localhost'
# port : 27017
# database : 'test'
schema : # annotations:
test1 :
type : Number
validator : ['not enough', (value) -> value > 10]
set : (value) -> Math.ceil 10 + value * 25
test2 :
type : Number
validator : ['range error', (value) -> 15 < value < 30]
test3 :
type : String
pattern : /[a-z]+/
test4 : Object
test5 :
type : Date
default : -> new Date
test6 : ObjectId
test7 : [String]
test8 : [Foo]
class Bar extends Model
@setSchema
best1 : String
best2 : Foo
# this is a normal embedded schema. nothing fancy.
# it works because Foo is a *constructor, not an instance.'
class SaveRecord extends Model
@setSchema
createdAt :
type : Date
default : -> new Date
collectionName : String
foo = new Foo
test1 : Math.random()
test2 : Math.floor 15 + Math.random() * 15
test3 : 'sldkajsldja'
test4 :
isAdHoc : yes
foo.on 'save', (step) ->
switch step
when 1 then log 'saving a root document.'
when Math.PI then log 'saving a nested document.'
bar = new Bar
best1: 'cowabunga, dude'
best2: foo
bar.on 'error', (err) ->
foo.save()
setInterval ->
model = if Math.random() > .5 then bar else foo
collectionName = model.getCollectionName()
log collectionName
saveRecord = new SaveRecord { collectionName }
saveRecord.save()
model.save()
, 1000
##foo.validate (errs) ->
## if errs.length is 1
## throw errs[0]
## else if errs.length
## throw new Error 'There were some problems with your input.'
#
## nested schemas:
##
#class Bar extends Model
# @setSchema
# best1 : String
# best2 : Foo
# # this is a normal embedded schema. nothing fancy.
# # it works because Foo is a *constructor, not an instance.'
#
##bar = new Bar
## best1: 'cowabunga, dude'
## best2: foo
##
#bar2 = new Bar
# best1 : 'second test'
# best2 :
# test1 : 6
# test2 : 18
# test8 :
# [
# {
# test1 : 4
# test2 : 16
# }
# {
# test1 : 13
# test2 : 25
# }
# ]
#
#class Baz extends Model
# @setSchema
# boast1: String
# boast2: Bar
#
#baz = new Baz
# boast1: 'arbitrary depth'
# boast2: bar2
#
#bar2.save console.log
#
#