Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

pgtyped-rescript

Package Overview
Dependencies
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

pgtyped-rescript - npm Package Compare versions

Comparing version 2.4.0 to 2.5.0

30

lib/generator.js

@@ -10,3 +10,3 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {

};
import { ParameterTransform, processSQLQueryIR, processTSQueryAST, } from '@pgtyped/runtime';
import { ParameterTransform, processSQLQueryIR, processTSQueryAST, } from 'pgtyped-rescript-runtime';
import { parseSQLFile, prettyPrintEvents, queryASTToIR, } from '@pgtyped/parser';

@@ -91,3 +91,3 @@ import { getTypes } from 'pgtyped-rescript-query';

!removeNullability) {
tsTypeName = 'Null.t<' + tsTypeName + '>';
tsTypeName = 'option<' + tsTypeName + '>';
}

@@ -116,3 +116,3 @@ if (addNullability || removeNullability) {

if (!param.required) {
tsTypeName = 'Null.t<' + tsTypeName + '>';
tsTypeName = tsTypeName;
}

@@ -213,3 +213,3 @@ // Allow optional scalar parameters to be missing from parameters object

// Second parameter has no effect here, we could have used any value
types.use({ name: 'PreparedQuery', from: '@pgtyped/runtime' }, TypeScope.Return);
types.use({ name: 'PreparedQuery', from: 'pgtyped-rescript-runtime' }, TypeScope.Return);
}

@@ -233,13 +233,9 @@ const typeDecs = yield generateTypedecsFromFile(contents, fileName, connection, mode, types, config);

}
const queryPP = typeDec.query.ast.statement.body
.split('\n')
.map((s) => ' * ' + s)
.join('\n');
declarationFileContents += `%%private(let ${typeDec.query.name}IR: IR.t = %raw(\`${JSON.stringify(typeDec.query.ir)}\`))\n\n`;
declarationFileContents +=
`/**\n` +
` * Query generated from SQL:\n` +
` * \`\`\`\n` +
`${queryPP}\n` +
` * \`\`\`\n` +
` Runnable query:\n` +
` \`\`\`sql\n` +
`${processSQLQueryIR(typeDec.query.ir).query}\n` +
` \`\`\`\n\n` +
` */\n`;

@@ -255,3 +251,3 @@ declarationFileContents += `@gentype

/** Returns exactly 1 result. Returns \`Error\` (with an optionally provided \`errorMessage\`) if more or less than exactly 1 result is returned. */
/** Returns exactly 1 result. Raises \`Exn.t\` (with an optionally provided \`errorMessage\`) if more or less than exactly 1 result is returned. */
@gentype

@@ -262,3 +258,3 @@ let expectOne: (

~errorMessage: string=?
) => promise<result<${typeDec.query.name}Result, string>>
) => promise<${typeDec.query.name}Result>

@@ -269,3 +265,3 @@ /** Executes the query, but ignores whatever is returned by it. */

} = {
@module("@pgtyped/runtime") @new external ${typeDec.query.name}: IR.t => PreparedStatement.t<${typeDec.query.paramTypeAlias}, ${typeDec.query.returnTypeAlias}> = "PreparedQuery";
@module("pgtyped-rescript-runtime") @new external ${typeDec.query.name}: IR.t => PreparedStatement.t<${typeDec.query.paramTypeAlias}, ${typeDec.query.returnTypeAlias}> = "PreparedQuery";
let query = ${typeDec.query.name}(${typeDec.query.name}IR)

@@ -285,4 +281,4 @@ let query = (params, ~client) => query->PreparedStatement.run(params, ~client)

let expectOne = async (client, params, ~errorMessage=?) => switch await query(params, ~client) {
| [item] => Ok(item)
| _ => Error(errorMessage->Option.getOr("More or less than one item was returned"))
| [item] => item
| _ => panic(errorMessage->Option.getOr("More or less than one item was returned"))
}

@@ -289,0 +285,0 @@

import { SQLParseResult } from '@pgtyped/parser/lib/loader/sql';
export declare function parseCode(fileContent: string, _fileName: string): SQLParseResult;
export declare function parseCode(fileContent: string, fileName: string): SQLParseResult;
import { parseSQLFile } from '@pgtyped/parser';
export function parseCode(fileContent, _fileName) {
import cp from 'child_process';
// @ts-ignore
import { getBinaryPath } from '@rescript/tools/npm/getBinaryPath.js';
export function parseCode(fileContent, fileName) {
if (!fileContent.includes('%sql')) {

@@ -10,8 +13,39 @@ return {

// Replace with more robust @rescript/tools CLI usage when that package ships linuxarm64 binary.
const regex = /%sql(?:\.\w+)?\(`([^`]*)`\)/g;
let match;
const content = JSON.parse(cp
.execFileSync(getBinaryPath(), [
'extract-embedded',
['sql', 'sql.one', 'sql.expectOne', 'sql.many', 'sql.execute'].join(','),
fileName,
])
.toString());
content.reverse();
const queries = [];
while ((match = regex.exec(fileContent)) !== null) {
queries.push(match[1]);
}
let unnamedQueriesCount = 0;
content.forEach((v) => {
let query = v.contents.trim();
if (!query.endsWith(';')) {
query += ';';
}
if (!query.includes('@name')) {
unnamedQueriesCount += 1;
// Handle potentially existing doc comment
if (query.trim().startsWith('/*')) {
const lines = query.split('\n');
let comment = `/*\n@name Query${unnamedQueriesCount}\n`;
for (let i = 0; i <= lines.length - 1; i += 1) {
const line = lines[i].trim().replace('/*', '');
comment += line + '\n';
if (line.endsWith('*/')) {
query = lines.slice(i + 1).join('\n');
break;
}
}
query = `${comment}\n${query}`;
}
else {
query = `/* @name Query${unnamedQueriesCount} */\n${query}`;
}
}
queries.push(query);
});
const asSql = queries.join('\n\n');

@@ -18,0 +52,0 @@ const res = parseSQLFile(asSql);

{
"name": "pgtyped-rescript",
"version": "2.4.0",
"version": "2.5.0",
"type": "module",

@@ -41,2 +41,3 @@ "main": "lib/index.js",

"@pgtyped/wire": "^2.2.0",
"@rescript/tools": "^0.6.2",
"camel-case": "^4.1.1",

@@ -53,3 +54,3 @@ "chalk": "^4.0.0",

"pascal-case": "^3.1.1",
"pgtyped-rescript-query": "^2.3.0",
"pgtyped-rescript-query": "^2.4.0",
"piscina": "^4.0.0",

@@ -56,0 +57,0 @@ "tinypool": "^0.7.0",

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc