Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/sail-sail/named_placeholders
compiles "select foo where foo.id = :bar and foo.baz < :baz" into "select foo where foo.id = ? and foo.baz < ?" + ["bar", "baz"]
https://github.com/sail-sail/named_placeholders
Last synced: 6 days ago
JSON representation
compiles "select foo where foo.id = :bar and foo.baz < :baz" into "select foo where foo.id = ? and foo.baz < ?" + ["bar", "baz"]
- Host: GitHub
- URL: https://github.com/sail-sail/named_placeholders
- Owner: sail-sail
- Created: 2022-05-16T01:20:37.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2022-05-16T02:17:05.000Z (over 2 years ago)
- Last Synced: 2024-12-10T21:26:22.608Z (2 months ago)
- Language: TypeScript
- Size: 2.93 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# named_placeholders
compiles "select foo where foo.id = :bar and foo.baz < :baz" into "select foo where foo.id = ? and foo.baz < ?" + ["bar", "baz"]
## usage
```ts
import { createCompiler, toNumbered } from "https://deno.land/x/[email protected]/mod.ts";
import { assert, assertEquals } from "https://deno.land/[email protected]/testing/asserts.ts";Deno.test("compile", function() {
const query = 'select users.json,EXISTS(select 1 from moderators where moderators.id = :id) as is_moderator from users where users.id = :id and users.status = :status and users.complete_status = :complete_status';
const compile = createCompiler();
const [ sql, args ] = compile(query, { id: 123, status: 'Yes!', complete_status: 'No!' });
assert(sql, "select users.json,EXISTS(select 1 from moderators where moderators.id = ?) as is_moderator from users where users.id = ? and users.status = ? and users.complete_status = ?");
assertEquals(args, [ 123, 123, 'Yes!', 'No!' ]);
});Deno.test("toNumbered", function() {
const query = 'select users.json,EXISTS(select 1 from moderators where moderators.id = :id) as is_moderator from users where users.id = :id and users.status = :status and users.complete_status = :complete_status';
const [ sql, args ] = toNumbered(query, { id: 123, status: 'Yes!', complete_status: 'No!' });
assert(sql, "select users.json,EXISTS(select 1 from moderators where moderators.id = $1) as is_moderator from users where users.id = $1 and users.status = $2 and users.complete_status = $3");
assertEquals(args, [ 123, 'Yes!', 'No!' ]);
});
```## credits
parser is based on @mscdex code of his excellent [node-mariasql](https://github.com/mscdex/node-mariasql) library