@edadma/rdb 0.1.0 → 0.1.6

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.
Files changed (4) hide show
  1. package/README.md +101 -26
  2. package/index.d.ts +40 -1
  3. package/main.js +61118 -34169
  4. package/package.json +2 -2
package/README.md CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  [![npm version](https://badge.fury.io/js/%40edadma%2Frdb.svg)](https://www.npmjs.com/package/@edadma/rdb)
4
4
 
5
- A lightweight, in-memory SQL database for JavaScript and TypeScript. No native dependencies, no external services — just import and query.
5
+ A lightweight, in-memory SQL database for JavaScript and TypeScript. No native dependencies, no external services — just import and query. Follows PostgreSQL conventions for SQL syntax, identifier handling, and type casting.
6
6
 
7
7
  ## Installation
8
8
 
@@ -21,7 +21,8 @@ db.execute(`
21
21
  CREATE TABLE users (
22
22
  id SERIAL,
23
23
  name TEXT NOT NULL,
24
- email TEXT
24
+ email TEXT,
25
+ PRIMARY KEY (id)
25
26
  )
26
27
  `);
27
28
 
@@ -46,50 +47,116 @@ const [{ rows }] = db.execute('SELECT id, name FROM users', { rowMode: 'array' }
46
47
  // rows: [[1, 'Alice'], [2, 'Bob']]
47
48
  ```
48
49
 
50
+ ## SQL Compatibility
51
+
52
+ RDB follows PostgreSQL conventions:
53
+
54
+ - **Case-insensitive keywords** — `SELECT`, `select`, and `Select` are equivalent
55
+ - **Unquoted identifier folding** — identifiers fold to lowercase (`CREATE TABLE Users` → table name `users`)
56
+ - **Double-quoted identifiers** — preserve case (`"MixedCase"` stays as-is)
57
+ - **String escaping** — doubled single quotes (`'it''s'`) and E-strings (`E'it\'s'`)
58
+ - **Operators** — both `!=` and `<>` for not-equal
59
+ - **Type casting** — `::` operator and `CAST(expr AS type)`
60
+
49
61
  ## Supported SQL
50
62
 
51
63
  ### Data Types
52
64
 
53
65
  | Type | Description |
54
66
  |------|-------------|
67
+ | `SMALLINT` | 16-bit integer |
55
68
  | `INT` / `INTEGER` | 32-bit integer |
56
- | `SERIAL` | Auto-incrementing 32-bit integer |
57
69
  | `BIGINT` | 64-bit integer |
58
- | `BIGSERIAL` | Auto-incrementing 64-bit integer |
59
- | `DOUBLE` | Double-precision float |
60
- | `NUMERIC(p, s)` | Fixed-precision decimal |
70
+ | `SMALLSERIAL` / `SERIAL` / `BIGSERIAL` | Auto-incrementing integers |
71
+ | `DOUBLE` / `FLOAT` / `REAL` | Double-precision float |
72
+ | `NUMERIC(p, s)` / `DECIMAL(p, s)` | Fixed-precision decimal |
61
73
  | `TEXT` | Variable-length string |
74
+ | `CHAR(n)` | Fixed-length string |
75
+ | `VARCHAR(n)` | Variable-length string (max n chars) |
62
76
  | `BOOLEAN` | True/false |
63
- | `TIMESTAMP` | Date and time |
77
+ | `DATE` / `TIME` / `TIMESTAMP` | Date and time types |
78
+ | `TIMESTAMP WITH TIME ZONE` | Timezone-aware timestamp |
79
+ | `INTERVAL` | Duration |
64
80
  | `UUID` | UUID (use `DEFAULT gen_random_uuid()`) |
65
- | `JSON` | JSON objects and arrays |
81
+ | `JSON` / `JSONB` | JSON objects and arrays |
82
+ | `BYTEA` | Binary data |
66
83
  | `ENUM` | Custom enumerated types |
84
+ | `INT[]`, `TEXT[]`, etc. | Typed arrays |
67
85
 
68
- ### Operations
86
+ ### DDL
69
87
 
70
88
  ```sql
71
- -- DDL
72
- CREATE TABLE, DROP TABLE, ALTER TABLE (ADD/DROP/ALTER COLUMN)
89
+ CREATE TABLE, CREATE TABLE IF NOT EXISTS
90
+ DROP TABLE, DROP TABLE IF EXISTS
91
+ ALTER TABLE (ADD/DROP/RENAME COLUMN, ADD CONSTRAINT, etc.)
73
92
  CREATE TYPE ... AS ENUM, DROP TYPE
93
+ CREATE INDEX, CREATE UNIQUE INDEX, DROP INDEX
94
+ TRUNCATE TABLE
95
+ ```
96
+
97
+ ### DML
98
+
99
+ ```sql
100
+ INSERT INTO ... VALUES
101
+ INSERT INTO ... SELECT -- insert from a query
102
+ UPDATE ... SET ... WHERE
103
+ UPDATE ... SET ... FROM ... -- bulk update with join semantics
104
+ DELETE FROM ... WHERE
105
+ TRUNCATE TABLE -- fast table reset, resets serial sequences
106
+ ```
74
107
 
75
- -- DML
76
- INSERT INTO ... VALUES, UPDATE ... SET ... WHERE, DELETE FROM ... WHERE
108
+ ### Queries
77
109
 
78
- -- Queries
110
+ ```sql
79
111
  SELECT, SELECT DISTINCT, WHERE, ORDER BY, LIMIT, OFFSET
80
- GROUP BY, HAVING, JOIN (INNER/LEFT/RIGHT/FULL)
81
- Subqueries, EXISTS, CTEs (WITH), CASE expressions
82
- LIKE, ILIKE, IS NULL, COALESCE
112
+ GROUP BY, HAVING
113
+ JOIN (INNER/LEFT/RIGHT/FULL/CROSS)
114
+ LATERAL joins -- correlated subqueries in FROM
115
+ Subqueries, EXISTS, IN, = ANY(...)
116
+ VALUES as standalone query and FROM source
117
+ Column aliases: AS alias (col1, col2, ...)
118
+ CASE expressions, BETWEEN, LIKE, ILIKE
83
119
  UNION, INTERSECT, EXCEPT
120
+ OVERLAPS -- date/time range overlap test
121
+ CAST(expr AS type), expr::type
122
+ ```
123
+
124
+ ### Constraints
125
+
126
+ ```sql
127
+ PRIMARY KEY, UNIQUE, NOT NULL, DEFAULT
128
+ FOREIGN KEY ... REFERENCES ... ON DELETE/UPDATE (CASCADE, SET NULL, RESTRICT)
129
+ ```
130
+
131
+ ### Transactions
132
+
133
+ ```sql
134
+ BEGIN, COMMIT, ROLLBACK
135
+ ```
136
+
137
+ ### Prepared Statements
138
+
139
+ ```sql
140
+ PREPARE name AS statement -- with $1, $2, ... parameters
141
+ EXECUTE name(arg1, arg2, ...)
142
+ DEALLOCATE name
84
143
  ```
85
144
 
86
145
  ### Aggregate Functions
87
146
 
88
- `COUNT`, `SUM`, `AVG`, `MIN`, `MAX`
147
+ `COUNT`, `SUM`, `AVG`, `MIN`, `MAX`, `string_agg`, `array_agg`, `bool_and`, `bool_or`, `every`, `variance`, `var_samp`, `var_pop`, `stddev`, `stddev_samp`, `stddev_pop`
89
148
 
90
149
  ### Built-in Functions
91
150
 
92
- `gen_random_uuid()`, `CURRENT_TIMESTAMP`, `COALESCE`, `UPPER`, `LOWER`, `LENGTH`, `SUBSTRING`, `TRIM`, `ABS`, `ROUND`, `CEIL`, `FLOOR`
151
+ **Text:** `lower`, `upper`, `initcap`, `length`, `trim`, `ltrim`, `rtrim`, `btrim`, `substring`, `left`, `right`, `lpad`, `rpad`, `replace`, `translate`, `concat`, `concat_ws`, `repeat`, `reverse`, `position`, `split_part`, `ascii`, `chr`, `regexp_replace`, `regexp_match`, `starts_with`, `ends_with`
152
+
153
+ **Numeric:** `abs`, `ceil`, `floor`, `round`, `trunc`, `sign`, `mod`, `%`, `power`, `sqrt`, `cbrt`, `exp`, `ln`, `log10`, `log`, `pi`, `degrees`, `radians`, `sin`, `cos`, `tan`, `asin`, `acos`, `atan`, `atan2`, `sinh`, `cosh`, `tanh`, `asinh`, `acosh`, `atanh`, `div`, `factorial`, `gcd`, `lcm`, `random`, `greatest`, `least`
154
+
155
+ **Date/Time:** `now`, `current_date`, `current_time`, `date_part`, `EXTRACT`, `date_trunc`, `make_date`, `make_time`, `make_timestamp`, `make_interval`, `age`, `to_char`, `to_date`, `to_timestamp`, `to_number`, `isfinite`
156
+
157
+ **Array:** `array_length`, `array_append`, `array_prepend`, `array_concat`, `array_cat`, `array_slice`, `array_remove`, `array_replace`, `array_position`, `array_distinct`, `array_lower`, `array_upper`, `array_ndims`, `cardinality`, `string_to_array`, `array_to_string`
158
+
159
+ **Other:** `coalesce`, `nullif`, `typeof`, `gen_random_uuid`, `octet_length`, `encode`, `decode`
93
160
 
94
161
  ## API
95
162
 
@@ -119,18 +186,25 @@ Every result has a `command` field for easy discrimination:
119
186
  { command: 'drop table', table: string }
120
187
  { command: 'create type', type: string }
121
188
  { command: 'drop type', type: string }
189
+ { command: 'create index', index: string }
122
190
  { command: 'drop index', index: string }
191
+ { command: 'truncate table', table: string }
123
192
  { command: 'alter table' }
124
193
 
125
- // INSERT — result contains generated/default values
194
+ // DML
126
195
  { command: 'insert', result: Record<string, any> }
127
-
128
- // SELECT
129
196
  { command: 'select', rows: T[], fields: { name: string, dataType: string }[] }
130
-
131
- // UPDATE / DELETE
132
197
  { command: 'update', rows: number }
133
198
  { command: 'delete', rows: number }
199
+
200
+ // Transactions
201
+ { command: 'begin' }
202
+ { command: 'commit' }
203
+ { command: 'rollback' }
204
+
205
+ // Prepared statements
206
+ { command: 'prepare', name: string }
207
+ { command: 'deallocate', name: string }
134
208
  ```
135
209
 
136
210
  ### Value Mapping
@@ -138,7 +212,7 @@ Every result has a `command` field for easy discrimination:
138
212
  | SQL Type | JavaScript Type |
139
213
  |----------|----------------|
140
214
  | INT, BIGINT, DOUBLE, NUMERIC | `number` |
141
- | TEXT | `string` |
215
+ | TEXT, CHAR, VARCHAR | `string` |
142
216
  | BOOLEAN | `boolean` |
143
217
  | UUID | `string` |
144
218
  | TIMESTAMP | `Date` |
@@ -179,7 +253,8 @@ db.execute(`
179
253
  price NUMERIC(10,2),
180
254
  status status DEFAULT 'active',
181
255
  tags JSON,
182
- created_at TIMESTAMP
256
+ created_at TIMESTAMP,
257
+ PRIMARY KEY (id)
183
258
  )
184
259
  `);
185
260
 
package/index.d.ts CHANGED
@@ -31,15 +31,47 @@ export interface DropTypeResult {
31
31
  type: string;
32
32
  }
33
33
 
34
+ export interface CreateIndexResult {
35
+ command: 'create index';
36
+ index: string;
37
+ }
38
+
34
39
  export interface DropIndexResult {
35
40
  command: 'drop index';
36
41
  index: string;
37
42
  }
38
43
 
44
+ export interface TruncateTableResult {
45
+ command: 'truncate table';
46
+ table: string;
47
+ }
48
+
39
49
  export interface AlterTableResult {
40
50
  command: 'alter table';
41
51
  }
42
52
 
53
+ export interface PrepareResult {
54
+ command: 'prepare';
55
+ name: string;
56
+ }
57
+
58
+ export interface DeallocateResult {
59
+ command: 'deallocate';
60
+ name: string;
61
+ }
62
+
63
+ export interface BeginResult {
64
+ command: 'begin';
65
+ }
66
+
67
+ export interface CommitResult {
68
+ command: 'commit';
69
+ }
70
+
71
+ export interface RollbackResult {
72
+ command: 'rollback';
73
+ }
74
+
43
75
  export interface InsertResult {
44
76
  command: 'insert';
45
77
  result: Record<string, any>;
@@ -66,12 +98,19 @@ export type ExecuteResult =
66
98
  | DropTableResult
67
99
  | CreateTypeResult
68
100
  | DropTypeResult
101
+ | CreateIndexResult
69
102
  | DropIndexResult
103
+ | TruncateTableResult
70
104
  | AlterTableResult
71
105
  | InsertResult
72
106
  | SelectResult
73
107
  | UpdateResult
74
- | DeleteResult;
108
+ | DeleteResult
109
+ | PrepareResult
110
+ | DeallocateResult
111
+ | BeginResult
112
+ | CommitResult
113
+ | RollbackResult;
75
114
 
76
115
  export class ConnectSQL {
77
116
  constructor(options?: ConnectSQLOptions);