cl-orm 0.1.2 → 2.0.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/README.md CHANGED
@@ -1,10 +1,16 @@
1
+ [npm-image]: https://img.shields.io/npm/v/cl-orm.svg
2
+ [npm-url]: https://npmjs.org/package/cl-orm
3
+ [downloads-image]: https://img.shields.io/npm/dt/cl-orm.svg
4
+ [downloads-url]: https://npmjs.org/package/cl-orm
5
+
1
6
  # CL ORM
2
7
 
3
8
  <img align="right" width="64" height="64" alt="Logo" src="website/static/img/favicon.svg">
4
9
 
5
- A lightweight **ORM** for **Cloudflare Workers** (**D1** and **Durable Objects**), designed to be intuitive, productive and focused on essential functionality.
10
+ ⛅️ A lightweight **ORM** for **Cloudflare Workers** (**D1** and **Durable Objects**), designed to be intuitive and productive, focused on essential functionality.
6
11
 
7
- - This project supports **Cloudflare D1** and **Durable Objects SQL Storage** as database drivers.
12
+ [![NPM Version][npm-image]][npm-url]
13
+ [![NPM Downloads][downloads-image]][downloads-url]
8
14
 
9
15
  ---
10
16
 
@@ -14,10 +20,10 @@ A lightweight **ORM** for **Cloudflare Workers** (**D1** and **Durable Objects**
14
20
 
15
21
  ## Why
16
22
 
17
- - Supports both **Cloudflare D1** and **Durable Objects** SQL Storage.
23
+ - Supports both **Cloudflare D1** and **Durable Objects SQL Storage**.
18
24
  - Unified **Connection** interface across different database drivers.
19
- - An user-friendly ORM for **INSERT**, **SELECT**, **UPDATE**, **DELETE** and **WHERE** clauses.
20
- - Automatic **Prepared Statements** (including **LIMIT** and **OFFSET**).
25
+ - User-friendly **ORM** for **INSERT**, **SELECT**, **UPDATE**, **DELETE** and **WHERE** clauses.
26
+ - Automatic **Prepared Statements**.
21
27
 
22
28
  ---
23
29
 
@@ -82,7 +88,7 @@ import { OP } from 'cl-orm';
82
88
 
83
89
  export const getUser = async (db: Connection, id: number) => {
84
90
  const user = await db.select({
85
- table: 'users',
91
+ from: 'users',
86
92
  where: OP.eq('id', id),
87
93
  limit: 1,
88
94
  });
package/lib/index.d.ts CHANGED
@@ -1,4 +1,4 @@
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 type { QueryResult, Connection, Meta, Condition, Param, WhereClause, WhereItem, } from './types.js';
4
+ export type { QueryResult, Connection, Meta, Condition, Param, WhereClause, WhereItem, WhereShorthand, } from './types.js';
@@ -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.table)];
7
+ const parts = ['DELETE FROM', (0, _utils_js_1.quoteIdentifier)(options.from)];
8
8
  const params = [];
