Skip to content

Better structure with namespaces. New like condition and select method. Updated orWhere and getStoreId method. - #40

Merged
rakibtg merged 7 commits into
SleekDB:masterfrom
Timu57:master
Oct 11, 2020
Merged

rakibtg merged 7 commits into
SleekDB:masterfrom
Timu57:master

Conversation

@Timu57

@Timu57 Timu57 commented Sep 26, 2020

Copy link
Copy Markdown
Member

Hi,
thank you for this nice project.
I made some improvements.
Should not interfere with any code using this SleekDB.
Hope this can be merged or further improved before merging.

A little summary:

[ADDED]
like condition with regex;
ext-json to composer as required, because of json_encode and json_decode methods;
own file for every exception class, for better structure and easier usage outside of SleekDB;
namespaces to Traits and Exceptions;
[UPDATED]
file names of traits;
folder structure;
[FIXED]
missing @throws tags;
wrong behavior of verifyWhereConditions method if condition is not matched by any if statements;

ext-json to composer as required, because of json_encode and json_decode methods;
own file for every exception class, for better structure;
like functionality;
[UPDATED]
name spaces of Traits and Exceptions;
file names of traits;
[FIXED]
missing @throws tags;
wrong behavior of verifyWhereConditions method if condition is not matched by any if statements;
@Timu57 Timu57 mentioned this pull request Sep 26, 2020
@Timu57 Timu57 changed the title Better Structure with Namespaces and new Like Condition. Better structure with namespaces, new like condition and select method. Sep 27, 2020
@Timu57

Timu57 commented Sep 27, 2020

Copy link
Copy Markdown
Member Author

Added new select method to specify which fields should be in result.

The field names to select or exclude are separated with a space between.
To exclude a field prepend a - (minus) to the field name.

By default the field _id is always selected.
To exclude it specify -_id.


Example without select (nothing changed with this pr):

$newsStore->where("_id", "=", 2)->fetch();

Output:

[
  [
    "title" => "New Select Method",
    "content" => "Lorem Ipsum",
    "author" => "Max",
    "_id" => 2
  ]
]

Example 1 - Select one field:

$newsStore->select("title")->where("_id", "=", 2)->fetch();

Output:

[[ "_id" => 2, "title" => "New Select Method" ]]

Example 2 - Select two fields:

$newsStore->select("title author")->where("_id", "=", 2)->fetch();

Output:

[[ "_id" => 2, "title" => "New Select Method", "author" => "Max" ]]

Example 3 - Don't select field:

$newsStore->select("-author")->where("_id", "=", 2)->fetch();

Output:

[[ "_id" => 2, "title" => "New Select Method", "content" => "Lorem Ipsum" ]]

Example 4 - Don't select two fields:

$newsStore->select("-author -title")->where("_id", "=", 2)->fetch();

Output:

[[ "_id" => 2, "content" => "Lorem Ipsum" ]]

Example 5 - Just select author, without _id field:

$newsStore->select("author -_id")->where("_id", "=", 2)->fetch();

Output:

[[ "author" => "Lorem Ipsum" ]]

Example 6 - Exclude not existing field:

$newsStore->select("-rating")->where("_id", "=", 2)->fetch();

Output:

[
  [
    "title" => "New Select Method",
    "content" => "Lorem Ipsum",
    "author" => "Max",
    "_id" => 2
  ]
]

Example 7 - Select not existing field:

$newsStore->select("rating")->where("_id", "=", 2)->fetch();

Output:

[[ "_id" => 2 ]]

#38 I think this is a good solution for this enhancement request.

@Timu57

Timu57 commented Sep 27, 2020

Copy link
Copy Markdown
Member Author

#31

Updated the orWhere method to allow infinite conditions that have an "and" between each condition.

Now the orWhere method can be used in four ways:

"Old way"

$newsStore->where("_id", "=", 2)->orWhere("author", "=", "Max")->fetch();

With arrays

$condition1 = ["author", "=", "Max"];
$condition2 = ["author", "=", "Isabelle"];
$newsStore->where("_id", "=", 2)->orWhere($condition1, $condition2)->fetch();

With maps

