Skip to content

Commit

Permalink
Added functions to sqlite
Browse files Browse the repository at this point in the history
  • Loading branch information
bgelatti committed Nov 7, 2024
1 parent fa9b565 commit df20207
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 13 deletions.
2 changes: 1 addition & 1 deletion packages/server/Helpers/removeNullValues.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*/
function removeNullValues<T>(value: T): T {
for (const key in value) {
if (value[key] === null || value[key] === undefined) {
if (value[key] === null || value[key] === undefined || value[key] === "") {
delete value[key];
}
}
Expand Down
4 changes: 3 additions & 1 deletion packages/server/Services/DeviceData/DeviceData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -544,6 +544,7 @@ export const getDeviceDataByDevice = async (
}
}

removeNullValues(response);
let items: any = await z.array(zDeviceData).parseAsync(response);
items = await z.array(zDeviceData).parseAsync(items);

Expand All @@ -559,7 +560,8 @@ export const getDeviceData = async (
query?: IDeviceDataQuery,
) => {
const device = await getDeviceInfo(id);
return await getDeviceDataByDevice(device, query);
const data = await getDeviceDataByDevice(device, query);
return data;
};

/**
Expand Down
33 changes: 22 additions & 11 deletions plugins/sqlite/src/Providers/DeviceData/importDeviceData.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,23 @@
import fs from "node:fs";
import type { TDeviceType, TGenericID } from "@tago-io/tcore-sdk/types";
import {
type TDeviceType,
type TGenericID,
generateResourceID,
} from "@tago-io/tcore-sdk/types";
import { parse } from "csv";
import type { Knex } from "knex";
import { getDeviceConnection } from "../../Helpers/DeviceDatabase.ts";

async function _insertData(client: any, data: any[]) {
return new Promise((resolve, reject) => {
client.batchInsert("data", data, 1000).catch((error) => {
reject(error);
async function _insertData(client: Knex, data: any[]) {
client
.batchInsert("data", data, 1000)
.then(() => {
return;
})
.catch((error) => {
console.log(error);
Promise.reject(error);
});
});
}

/**
Expand All @@ -23,21 +32,23 @@ async function importDeviceData(
return new Promise((resolve, reject) => {
const data: any[] = [];
fs.createReadStream(fileName)
.pipe(parse())
.on("data", async (row) => {
.pipe(parse({ columns: true, encoding: "utf8" }))
.on("data", (row) => {
row.id = generateResourceID();
data.push(row);
if (data.length === 1000) {
await _insertData(client, data);
_insertData(client, data);
data.length = 0;
}
})
.on("end", async () => {
.on("end", () => {
if (data.length > 0) {
await _insertData(client, data);
_insertData(client, data);
}
resolve("Data imported successfully");
})
.on("error", (error) => {
console.log(error);
reject(error);
});
});
Expand Down

0 comments on commit df20207

Please sign in to comment.