tinybird-sdk
Version:
Tinybird Node.js SDK
1 lines • 5 kB
JavaScript
;Object.defineProperty(exports,"__esModule",{value:!0}),exports.default=void 0;var _logger=require("../lib/logger"),_http=require("../lib/http"),_exceptions=_interopRequireDefault(require("../lib/exceptions")),_utils=require("../lib/utils"),_formData=_interopRequireDefault(require("form-data")),_fs=_interopRequireDefault(require("fs"));function _interopRequireDefault(a){return a&&a.__esModule?a:{default:a}}const logger=(0,_logger.getLogger)("datasources-module");var _default={getAllDatasources:async()=>{try{return await(0,_http.fetch)("/v0/datasources")}catch(a){logger.error("Error while fetching /v0/datasources"),logger.debug("Request: /v0/datasources/"),logger.debug(a)}},getDatasource:async a=>{try{return await(0,_http.fetch)(`/v0/datasources/${a}`)}catch(b){logger.error("Error while fetching /v0/datasources"),logger.debug(`Request: /v0/datasources/${a}`),logger.debug(b)}},createDatasource:async(a,b)=>{try{const c=new URLSearchParams;c.append("name",a),c.append("schema",b),c.append("format",b.includes("`json:")?"ndjson":"csv");const d=await(0,_http.fetch)("/v0/datasources",{method:"POST",body:c});return logger.debug(`Datasource created: ${a}`),d}catch(b){logger.error(`Error while creating datasource ${a}`),logger.debug("Request: /v0/datasources/"),logger.debug(b)}},createDatasourceFromFile:()=>{throw new Error(_exceptions.default.METHOD_NOT_IMPLEMENTED)},alterDatasource:async(a,b)=>{try{const c=new URLSearchParams;c.append("schema",b);const d=await(0,_http.fetch)(`/v0/datasources/${a}/alter`,{method:"POST",body:c});return logger.debug(`Datasource altered: ${a}, new schema: ${b}`),d}catch(c){logger.error(`Error while altering datasource ${a} with schema: ${b}`),logger.debug("Request: /v0/datasources/(.+)/alter"),logger.debug(c)}},renameDatasource:async(a,b)=>{try{const c=new URLSearchParams;c.append("name",b);const d=await(0,_http.fetch)(`/v0/datasources/${a}`,{method:"PUT",body:c});return logger.debug(`Datasource ${a} renamed to ${b}`),d}catch(c){logger.error(`Error while renaming datasource ${a} to ${b}`),logger.debug("Request: /v0/datasources/(.+)"),logger.debug(c)}},dropDatasource:async a=>{try{return await(0,_http.fetch)(`/v0/datasources/${a}`,{method:"DELETE"}),logger.debug(`Datasource deleted: ${a}`),!0}catch(b){logger.error(`Error while deleting ${a}`),logger.debug(`Request: /v0/datasources/${a}`),logger.debug(b)}},appendRows:async(a,b,c="csv")=>{try{let d;"csv"===c?d=await(0,_utils.rowsToCSV)(b):"ndjson"===c?d=(0,_utils.rowsToNDJSON)(b):"json"===c?d=JSON.stringify(b):void 0,"csv"===c&&(c+="&dialect_delimiter=,");const e=await(0,_http.fetch)(`/v0/datasources?name=${a}&format=${c}&mode=append`,{method:"POST",headers:{"Content-Type":"text/csv;charset=utf-8"},body:d});return logger.debug(`Rows appended to ${a}: ${b.length}`),!1===e.error}catch(b){logger.error(`Error while appending rows to datasource ${a}`),logger.debug("Request: /v0/datasources/(.+)"),logger.debug(b)}},replaceWithRows:()=>{throw new Error(_exceptions.default.METHOD_NOT_IMPLEMENTED)},appendFile:async(a,b)=>{try{const c=new _formData.default,d=_fs.default.statSync(b),e=d.size,f=_fs.default.createReadStream(b),g=b.split(".")[b.split(".").length-1];c.append(g,f,{knownLength:e});const h=await(0,_http.fetch)(`/v0/datasources?name=${a}&format=${g}&mode=append&dialect_delimiter=,`,{method:"POST",body:c});return logger.debug(`Rows appended to ${a} from file ${b}`),!1===h.error}catch(c){logger.error(`Error while appending file ${b} to datasource ${a}`),logger.debug("Request: /v0/datasources/(.+)"),logger.debug(c)}},replaceWithFile:()=>{throw new Error(_exceptions.default.METHOD_NOT_IMPLEMENTED)},deleteRows:async(a,b)=>{try{const c=new URLSearchParams;return c.append("delete_condition",`(${b})`),await(0,_http.fetch)(`/v0/datasources/${a}/delete`,{method:"POST",body:c}),logger.debug(`Deleted rows from datasource ${a}`),!0}catch(b){logger.error(`Error while deleting rows from datasource ${a}`),logger.debug(`Request: /v0/datasources/${a}/delete`),logger.debug(b)}},truncateDatasource:async a=>{try{return await(0,_http.fetch)(`/v0/datasources/${a}/truncate`,{method:"POST"}),logger.debug(`Datasource truncated: ${a}`),!0}catch(b){logger.error(`Error while truncating ${a}`),logger.debug(`Request: /v0/datasources/${a}/truncate`),logger.debug(b)}},sendEvents:async(a,b,c=!1)=>{try{let d;switch(typeof b){case"object":d=(0,_utils.rowsToNDJSON)(b);break;default:d=b.toString();}let e=await(0,_http.fetch)(`/v0/events?name=${a}&wait=${c}`,{method:"POST",body:d});if(e=JSON.parse(e),!e.error||!1===e.error)return logger.debug(`Events (${Array.isArray(b)?b.length:1}) appended to ${a}`),{successful_rows:e.successful_rows,quarantined_rows:e.quarantined_rows};throw new Error(e.error||_exceptions.default.ERROR_APPENDING_EVENTS)}catch(b){logger.error(`Error while appending events to ${a}`),logger.debug(`Request: /v0/events?name=${a}&wait=${c}`),logger.debug("Events: formattedRows"),logger.debug(b)}}};exports.default=_default,module.exports=exports.default;