$condition1 = ["fieldName" => "author", "condition" => "=", "value" => "Max"];
$condition2 = ["fieldName" => "author", "condition" => "=", "value" => "Isabelle"];
$newsStore->where("_id", "=", 2)->orWhere($condition1, $condition2)->fetch();

Mix of array and map

$condition1 = ["author", "=", "Max"];
$condition2 = ["fieldName" => "author","condition" =>  "=", "value" => "Isabelle"];
$newsStore->where("_id", "=", 2)->orWhere($condition1, $condition2)->fetch();

@Timu57 Timu57 changed the title Better structure with namespaces, new like condition and select method. Better structure with namespaces. New like condition and select method. Updated orWhere method. Sep 27, 2020
@Timu57

Timu57 commented Sep 27, 2020

Copy link
Copy Markdown
Member Author

#30
Updated getStoreId method to use flock. This prevents duplicate _id

@Timu57 Timu57 changed the title Better structure with namespaces. New like condition and select method. Updated orWhere method. Better structure with namespaces. New like condition and select method. Updated orWhere and storeId method. Sep 27, 2020
@Timu57 Timu57 changed the title Better structure with namespaces. New like condition and select method. Updated orWhere and storeId method. Better structure with namespaces. New like condition and select method. Updated orWhere and getStoreId method. Sep 27, 2020
@rakibtg

rakibtg commented Sep 28, 2020 •

Copy link
Copy Markdown
Member

@Timu57 Many thanks for your contributions, it's inspiring and much needed.
I am all booked till this Sunday, then the first thing I would do is to review your code and merge it right away. Sorry for the delay.

Here are some quick thoughts:
The way you have implemented the select method is really interesting, we can select or de-select at once like this:

$newsStore->select("author -_id");

After a while the first thing I have noticed that it might create confusions, for example some one might have a key called

-name

Now if he want to select only name he would write the query like this: (If I understood it correctly)

$newsStore->select("-name");

It will work now in opposite direction! Some times it could be also readability issue for other team members.

So, I was thinking of two different methods,

  • select (Keeps provided key's)
  • except (Omits provided key's)

Making sure,

  • They only accepts an array of key names. Not string exploded with a space as "first name" is also valid as a key name!
    // Select few fields.
    select(['_id', 'name', 'country']) 
    
    // Exclude fields.
    except(['name'])

Please let me know what you think about this update.
Have a nice day!

@Timu57

Timu57 commented Sep 29, 2020

Copy link
Copy Markdown
Member Author

@rakibtg Thank you for your response.
No problem, take your time.
My inspiration for this implementation was the way mongoose is handling the select.
https://mongoosejs.com/docs/api/query.html#query_Query-select
But I see your point.
I will update the implementation to use arrays instead of a String, to allow keys with space and a minus prepended.

[ADDED] except method to exclude specified fields
@Timu57

Timu57 commented Sep 29, 2020

Copy link
Copy Markdown
Member Author

Now the implementation is like this:

// select few fields
$newsStore->select(["title","content"])->where("_id", "=", 2)->fetch();
// exclude fields
$newsStore->except(["_id"])->where("_id", "=", 2)->fetch();
// select and exclude fields
$newsStore->select(["title"])->except(["_id","author"])->where("_id", "=", 2)->fetch();

@Timu57

Timu57 commented Oct 8, 2020

Copy link
Copy Markdown
Member Author

Hi, is something wrong with the pr?

@rakibtg

rakibtg commented Oct 9, 2020

Copy link
Copy Markdown
Member

Hi @Timu57 Extremely sorry for the delay, hopefully I can merge it within tomorrow :)

@rakibtg
rakibtg merged commit 9f4c2d9 into SleekDB:master Oct 11, 2020
@rakibtg

rakibtg commented Oct 11, 2020 •

Copy link
Copy Markdown
Member

@Timu57 Merged 🎉 Thanks for the PR ❤️. Going to create a new release soon.

@Timu57

Timu57 commented Oct 12, 2020 •

Copy link
Copy Markdown
Member Author

@rakibtg Thank you for merging this PR 🥰 some open issues are covered by this PR.

#30 #31 #34 #38

Just wanted to say, that you could consider closing them 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants