Skip to content

Commit

Permalink
Adds index.ts back to the template so that the user can control SQL file
Browse files Browse the repository at this point in the history
loading
  • Loading branch information
coco98 committed Oct 24, 2024
1 parent 9f32aac commit c148f5b
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 92 deletions.
2 changes: 2 additions & 0 deletions connector-definition/template/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
node_modules/
duck.db
duck.db.wal
104 changes: 55 additions & 49 deletions connector-definition/template/functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,55 @@ import { JSONValue } from "@hasura/ndc-lambda-sdk";
import { GoogleCalendar } from "@hasura/ndc-duckduckapi/services";
import { getOAuthCredentialsFromHeader } 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'
}

/**
* $ddn.jobs.calendar-loader.init
*/
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) {
console.log(credentials);
calendarLoaderState.state = `Error in getting the google-calendar oauth credentials. Login to google-calendar?`;
return calendarLoaderState.state;
}

const syncManager = new GoogleCalendar.syncManager (
credentials['google-calendar'].access_token,
1, // sync every minute
calendarLoaderState
);

return await syncManager.initialize();

}

/**
* $ddn.jobs.calendar-loader.status
*
* @readonly
* */
export function __dda_calendar_loader_status(): string {
return calendarLoaderState.state;
}

/**********************************************************************************************/
/*************************** Add your own loader ********************************************/
/**********************************************************************************************/

const myLoaderState = {
state: 'unimplemented'
Expand All @@ -15,7 +64,7 @@ const myLoaderState = {
*/
export async function __dda_my_loader_init(headers: JSONValue): Promise<string> {

// If the loader is already running, return the current state
// If the loader is already running
if (myLoaderState.state === "running") {
return myLoaderState.state;
}
Expand Down Expand Up @@ -54,7 +103,11 @@ 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 hello(name: string, year: number): string {
return `Helloooo ${name}, welcome to ${year}`;
Expand All @@ -67,51 +120,4 @@ export function bye(name: string): string {

export async function sendEmail(email: string): Promise<string> {
return `Email sent to: ${email}!`;
}


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

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

/**
* 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 sample-loader to create your own unique name, eg: my-saas-loader, and to group it with the right job status method.
* $ddn.jobs.calendar-loader.init
*/
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) {
console.log(credentials);
calendarLoaderState.state = `Error in getting the google-calendar oauth credentials. Login to google-calendar?`;
return calendarLoaderState.state;
}

const syncManager = new GoogleCalendar.syncManager (
credentials['google-calendar'].access_token,
1, // sync every minute
calendarLoaderState
);

return await syncManager.initialize();

}

/**
* @readonly
* */
export function __dda_calendar_loader_status(): string {
return calendarLoaderState.state;
}

}
21 changes: 21 additions & 0 deletions connector-definition/template/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// From original index.ts
import { start } from "@hasura/ndc-duckduckapi";
import { makeConnector, duckduckapi } from "@hasura/ndc-duckduckapi";
import * as path from "path";
import { GoogleCalendar } from "@hasura/ndc-duckduckapi/services";

const connectorConfig: duckduckapi = {
dbSchema: `
-- Add your SQL schema here.
-- This SQL will be run on startup every time.
-- CREATE TABLE SAAS_TABLE_NAME (.....);
` + GoogleCalendar.schema,
functionsFilePath: path.resolve(__dirname, "./functions.ts"),
};

(async () => {
const connector = await makeConnector(connectorConfig);
start(connector);
})();
4 changes: 2 additions & 2 deletions connector-definition/template/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
"node": ">=20"
},
"scripts": {
"start": "ndc-duckduckapi host -f functions.ts -s schema.sql serve --configuration ./",
"watch": "ndc-duckduckapi host -f functions.ts -s schema.sql --watch serve --configuration ./ --pretty-print-logs"
"start": "ts-node index.ts serve --configuration .",
"watch": "nodemon --exec ts-node index.ts serve --configuration ./ --pretty-print-logs"
},
"dependencies": {
"@hasura/ndc-lambda-sdk": "^1.9.0",
Expand Down
39 changes: 0 additions & 39 deletions connector-definition/template/schema.sql

This file was deleted.

4 changes: 2 additions & 2 deletions ndc-duckduckapi/src/services/google-calendar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,15 @@ CREATE TABLE IF NOT EXISTS calendar_events (
is_all_day BOOLEAN
);
COMMENT ON TABLE calendar_events IS 'This table contains events from google calendar. While querying this table keep the following in mind. 1) The title of the event is stored in the summary field. 2) typically add a filter to remove cancelled events by checking status != \'cancelled\', and similarly remove deleted events by check if status != \
'deleted\'.';
COMMENT ON TABLE calendar_events IS 'This table contains events from google calendar. While querying this table keep the following in mind. 1) The title of the event is stored in the summary field. 2) typically add a filter to remove cancelled events by checking status != ''cancelled'', and similarly remove deleted events by check if status != ''deleted''.';
CREATE TABLE IF NOT EXISTS sync_state (
calendar_id VARCHAR PRIMARY KEY,
sync_token VARCHAR,
last_sync TIMESTAMP
);
COMMENT ON TABLE sync_state IS 'This table contains the sync state for the calendar job. This is not a table that would typically be queried. The sync_token is used to sync incremental changes from the google calendar API.';
`

Expand Down

0 comments on commit c148f5b

Please sign in to comment.