0.0.8 • Published 1 year ago

sqliteformat v0.0.8

Weekly downloads
-
License
ISC
Repository
github
Last release
1 year ago

sqliteformat

A utility function for merging SQL strings with placeholders and handling single quotes for SQLite.

format(sql: string, parameters: any[]): string

Merges the provided SQL string by replacing placeholders (?) with corresponding values from the parameters array. This function handles single quotes in parameter values, ensuring proper formatting for SQLite.

Parameters

  • sql (string): The SQL string with placeholders to be merged.
  • parameters (array): An array of values to replace the placeholders in the SQL string.

Returns

  • result (string): The formatted SQL string with replaced placeholders.

Throws

  • Error - If the number of parameters does not match the number of placeholders in the SQL string.
  • Error - If an invalid start index is encountered during replacement.

Example

import { format } from 'sqliteformat';

const sql = "SELECT * FROM users WHERE name = ? AND age > ?";
const parameters = ['John Doe', 30];

const formattedSql = format(sql, parameters);
console.log(formattedSql);
// Output: SELECT * FROM users WHERE name = 'John Doe' AND age > 30
0.0.8

1 year ago

0.0.7

1 year ago

0.0.6

1 year ago

0.0.5

1 year ago

0.0.4

1 year ago

0.0.3

1 year ago

0.0.2

1 year ago

0.0.1

1 year ago