Skip to content

Commit

Permalink
Refactor duckdb database, connections, and transactions
Browse files Browse the repository at this point in the history
  • Loading branch information
nullxone committed Oct 30, 2024
1 parent 2592158 commit 3aed5a2
Show file tree
Hide file tree
Showing 14 changed files with 3,944 additions and 536 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,7 @@ duck.db.wal
ndc-duckduckapi/dist

connector-definition/dist
connector-definition/template/package-lock.json
connector-definition/template/package-lock.json

ndc-duckduckapi/test.db
ndc-duckduckapi/test.db.wal
108 changes: 74 additions & 34 deletions connector-definition/template/functions.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,27 @@
import { JSONValue } from "@hasura/ndc-lambda-sdk";
import { GMail, GoogleCalendar } from "@hasura/ndc-duckduckapi/services";
import { getOAuthCredentialsFromHeader } from "@hasura/ndc-duckduckapi";
import { db, AsyncConnection} from "@hasura/ndc-duckduckapi";
import { getDB } from "@hasura/ndc-duckduckapi";

/***********************************************************************************/
/************************** BUILT-IN EXAMPLES **************************************/
/***********************************************************************************/

/* To add more built in examples, check out other services at @hasura/ndc-duckduckapi/services */
const calendarLoaderState = {
state: 'Stopped'
state: "Stopped",
};
let syncManager;

