Comments (9)
Hey, @duolabmeng6, what would be the advantage of such a functionality?
from clover.
Hey, @duolabmeng6, what would be the advantage of such a functionality?
The current writing method is too long, I hope it's simpler
Where similar to SQL statements
from clover.
I see the value of having a query language, but I don't think it should be part of clover, as it is meant to stay very simple.
Also, I would say there are a lot of potential query languages that could be supported, and for such a reason, I would delegate to the user of the library the responsibility of building such an utility on top of the lib
from clover.
func TestBUG(t *testing.T) {
store, _ := badgerstore.OpenWithOptions(badger.DefaultOptions("").WithInMemory(true))
db, _ := c.OpenWithStore(store)
text := `[
{
"name": "liming",
"age": 18
},
{
"name": "xiaobai",
"age": 20
},
{
"name": "xiaohong",
"age": 22
}
]`
os.WriteFile("text.json", []byte(text), 0666)
db.ImportCollection("names", "text.json")
dataAll, err := db.FindAll(
query.NewQuery("names").
Where(
query.Field("name").Eq("xiaobai").Or(query.Field("name").GtEq("liming")),
))
if err != nil {
println("SEARCH_FAILED")
}
for _, doc := range dataAll {
fmt.Printf("%+v\n", doc)
}
//&{fields:map[_id:26c9acd6-f9b1-46fe-81ce-2da6f96f886c age:20 name:xiaobai]}
//&{fields:map[_id:83dc8aaa-94ad-4333-9945-98ec6198b83d age:18 name:liming]}
//&{fields:map[_id:ff5f6b48-0687-49ee-a4fe-ae392ac488af age:22 name:xiaohong]}
}
Your current API is not simple and has logic bugs
from clover.
func TestBUG(t *testing.T) {
store, _ := badgerstore.OpenWithOptions(badger.DefaultOptions("").WithInMemory(true))
db, _ := c.OpenWithStore(store)
text := `[
{
"name": "liming",
"age": 18
},
{
"name": "xiaobai",
"age": 20
},
{
"name": "xiaohong",
"age": 22
}
]`
os.WriteFile("text.json", []byte(text), 0666)
db.ImportCollection("names", "text.json")
dataAll, err := db.FindAll(
query.NewQuery("names").
Where(
query.Field("name").Eq("xiaobai").And(query.Field("age").Gt("18")),
))
if err != nil {
println("SEARCH_FAILED")
}
for _, doc := range dataAll {
fmt.Printf("%+v\n", doc)
}
}
Your current API is difficult to use
query.Field("name").Eq("xiaobai").And(query.Field("age").Gt("18")),
could not find it
query.Field("name").Eq("xiaobai").And(query.Field("age").Gt(18)),
have found
from clover.
Your design concept is very different from tinydb’s query statement
A simple query should look like this
>>> User = Query()
>>> # Search for a field value
>>> db.search(User.name == 'John')
[{'name': 'John', 'age': 22}, {'name': 'John', 'age': 37}]
>>> # Combine two queries with logical and
>>> db.search((User.name == 'John') & (User.age <= 30))
[{'name': 'John', 'age': 22}]
>>> # Combine two queries with logical or
>>> db.search((User.name == 'John') | (User.name == 'Bob'))
[{'name': 'John', 'age': 22}, {'name': 'John', 'age': 37}, {'name': 'Bob', 'age': 42}]
>>> # Apply transformation to field with `map`
>>> db.search((User.age.map(lambda x: x + x) == 44))
>>> [{'name': 'John', 'age': 22}]
>>> # More possible comparisons: != < > <= >=
>>> # More possible checks: where(...).matches(regex), where(...).test(your_test_func)
from clover.
If there are bugs, please open separate issue, as they are not related to supporting the query language you suggest.
Also, regarding TinyDB API, it's clear that Golang doesn't allow to reach same level of simplicity, but if you have some suggestions on how to improve the API your are welcome.
Other than that I'm not going to support a query language from text at the moment
from clover.
I understand that Golang cannot achieve the syntax effects of Python, but it can refer to the query building API of Laravel. It is very simple and easy to write.
$users = DB::table('users')
->where('votes', '>', 100)
->orWhere('name', 'John')
->get();
$users = DB::table('users')->where([
['status', '=', '1'],
['subscribed', '<>', '1'],
])->get();
https://laravel.com/docs/10.x/queries#where-clauses
from clover.
Supporting such API would require major effort and time that I currently don't have. Will consider to do it in future if more and more users see an advantage in that
from clover.
Related Issues (20)
- Example Request: Unmarshal/Update/Replace HOT 6
- Cannot allocate initial memory HOT 1
- GT LT operation very slow HOT 3
- Question: Is there a way to search for a value in all fields? HOT 2
- Getting error when during bulk insert HOT 3
- Small v2 package annoyance HOT 10
- Question: Searching for all documents within time range HOT 1
- distributed? HOT 1
- Support struct in Save() method HOT 7
- Add `OpenWithOptions()` to badger store
- Memory efficient `ExportCollection()` HOT 1
- need update readme HOT 2
- Memory store was removed in v2 HOT 1
- doc.Unmarshal does not play well with `json` tag HOT 5
- Badger storage | RunValueLogGC() is managed with a fatal error HOT 1
- using BadgerDB: DropCollection returns a "Txn is too big to fit into one request" HOT 2
- Why is the _id a uuid
- Runtime error by using of function db.Insert HOT 2
- Unmarshalling with embedded structs is failing
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from clover.