@edadma/rdb 0.1.0 → 0.1.5

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 +100 -26
  2. package/index.d.ts +40 -1
  3. package/main.js +58710 -34741
  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,115 @@ 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 |
62
75
  | `BOOLEAN` | True/false |
63
- | `TIMESTAMP` | Date and time |
76
+ | `DATE` / `TIME` / `TIMESTAMP` | Date and time types |
77
+ | `TIMESTAMP WITH TIME ZONE` | Timezone-aware timestamp |
78
+ | `INTERVAL` | Duration |
64
79
  | `UUID` | UUID (use `DEFAULT gen_random_uuid()`) |
65
- | `JSON` | JSON objects and arrays |
80
+ | `JSON` / `JSONB` | JSON objects and arrays |
81
+ | `BYTEA` | Binary data |
66
82
  | `ENUM` | Custom enumerated types |
83
+ | `INT[]`, `TEXT[]`, etc. | Typed arrays |
67
84
 
68
- ### Operations
85
+ ### DDL
69
86
 
70
87
  ```sql
71
- -- DDL
72
- CREATE TABLE, DROP TABLE, ALTER TABLE (ADD/DROP/ALTER COLUMN)
88
+ CREATE TABLE, CREATE TABLE IF NOT EXISTS
89
+ DROP TABLE, DROP TABLE IF EXISTS
90
+ ALTER TABLE (ADD/DROP/RENAME COLUMN, ADD CONSTRAINT, etc.)
73
91
  CREATE TYPE ... AS ENUM, DROP TYPE
92
+ CREATE INDEX, CREATE UNIQUE INDEX, DROP INDEX
93
+ TRUNCATE TABLE
94
+ ```
95
+
96
+ ### DML
97
+
98
+ ```sql
99
+ INSERT INTO ... VALUES
100
+ INSERT INTO ... SELECT -- insert from a query
101
+ UPDATE ... SET ... WHERE
102
+ UPDATE ... SET ... FROM ... -- bulk update with join semantics
103
+ DELETE FROM ... WHERE
104
+ TRUNCATE TABLE -- fast table reset, resets serial sequences
105
+ ```
74
106
 
75
- -- DML
76
- INSERT INTO ... VALUES, UPDATE ... SET ... WHERE, DELETE FROM ... WHERE
107
+ ### Queries
77
108
 
78
- -- Queries
109
+ ```sql
79
110
  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
111
+ GROUP BY, HAVING
112
+ JOIN (INNER/LEFT/RIGHT/FULL/CROSS)
113
+ LATERAL joins -- correlated subqueries in FROM
114
+ Subqueries, EXISTS, IN, = ANY(...)
115
+ VALUES as standalone query and FROM source
116
+ Column aliases: AS alias (col1, col2, ...)
117
+ CASE expressions, BETWEEN, LIKE, ILIKE
83
118
  UNION, INTERSECT, EXCEPT
119
+ OVERLAPS -- date/time range overlap test
120
+ CAST(expr AS type), expr::type
121
+ ```
122
+
123
+ ### Constraints
124
+
125
+ ```sql
126
+ PRIMARY KEY, UNIQUE, NOT NULL, DEFAULT
127
+ FOREIGN KEY ... REFERENCES ... ON DELETE/UPDATE (CASCADE, SET NULL, RESTRICT)
128
+ ```
129
+
130
+ ### Transactions
131
+
132
+ ```sql
133
+ BEGIN, COMMIT, ROLLBACK
134
+ ```
135
+
136
+ ### Prepared Statements
137
+
138
+ ```sql
139
+ PREPARE name AS statement -- with $1, $2, ... parameters
140
+ EXECUTE name(arg1, arg2, ...)
141
+ DEALLOCATE name
84
142
  ```
85
143
 
86
144
  ### Aggregate Functions
87
145
 
