diff --git a/CHANGELOG.md b/CHANGELOG.md index dfc78ec..5ace344 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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/postgres@v0.17.0 + ## v0.14.1 Diff: b1bdc55..17e85d5 diff --git a/src/infer.ts b/src/infer.ts index 6beb3ae..daab65c 100644 --- a/src/infer.ts +++ b/src/infer.ts @@ -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 }); @@ -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++; } }); diff --git a/src/test/data/pg_dep.ts b/src/test/data/pg_dep.ts new file mode 100644 index 0000000..75c6863 --- /dev/null +++ b/src/test/data/pg_dep.ts @@ -0,0 +1,102 @@ +import { Client } from "https://deno.land/x/postgres@v0.17.0/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(); + } + + } \ No newline at end of file diff --git a/src/test/pg_dep_test.ts b/src/test/pg_dep_test.ts new file mode 100644 index 0000000..a3eb993 --- /dev/null +++ b/src/test/pg_dep_test.ts @@ -0,0 +1,103 @@ + +import * as test from "https://deno.land/std@0.202.0/assert/mod.ts"; +import * as path from "https://deno.land/std@0.203.0/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: {}, + } + }); +});