Skip to content

merge #20

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 33 commits into from
Feb 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
459d7e8
applied pull request https://github.com/jenssegers/laravel-mongodb/pu…
hlorofos Apr 19, 2018
e1e95a5
get database from DSN string
hlorofos Apr 19, 2018
068a0af
bugfix: improper database configuration values used
hlorofos Apr 19, 2018
76a5dbc
bugfix: failover to database configuration values if no ability to ge…
hlorofos Apr 19, 2018
929120f
bugfix: failover to database configuration values if no ability to ge…
hlorofos Apr 19, 2018
d26a192
Fix Regex example
divine Jan 30, 2020
c25900b
Merge pull request #1935 from divine/pr_1886
Smolevich Jan 31, 2020
2c95ab7
allow setting hint option on querybuilder
divine Jan 31, 2020
8a51887
Remove depereced arraysubset
divine Jan 31, 2020
c55d42d
Merge pull request #1939 from divine/pr_1556
Smolevich Jan 31, 2020
e2e5917
Add explanation for database migration resets
divine Jan 31, 2020
9f1b581
Add explanation for not working classess
divine Feb 4, 2020
0bbb404
update clarification for database migration
divine Feb 4, 2020
ef147ea
Merge pull request #1941 from divine/pr_1476
Smolevich Feb 5, 2020
ca0f710
Fix dropIndex for compound indexes with sorting order
mauri870 Dec 11, 2019
9442df1
Fix truncate on models
divine Feb 8, 2020
4e92dcc
Add tests for model truncate
divine Feb 8, 2020
79cb569
Merge pull request #1949 from divine/fix_truncate_on_models
Smolevich Feb 8, 2020
e1135e8
[GF] Added issue template files
Feb 8, 2020
f7f326b
[GF] Add config for codacy
Feb 8, 2020
62f8cff
Merge pull request #1951 from Giacomo92/add-config-for-codacy
Smolevich Feb 8, 2020
7bca359
Merge branch 'master' into add-issues-templates
Feb 8, 2020
c6313cb
Merge pull request #1950 from Giacomo92/add-issues-templates
Smolevich Feb 8, 2020
9b062ac
Merge branch 'master' into drop-compound-index
Smolevich Feb 8, 2020
4d81b40
Merge pull request #1885 from mauri870/drop-compound-index
Smolevich Feb 8, 2020
1ffc7a2
Merge branch 'pr_1491' into pr/17
divine Feb 9, 2020
5cb9a01
Remove duplicate dsn
divine Feb 9, 2020
d0c7b4d
Merge pull request #18 from divine/pr/17
divine Feb 9, 2020
3f120e8
Move coveralls to dev dependency
divine Feb 9, 2020
1c8a84b
Merge pull request #1955 from divine/move_coveralls_to_dev
Smolevich Feb 10, 2020
0f5b177
Merge branch 'master' into pr_1491
divine Feb 10, 2020
89fe91f
Merge pull request #1954 from divine/pr_1491
Smolevich Feb 10, 2020
c7278e0
Merge branch 'laravel-7' into pr/19
divine Feb 12, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .codacy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
exclude_paths:
- '.github/**'
25 changes: 25 additions & 0 deletions .github/ISSUE_TEMPLATE/BUG_REPORT.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
---
name: "Bug report"
about: 'Report errors or unexpected behavior.'
---

- Laravel-mongodb Version: #.#.#
- PHP Version: #.#.#
- Database Driver & Version:

### Description:

### Steps to reproduce
1.
2.
3.

### Expected behaviour
Tell us what should happen

### Actual behaviour
Tell us what happens instead

<details><summary><b>Logs</b>:</summary>
Insert log.txt here (if necessary)
</details>
18 changes: 18 additions & 0 deletions .github/ISSUE_TEMPLATE/FEATURE-REQUEST.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
---
name: Feature request
about: Suggest an idea.
title: "[Feature Request] "

---

### Is your feature request related to a problem?
A clear and concise description of what the problem is.

