UNPKG

2.2 kBJavaScriptView Raw
1/**
2 * @license
3 * Copyright 2018 Google LLC
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * https://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18'use strict';
19
20require('module-keys/cjs').polyfill(module, require);
21
22const { SqlFragment } = require('./lib/fragment.js');
23const { SqlId } = require('./lib/id.js');
24const { makeSqlTagFunction } = require('./lib/tag-fn.js');
25const { Mintable } = require('node-sec-patterns');
26
27const mintSqlFragment = require.keys.unbox(
28 Mintable.minterFor(SqlFragment),
29 () => true,
30 String);
31
32let mysql = null;
33let pg = null; // eslint-disable-line id-length
34
35Object.defineProperties(module.exports, {
36 mysql: {
37 // Lazily load MySQL machinery since
38 // PG users are unlikely to use MySQL and vice-versa.
39 get() {
40 if (!mysql) {
41 // eslint-disable-next-line global-require
42 const lexer = require('./lib/mysql-lexer.js');
43 // eslint-disable-next-line global-require
44 const { escape, escapeDelimited } = require('./lib/mysql-escaper.js');
45 mysql = makeSqlTagFunction(
46 lexer, escape, escapeDelimited, true, mintSqlFragment);
47 }
48 return mysql;
49 },
50 enumerable: true,
51 },
52 pg: {
53 get() {
54 if (!pg) {
55 // eslint-disable-next-line global-require
56 const lexer = require('./lib/pg-lexer.js');
57 // eslint-disable-next-line global-require
58 const { escape, escapeDelimited } = require('./lib/pg-escaper.js');
59 pg = makeSqlTagFunction(
60 lexer, escape, escapeDelimited, false, mintSqlFragment);
61 }
62 return pg;
63 },
64 enumerable: true,
65 },
66 SqlId: {
67 value: SqlId,
68 enumerable: true,
69 },
70 SqlFragment: {
71 value: SqlFragment,
72 enumerable: true,
73 },
74});