-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlifecycle.html
More file actions
74 lines (54 loc) · 2.89 KB
/
Copy pathlifecycle.html
File metadata and controls
74 lines (54 loc) · 2.89 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
---
layout: default
---
<div class="content">
<h1>Lifecycle</h1>
<p>Tyranid supports a lifecycle for its models to help model data workflows and lifecycle within Tyranid models.</p>
<h2><u>boot</u></h2>
<p>Initialization code should be placed in the collection <a href="component#boot"><u><i>Component.</i>boot</u></a>.</p>
<p>Any sort of code that needs to run when the application servers are first launched should be placed here.</p>
<p>The Tyranid bootstrapping framework also supports dealing with complex bootstrapping dependencies and
orderings -- including circular dependencies.</p>
<h2>Events</h2>
<p><a href="event">Events</a> are the primary place to handle most lifecycle and workflow concerns.</p>
<p><b>On-change handlers</b> provide a place to do additional tasks when documents are created
or modified. For example, updating ElasticSearch indices, updating permissions, creation of related
documents, and so on.</p>
<p><b>On-remove handlers</b> provide a place to perform any tasks that are needed when a document is removed. For example,
removing documents from external indices, removing dependent documents, and so on.</p>
<h2>Validation</h2>
<p>In addition to standard built-in validations like <u>required</u>, <u>minlength</u>, and so on (see
<a href="field#def">field definitions</a> for a full list), <a href="field"><u>Field</u></a>s can have
also an (optionally asynchronous) handler defined for each field. This can be used to perform field-level
validations that are triggered while interacting with a client-side form.</p>
<p>For example, the following validation could be provided which front-end controls would invoke when a user
focuses out of an email field:</p>
<pre><code class="js"> <i>...,</i>
email: {
is: 'email',
required: true,
async validate() {
if (this.email) {
return (
(await User.exists({
query: {
email: {
$regex: '^' + escapeRegex(this.email) + '$',
$options: 'i'
},
_id: { $ne: this._id }
}
})) && `Sorry, that email is already used: ${this.email}`
);
}
}
},
<i>...</i></code></pre>
<h2><u><i>Collection.</i>fromClient</u></h2>
<p>Implementing a collection <a href="collection#def"><u><i>Collection.</i>fromClient</u></a> handler
provides the primary place to perform updates to collection instances after being created or edited
on the client.</p>
<p>It can also be used as a place to provide additional security checks and verifications as all updates from client
will go through this method before being committed to the database.</p>
<p>This method handler can optionally be <u>async</u> so that security checks and other asynchronous code can be called.</p>
</div>