### Describe the solution you'd like
A clear and concise description of what you want to happen.

### Describe alternatives you've considered
A clear and concise description of any alternative solutions or features you've considered.

### Additional context
Add any other context or screenshots about the feature request here.
8 changes: 8 additions & 0 deletions .github/ISSUE_TEMPLATE/QUESTION.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
name: Question
about: Ask a question.
title: "[Question] "
labels: 'question'
assignees: ''

---
22 changes: 21 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ This package adds functionalities to the Eloquent model and Query builder for Mo
- [Lumen](#lumen)
- [Non-Laravel projects](#non-laravel-projects)
- [Testing](#testing)
- [Database Testing](#database-testing)
- [Configuration](#configuration)
- [Eloquent](#eloquent)
- [Extending the base model](#extending-the-base-model)
Expand Down Expand Up @@ -113,6 +114,25 @@ To run the test for this package, run:
docker-compose up
```

Database Testing
-------

To reset the database after each test, add:

```php
use Illuminate\Foundation\Testing\DatabaseMigrations;
```

Also inside each test classes, add:

```php
use DatabaseMigrations;
```

Keep in mind that these traits are not yet supported:
- `use Database Transactions;`
- `use RefreshDatabase;`

Configuration
-------------
You can use MongoDB either as the main database, either as a side database. To do so, add a new `mongodb` connection to `config/database.php`:
Expand Down Expand Up @@ -460,7 +480,7 @@ Selects documents where values match a specified regular expression.
```php
use MongoDB\BSON\Regex;

User::where('name', 'regex', new Regex("/.*doe/i"))->get();
User::where('name', 'regex', new Regex('.*doe', 'i'))->get();
```

**NOTE:** you can also use the Laravel regexp operations. These are a bit more flexible and will automatically convert your regular expression string to a `MongoDB\BSON\Regex` object.
Expand Down
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@
"illuminate/container": "^5.8|^6.0|^7.0",
"illuminate/database": "^5.8|^6.0|^7.0",
"illuminate/events": "^5.8|^6.0|^7.0",
"mongodb/mongodb": "^1.4",
"cedx/coveralls": "^11.2"
"mongodb/mongodb": "^1.4"
},
"require-dev": {
"phpunit/phpunit": "^6.0|^7.0|^8.0",
"orchestra/testbench": "^3.1|^4.0",
"mockery/mockery": "^1.0",
"doctrine/dbal": "^2.5",
"phpunit/phpcov": "^6.0"
"phpunit/phpcov": "^6.0",
"cedx/coveralls": "^11.2"
},
"autoload": {
"psr-0": {
Expand Down
15 changes: 13 additions & 2 deletions src/Jenssegers/Mongodb/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public function __construct(array $config)
$this->connection = $this->createConnection($dsn, $config, $options);

// Select database
$this->db = $this->connection->selectDatabase($config['database']);
$this->db = $this->connection->selectDatabase($this->getDatabaseDsn($dsn, $config['database']));

$this->useDefaultPostProcessor();

Expand Down Expand Up @@ -188,10 +188,21 @@ protected function getHostDsn(array $config)

// Check if we want to authenticate against a specific database.
$auth_database = isset($config['options']) && !empty($config['options']['database']) ? $config['options']['database'] : null;

return 'mongodb://' . implode(',', $hosts) . ($auth_database ? '/' . $auth_database : '');
}

/**
* Get database name from DSN string, if there is no database in DSN path - returns back $database argument.
* @param string $dsn
* @param $database
* @return string
*/
protected function getDatabaseDsn($dsn, $database)
{
$dsnDatabase = trim(parse_url($dsn, PHP_URL_PATH), '/');
return trim($dsnDatabase) ? $dsnDatabase : $database;
}

/**
* Create a DSN string from a configuration.
* @param array $config
Expand Down
10 changes: 8 additions & 2 deletions src/Jenssegers/Mongodb/Query/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -384,10 +384,12 @@ public function getFresh($columns = [])
if ($this->limit) {
$options['limit'] = $this->limit;
}
if ($this->hint) {
$options['hint'] = $this->hint;
}
if ($columns) {
$options['projection'] = $columns;
}
// if ($this->hint) $cursor->hint($this->hint);

// Fix for legacy support, converts the results to arrays instead of objects.
$options['typeMap'] = ['root' => 'array', 'document' => 'array'];
Expand Down Expand Up @@ -698,7 +700,11 @@ public function from($collection, $as = null)
*/
public function truncate()
{
$result = $this->collection->drop();
$options = [
'typeMap' => ['root' => 'object', 'document' => 'object'],
];

$result = $this->collection->drop($options);

return (1 == (int) $result->ok);
}
Expand Down
14 changes: 12 additions & 2 deletions src/Jenssegers/Mongodb/Schema/Blueprint.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,18 @@ protected function transformColumns($indexOrColumns)
// Transform the columns to the index name.
$transform = [];

foreach ($indexOrColumns as $column) {
$transform[$column] = $column . '_1';
foreach ($indexOrColumns as $key => $value) {
if (is_int($key)) {
// There is no sorting order, use the default.
$column = $value;
$sorting = '1';
} else {
// This is a column with sorting order e.g 'my_column' => -1.
$column = $key;
$sorting = $value;
}

$transform[$column] = $column . "_" . $sorting;
}

$indexOrColumns = implode('_', $transform);
Expand Down
9 changes: 9 additions & 0 deletions tests/ModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -572,4 +572,13 @@ public function testChunkById(): void

$this->assertEquals(3, $count);
}

public function testTruncateModel()
{
User::create(['name' => 'John Doe']);

User::truncate();

$this->assertEquals(0, User::count());
}
}
24 changes: 23 additions & 1 deletion tests/QueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,8 @@ public function testDelete()
public function testTruncate()
{
DB::collection('users')->insert(['name' => 'John Doe']);
DB::collection('users')->truncate();
$result = DB::collection('users')->truncate();
$this->assertEquals(1, $result);
$this->assertEquals(0, DB::collection('users')->count());
}

Expand Down Expand Up @@ -737,4 +738,25 @@ public function testValue()
$this->assertEquals('Herman', DB::collection('books')->value('author.first_name'));
$this->assertEquals('Melville', DB::collection('books')->value('author.last_name'));
}

public function testHintOptions()
{
DB::collection('items')->insert([
['name' => 'fork', 'tags' => ['sharp', 'pointy']],
['name' => 'spork', 'tags' => ['sharp', 'pointy', 'round', 'bowl']],
['name' => 'spoon', 'tags' => ['round', 'bowl']],
]);

$results = DB::collection('items')->hint(['$natural' => -1])->get();

$this->assertEquals('spoon', $results[0]['name']);
$this->assertEquals('spork', $results[1]['name']);
$this->assertEquals('fork', $results[2]['name']);

$results = DB::collection('items')->hint(['$natural' => 1])->get();

$this->assertEquals('spoon', $results[2]['name']);
$this->assertEquals('spork', $results[1]['name']);
$this->assertEquals('fork', $results[0]['name']);
}
}
14 changes: 14 additions & 0 deletions tests/SchemaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,20 @@ public function testDropIndex(): void
$index = $this->getIndex('newcollection', 'field_a_1_field_b_1');
$this->assertFalse($index);

Schema::collection('newcollection', function ($collection) {
$collection->index(['field_a' => -1, 'field_b' => 1]);
});

$index = $this->getIndex('newcollection', 'field_a_-1_field_b_1');
$this->assertNotNull($index);

Schema::collection('newcollection', function ($collection) {
$collection->dropIndex(['field_a' => -1, 'field_b' => 1]);
});

$index = $this->getIndex('newcollection', 'field_a_-1_field_b_1');
$this->assertFalse($index);

Schema::collection('newcollection', function ($collection) {
$collection->index(['field_a', 'field_b'], 'custom_index_name');
});
Expand Down