@edadma/rdb 0.1.0-pre.40 → 0.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/README.md +204 -0
- package/index.d.ts +77 -1
- package/main.js +93107 -63745
- package/package.json +16 -7
- package/edadma-rdb-0.1.0-pre.40.tgz +0 -0
- package/main.js.map +0 -8
package/README.md
ADDED
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
# @edadma/rdb
|
|
2
|
+
|
|
3
|
+
[](https://www.npmjs.com/package/@edadma/rdb)
|
|
4
|
+
|
|
5
|
+
A lightweight, in-memory SQL database for JavaScript and TypeScript. No native dependencies, no external services — just import and query.
|
|
6
|
+
|
|
7
|
+
## Installation
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm install @edadma/rdb
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Quick Start
|
|
14
|
+
|
|
15
|
+
```javascript
|
|
16
|
+
import { ConnectSQL } from '@edadma/rdb';
|
|
17
|
+
|
|
18
|
+
const db = new ConnectSQL();
|
|
19
|
+
|
|
20
|
+
db.execute(`
|
|
21
|
+
CREATE TABLE users (
|
|
22
|
+
id SERIAL,
|
|
23
|
+
name TEXT NOT NULL,
|
|
24
|
+
email TEXT
|
|
25
|
+
)
|
|
26
|
+
`);
|
|
27
|
+
|
|
28
|
+
db.execute("INSERT INTO users (name, email) VALUES ('Alice', 'alice@example.com')");
|
|
29
|
+
db.execute("INSERT INTO users (name, email) VALUES ('Bob', 'bob@example.com')");
|
|
30
|
+
|
|
31
|
+
const [{ rows, fields }] = db.execute('SELECT * FROM users');
|
|
32
|
+
// rows: [{ id: 1, name: 'Alice', email: 'alice@example.com' }, ...]
|
|
33
|
+
// fields: [{ name: 'id', dataType: 'serial' }, { name: 'name', dataType: 'text' }, ...]
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
## Row Modes
|
|
37
|
+
|
|
38
|
+
By default, SELECT rows are returned as objects keyed by column name. Use `rowMode: 'array'` for positional arrays instead.
|
|
39
|
+
|
|
40
|
+
```javascript
|
|
41
|
+
// Set default for all queries
|
|
42
|
+
const db = new ConnectSQL({ rowMode: 'array' });
|
|
43
|
+
|
|
44
|
+
// Or override per call
|
|
45
|
+
const [{ rows }] = db.execute('SELECT id, name FROM users', { rowMode: 'array' });
|
|
46
|
+
// rows: [[1, 'Alice'], [2, 'Bob']]
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
## Supported SQL
|
|
50
|
+
|
|
51
|
+
### Data Types
|
|
52
|
+
|
|
53
|
+
| Type | Description |
|
|
54
|
+
|------|-------------|
|
|
55
|
+
| `INT` / `INTEGER` | 32-bit integer |
|
|
56
|
+
| `SERIAL` | Auto-incrementing 32-bit integer |
|
|
57
|
+
| `BIGINT` | 64-bit integer |
|
|
58
|
+
| `BIGSERIAL` | Auto-incrementing 64-bit integer |
|
|
59
|
+
| `DOUBLE` | Double-precision float |
|
|
60
|
+
| `NUMERIC(p, s)` | Fixed-precision decimal |
|
|
61
|
+
| `TEXT` | Variable-length string |
|
|
62
|
+
| `BOOLEAN` | True/false |
|
|
63
|
+
| `TIMESTAMP` | Date and time |
|
|
64
|
+
| `UUID` | UUID (use `DEFAULT gen_random_uuid()`) |
|
|
65
|
+
| `JSON` | JSON objects and arrays |
|
|
66
|
+
| `ENUM` | Custom enumerated types |
|
|
67
|
+
|
|
68
|
+
### Operations
|
|
69
|
+
|
|
70
|
+
```sql
|
|
71
|
+
-- DDL
|
|
72
|
+
CREATE TABLE, DROP TABLE, ALTER TABLE (ADD/DROP/ALTER COLUMN)
|
|
73
|
+
CREATE TYPE ... AS ENUM, DROP TYPE
|
|
74
|
+
|
|
75
|
+
-- DML
|
|
76
|
+
INSERT INTO ... VALUES, UPDATE ... SET ... WHERE, DELETE FROM ... WHERE
|
|
77
|
+
|
|
78
|
+
-- Queries
|
|
79
|
+
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
|
|
83
|
+
UNION, INTERSECT, EXCEPT
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
### Aggregate Functions
|
|
87
|
+
|
|
88
|
+
`COUNT`, `SUM`, `AVG`, `MIN`, `MAX`
|
|
89
|
+
|
|
90
|
+
### Built-in Functions
|
|
91
|
+
|
|
92
|
+
`gen_random_uuid()`, `CURRENT_TIMESTAMP`, `COALESCE`, `UPPER`, `LOWER`, `LENGTH`, `SUBSTRING`, `TRIM`, `ABS`, `ROUND`, `CEIL`, `FLOOR`
|
|
93
|
+
|
|
94
|
+
## API
|
|
95
|
+
|
|
96
|
+
### `new ConnectSQL(options?)`
|
|
97
|
+
|
|
98
|
+
Creates a new database instance. Each instance is fully isolated.
|
|
99
|
+
|
|
100
|
+
| Option | Type | Default | Description |
|
|
101
|
+
|--------|------|---------|-------------|
|
|
102
|
+
| `rowMode` | `'object' \| 'array'` | `'object'` | Default row format for SELECT results |
|
|
103
|
+
|
|
104
|
+
### `db.execute(sql, options?)`
|
|
105
|
+
|
|
106
|
+
Executes one or more SQL statements separated by `;`. Returns an array of results.
|
|
107
|
+
|
|
108
|
+
| Option | Type | Default | Description |
|
|
109
|
+
|--------|------|---------|-------------|
|
|
110
|
+
| `rowMode` | `'object' \| 'array'` | constructor default | Row format for this call |
|
|
111
|
+
|
|
112
|
+
### Result Types
|
|
113
|
+
|
|
114
|
+
Every result has a `command` field for easy discrimination:
|
|
115
|
+
|
|
116
|
+
```typescript
|
|
117
|
+
// DDL
|
|
118
|
+
{ command: 'create table', table: string }
|
|
119
|
+
{ command: 'drop table', table: string }
|
|
120
|
+
{ command: 'create type', type: string }
|
|
121
|
+
{ command: 'drop type', type: string }
|
|
122
|
+
{ command: 'drop index', index: string }
|
|
123
|
+
{ command: 'alter table' }
|
|
124
|
+
|
|
125
|
+
// INSERT — result contains generated/default values
|
|
126
|
+
{ command: 'insert', result: Record<string, any> }
|
|
127
|
+
|
|
128
|
+
// SELECT
|
|
129
|
+
{ command: 'select', rows: T[], fields: { name: string, dataType: string }[] }
|
|
130
|
+
|
|
131
|
+
// UPDATE / DELETE
|
|
132
|
+
{ command: 'update', rows: number }
|
|
133
|
+
{ command: 'delete', rows: number }
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
### Value Mapping
|
|
137
|
+
|
|
138
|
+
| SQL Type | JavaScript Type |
|
|
139
|
+
|----------|----------------|
|
|
140
|
+
| INT, BIGINT, DOUBLE, NUMERIC | `number` |
|
|
141
|
+
| TEXT | `string` |
|
|
142
|
+
| BOOLEAN | `boolean` |
|
|
143
|
+
| UUID | `string` |
|
|
144
|
+
| TIMESTAMP | `Date` |
|
|
145
|
+
| ENUM | `string` (label) |
|
|
146
|
+
| JSON array | `Array` |
|
|
147
|
+
| JSON object | `Object` |
|
|
148
|
+
| NULL | `null` |
|
|
149
|
+
|
|
150
|
+
## TypeScript
|
|
151
|
+
|
|
152
|
+
Full type definitions are included. Use discriminated unions to narrow result types:
|
|
153
|
+
|
|
154
|
+
```typescript
|
|
155
|
+
import { ConnectSQL, ExecuteResult } from '@edadma/rdb';
|
|
156
|
+
|
|
157
|
+
const db = new ConnectSQL();
|
|
158
|
+
const results: ExecuteResult[] = db.execute('SELECT * FROM users');
|
|
159
|
+
|
|
160
|
+
for (const result of results) {
|
|
161
|
+
if (result.command === 'select') {
|
|
162
|
+
// result.rows and result.fields are typed here
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
## Example
|
|
168
|
+
|
|
169
|
+
```javascript
|
|
170
|
+
import { ConnectSQL } from '@edadma/rdb';
|
|
171
|
+
|
|
172
|
+
const db = new ConnectSQL();
|
|
173
|
+
|
|
174
|
+
db.execute(`
|
|
175
|
+
CREATE TYPE status AS ENUM ('active', 'inactive');
|
|
176
|
+
CREATE TABLE products (
|
|
177
|
+
id SERIAL,
|
|
178
|
+
name TEXT NOT NULL,
|
|
179
|
+
price NUMERIC(10,2),
|
|
180
|
+
status status DEFAULT 'active',
|
|
181
|
+
tags JSON,
|
|
182
|
+
created_at TIMESTAMP
|
|
183
|
+
)
|
|
184
|
+
`);
|
|
185
|
+
|
|
186
|
+
db.execute(`
|
|
187
|
+
INSERT INTO products (name, price, tags, created_at) VALUES
|
|
188
|
+
('Laptop', 999.99, '["electronics", "computers"]', '2025-01-15 10:30:00');
|
|
189
|
+
INSERT INTO products (name, price, tags, created_at) VALUES
|
|
190
|
+
('Coffee', 4.50, '["food", "organic"]', '2025-01-16 08:00:00')
|
|
191
|
+
`);
|
|
192
|
+
|
|
193
|
+
const [{ rows }] = db.execute(`
|
|
194
|
+
SELECT name, price FROM products
|
|
195
|
+
WHERE price > 10
|
|
196
|
+
ORDER BY price DESC
|
|
197
|
+
`);
|
|
198
|
+
|
|
199
|
+
console.log(rows); // [{ name: 'Laptop', price: 999.99 }]
|
|
200
|
+
```
|
|
201
|
+
|
|
202
|
+
## License
|
|
203
|
+
|
|
204
|
+
[ISC](https://opensource.org/licenses/ISC)
|
package/index.d.ts
CHANGED
|
@@ -1,3 +1,79 @@
|
|
|
1
|
+
export interface ConnectSQLOptions {
|
|
2
|
+
rowMode?: 'object' | 'array';
|
|
3
|
+
}
|
|
4
|
+
|
|
5
|
+
export interface ExecuteOptions {
|
|
6
|
+
rowMode?: 'object' | 'array';
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export interface FieldInfo {
|
|
10
|
+
name: string;
|
|
11
|
+
dataType: string;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export interface CreateTableResult {
|
|
15
|
+
command: 'create table';
|
|
16
|
+
table: string;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export interface DropTableResult {
|
|
20
|
+
command: 'drop table';
|
|
21
|
+
table: string;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export interface CreateTypeResult {
|
|
25
|
+
command: 'create type';
|
|
26
|
+
type: string;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export interface DropTypeResult {
|
|
30
|
+
command: 'drop type';
|
|
31
|
+
type: string;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export interface DropIndexResult {
|
|
35
|
+
command: 'drop index';
|
|
36
|
+
index: string;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export interface AlterTableResult {
|
|
40
|
+
command: 'alter table';
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export interface InsertResult {
|
|
44
|
+
command: 'insert';
|
|
45
|
+
result: Record<string, any>;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export interface SelectResult<T = Record<string, any>> {
|
|
49
|
+
command: 'select';
|
|
50
|
+
rows: T[];
|
|
51
|
+
fields: FieldInfo[];
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export interface UpdateResult {
|
|
55
|
+
command: 'update';
|
|
56
|
+
rows: number;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export interface DeleteResult {
|
|
60
|
+
command: 'delete';
|
|
61
|
+
rows: number;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export type ExecuteResult =
|
|
65
|
+
| CreateTableResult
|
|
66
|
+
| DropTableResult
|
|
67
|
+
| CreateTypeResult
|
|
68
|
+
| DropTypeResult
|
|
69
|
+
| DropIndexResult
|
|
70
|
+
| AlterTableResult
|
|
71
|
+
| InsertResult
|
|
72
|
+
| SelectResult
|
|
73
|
+
| UpdateResult
|
|
74
|
+
| DeleteResult;
|
|
75
|
+
|
|
1
76
|
export class ConnectSQL {
|
|
2
|
-
|
|
77
|
+
constructor(options?: ConnectSQLOptions);
|
|
78
|
+
execute(sql: string, options?: ExecuteOptions): ExecuteResult[];
|
|
3
79
|
}
|