(async () => {

try {
console.log("Trying to initialize calendar loader in case credentials are already available");
const calendarSyncManager = await GoogleCalendar.SyncManager.create(calendarLoaderState, 1);
console.log(
"Trying to initialize calendar loader in case credentials are already available"
);
const calendarSyncManager = await GoogleCalendar.SyncManager.create(
calendarLoaderState,
1
);
await calendarSyncManager.initialize();
} catch (error) {
calendarLoaderState.state = `Stopped`;
Expand All @@ -28,25 +32,30 @@ let syncManager;
/**
* $ddn.jobs.calendar-loader.init
*/
export async function __dda_calendar_loader_init(headers: JSONValue): Promise<string> {

export async function __dda_calendar_loader_init(
headers: JSONValue
): Promise<string> {
if (calendarLoaderState.state == "Running") {
return calendarLoaderState.state;
}

let credentials;
credentials = getOAuthCredentialsFromHeader(headers);

if (!credentials || !credentials['google-calendar'] || !credentials['google-calendar'].access_token) {
if (
!credentials ||
!credentials["google-calendar"] ||
!credentials["google-calendar"].access_token
) {
console.log(credentials);
calendarLoaderState.state = `Error in getting the google-calendar oauth credentials. Login to google-calendar?`;
return calendarLoaderState.state;
}

syncManager = await GoogleCalendar.SyncManager.create (
syncManager = await GoogleCalendar.SyncManager.create(
calendarLoaderState,
1, // sync every minute
credentials['google-calendar']
credentials["google-calendar"]
);

return await syncManager.initialize();
Expand All @@ -63,29 +72,46 @@ export function __dda_calendar_loader_status(): string {
/**
* This is a function to create an event on your google calendar
*/
export async function createCalendarEvent(headers: JSONValue,
export async function createCalendarEvent(
headers: JSONValue,
summary: string,
startDateTime: Date,
endDateTime: Date,
description?: string,
attendees?: string[],
timeZone?: string,
location?: string): Promise<{success: boolean, message: string}> {

location?: string
): Promise<{ success: boolean; message: string }> {
let credentials;
credentials = getOAuthCredentialsFromHeader(headers);

if (!credentials || !credentials['google-calendar'] || !credentials['google-calendar'].access_token) {
if (
!credentials ||
!credentials["google-calendar"] ||
!credentials["google-calendar"].access_token
) {
console.log(credentials);
return {success: false, message: `Error in getting the google-calendar oauth credentials. Login to google-calendar?`};
return {
success: false,
message: `Error in getting the google-calendar oauth credentials. Login to google-calendar?`,
};
}

const event = await GoogleCalendar.CreateCalendarEvent(credentials['google-calendar'], summary, description, startDateTime, endDateTime, attendees, timeZone, location);
const event = await GoogleCalendar.CreateCalendarEvent(
credentials["google-calendar"],
summary,
description,
startDateTime,
endDateTime,
attendees,
timeZone,
location
);

if (event.status === 'error') {
return {success: false, message: `Error creating event: ${event.error}`};
if (event.status === "error") {
return { success: false, message: `Error creating event: ${event.error}` };
} else {
return {success: true, message: `Event created successfully: ${event}`};
return { success: true, message: `Event created successfully: ${event}` };
}
}

Expand All @@ -94,13 +120,18 @@ export async function createCalendarEvent(headers: JSONValue,
***********************************************************************************************/
/* To add more built in examples, check out other services at @hasura/ndc-duckduckapi/services */
const gmailLoaderState = {
state: 'Stopped'
state: "Stopped",
};

(async () => {
try {
console.log("Trying to initialize gmail loader in case credentials are already available");
const gmailSyncManager = await GMail.SyncManager.create(gmailLoaderState, 1);
console.log(
"Trying to initialize gmail loader in case credentials are already available"
);
const gmailSyncManager = await GMail.SyncManager.create(
gmailLoaderState,
1
);
await gmailSyncManager.initialize();
} catch (error) {
gmailLoaderState.state = `Stopped`;
Expand All @@ -111,25 +142,30 @@ const gmailLoaderState = {
/**
* $ddn.jobs.gmail-loader.init
*/
export async function __dda_gmail_loader_init(headers: JSONValue): Promise<string> {

export async function __dda_gmail_loader_init(
headers: JSONValue
): Promise<string> {
if (gmailLoaderState.state == "Running") {
return gmailLoaderState.state;
}

let credentials;
credentials = getOAuthCredentialsFromHeader(headers);

if (!credentials || !credentials['google-gmail'] || !credentials['google-gmail'].access_token) {
if (
!credentials ||
!credentials["google-gmail"] ||
!credentials["google-gmail"].access_token
) {
console.log(credentials);
gmailLoaderState.state = `Error in getting the GMail oauth credentials. Login to google-gmail?`;
return gmailLoaderState.state;
}

const syncManager = await GMail.SyncManager.create (
const syncManager = await GMail.SyncManager.create(
gmailLoaderState,
1, // sync every minute
credentials['google-gmail']
credentials["google-gmail"]
);

return await syncManager.initialize();
Expand All @@ -149,17 +185,18 @@ export function __dda_gmail_loader_status(): string {
/**********************************************************************************************/

const myLoaderState = {
state: 'This is an unimplemented loader. Edit it in functions.ts.'
}
state: "This is an unimplemented loader. Edit it in functions.ts.",
};

/**
* This is the loader function which will start loading data into duckdb.
* // Mark your functions with this annotation to see it in the console
* // Replace my-loader to create your own unique name, eg: my-saas-loader, and to group it with the right job status method.
* $ddn.jobs.my-loader.init
*/
export async function __dda_my_loader_init(headers: JSONValue): Promise<string> {

export async function __dda_my_loader_init(
headers: JSONValue
): Promise<string> {
// If the loader is already running
if (myLoaderState.state === "running") {
return myLoaderState.state;
Expand All @@ -171,7 +208,11 @@ export async function __dda_my_loader_init(headers: JSONValue): Promise<string>
console.log(credentials);

// If the credentials are not found, return an error and update the status so that the user can login
if (!credentials || !credentials['my-service'] || !credentials['my-service'].access_token) {
if (
!credentials ||
!credentials["my-service"] ||
!credentials["my-service"].access_token
) {
myLoaderState.state = `Error in getting the my-service oauth credentials. Login to my-service?`;
return myLoaderState.state;
}
Expand Down Expand Up @@ -199,15 +240,14 @@ export function __dda_my_loader_status(): string {
return myLoaderState.state;
}


/*******************************************************************************************/
/* Some other examples of custom actions you want to provide or APIs you want to wrap over */
/*******************************************************************************************/

/** @readonly */
export function testCalendar(headers: JSONValue): JSONValue {
return new JSONValue({
success: true
success: true,
});
}

Expand Down
14 changes: 14 additions & 0 deletions ndc-duckduckapi/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/** @type {import('ts-jest').JestConfigWithTsJest} */

module.exports = {
preset: "ts-jest",
testEnvironment: "node",
transform: {
"^.+\\.tsx?$": [
"ts-jest",
{
diagnostics: false,
},
],
},
};
Loading

0 comments on commit 3aed5a2

Please sign in to comment.