Skip to content

Commit

Permalink
Merge pull request #73 from hasura/lyndon/pg-bug-report-sooraj
Browse files Browse the repository at this point in the history
Updating TS target to 2022 to fix pg dependency bug
  • Loading branch information
sordina authored Nov 23, 2023
2 parents 833c827 + 341ef29 commit 34ea797
Show file tree
Hide file tree
Showing 4 changed files with 218 additions and 4 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@ This changelog documents the changes between release versions.

Changes to be included in the next upcoming releaase.

## v0.15

Updating TypeScript target version from ES2017 to ES2022.

PR: https://github.com/hasura/ndc-typescript-deno/pull/73

* Resolves issues with some dependencies such as deno.land/x/[email protected]

## v0.14.1

Diff: b1bdc55..17e85d5
Expand Down
9 changes: 5 additions & 4 deletions src/infer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -371,16 +371,17 @@ export function programInfoException(filename_arg?: string, vendor_arg?: string,
`);

const program = ts.createProgram([filename], {
target: ts.ScriptTarget.ES5,
// This should match the version targeted in the deno version that is being used.
target: ts.ScriptTarget.ES2022,
module: ts.ModuleKind.CommonJS,
noImplicitAny: true,
// NOTE: We just declare Deno globally as any in order to allow users to omit it's declaration in their function files
lib: ['lib.d.ts', 'lib.es2017.d.ts', resolve(deno_d_ts)],
// This should ideally use the real deno type definitions.
lib: ['lib.d.ts', 'lib.es2022.d.ts', resolve(deno_d_ts)],
allowJs: true,
allowImportingTsExtensions: true,
noEmit: true,
baseUrl: '.',
// '@/*': ['vendor/*'],
paths: pathsMap
});

Expand All @@ -401,7 +402,7 @@ export function programInfoException(filename_arg?: string, vendor_arg?: string,
const message = ts.flattenDiagnosticMessageText(diagnostic.messageText, "\n");
console.error(`${diagnostic.file.fileName} (${line + 1},${character + 1}): ${message}`);
} else {
console.error(ts.flattenDiagnosticMessageText(diagnostic.messageText, "\n"));
console.error(`FATAL: ${ts.flattenDiagnosticMessageText(diagnostic.messageText, "\n")}`);
fatal++;
}
});
Expand Down
102 changes: 102 additions & 0 deletions src/test/data/pg_dep.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
import { Client } from "https://deno.land/x/[email protected]/mod.ts";

const dbConfig = {
user: "aaysha",
hostname: "asdfasdfasd.us-west-2.aws.neon.tech",
port: 5432,
password: "asdfasdasdf",
database: "asdfasdfasdf",
ssl: true,
sslmode: "require",
};

export async function insert_user(
user_name: string,
): Promise<
{ id: string; name: string; created_at: string } | { message: string } | {
error: string;
} | {}
> {
const client = new Client(dbConfig);

try {
await client.connect();

const result = await client.queryObject({
text: `INSERT INTO users(name) VALUES ('${user_name}') RETURNING *`,
});

if (result && result.rows.length > 0 && result.rows[0]) {
return result.rows[0];
} else {
return { message: "Insert Failed" };
}
} catch (error) {
console.error("Error:", error);
return { error: "Error: " + error.message };
} finally {
await client.end();
}
}

export async function insert_todos(
user_id: string,
todo: string
): Promise<
{ id: string; user_id: string; todo: string; created_at: string } | { message: string } | {
error: string;
} | {}
> {
const client = new Client(dbConfig);

try {
await client.connect();


// Check if the user exists in the users table

const userExistsQuery = await client.queryObject({
text: `SELECT id FROM users where id =${user_id}`
})

if (userExistsQuery.rows.length === 0) {
return { message: "User not found. Insert Failed" };
}
const result = await client.queryObject({
text: `INSERT INTO todos(user_id,todo) VALUES ('${user_id}','${todo}') RETURNING *`,
});

if (result && result.rows.length > 0 && result.rows[0]) {
return result.rows[0];
} else {
return { message: "Insert Failed" };
}
} catch (error) {
console.error("Error:", error);
return { error: "Error: " + error.message };
} finally {
await client.end();
}
}


export async function delete_todos(
todo_id: string
){
const client = new Client(dbConfig);
try{
await client.connect();

const result = await client.queryObject({ text: `DELETE FROM todos WHERE id =${todo_id}`})
if(result.rowCount===1){
return `Deleted todo with id= ${todo_id} sucesssfully`
}else{
return "Deletion unsucessfull"
}
}catch(error){
return "Error : "+ error.message
}finally{
client.end();
}

}
103 changes: 103 additions & 0 deletions src/test/pg_dep_test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@

import * as test from "https://deno.land/[email protected]/assert/mod.ts";
import * as path from "https://deno.land/[email protected]/path/mod.ts";
import * as infer from '../infer.ts';

// NOTE: It would be good to have explicit timeout for this
// See: https://github.com/denoland/deno/issues/11133
// Test bug: https://github.com/hasura/ndc-typescript-deno/issues/45
Deno.test("Inferred Dependency Based Result Type", () => {
const program_path = path.fromFileUrl(import.meta.resolve('./data/pg_dep.ts'));
const vendor_path = path.fromFileUrl(import.meta.resolve('./vendor'));
const program_results = infer.programInfo(program_path, vendor_path, true);

test.assertEquals(program_results, {
positions: {
delete_todos: [
"todo_id",
],
insert_todos: [
"user_id",
"todo",
],
insert_user: [
"user_name",
],
},
schema: {
collections: [],
functions: [],
procedures: [
{
arguments: {
user_name: {
type: {
name: "String",
type: "named",
},
},
},
name: "insert_user",
result_type: {
name: "insert_user_output",
type: "named",
},
},
{
arguments: {
todo: {
type: {
name: "String",
type: "named",
},
},
user_id: {
type: {
name: "String",
type: "named",
},
},
},
name: "insert_todos",
result_type: {
name: "insert_todos_output",
type: "named",
},
},
{
arguments: {
todo_id: {
type: {
name: "String",
type: "named",
},
},
},
name: "delete_todos",
result_type: {
name: "String",
type: "named",
},
},
],
scalar_types: {
String: {
aggregate_functions: {},
comparison_operators: {},
update_operators: {},
},
insert_todos_output: {
aggregate_functions: {},
comparison_operators: {},
update_operators: {},
},
insert_user_output: {
aggregate_functions: {},
comparison_operators: {},
update_operators: {},
},
},
object_types: {},
}
});
});

0 comments on commit 34ea797

Please sign in to comment.