Skip to content

Commit 62445fd

Browse files
committed
Add index method
1 parent 147b252 commit 62445fd

File tree

5 files changed

+145
-2
lines changed

5 files changed

+145
-2
lines changed

composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "mehedimi/laravel-dynamodb",
3-
"version": "0.2.0",
3+
"version": "0.3.0",
44
"description": "A package for Laravel to communicate DynamoDB",
55
"type": "package",
66
"license": "MIT",

src/Concerns/BuildQueries.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public function chunk($count, callable $callback): bool
1818
$page = 1;
1919

2020
do {
21-
$results = $this->get([]);
21+
$results = $this->get();
2222

2323
if (call_user_func_array($callback, [$results, $page]) === false) {
2424
return false;
+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace Mehedi\LaravelDynamoDB\Eloquent\Concerns;
4+
5+
use Mehedi\LaravelDynamoDB\Eloquent\Relations\HasOne;
6+
7+
trait HasRelationships
8+
{
9+
/**
10+
* Define a one-to-one relationship.
11+
*
12+
* @param string $related
13+
* @param array|null $foreignKey
14+
* @param array|null $localKey
15+
* @return \Mehedi\LaravelDynamoDB\Eloquent\Relations\HasOne
16+
*/
17+
public function hasOne($related, $foreignKey = null, $localKey = null)
18+
{
19+
$instance = $this->newRelatedInstance($related);
20+
21+
$foreignKey = $foreignKey ?? $instance->getKeysName();
22+
23+
$localKey = $localKey ?? $this->getKeysName();
24+
25+
26+
return new HasOne($instance->newQuery(), $this, $foreignKey, $localKey);
27+
}
28+
}

src/Eloquent/Relations/HasOne.php

+102
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
<?php
2+
3+
namespace Mehedi\LaravelDynamoDB\Eloquent\Relations;
4+
5+
use Illuminate\Database\Eloquent\Model;
6+
use Illuminate\Database\Eloquent\Relations\HasOne as BaseHasOne;
7+
8+
class HasOne extends BaseHasOne
9+
{
10+
public function addConstraints()
11+
{
12+
if (static::$constraints) {
13+
/** @var \Mehedi\LaravelDynamoDB\Eloquent\Builder $query */
14+
$query = $this->getRelationQuery();
15+
16+
$query->query->key([
17+
$this->getForeignPartitionKeyName() => $this->getForeignPartionKey(),
18+
$this->getForeignSortKeyName() => $this->getForeignSortKey(),
19+
]);
20+
}
21+
}
22+
23+
/**
24+
* Get foreign partition key name
25+
*
26+
* @return string
27+
*/
28+
public function getForeignPartitionKeyName()
29+
{
30+
return $this->foreignKey[0];
31+
}
32+
33+
/**
34+
* Get foreign partition key name
35+
*
36+
* @return string
37+
*/
38+
public function getForeignPartionKey()
39+
{
40+
return $this->parent->getAttribute($this->localKey[0]);
41+
}
42+
43+
/**
44+
* Get foreign sort key name
45+
*
46+
* @return mixed
47+
*/
48+
public function getForeignSortKeyName()
49+
{
50+
return $this->foreignKey[1];
51+
}
52+
53+
/**
54+
* Get foreign partition key value
55+
*
56+
* @return string
57+
*/
58+
public function getForeignSortKey()
59+
{
60+
return $this->parent->getAttribute($this->localKey[1]);
61+
}
62+
63+
/**
64+
* Get the results of the relationship.
65+
*
66+
* @return mixed
67+
*/
68+
public function getResults()
69+
{
70+
if (is_null($this->getParentKey())) {
71+
return $this->getDefaultFor($this->parent);
72+
}
73+
74+
return $this->query->find(null) ?: $this->getDefaultFor($this->parent);
75+
}
76+
77+
/**
78+
* Get the key value of the parent's local key.
79+
*
80+
* @return mixed
81+
*/
82+
public function getParentKey()
83+
{
84+
return $this->parent->getKey();
85+
}
86+
87+
/**
88+
* Set the constraints for an eager load of the relation.
89+
*
90+
* @param array $models
91+
* @return void
92+
*/
93+
public function addEagerConstraints(array $models)
94+
{
95+
dd($models);
96+
$whereIn = $this->whereInMethod($this->parent, $this->localKey);
97+
98+
$this->getRelationQuery()->{$whereIn}(
99+
$this->foreignKey, $this->getKeys($models, $this->localKey)
100+
);
101+
}
102+
}

src/Query/Builder.php

+13
Original file line numberDiff line numberDiff line change
@@ -703,6 +703,19 @@ public function insert(array $values)
703703
return $this->putItem($values);
704704
}
705705

706+
/**
707+
* Set different index
708+
*
709+
* @param $indexName
710+
* @return $this
711+
*/
712+
public function index($indexName)
713+
{
714+
$this->indexName = $indexName;
715+
716+
return $this;
717+
}
718+
706719
/**
707720
* Insert or replace an item
708721
*

0 commit comments

Comments
 (0)