Comments (7)
Thanks @mathe42 would be good to have an understanding of what kind of things it would need to return...
- What would be passed in (presuming SQL query)?
- What would an example response look like (i.e. what could an ideal response look like)?
- What are examples of how the response would be used (would help me to think about what kind of functionality can be exposed)?
from surrealdb.wasm.
- Yes basicly any string (can be potentaly "fksdjflkjdf").
- Honestly I don't care about the format that much.
- Main use case is a LanguageServer.
Main goal for this would be (we would also need something like surrealdb/surrealdb#248) to have something like in the picture:
Where you could dynamicly select the namespace you want.
For long and complex queries that would be great!
The thing is: I think I can help to write a LanguageServer written in TypeScript. The alternative is to implement it in rust.
Here is a list of LangueServers:
https://microsoft.github.io/language-server-protocol/implementors/servers/
And the LSP-Spec
https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#languageFeatures
from surrealdb.wasm.
I got it!
fn parse2(sql: String) {
let parsedQueryResult = surrealdb::sql::parse(&sql);
let parsedQuery = parsedQueryResult.unwrap();
let jsonResult = serde_json::to_string(&parsedQuery);
let json = jsonResult.unwrap();
return json;
}
That was the result I wanted. It is not 100% the best output as the way from the AST to the SQL is not defined but I think that is a good starting point!
Example
For
INSERT IGNORE INTO company (name, founded) VALUES ('SurrealDB', '2021-09-10') ON DUPLICATE KEY UPDATE tags += 'developer tools'
you get
[
{
"Insert": {
"into": "company",
"data": {
"ValuesExpression": [
[
[
[
{
"Field": "name"
}
],
"SurrealDB"
],
[
[
{
"Field": "founded"
}
],
"2021-09-10T00:00:00Z"
]
]
]
},
"ignore": true,
"update": {
"UpdateExpression": [
[
[
{
"Field": "tags"
}
],
"Inc",
"developer tools"
]
]
},
"output": null,
"timeout": null,
"parallel": false
}
}
]
from surrealdb.wasm.
I got it!
fn parse2(sql: String) { let parsedQueryResult = surrealdb::sql::parse(&sql); let parsedQuery = parsedQueryResult.unwrap(); let jsonResult = serde_json::to_string(&parsedQuery); let json = jsonResult.unwrap(); return json; }That was the result I wanted. It is not 100% the best output as the way from the AST to the SQL is not defined but I think that is a good starting point!
Example
For
INSERT IGNORE INTO company (name, founded) VALUES ('SurrealDB', '2021-09-10') ON DUPLICATE KEY UPDATE tags += 'developer tools'you get
[ { "Insert": { "into": "company", "data": { "ValuesExpression": [ [ [ [ { "Field": "name" } ], "SurrealDB" ], [ [ { "Field": "founded" } ], "2021-09-10T00:00:00Z" ] ] ] }, "ignore": true, "update": { "UpdateExpression": [ [ [ { "Field": "tags" } ], "Inc", "developer tools" ] ] }, "output": null, "timeout": null, "parallel": false } } ]
Nice find :D
I was playing/digging a little more just; and I managed to figure how to add a new rpc command, it's so simple but because i know 0 rust I'm so proud 😂
You can dump the AST with your logic and this
src/net/rpc.rs, around line 130, the methods are matched, add this one below
"ast" => match params.take_two() {
(Value::Strand(s), o) if o.is_none() => rpc.read().await.query_ast(s).await,
(Value::Strand(s), Value::Object(o)) => rpc.read().await.query_with_ast(s, o).await,
_ => return Response::failure(id, Failure::INVALID_PARAMS).send(chn).await,
},
then futher down, line 350 ish:
async fn query_ast(&self, sql: Strand) -> Result<Value, Error> {
let parsed_query_result = surrealdb::sql::parse(&sql);
let parsed_query = parsed_query_result.unwrap();
let json_result = serde_json::to_string(&parsed_query);
let json = json_result.unwrap();
debug!(target: LOG, "Executing AST: {}", json);
Ok(Value::from(json))
}
async fn query_with_ast(&self, sql: Strand, mut vars: Object) -> Result<Value, Error> {
let parsed_query_result = surrealdb::sql::parse(&sql);
let parsed_query = parsed_query_result.unwrap();
let json_result = serde_json::to_string(&parsed_query);
let json = json_result.unwrap();
debug!(target: LOG, "Executing AST: {}", json);
Ok(Value::from(json))
}
Now you can use rpc query :D
Simple additional for local testing, maybe @tobiemh can do a proper safe integration that's written properly in the future 👍
from surrealdb.wasm.
@iDevelopThings cool. For my use case that is to little information so I will rewrite the parser in typescript...
from surrealdb.wasm.
What is the optimum output format @iDevelopThings ?
from surrealdb.wasm.
Honestly I'm totally okay with that format, it's better than trying to parse segments of the queries with regex 😂
From the few queries i tried too, i think it gave all the information that I'd need
My use case was to parse info from info for db; info for table x;
so I could set up a migration system and know about other db/table configurations
from surrealdb.wasm.
Related Issues (20)
- Feature: Closely matches the API of the Javascript library HOT 1
- Bug: doesn't compile on Apple M1? HOT 8
- Feature: Proper typings HOT 1
- Feature: live queries HOT 5
- Bug: The transaction is not active in IndexedDB HOT 1
- Feature: surrealdb.wasm without wasm-bindgen
- Bug: The Development Server (index.html) is not working in 0.8.0 release HOT 3
- Bug: Instantiating Surreal causes "node:crypto" error HOT 1
- Documentation: add a list of API changes from 0.7 to 0.8 HOT 1
- Bug: ERROR Encountered a non-object value in array
- Feature: OPFS rather than Indexeddb for Persistence HOT 1
- Bug: Unable to run the Development Server (index.html) of the v1.0.0-beta.9
- Documentation: How to build surrealdb.wasm HOT 1
- Feature: provide a pre-built surrealdb.wasm binary HOT 2
- Feature: Smaller surrealdb.wasm binary with more feature flags HOT 4
- Documentation: How can we build surrealdb.wasm with SurrealDB 2.0 Alpha? HOT 1
- Bug: recursive use of an object detected which would lead to unsafe aliasing in rust HOT 7
- Bug: surrealdbWasmEngines is not assignable to Surreal engines
- Bug: The demo (index.html) throwing error "Uncaught UnsupportedVersion at versionCheck" HOT 1
- Bug: GitHub actions failed to build and push to npmjs HOT 1
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 surrealdb.wasm.