9
9
  if (options.where) {
10
10
  const where = (0, where_js_1.buildWhere)(options.where);
@@ -12,7 +12,7 @@ const buildInsert = (options) => {
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.table)} (${columnsSql}) VALUES ${valuesSql}`,
15
+ sql: `INSERT INTO ${(0, _utils_js_1.quoteIdentifier)(options.into)} (${columnsSql}) VALUES ${valuesSql}`,
16
16
  params,
17
17
  };
18
18
  };
@@ -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.table));
25
+ parts.push('FROM', (0, _utils_js_1.quoteIdentifier)(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) {
@@ -20,4 +20,7 @@ export declare const OP: {
20
20
  };
21
21
  between: (column: string, params: [Param, Param]) => Condition;
22
22
  notBetween: (column: string, params: [Param, Param]) => Condition;
23
+ AND: (...conditions: Condition[]) => Condition;
24
+ OR: (...conditions: Condition[]) => Condition;
25
+ XOR: (...conditions: Condition[]) => Condition;
23
26
  };
@@ -2,6 +2,10 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.OP = void 0;
4
4
  const _utils_js_1 = require("../_utils.js");
5
+ const logical = (connector, conditions) => ({
6
+ condition: `(${conditions.map((c) => c.condition).join(` ${connector} `)})`,
7
+ params: conditions.flatMap((c) => c.params),
8
+ });
5
9
  const comparison = (column, operator, param) => ({
6
10
  condition: `${(0, _utils_js_1.quoteIdentifier)(column)} ${operator} ?`,
7
11
  params: [param],
@@ -57,4 +61,7 @@ exports.OP = {
57
61
  condition: `${(0, _utils_js_1.quoteIdentifier)(column)} NOT BETWEEN ? AND ?`,
58
62
  params,
59
63
  }),
64
+ AND: (...conditions) => logical('AND', conditions),
65
+ OR: (...conditions) => logical('OR', conditions),
66
+ XOR: (...conditions) => logical('XOR', conditions),
60
67
  };
@@ -1,8 +1,18 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.buildWhere = void 0;
4
+ const _utils_js_1 = require("../_utils.js");
4
5
  const isConnector = (value) => typeof value === 'string' && ['AND', 'OR', 'XOR', 'NOT'].includes(value);
5
6
  const isCondition = (value) => typeof value === 'object' && !Array.isArray(value) && 'condition' in value;
7
+ const isShorthand = (value) => typeof value === 'object' && !Array.isArray(value) && !('condition' in value);
8
+ const fromShorthand = (shorthand) => {
9
+ const entries = Object.entries(shorthand);
10
+ const sql = entries
11
+ .map(([key]) => `${(0, _utils_js_1.quoteIdentifier)(key)} = ?`)
12
+ .join(' AND ');
13
+ const params = entries.map(([, value]) => value);
14
+ return { sql, params };
15
+ };
6
16
  const processItems = (items) => {
7
17
  const parts = [];
8
18
  const params = [];
@@ -27,6 +37,8 @@ const processItems = (items) => {
27
37
  const buildWhere = (where) => {
28
38
  if (typeof where === 'string')
29
39
  return { sql: where, params: [] };
40
+ if (isShorthand(where))
41
+ return fromShorthand(where);
30
42
  if (isCondition(where))
31
43
  return { sql: where.condition, params: [...where.params] };
32
44
  return processItems(where);
package/lib/types.d.ts CHANGED
@@ -5,10 +5,11 @@ export type Condition = {
5
5
  };
6
6
  export type Connector = 'AND' | 'OR' | 'XOR' | 'NOT';
7
7
  export type WhereItem = Condition | Connector | WhereItem[];
8
- export type WhereClause = string | Condition | WhereItem[];
8
+ export type WhereShorthand = Record<string, Param>;
9
+ export type WhereClause = string | Condition | WhereItem[] | WhereShorthand;
9
10
  type Values = Record<string, unknown>;
10
11
  export type InsertOptions = {
11
- table: string;
12
+ into: string;
12
13
  values: Values | Values[];
13
14
  };
14
15
  export type JoinOptions = {
@@ -23,7 +24,7 @@ export type JoinOptions = {
23
24
  export type SelectOptions = {
24
25
  distinct?: boolean;
25
26
  columns?: string | string[];
26
- table: string;
27
+ from: string;
27
28
  join?: JoinOptions | JoinOptions[];
28
29
  where?: WhereClause;
29
30
  limit?: number;
@@ -39,7 +40,7 @@ export type UpdateOptions = {
39
40
  params?: unknown[];
40
41
  };
41
42
  export type DeleteOptions = {
42
- table: string;
43
+ from: string;
43
44
  where?: WhereClause;
44
45
  limit?: number;
45
46
  params?: unknown[];
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "cl-orm",
3
3
  "main": "./lib/index.js",
4
- "version": "0.1.2",
4
+ "version": "2.0.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",