88
- `COUNT`, `SUM`, `AVG`, `MIN`, `MAX`
146
+ `COUNT`, `SUM`, `AVG`, `MIN`, `MAX`, `string_agg`, `array_agg`, `bool_and`, `bool_or`, `variance`, `var_samp`, `var_pop`, `stddev`, `stddev_samp`, `stddev_pop`
89
147
 
90
148
  ### Built-in Functions
91
149
 
92
- `gen_random_uuid()`, `CURRENT_TIMESTAMP`, `COALESCE`, `UPPER`, `LOWER`, `LENGTH`, `SUBSTRING`, `TRIM`, `ABS`, `ROUND`, `CEIL`, `FLOOR`
150
+ **Text:** `lower`, `upper`, `initcap`, `length`, `trim`, `ltrim`, `rtrim`, `substring`, `left`, `right`, `lpad`, `rpad`, `replace`, `concat`, `concat_ws`, `repeat`, `reverse`, `position`, `split_part`, `ascii`, `chr`, `regexp_replace`, `regexp_match`, `starts_with`, `ends_with`
151
+
152
+ **Numeric:** `abs`, `ceil`, `floor`, `round`, `trunc`, `sign`, `mod`, `power`, `sqrt`, `exp`, `ln`, `log10`, `log`, `pi`, `degrees`, `radians`, `sin`, `cos`, `tan`, `asin`, `acos`, `atan`, `atan2`, `random`, `greatest`, `least`
153
+
154
+ **Date/Time:** `now`, `current_date`, `current_time`, `date_part`, `EXTRACT`, `date_trunc`, `make_date`, `make_time`, `age`, `to_char`, `to_date`, `to_timestamp`
155
+
156
+ **Array:** `array_length`, `array_append`, `array_prepend`, `array_concat`, `array_slice`, `array_remove`, `array_position`, `array_distinct`, `string_to_array`, `array_to_string`
157
+
158
+ **Other:** `coalesce`, `nullif`, `typeof`, `gen_random_uuid`, `octet_length`, `encode`, `decode`
93
159
 
94
160
  ## API
95
161
 
@@ -119,18 +185,25 @@ Every result has a `command` field for easy discrimination:
119
185
  { command: 'drop table', table: string }
120
186
  { command: 'create type', type: string }
121
187
  { command: 'drop type', type: string }
188
+ { command: 'create index', index: string }
122
189
  { command: 'drop index', index: string }
190
+ { command: 'truncate table', table: string }
123
191
  { command: 'alter table' }
124
192
 
125
- // INSERT — result contains generated/default values
193
+ // DML
126
194
  { command: 'insert', result: Record<string, any> }
127
-
128
- // SELECT
129
195
  { command: 'select', rows: T[], fields: { name: string, dataType: string }[] }
130
-
131
- // UPDATE / DELETE
132
196
  { command: 'update', rows: number }
133
197
  { command: 'delete', rows: number }
198
+
199
+ // Transactions
200
+ { command: 'begin' }
201
+ { command: 'commit' }
202
+ { command: 'rollback' }
203
+
204
+ // Prepared statements
205
+ { command: 'prepare', name: string }
206
+ { command: 'deallocate', name: string }
134
207
  ```
135
208
 
136
209
  ### Value Mapping
@@ -138,7 +211,7 @@ Every result has a `command` field for easy discrimination:
138
211
  | SQL Type | JavaScript Type |
139
212
  |----------|----------------|
140
213
  | INT, BIGINT, DOUBLE, NUMERIC | `number` |
141
- | TEXT | `string` |
214
+ | TEXT, CHAR | `string` |
142
215
  | BOOLEAN | `boolean` |
143
216
  | UUID | `string` |
144
217
  | TIMESTAMP | `Date` |
@@ -179,7 +252,8 @@ db.execute(`
179
252
  price NUMERIC(10,2),
180
253
  status status DEFAULT 'active',
181
254
  tags JSON,
182
- created_at TIMESTAMP
255
+ created_at TIMESTAMP,
256
+ PRIMARY KEY (id)
183
257
  )
184
258
  `);
185
259
 
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);