wunder-js-sdk
Version:
wunderDB Javascript SDK
404 lines (369 loc) • 11.2 kB
JavaScript
/*
=== wunderDB JavaScript SDK
=== Version: 1.1.0
=== Author: Tanmoy Sen Gupta
Legends -
[-] - Yet to be Implemented
[/] - Implemented
# Methods in SDK
- Connection Methods
[/] validate-connection
[/] initialize
[/] get-cluster
- Database Methods
[/] get-database
[/] create-database
[/] delete-database
- Collection Methods
[/] create-collection
[/] clone-collection
[/] migrate-collection
[/] delete-collection
[/] get-collection
- Data Mthods
[/] add-data
[/] update-data
[/] delete-data
[/] get-data
*/
// Connection Methods
function validate_connection(cluster_id, token) {
//const { cluster_id, token } = passed_params;
if (cluster_id && token) {
return fetch(`https://wdb.tanmoysg.com/validate-connection?cluster=${cluster_id}&token=${token}`, {
method: "GET",
cache: "no-cache",
headers: { "content_type": "application/json" }
}).then(response => {
return response.json()
}).then(json => {
if (json.status_code === '1') {
return true;
} else {
return false;
}
})
}
}
function initialize(cluster_id, token) {
return validate_connection(cluster_id, token).then((val) => {
if (val) {
return `https://wdb.tanmoysg.com/connect?cluster=${cluster_id}&token=${token}`;
} else {
throw 'wrong_cluster_or_token'
}
})
}
function get_cluster(cluster) {
if (cluster && database) {
return fetch(cluster, {
method: "POST",
cache: "no-cache",
headers: { "content_type": "application/json" },
body: {
"action": "get-cluster"
}
}).then(response => {
return response.json()
}).then(json => {
return json;
})
} else {
throw 'wrong_request_format';
}
}
// Database Methods
function create_database(cluster, name) {
if (cluster && name) {
return fetch(cluster, {
method: "POST",
cache: "no-cache",
headers: { "content_type": "application/json" },
body: JSON.stringify({
"action": "create-database",
"payload": {
"name": name
}
})
}).then(response => {
return response.json()
}).then(json => {
return json.response;
})
} else {
throw 'wrong_request_format';
}
}
function delete_database(cluster, targetDatabase, migrate = 'false', destinationDatabase, ifCollectionExists = 'skip') {
if (cluster && targetDatabase && destinationDatabase) {
return fetch(cluster, {
method: "POST",
cache: "no-cache",
headers: { "content_type": "application/json" },
body: {
"action": "delete-database",
"payload": {
"targetDatabase": targetDatabase,
"migrateCollection": migrate,
"destinationDatabase": destinationDatabase,
"ifCollectionExists": ifCollectionExists
}
}
}).then(response => {
return response.json()
}).then(json => {
return json.response;
})
} else {
throw 'wrong_request_format';
}
}
function get_database(cluster, database = 'all') {
if (cluster && database) {
return fetch(cluster, {
method: "POST",
cache: "no-cache",
headers: { "content_type": "application/json" },
body: {
"action": "get-database",
"payload": {
"database": database,
}
}
}).then(response => {
return response.json()
}).then(json => {
return json.response;
})
} else {
throw 'wrong_request_format';
}
}
// Collection Methods
function create_collection(cluster, database, name, schema) {
if (cluster && database && name && schema) {
return fetch(cluster, {
method: "POST",
cache: "no-cache",
headers: { "content_type": "application/json" },
body: {
"action": "create-collection",
"payload": {
"database": database,
"name": name,
"schema": schema
}
}
}).then(response => {
return response.json()
}).then(json => {
return json.response;
})
} else {
throw 'wrong_request_format';
}
}
function clone_collection(cluster, sourceDatabase, collection, destinationDatabase, actionIfExists = 'abort', newName) {
if (cluster && sourceDatabase && collection && destinationDatabase) {
return fetch(cluster, {
method: "POST",
cache: "no-cache",
headers: { "content_type": "application/json" },
body: {
"action": "clone-collection",
"payload": {
"sourceDatabase": sourceDatabase,
"collection": collection,
"destinationDatabase": destinationDatabase,
"actionIfExists": actionIfExists,
"newName": newName
}
}
}).then(response => {
return response.json()
}).then(json => {
return json.response;
})
} else {
throw 'wrong_request_format';
}
}
function migrate_collection(cluster, sourceDatabase, collection, destinationDatabase, actionIfExists = 'abort', newName) {
if (cluster && sourceDatabase && collection && destinationDatabase) {
return fetch(cluster, {
method: "POST",
cache: "no-cache",
headers: { "content_type": "application/json" },
body: {
"action": "migrate-collection",
"payload": {
"sourceDatabase": sourceDatabase,
"collection": collection,
"destinationDatabase": destinationDatabase,
"actionIfExists": actionIfExists,
"newName": newName
}
}
}).then(response => {
return response.json()
}).then(json => {
return json.response;
})
} else {
throw 'wrong_request_format';
}
}
function delete_collection(cluster, database, collection) {
if (cluster && database && collection) {
return fetch(cluster, {
method: "POST",
cache: "no-cache",
headers: { "content_type": "application/json" },
body: {
"action": "delete-collection",
"payload": {
"database": database,
"collection": collection
}
}
}).then(response => {
return response.json()
}).then(json => {
return json.response;
})
} else {
throw 'wrong_request_format';
}
}
function get_collection(cluster, database, collection = 'all') {
if (cluster && database && collection) {
return fetch(cluster, {
method: "POST",
cache: "no-cache",
headers: { "content_type": "application/json" },
body: {
"action": "get-collection",
"payload": {
"database": database,
"collection": collection
}
}
}).then(response => {
return response.json()
}).then(json => {
return json.response;
})
} else {
throw 'wrong_request_format';
}
}
// Data Methods
function add_data(cluster, database, collection, data) {
if (cluster && database && collection && data) {
return fetch(cluster, {
method: "POST",
cache: "no-cache",
headers: { "content_type": "application/json" },
body: {
"action": "add-data",
"payload": {
"database": database,
"collection": collection,
"data": data
}
}
}).then(response => {
return response.json()
}).then(json => {
return json.response;
})
} else {
throw 'wrong_request_format';
}
}
function update_data(cluster, database, collection, data, key, value) {
if (cluster && database && collection && data && key && value) {
return fetch(cluster, {
method: "POST",
cache: "no-cache",
headers: { "content_type": "application/json" },
body: {
"action": "update-data",
"payload": {
"database": database,
"collection": collection,
"marker": key + " : " + value,
"data": data
}
}
}).then(response => {
return response.json()
}).then(json => {
return json.response;
})
} else {
throw 'wrong_request_format';
}
}
function delete_data(cluster, database, collection, key, value) {
if (cluster && database && collection && key && value) {
return fetch(cluster, {
method: "POST",
cache: "no-cache",
headers: { "content_type": "application/json" },
body: {
"action": "delete-data",
"payload": {
"database": database,
"collection": collection,
"marker": key + " : " + value,
}
}
}).then(response => {
return response.json()
}).then(json => {
return json.response;
})
} else {
throw 'wrong_request_format';
}
}
function get_data(cluster, database, collection) {
if (cluster && database && collection) {
return fetch(cluster, {
method: "POST",
cache: "no-cache",
headers: { "content_type": "application/json" },
body: {
"action": "get-data",
"payload": {
"database": database,
"collection": collection
}
}
}).then(response => {
return response.json()
}).then(json => {
return json.response;
})
} else {
throw 'wrong_request_format';
}
}
module.exports = {
validate_connection,
initialize,
get_cluster,
get_database,
create_database,
delete_database,
get_collection,
create_collection,
clone_collection,
migrate_collection,
delete_collection,
get_data,
add_data,
update_data,
delete_data
};