cl-orm 2.0.0 → 2.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/index.d.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  export { useD1 } from './drivers/d1.js';
2
2
  export { useDO } from './drivers/do.js';
3
3
  export { OP } from './queries/where/operators.js';
4
+ export { backtick as bt } from './queries/_utils.js';
4
5
  export type { QueryResult, Connection, Meta, Condition, Param, WhereClause, WhereItem, WhereShorthand, } from './types.js';
package/lib/index.js CHANGED
@@ -1,9 +1,11 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.OP = exports.useDO = exports.useD1 = void 0;
3
+ exports.bt = exports.OP = exports.useDO = exports.useD1 = void 0;
4
4
  var d1_js_1 = require("./drivers/d1.js");
5
5
  Object.defineProperty(exports, "useD1", { enumerable: true, get: function () { return d1_js_1.useD1; } });
6
6
  var do_js_1 = require("./drivers/do.js");
7
7
  Object.defineProperty(exports, "useDO", { enumerable: true, get: function () { return do_js_1.useDO; } });
8
8
  var operators_js_1 = require("./queries/where/operators.js");
9
9
  Object.defineProperty(exports, "OP", { enumerable: true, get: function () { return operators_js_1.OP; } });
10
+ var _utils_js_1 = require("./queries/_utils.js");
11
+ Object.defineProperty(exports, "bt", { enumerable: true, get: function () { return _utils_js_1.backtick; } });
@@ -1 +1 @@
1
- export declare const quoteIdentifier: (name: string) => string;
1
+ export declare const backtick: (name: string) => string;
@@ -1,13 +1,14 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.quoteIdentifier = void 0;
4
- const quoteIdentifier = (name) => {
5
- if (name.includes('.')) {
3
+ exports.backtick = void 0;
4
+ const backtick = (name) => {
5
+ if (name.includes('`'))
6
+ return name;
7
+ if (name.includes('.'))
6
8
  return name
7
9
  .split('.')
8
10
  .map((part) => `\`${part}\``)
9
11
  .join('.');
10
- }
11
12
  return `\`${name}\``;
12
13
  };
13
- exports.quoteIdentifier = quoteIdentifier;
14
+ exports.backtick = backtick;
@@ -4,7 +4,7 @@ exports.buildDelete = void 0;
4
4
  const _utils_js_1 = require("./_utils.js");
5
5
  const where_js_1 = require("./where/where.js");
6
6
  const buildDelete = (options) => {
7
- const parts = ['DELETE FROM', (0, _utils_js_1.quoteIdentifier)(options.from)];
7
+ const parts = ['DELETE FROM', (0, _utils_js_1.backtick)(options.from)];
8
8
  const params = [];
9
9
  if (options.where) {
10
10
  const where = (0, where_js_1.buildWhere)(options.where);
@@ -7,12 +7,12 @@ const buildInsert = (options) => {
7
7
  ? options.values
8
8
  : [options.values];
9
9
  const columns = Object.keys(rows[0]);
10
- const columnsSql = columns.map(_utils_js_1.quoteIdentifier).join(', ');
10
+ const columnsSql = columns.map(_utils_js_1.backtick).join(', ');
11
11
  const placeholders = `(${columns.map(() => '?').join(', ')})`;
12
12
  const valuesSql = rows.map(() => placeholders).join(', ');
13
13
  const params = rows.flatMap((row) => columns.map((col) => row[col]));
14
14
  return {
15
- sql: `INSERT INTO ${(0, _utils_js_1.quoteIdentifier)(options.into)} (${columnsSql}) VALUES ${valuesSql}`,
15
+ sql: `INSERT INTO ${(0, _utils_js_1.backtick)(options.into)} (${columnsSql}) VALUES ${valuesSql}`,
16
16
  params,
17
17
  };
18
18
  };
@@ -8,13 +8,13 @@ const buildColumns = (columns) => {
8
8
  return '*';
9
9
  if (typeof columns === 'string')
10
10
  return columns;
11
- return columns.map(_utils_js_1.quoteIdentifier).join(', ');
11
+ return columns.map(_utils_js_1.backtick).join(', ');
12
12
  };
13
13
  const buildJoin = (join) => {
14
14
  const type = join.outer
15
15
  ? `${join.type.toUpperCase()} OUTER`
16
16
  : join.type.toUpperCase();
17
- return `${type} JOIN ${(0, _utils_js_1.quoteIdentifier)(join.table)} ON ${(0, _utils_js_1.quoteIdentifier)(join.on.a)} = ${(0, _utils_js_1.quoteIdentifier)(join.on.b)}`;
17
+ return `${type} JOIN ${(0, _utils_js_1.backtick)(join.table)} ON ${(0, _utils_js_1.backtick)(join.on.a)} = ${(0, _utils_js_1.backtick)(join.on.b)}`;
18
18
  };
19
19
  const buildSelect = (options) => {
20
20
  const parts = ['SELECT'];
@@ -22,7 +22,7 @@ const buildSelect = (options) => {
22
22
  if (options.distinct)
23
23
  parts.push('DISTINCT');
24
24
  parts.push(buildColumns(options.columns));
25
- parts.push('FROM', (0, _utils_js_1.quoteIdentifier)(options.from));
25
+ parts.push('FROM', (0, _utils_js_1.backtick)(options.from));
26
26
  if (options.join) {
27
27
  const joins = Array.isArray(options.join) ? options.join : [options.join];
28
28
  for (const join of joins) {
@@ -35,10 +35,10 @@ const buildSelect = (options) => {
35
35
  params.push(...where.params);
36
36
  }
37
37
  if (options.groupBy)
38
- parts.push('GROUP BY', (0, _utils_js_1.quoteIdentifier)(options.groupBy));
38
+ parts.push('GROUP BY', (0, _utils_js_1.backtick)(options.groupBy));
39
39
  if (options.orderBy) {
40
40
  const [column, direction = 'ASC'] = options.orderBy;
41
- parts.push('ORDER BY', (0, _utils_js_1.quoteIdentifier)(column), direction);
41
+ parts.push('ORDER BY', (0, _utils_js_1.backtick)(column), direction);
42
42
  }
43
43
  if (options.limit !== undefined) {
44
44
  parts.push('LIMIT ?');
@@ -5,14 +5,9 @@ const _utils_js_1 = require("./_utils.js");
5
5
  const where_js_1 = require("./where/where.js");
6
6
  const buildUpdate = (options) => {
7
7
  const columns = Object.keys(options.set);
8
- const setSql = columns.map((col) => `${(0, _utils_js_1.quoteIdentifier)(col)} = ?`).join(', ');
8
+ const setSql = columns.map((col) => `${(0, _utils_js_1.backtick)(col)} = ?`).join(', ');
9
9
  const params = columns.map((col) => options.set[col]);
10
- const parts = [
11
- 'UPDATE',
12
- (0, _utils_js_1.quoteIdentifier)(options.table),
13
- 'SET',
14
- setSql,
15
- ];
10
+ const parts = ['UPDATE', (0, _utils_js_1.backtick)(options.table), 'SET', setSql];
16
11
  if (options.where) {
17
12
  const where = (0, where_js_1.buildWhere)(options.where);
18
13
  parts.push('WHERE', where.sql);
@@ -7,7 +7,7 @@ const logical = (connector, conditions) => ({
7
7
  params: conditions.flatMap((c) => c.params),
8
8
  });
9
9
  const comparison = (column, operator, param) => ({
10
- condition: `${(0, _utils_js_1.quoteIdentifier)(column)} ${operator} ?`,
10
+ condition: `${(0, _utils_js_1.backtick)(column)} ${operator} ?`,
11
11
  params: [param],
12
12
  });
13
13
  exports.OP = {
@@ -20,45 +20,45 @@ exports.OP = {
20
20
  like: (column, param) => comparison(column, 'LIKE', param),
21
21
  notLike: (column, param) => comparison(column, 'NOT LIKE', param),
22
22
  isNull: (column) => ({
23
- condition: `${(0, _utils_js_1.quoteIdentifier)(column)} IS NULL`,
23
+ condition: `${(0, _utils_js_1.backtick)(column)} IS NULL`,
24
24
  params: [],
25
25
  }),
26
26
  isNotNull: (column) => ({
27
- condition: `${(0, _utils_js_1.quoteIdentifier)(column)} IS NOT NULL`,
27
+ condition: `${(0, _utils_js_1.backtick)(column)} IS NOT NULL`,
28
28
  params: [],
29
29
  }),
30
30
  in: ((column, valuesOrSubquery, subqueryParams) => {
31
31
  if (typeof valuesOrSubquery === 'string') {
32
32
  return {
33
- condition: `${(0, _utils_js_1.quoteIdentifier)(column)} IN (${valuesOrSubquery})`,
33
+ condition: `${(0, _utils_js_1.backtick)(column)} IN (${valuesOrSubquery})`,
34
34
  params: subqueryParams ?? [],
35
35
  };
36
36
  }
37
37
  const placeholders = valuesOrSubquery.map(() => '?').join(', ');
38
38
  return {
39
- condition: `${(0, _utils_js_1.quoteIdentifier)(column)} IN (${placeholders})`,
39
+ condition: `${(0, _utils_js_1.backtick)(column)} IN (${placeholders})`,
40
40
  params: valuesOrSubquery,
41
41
  };
42
42
  }),
43
43
  notIn: ((column, valuesOrSubquery, subqueryParams) => {
44
44
  if (typeof valuesOrSubquery === 'string') {
45
45
  return {
46
- condition: `${(0, _utils_js_1.quoteIdentifier)(column)} NOT IN (${valuesOrSubquery})`,
46
+ condition: `${(0, _utils_js_1.backtick)(column)} NOT IN (${valuesOrSubquery})`,
47
47
  params: subqueryParams ?? [],
48
48
  };
49
49
  }
50
50
  const placeholders = valuesOrSubquery.map(() => '?').join(', ');
51
51
  return {
52
- condition: `${(0, _utils_js_1.quoteIdentifier)(column)} NOT IN (${placeholders})`,
52
+ condition: `${(0, _utils_js_1.backtick)(column)} NOT IN (${placeholders})`,
53
53
  params: valuesOrSubquery,
54
54
  };
55
55
  }),
56
56
  between: (column, params) => ({
57
- condition: `${(0, _utils_js_1.quoteIdentifier)(column)} BETWEEN ? AND ?`,
57
+ condition: `${(0, _utils_js_1.backtick)(column)} BETWEEN ? AND ?`,
58
58
  params,
59
59
  }),
60
60
  notBetween: (column, params) => ({
61
- condition: `${(0, _utils_js_1.quoteIdentifier)(column)} NOT BETWEEN ? AND ?`,
61
+ condition: `${(0, _utils_js_1.backtick)(column)} NOT BETWEEN ? AND ?`,
62
62
  params,
63
63
  }),
64
64
  AND: (...conditions) => logical('AND', conditions),
@@ -7,9 +7,7 @@ const isCondition = (value) => typeof value === 'object' && !Array.isArray(value
7
7
  const isShorthand = (value) => typeof value === 'object' && !Array.isArray(value) && !('condition' in value);
8
8
  const fromShorthand = (shorthand) => {
9
9
  const entries = Object.entries(shorthand);
10
- const sql = entries
11
- .map(([key]) => `${(0, _utils_js_1.quoteIdentifier)(key)} = ?`)
12
- .join(' AND ');
10
+ const sql = entries.map(([key]) => `${(0, _utils_js_1.backtick)(key)} = ?`).join(' AND ');
13
11
  const params = entries.map(([, value]) => value);
14
12
  return { sql, params };
15
13
  };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "cl-orm",
3
3
  "main": "./lib/index.js",
4
- "version": "2.0.0",
4
+ "version": "2.1.0",
5
5
  "description": "A lightweight ORM for Cloudflare Workers (D1 and Durable Objects), designed to be intuitive, productive and focused on essential functionality",
6
6
  "homepage": "https://wellwelwel.github.io/cl-orm/docs/",
7
7
  "license": "MIT",