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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
<?php namespace Jenssegers\Mongodb\Schema;
use Closure;
use Illuminate\Database\Connection;
class Blueprint extends \Illuminate\Database\Schema\Blueprint {
/**
* The MongoConnection object for this blueprint.
*
* @var MongoConnection
*/
protected $connection;
/**
* The MongoCollection object for this blueprint.
*
* @var MongoCollection
*/
protected $collection;
/**
* Fluent columns
*
* @var array
*/
protected $columns = array();
/**
* Create a new schema blueprint.
*
* @param string $table
* @param Closure $callback
* @return void
*/
public function __construct(Connection $connection, $collection)
{
$this->connection = $connection;
$this->collection = $connection->getCollection($collection);
}
/**
* Specify an index for the collection.
*
* @param string|array $columns
* @param array $options
* @return bool
*/
public function index($columns = null, $options = array())
{
$columns = $this->fluent($columns);
// Columns are passed as a default array
if (is_array($columns) && is_int(key($columns)))
{
// Transform the columns to the required array format
$transform = array();
foreach ($columns as $column)
{
$transform[$column] = 1;
}
$columns = $transform;
}
$this->collection->ensureIndex($columns, $options);
return $this;
}
/**
* Indicate that the given index should be dropped.
*
* @param string|array $columns
* @return bool
*/
public function dropIndex($columns = null)
{
$columns = $this->fluent($columns);
foreach ($columns as $column)
{
$this->collection->deleteIndex($column);
}
return $this;
}
/**
* Specify a unique index for the collection.
*
* @param string|array $columns
* @return bool
*/
public function unique($columns = null, $name = null)
{
$columns = $this->fluent($columns);
$this->index($columns, array('unique' => true));
return $this;
}
/**
* Specify a non blocking index for the collection.
*
* @param string|array $columns
* @return bool
*/
public function background($columns = null)
{
$columns = $this->fluent($columns);
$this->index($columns, array('background' => true));
return $this;
}
/**
* Specify a sparse index for the collection.
*
* @param string|array $columns
* @return bool
*/
public function sparse($columns = null)
{
$columns = $this->fluent($columns);
$this->index($columns, array('sparse' => true));
return $this;
}
/**
* Specify the number of seconds after wich a document should be considered expired based,
* on the given single-field index containing a date.
*
* @param string|array $columns
* @param int $seconds
* @return bool
*/
public function expire($columns, $seconds)
{
$columns = $this->fluent($columns);
$this->index($columns, array('expireAfterSeconds' => $seconds));
return $this;
}
/**
* Indicate that the table needs to be created.
*
* @return bool
*/
public function create()
{
$collection = $this->collection->getName();
// Ensure the collection is created
$db = $this->connection->getMongoDB();
$db->createCollection($collection);
}
/**
* Indicate that the collection should be dropped.
*
* @return bool
*/
public function drop()
{
$this->collection->drop();
}
/**
* Add a new column to the blueprint.
*
* @param string $type
* @param string $name
* @param array $parameters
* @return Blueprint
*/
protected function addColumn($type, $name, array $parameters = array())
{
$this->fluent($name);
return $this;
}
/**
* Allow fluent columns
*
* @param string|array $columns
* @return string|array
*/
protected function fluent($columns = null)
{
if (is_null($columns))
{
return $this->columns;
}
else if (is_string($columns))
{
return $this->columns = array($columns);
}
else
{
return $this->columns = $columns;
}
}
}