How To Connect
Here is a sample code in different languages to connect with MekaDB and insert records to it.
- NodeJS
- Rust
- Python
- Java
var mekadb = require('@hypi/mekadb');
let client = new mekadb.Client("mekadb.hypi.app", "<db>", "<username>", "<password>");
async function main() {
let createTableRes = await client.query('CREATE TABLE IF NOT EXISTS user(username VARCHAR, pass VARCHAR, PRIMARY KEY (username))');
console.log('Creat table res', createTableRes);
let insertRes = await client.query("INSERT INTO user(username,pass) VALUES('courtney','pass1'),('damion','pass2')")
console.log('Inserted data into table', insertRes);
let rows = await client.query("SELECT * FROM user");
console.log('ROWS:', rows)
}
let _ = main();
let client = MekaDBClient::connect("https://mekadb.hypi.app".to_owned()).await?;
let ctx = client.login("databse_name".to_owned(), "username".to_owned(), "password".to_owned(), ).await?;
let _ = client.query("CREATE TABLE IF NOT EXISTS user(username VARCHAR, pass VARCHAR, PRIMARY KEY (username))".to_owned(),ctx.clone(),None::>).await?;
let _ = client.query("INSERT INTO user(username,pass) VALUES('courtney','pass1'),('damion','pass2')".to_owned(), ctx.clone(), None::>, ).await?;
let rows = client.query("SELECT * FROM user".to_owned(), ctx.clone(), None::>,).await?;
println!("Rows {:?}", rows);
if __name__ == '__main__':
async def main():
# Create an instance of the MekaDBClient
client = MekaDBClient()
# Connect to Hypi and keep a connection in the background
await client.run()
# Login to get authentication context
auth = await client.login('<username>', '<password>', '<database name>')
res = await client.query(creds=auth,sql='CREATE TABLE IF NOT EXISTS user(username VARCHAR, pass VARCHAR, PRIMARY KEY (username))')
print("Create table:", res)
res = await client.query(creds=auth,sql="INSERT INTO user(username,pass) VALUES('courtney','pass1'),('damion','pass2')")
print("Insert:", res)
res = await client.query(creds=auth, sql='SELECT * FROM user WHERE pass = :pass', params={'pass': 'pass99'})
print("Select:", res)
# Close the connection
await client.close()
# Run the main function within an asyncio event loop
asyncio.run(main())
public class Demo {
public static void main(String[] args) throws ExecutionException, InterruptedException {
MekaDBClient client = new MekaDBClient();
AuthCtx auth = client.login("<username>", "<password>", "<database>");
List<Map<String, Object>> createTableRes = client.query(auth, "CREATE TABLE IF NOT EXISTS user(username VARCHAR, pass VARCHAR, PRIMARY KEY (username))").get();
System.out.println(createTableRes);
List<Map<String, Object>> insertRes = client.query(auth, "INSERT INTO user(username,pass) VALUES('courtney','pass1'),('damion','pass2')").get();
System.out.println(insertRes);
Map<String, Object> params = new LinkedHashMap<>();
params.put("pass", "pass1");
List<Map<String, Object>> rows = client.query(auth, "SELECT * FROM user where pass = :pass", params).get();
System.out.println(rows);
}
}
First, the code connects you to database after entering database id, username and password. Copy the credentials from the MekaDB Database screen.
Then in the subsequent lines of code, it creates a table, insert two rows, and then select those two rows using SQL queries.
Click on the below links to check the code repositories for different languages.
Check this SQL guide to frame your own SQL queries!