@rwillians/exto 0.0.0-stage → 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/LICENSE +21 -0
- package/README.md +346 -2
- package/dist/.gitignore +2 -0
- package/package.json +63 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Rafael Willians
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,347 @@
|
|
|
1
|
-
#
|
|
1
|
+
# exto
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
A teeny tiny, type-safe and zero-dependency ORM for [Bun](https://bun.sh),
|
|
4
|
+
with adapters for SQLite and PostgreSQL.
|
|
5
|
+
|
|
6
|
+
```ts
|
|
7
|
+
import { create, from, into, table } from '@rwillians/exto';
|
|
8
|
+
import * as sqlite from '@rwillians/exto/bun-sqlite';
|
|
9
|
+
|
|
10
|
+
const users = table('users', t => ({
|
|
11
|
+
id: t.sequence().primaryKey(),
|
|
12
|
+
name: t.string(),
|
|
13
|
+
email: t.citext().unique(),
|
|
14
|
+
createdAt: t.datetime().default(() => new Date()),
|
|
15
|
+
}));
|
|
16
|
+
|
|
17
|
+
const db = sqlite.connect('./db.sqlite');
|
|
18
|
+
await create.table(users, { ifNotExists: true }).onto(db);
|
|
19
|
+
|
|
20
|
+
await into(users)
|
|
21
|
+
.values({ name: 'Jane Doe', email: 'jane@example.com' })
|
|
22
|
+
.insert(db);
|
|
23
|
+
|
|
24
|
+
const jane = await from(users)
|
|
25
|
+
.where('email', '=', 'JANE@example.com')
|
|
26
|
+
.select(['id', 'name'])
|
|
27
|
+
.one(db);
|
|
28
|
+
// → { id: number, name: string } | null
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
- Keys, operators and values are type-checked and autocompleted.
|
|
32
|
+
- Inserted and updated values are validated before any SQL runs.
|
|
33
|
+
- No singletons: you always pass the database explicitly.
|
|
34
|
+
|
|
35
|
+
> [!NOTE]
|
|
36
|
+
> exto covers single-table queries for now. See [what's not supported yet](#not-supported-yet).
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
## Install
|
|
40
|
+
|
|
41
|
+
```sh
|
|
42
|
+
bun add @rwillians/exto
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
## Tables
|
|
47
|
+
|
|
48
|
+
```ts
|
|
49
|
+
import { table } from '@rwillians/exto';
|
|
50
|
+
import { z } from 'zod';
|
|
51
|
+
|
|
52
|
+
const tasks = table('tasks', t => ({
|
|
53
|
+
id: t.uuid().generated().primaryKey(),
|
|
54
|
+
parentId: t.uuid().nullable(),
|
|
55
|
+
status: t.enum(['queued', 'running', 'done']),
|
|
56
|
+
payload: t.json(z.record(z.string(), z.any())),
|
|
57
|
+
attempts: t.integer().default(0),
|
|
58
|
+
scheduledAt: t.datetime(),
|
|
59
|
+
}));
|
|
60
|
+
|
|
61
|
+
type Task = typeof tasks.infer; // a row
|
|
62
|
+
type TaskInsert = typeof tasks.inferForInsert; // what `.values()` takes
|
|
63
|
+
type TaskUpdate = typeof tasks.inferForUpdate; // what `set` takes
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
Keys are camelCase in TypeScript and snake_case in the database
|
|
67
|
+
(`scheduledAt` → `scheduled_at`).
|
|
68
|
+
|
|
69
|
+
| builder | TS type | postgres | sqlite |
|
|
70
|
+
| :--------------------------------- | :-------------------- | :----------------- | :---------------------- |
|
|
71
|
+
| `t.bigint()` | `bigint` | `BIGINT` | `INTEGER` |
|
|
72
|
+
| `t.binary()` | `Uint8Array` | `BYTEA` | `BLOB` |
|
|
73
|
+
| `t.boolean()` | `boolean` | `BOOLEAN` | `INTEGER` |
|
|
74
|
+
| `t.citext()` | `string` | `CITEXT` | `TEXT COLLATE NOCASE` |
|
|
75
|
+
| `t.date()` | `string` (YYYY-MM-DD) | `DATE` | `TEXT` |
|
|
76
|
+
| `t.datetime()` | `Date` | `TIMESTAMPTZ` | `INTEGER` |
|
|
77
|
+
| `t.decimal({ precision, scale }?)` | `number` | `NUMERIC(p, s)` | `NUMERIC` |
|
|
78
|
+
| `t.enum([...values])` | union of values | `VARCHAR(n)` | `TEXT` |
|
|
79
|
+
| `t.float()` | `number` | `DOUBLE PRECISION` | `REAL` |
|
|
80
|
+
| `t.integer()` | `number` | `BIGINT` | `INTEGER` |
|
|
81
|
+
| `t.json(schema)` | schema's output | `JSONB` | `TEXT` |
|
|
82
|
+
| `t.sequence()` | `number` | `BIGINT` identity | `INTEGER` autoincrement |
|
|
83
|
+
| `t.string({ size? })` | `string` | `VARCHAR(size)` | `TEXT` |
|
|
84
|
+
| `t.text()` | `string` | `TEXT` | `TEXT` |
|
|
85
|
+
| `t.uuid()` | `string` | `UUID` | `TEXT` |
|
|
86
|
+
|
|
87
|
+
| modifier | effect |
|
|
88
|
+
| :-------------- | :---------------------------------------------- |
|
|
89
|
+
| `.primaryKey()` | primary key |
|
|
90
|
+
| `.nullable()` | accepts `null`, optional on insert |
|
|
91
|
+
| `.default(v)` | value or function used when omitted on insert |
|
|
92
|
+
| `.unique()` | unique constraint |
|
|
93
|
+
| `.generated()` | value generated on insert (`t.uuid()` → UUIDv7) |
|
|
94
|
+
|
|
95
|
+
`t.json()` takes any [Standard Schema](https://standardschema.dev) (zod,
|
|
96
|
+
valibot, arktype, ...). `t.citext()` needs `CREATE EXTENSION citext` on
|
|
97
|
+
postgres (see [`exec`](#migrations)). `t.decimal()` values are JS numbers;
|
|
98
|
+
precision and scale only shape postgres' column.
|
|
99
|
+
|
|
100
|
+
### Custom columns
|
|
101
|
+
|
|
102
|
+
```ts
|
|
103
|
+
import { column } from '@rwillians/exto';
|
|
104
|
+
|
|
105
|
+
const email = () => column({ type: 'CITEXT', schema: z.email() });
|
|
106
|
+
|
|
107
|
+
const users = table('users', t => ({
|
|
108
|
+
id: t.sequence().primaryKey(),
|
|
109
|
+
email: email().unique(),
|
|
110
|
+
}));
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
## Connecting
|
|
115
|
+
|
|
116
|
+
```ts
|
|
117
|
+
import * as sqlite from '@rwillians/exto/bun-sqlite';
|
|
118
|
+
import * as pg from '@rwillians/exto/bun-postgres';
|
|
119
|
+
|
|
120
|
+
const db = sqlite.connect(':memory:');
|
|
121
|
+
const db = pg.connect('postgres://user:pass@localhost:5432/app');
|
|
122
|
+
|
|
123
|
+
// log every statement
|
|
124
|
+
const db = sqlite.connect(':memory:', { loggers: [console] });
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
|
|
128
|
+
## Migrations
|
|
129
|
+
|
|
130
|
+
```ts
|
|
131
|
+
import { create, defineMigrations, exec } from '@rwillians/exto';
|
|
132
|
+
|
|
133
|
+
const migrate = defineMigrations({
|
|
134
|
+
'0001': async (db) => {
|
|
135
|
+
if (db.dialect.name === 'postgres') await exec(db, 'CREATE EXTENSION IF NOT EXISTS citext');
|
|
136
|
+
await create.table(users).onto(db);
|
|
137
|
+
await create.uniqueIndex(users, ['email']).onto(db);
|
|
138
|
+
},
|
|
139
|
+
'0002': async (db) => {
|
|
140
|
+
await create.table(tasks).onto(db);
|
|
141
|
+
await create.index(tasks, ['status', 'scheduledAt']).onto(db);
|
|
142
|
+
},
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
await migrate(db);
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
Migrations run in key order, each in its own transaction, and only once.
|
|
149
|
+
|
|
150
|
+
|
|
151
|
+
## Insert
|
|
152
|
+
|
|
153
|
+
```ts
|
|
154
|
+
import { into } from '@rwillians/exto';
|
|
155
|
+
|
|
156
|
+
const row = { status: 'queued', payload: {}, scheduledAt: new Date() } as const;
|
|
157
|
+
|
|
158
|
+
const inserted: Task[] = await into(tasks)
|
|
159
|
+
.values(row)
|
|
160
|
+
.values([row, row])
|
|
161
|
+
.insert(db);
|
|
162
|
+
|
|
163
|
+
await into(tasks).values(row).insert(db, { returning: ['id'] }); // → { id: string }[]
|
|
164
|
+
await into(tasks).values(row).insert(db, { returning: false }); // → undefined
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
`insert`, `update` and `delete` all take `returning`. It defaults to `true`,
|
|
168
|
+
except for `delete`, which defaults to `false`:
|
|
169
|
+
|
|
170
|
+
| `returning` | result |
|
|
171
|
+
| :---------- | :------------------------ |
|
|
172
|
+
| `true` | full rows |
|
|
173
|
+
| `[...keys]` | only those fields |
|
|
174
|
+
| `false` | `undefined` |
|
|
175
|
+
|
|
176
|
+
### On conflict
|
|
177
|
+
|
|
178
|
+
```ts
|
|
179
|
+
const users = table('users', t => ({
|
|
180
|
+
id: t.sequence().primaryKey(),
|
|
181
|
+
email: t.citext().unique(),
|
|
182
|
+
name: t.string(),
|
|
183
|
+
logins: t.integer().default(0),
|
|
184
|
+
banned: t.boolean().default(false),
|
|
185
|
+
}));
|
|
186
|
+
|
|
187
|
+
const rows = [{ email: 'jane@example.com', name: 'Jane' }];
|
|
188
|
+
|
|
189
|
+
// skip rows that conflict
|
|
190
|
+
await into(users).values(rows).insert(db, {
|
|
191
|
+
conflict: { on: ['email'], do: 'nothing' },
|
|
192
|
+
});
|
|
193
|
+
|
|
194
|
+
// update the existing row
|
|
195
|
+
await into(users).values(rows).insert(db, {
|
|
196
|
+
conflict: {
|
|
197
|
+
on: ['email'],
|
|
198
|
+
do: {
|
|
199
|
+
set: { banned: false }, // a given value
|
|
200
|
+
inc: { logins: 1 }, // an increment
|
|
201
|
+
replace: ['name'], // the value being inserted
|
|
202
|
+
},
|
|
203
|
+
},
|
|
204
|
+
});
|
|
205
|
+
|
|
206
|
+
// throw, same as no `conflict` at all
|
|
207
|
+
await into(users).values(rows).insert(db, {
|
|
208
|
+
conflict: { on: ['email'], do: 'throw' },
|
|
209
|
+
});
|
|
210
|
+
```
|
|
211
|
+
|
|
212
|
+
|
|
213
|
+
## Select
|
|
214
|
+
|
|
215
|
+
```ts
|
|
216
|
+
import { expr, from } from '@rwillians/exto';
|
|
217
|
+
|
|
218
|
+
const rows: Task[] = await from(tasks)
|
|
219
|
+
.where('status', '=', 'queued')
|
|
220
|
+
.where('scheduledAt', '<', new Date()) // multiple `where`s are ANDed
|
|
221
|
+
.orderBy(['scheduledAt', ['attempts', 'DESC']])
|
|
222
|
+
.limit(10)
|
|
223
|
+
.offset(20)
|
|
224
|
+
.all(db);
|
|
225
|
+
|
|
226
|
+
const queued = from(tasks).where('status', '=', 'queued');
|
|
227
|
+
|
|
228
|
+
await queued.one(db); // → Task | null (the top row)
|
|
229
|
+
await queued.select(['id', 'status']).all(db); // → Pick<Task, 'id' | 'status'>[]
|
|
230
|
+
await queued.exists(db); // → boolean
|
|
231
|
+
await queued.count(db); // → number
|
|
232
|
+
```
|
|
233
|
+
|
|
234
|
+
| operator | value |
|
|
235
|
+
| :------------------------------------- | :------------------------- |
|
|
236
|
+
| `'='` `'!='` `'<'` `'<='` `'>'` `'>='` | the column's type |
|
|
237
|
+
| `'like'` `'not like'` | `string` |
|
|
238
|
+
| `'in'` `'not in'` | array of the column's type |
|
|
239
|
+
| `'is'` `'is not'` | `null` |
|
|
240
|
+
|
|
241
|
+
### OR and grouping
|
|
242
|
+
|
|
243
|
+
```ts
|
|
244
|
+
await from(tasks)
|
|
245
|
+
.where(expr.or([
|
|
246
|
+
expr('status', '=', 'queued'),
|
|
247
|
+
expr.and([
|
|
248
|
+
expr('status', '=', 'running'),
|
|
249
|
+
expr('attempts', '>', 3),
|
|
250
|
+
]),
|
|
251
|
+
]))
|
|
252
|
+
.all(db);
|
|
253
|
+
// WHERE ("status" = ? OR ("status" = ? AND "attempts" > ?))
|
|
254
|
+
```
|
|
255
|
+
|
|
256
|
+
|
|
257
|
+
## Update
|
|
258
|
+
|
|
259
|
+
```ts
|
|
260
|
+
const updated: Pick<Task, 'id'>[] = await from(tasks)
|
|
261
|
+
.where('status', '=', 'queued')
|
|
262
|
+
.update(db, {
|
|
263
|
+
set: { status: 'running' },
|
|
264
|
+
inc: { attempts: 1 },
|
|
265
|
+
returning: ['id'], // optional, defaults to `true` (full rows)
|
|
266
|
+
});
|
|
267
|
+
```
|
|
268
|
+
|
|
269
|
+
|
|
270
|
+
## Delete
|
|
271
|
+
|
|
272
|
+
```ts
|
|
273
|
+
await from(tasks).where('status', '=', 'done').delete(db); // → undefined
|
|
274
|
+
await from(tasks).where('status', '=', 'done').delete(db, { returning: true }); // → Task[]
|
|
275
|
+
```
|
|
276
|
+
|
|
277
|
+
`update` and `delete` refuse `orderBy`, `limit` and `offset`, so they never
|
|
278
|
+
touch more rows than the query suggests.
|
|
279
|
+
|
|
280
|
+
|
|
281
|
+
## Transactions
|
|
282
|
+
|
|
283
|
+
```ts
|
|
284
|
+
await db.transaction(async (tx) => {
|
|
285
|
+
await into(users).values({ email: 'jane@example.com', name: 'Jane' }).insert(tx);
|
|
286
|
+
|
|
287
|
+
await tx.transaction(async (savepoint) => {
|
|
288
|
+
// nested transactions use savepoints
|
|
289
|
+
});
|
|
290
|
+
});
|
|
291
|
+
```
|
|
292
|
+
|
|
293
|
+
The transaction rolls back if the callback throws.
|
|
294
|
+
|
|
295
|
+
|
|
296
|
+
## Validation
|
|
297
|
+
|
|
298
|
+
Rows and changes are validated against the columns before any SQL runs:
|
|
299
|
+
|
|
300
|
+
```ts
|
|
301
|
+
import { ValidationError } from '@rwillians/exto';
|
|
302
|
+
|
|
303
|
+
const people = table('people', t => ({
|
|
304
|
+
id: t.sequence().primaryKey(),
|
|
305
|
+
name: t.string({ size: 5 }),
|
|
306
|
+
age: t.integer(),
|
|
307
|
+
}));
|
|
308
|
+
|
|
309
|
+
try {
|
|
310
|
+
await into(people).values({ name: 'Jane Doe', age: 1.5 }).insert(db);
|
|
311
|
+
} catch (error) {
|
|
312
|
+
if (error instanceof ValidationError) {
|
|
313
|
+
error.message; // Failed validation:
|
|
314
|
+
// - 0.name: must be at most 5 characters long
|
|
315
|
+
// - 0.age: must be an integer
|
|
316
|
+
error.issues; // [{ message, path: [0, 'name'] }, ...]
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
```
|
|
320
|
+
|
|
321
|
+
|
|
322
|
+
## Not supported yet
|
|
323
|
+
|
|
324
|
+
exto is young, and it focuses on queries against a single table. Every query
|
|
325
|
+
reads from one table only, so joins aren't possible yet. Neither is nesting one
|
|
326
|
+
query inside another, whether as a subquery (`WHERE id IN (SELECT ...)`) or as
|
|
327
|
+
a common table expression (`WITH ... SELECT`).
|
|
328
|
+
|
|
329
|
+
Selects return columns as they're stored: `.select()` takes column keys only,
|
|
330
|
+
so computed values such as `attempts * 2` can't be selected. Aggregation stops
|
|
331
|
+
at `.count()`, too. There's no `.groupBy()` to group rows, and no `.having()`
|
|
332
|
+
to filter those groups.
|
|
333
|
+
|
|
334
|
+
All of these are planned. Until then, `db.execute()` runs raw SQL and returns
|
|
335
|
+
the rows as the driver gives them, without exto's decoding:
|
|
336
|
+
|
|
337
|
+
```ts
|
|
338
|
+
const totals = await db.execute(
|
|
339
|
+
'SELECT status, COUNT(*) AS total FROM tasks GROUP BY status',
|
|
340
|
+
[],
|
|
341
|
+
);
|
|
342
|
+
```
|
|
343
|
+
|
|
344
|
+
|
|
345
|
+
## License
|
|
346
|
+
|
|
347
|
+
[MIT](LICENSE)
|
package/dist/.gitignore
ADDED
package/package.json
CHANGED
|
@@ -1,6 +1,65 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@rwillians/exto",
|
|
3
|
-
"
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"description": "A zero-dependencies teeny tiny ORM for SQL databases.",
|
|
4
|
+
"version": "0.1.0",
|
|
5
|
+
"author": "Rafael Willians <me@rwillians.com>",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"publishConfig": {
|
|
8
|
+
"access": "public"
|
|
9
|
+
},
|
|
10
|
+
"repository": {
|
|
11
|
+
"type": "git",
|
|
12
|
+
"url": "git://github.com/rwillians/exto.git"
|
|
13
|
+
},
|
|
14
|
+
"keywords": [
|
|
15
|
+
"query",
|
|
16
|
+
"builder",
|
|
17
|
+
"query builder",
|
|
18
|
+
"postgres",
|
|
19
|
+
"sql",
|
|
20
|
+
"sqlite",
|
|
21
|
+
"typescript",
|
|
22
|
+
"type-safe",
|
|
23
|
+
"type-safety",
|
|
24
|
+
"orm",
|
|
25
|
+
"bun"
|
|
26
|
+
],
|
|
27
|
+
"type": "module",
|
|
28
|
+
"module": "dist/esm/index.js",
|
|
29
|
+
"main": "dist/cjs/index.js",
|
|
30
|
+
"files": ["dist/", "LICENSE"],
|
|
31
|
+
"exports": {
|
|
32
|
+
".": {
|
|
33
|
+
"types": "./dist/types/index.d.ts",
|
|
34
|
+
"import": "./dist/esm/index.js",
|
|
35
|
+
"require": "./dist/cjs/index.js",
|
|
36
|
+
"default": "./dist/cjs/index.js"
|
|
37
|
+
},
|
|
38
|
+
"./bun-postgres": {
|
|
39
|
+
"types": "./dist/types/bun-postgres.d.ts",
|
|
40
|
+
"import": "./dist/esm/bun-postgres.js",
|
|
41
|
+
"require": "./dist/cjs/bun-postgres.js",
|
|
42
|
+
"default": "./dist/cjs/bun-postgres.js"
|
|
43
|
+
},
|
|
44
|
+
"./bun-sqlite": {
|
|
45
|
+
"types": "./dist/types/bun-sqlite.d.ts",
|
|
46
|
+
"import": "./dist/esm/bun-sqlite.js",
|
|
47
|
+
"require": "./dist/cjs/bun-sqlite.js",
|
|
48
|
+
"default": "./dist/cjs/bun-sqlite.js"
|
|
49
|
+
}
|
|
50
|
+
},
|
|
51
|
+
"scripts": {
|
|
52
|
+
"build": "bun run build:cjs && bun run build:esm",
|
|
53
|
+
"build:cjs": "bun run --bun tsc --project tsconfig-cjs.json",
|
|
54
|
+
"build:esm": "bun run --bun tsc --project tsconfig-esm.json",
|
|
55
|
+
"test": "bun test",
|
|
56
|
+
"typecheck": "bun check"
|
|
57
|
+
},
|
|
58
|
+
"devDependencies": {
|
|
59
|
+
"@types/bun": "latest",
|
|
60
|
+
"zod": "^4.6.7"
|
|
61
|
+
},
|
|
62
|
+
"peerDependencies": {
|
|
63
|
+
"typescript": "^7"
|
|
64
|
+
}
|
|
65
|
+
}
|