rado 1.0.0-preview.3 → 1.0.0-preview.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.
- package/LICENSE +21 -0
- package/README.md +63 -146
- package/dist/compat.d.ts +8 -0
- package/dist/compat.js +7 -0
- package/dist/core/Builder.d.ts +2 -8
- package/dist/core/Builder.js +10 -26
- package/dist/core/Column.d.ts +22 -14
- package/dist/core/Column.js +40 -21
- package/dist/core/Constraint.d.ts +6 -5
- package/dist/core/Database.d.ts +19 -7
- package/dist/core/Database.js +59 -23
- package/dist/core/Dialect.d.ts +6 -4
- package/dist/core/Dialect.js +16 -6
- package/dist/core/Diff.d.ts +6 -0
- package/dist/core/Driver.d.ts +20 -6
- package/dist/core/Emitter.d.ts +13 -8
- package/dist/core/Emitter.js +50 -42
- package/dist/core/Index.d.ts +19 -14
- package/dist/core/Index.js +29 -8
- package/dist/core/Internal.d.ts +8 -8
- package/dist/core/Internal.js +8 -6
- package/dist/core/MetaData.d.ts +1 -0
- package/dist/core/Query.d.ts +29 -26
- package/dist/core/Query.js +48 -25
- package/dist/core/Resolver.d.ts +20 -5
- package/dist/core/Resolver.js +75 -6
- package/dist/core/Schema.d.ts +6 -3
- package/dist/core/Selection.d.ts +15 -5
- package/dist/core/Selection.js +26 -13
- package/dist/core/Sql.d.ts +17 -13
- package/dist/core/Sql.js +17 -2
- package/dist/core/Table.d.ts +13 -12
- package/dist/core/Table.js +46 -14
- package/dist/core/expr/Conditions.d.ts +27 -35
- package/dist/core/expr/Conditions.js +52 -63
- package/dist/core/expr/Field.d.ts +2 -1
- package/dist/core/expr/Include.d.ts +19 -0
- package/dist/core/expr/Include.js +52 -0
- package/dist/core/expr/Input.d.ts +2 -1
- package/dist/core/expr/Json.d.ts +13 -0
- package/dist/core/expr/Json.js +16 -0
- package/dist/core/query/Delete.d.ts +4 -3
- package/dist/core/query/Delete.js +1 -7
- package/dist/core/query/Insert.d.ts +11 -8
- package/dist/core/query/Insert.js +10 -15
- package/dist/core/query/Select.d.ts +15 -27
- package/dist/core/query/Select.js +26 -85
- package/dist/core/query/Union.d.ts +24 -17
- package/dist/core/query/Union.js +37 -21
- package/dist/core/query/Update.d.ts +7 -6
- package/dist/core/query/Update.js +13 -13
- package/dist/driver/better-sqlite3.d.ts +1 -1
- package/dist/driver/better-sqlite3.js +25 -7
- package/dist/driver/bun-sqlite.d.ts +1 -2
- package/dist/driver/bun-sqlite.js +8 -1
- package/dist/driver/mysql2.d.ts +29 -0
- package/dist/driver/mysql2.js +87 -0
- package/dist/driver/pg.d.ts +11 -8
- package/dist/driver/pg.js +24 -12
- package/dist/driver/pglite.d.ts +7 -4
- package/dist/driver/pglite.js +28 -6
- package/dist/driver/sql.js.d.ts +1 -1
- package/dist/driver/sql.js.js +17 -8
- package/dist/index.d.ts +7 -5
- package/dist/index.js +7 -5
- package/dist/mysql/builder.d.ts +3 -0
- package/dist/mysql/builder.js +6 -0
- package/dist/mysql/columns.d.ts +70 -0
- package/dist/mysql/columns.js +151 -0
- package/dist/mysql/dialect.d.ts +2 -0
- package/dist/mysql/dialect.js +57 -0
- package/dist/mysql/diff.d.ts +2 -0
- package/dist/mysql/diff.js +8 -0
- package/dist/mysql.d.ts +7 -0
- package/dist/mysql.js +28 -0
- package/dist/postgres/builder.d.ts +3 -0
- package/dist/postgres/builder.js +6 -0
- package/dist/postgres/columns.d.ts +63 -0
- package/dist/postgres/columns.js +185 -0
- package/dist/postgres/dialect.d.ts +2 -0
- package/dist/postgres/dialect.js +65 -0
- package/dist/postgres/diff.d.ts +2 -0
- package/dist/postgres/diff.js +193 -0
- package/dist/postgres.d.ts +7 -2
- package/dist/postgres.js +27 -2
- package/dist/sqlite/builder.d.ts +3 -0
- package/dist/sqlite/builder.js +6 -0
- package/dist/sqlite/{SqliteColumns.d.ts → columns.d.ts} +2 -1
- package/dist/sqlite/{SqliteColumns.js → columns.js} +19 -18
- package/dist/sqlite/dialect.d.ts +2 -0
- package/dist/sqlite/dialect.js +61 -0
- package/dist/sqlite/diff.d.ts +2 -0
- package/dist/sqlite/diff.js +169 -0
- package/dist/sqlite/functions.d.ts +165 -0
- package/dist/sqlite/functions.js +158 -0
- package/dist/sqlite.d.ts +7 -4
- package/dist/sqlite.js +22 -4
- package/dist/universal/{UniversalColumns.d.ts → columns.d.ts} +2 -2
- package/dist/universal/{UniversalColumns.js → columns.js} +12 -23
- package/dist/universal/functions.d.ts +4 -0
- package/dist/universal/functions.js +13 -0
- package/dist/universal/transactions.d.ts +10 -0
- package/dist/universal/transactions.js +29 -0
- package/dist/universal.d.ts +3 -2
- package/dist/universal.js +3 -2
- package/package.json +8 -7
- package/dist/core/query/CreateTable.d.ts +0 -13
- package/dist/core/query/CreateTable.js +0 -24
- package/dist/core/query/DropTable.d.ts +0 -13
- package/dist/core/query/DropTable.js +0 -24
- package/dist/migrate/Scan.d.ts +0 -1
- package/dist/postgres/PostgresColumns.d.ts +0 -10
- package/dist/postgres/PostgresColumns.js +0 -59
- package/dist/postgres/PostgresDialect.d.ts +0 -2
- package/dist/postgres/PostgresDialect.js +0 -59
- package/dist/postgres/PostgresSchema.d.ts +0 -1
- package/dist/postgres/PostgresSchema.js +0 -5
- package/dist/postgres/PostgresTable.d.ts +0 -1
- package/dist/postgres/PostgresTable.js +0 -5
- package/dist/sqlite/SqliteDialect.d.ts +0 -2
- package/dist/sqlite/SqliteDialect.js +0 -52
- package/dist/sqlite/SqliteFunctions.d.ts +0 -8
- package/dist/sqlite/SqliteFunctions.js +0 -155
- package/dist/sqlite/SqliteTable.d.ts +0 -1
- package/dist/sqlite/SqliteTable.js +0 -5
- package/dist/universal/UniversalTransaction.d.ts +0 -3
- package/dist/universal/UniversalTransaction.js +0 -15
- /package/dist/{migrate/Scan.js → core/Diff.js} +0 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 Ben Merckx
|
|
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
|
@@ -3,11 +3,9 @@
|
|
|
3
3
|
# rado
|
|
4
4
|
|
|
5
5
|
Fully typed, lightweight TypeScript query builder.
|
|
6
|
-
Currently focused on SQLite.
|
|
7
6
|
|
|
8
7
|
- Definition via TypeScript types
|
|
9
8
|
- Composable queries
|
|
10
|
-
- Aggregate rows in selects
|
|
11
9
|
- First class JSON columns
|
|
12
10
|
- No code generation step
|
|
13
11
|
- No dependencies
|
|
@@ -18,39 +16,21 @@ Currently focused on SQLite.
|
|
|
18
16
|
|
|
19
17
|
#### Where
|
|
20
18
|
|
|
21
|
-
Conditions can be created by accessing the fields of the table
|
|
22
|
-
instances and using the operators of `Expr`:
|
|
19
|
+
Conditions can be created by accessing the fields of the table:
|
|
23
20
|
|
|
24
21
|
```ts
|
|
25
|
-
|
|
26
|
-
User.id.is
|
|
27
|
-
User.id
|
|
22
|
+
import {eq} from 'rado'
|
|
23
|
+
eq(User.id.is, 1) // Compare with a value
|
|
24
|
+
eq(User.id, Post.userId) // Compare to fields on other tables
|
|
28
25
|
```
|
|
29
26
|
|
|
30
27
|
Rows can be selected by passing the condition in the where method:
|
|
31
28
|
|
|
32
29
|
```ts
|
|
33
|
-
|
|
34
|
-
|
|
30
|
+
import {eq, or, gt, lt} from 'rado'
|
|
31
|
+
db.select().from(User).where(eq(User.id, 1))
|
|
35
32
|
// Conditions can be as complex as you want
|
|
36
|
-
|
|
37
|
-
// `And` conditions can also be comma separated in `where` to improve readability
|
|
38
|
-
User().where(User.id.isGreater(1), User.id.isLess(5))
|
|
39
|
-
```
|
|
40
|
-
|
|
41
|
-
There's a shortcut for comparing columns of a table directly in its call.
|
|
42
|
-
The first comparison above can be simplified to:
|
|
43
|
-
|
|
44
|
-
```ts
|
|
45
|
-
User({id: 1}) // Is the same as User().where(User.id.is(1))
|
|
46
|
-
```
|
|
47
|
-
|
|
48
|
-
This comes in handy when joining tables too:
|
|
49
|
-
|
|
50
|
-
```ts
|
|
51
|
-
User({id: 1}).innerJoin(Post({userId: User.id}))
|
|
52
|
-
// Is equal to:
|
|
53
|
-
User().innerJoin(Post, Post.userId.is(User.id)).where(User.id.is(1))
|
|
33
|
+
db.select().from(User).where(or(gt(User.id, 1), lt(User.id, 5)))
|
|
54
34
|
```
|
|
55
35
|
|
|
56
36
|
#### Select
|
|
@@ -58,92 +38,56 @@ User().innerJoin(Post, Post.userId.is(User.id)).where(User.id.is(1))
|
|
|
58
38
|
Retrieve a single field
|
|
59
39
|
|
|
60
40
|
```ts
|
|
61
|
-
|
|
41
|
+
db.select(User.name).from(User)
|
|
62
42
|
```
|
|
63
43
|
|
|
64
44
|
Or an object of data
|
|
65
45
|
|
|
66
46
|
```ts
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
})
|
|
71
|
-
```
|
|
72
|
-
|
|
73
|
-
You can call the `posts` helper method defined in the example schema below
|
|
74
|
-
to achieve the same:
|
|
75
|
-
|
|
76
|
-
```ts
|
|
77
|
-
User().select({
|
|
47
|
+
import {eq, include} from 'rado'
|
|
48
|
+
db.select({
|
|
49
|
+
// Get all user fields: id, username
|
|
78
50
|
...User,
|
|
79
|
-
posts
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
Retrieve all posts with their author and tags and count the tag usage in other
|
|
84
|
-
posts:
|
|
85
|
-
|
|
86
|
-
```ts
|
|
87
|
-
import {count} from 'rado/sqlite'
|
|
88
|
-
// Alias tables using `as`
|
|
89
|
-
const pt = PostTags().as('pt')
|
|
90
|
-
Post().select({
|
|
91
|
-
...Post,
|
|
92
|
-
author: Post.author().select(User.username),
|
|
93
|
-
tags: Post.tags().select({
|
|
94
|
-
count: pt({tagId: Tag.id}).select(count()).first(),
|
|
95
|
-
name: Tag.name
|
|
96
|
-
})
|
|
97
|
-
})
|
|
51
|
+
// Aggregate posts of this user
|
|
52
|
+
posts: include(db.select().from(Post).where(eq(Post.userId, User.id)))
|
|
53
|
+
}).from(User)
|
|
98
54
|
```
|
|
99
55
|
|
|
100
56
|
Join other tables and select fields from either table:
|
|
101
57
|
|
|
102
58
|
```ts
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
```ts
|
|
112
|
-
import {iif} from 'rado/sqlite'
|
|
113
|
-
Person().select({
|
|
114
|
-
name: Person.firstName.concat(' ').concat(Person.lastName),
|
|
115
|
-
isMario: Person.firstName.is('Mario'),
|
|
116
|
-
isActor: Person.id.isIn(Actor().select(Actor.personId)),
|
|
117
|
-
email: iif(Person.email.isNotNull(), Person.email, 'its.me@mario'),
|
|
118
|
-
twitterHandle: Person.socialMedia
|
|
119
|
-
.filter(media => media.type.is('twitter'))
|
|
120
|
-
.map(media => media.handle)
|
|
121
|
-
.maybeFirst()
|
|
122
|
-
})
|
|
59
|
+
db
|
|
60
|
+
.select({
|
|
61
|
+
username: User.username
|
|
62
|
+
post: Post
|
|
63
|
+
})
|
|
64
|
+
.from(User)
|
|
65
|
+
.innerJoin(Post, eq(Post.userId, User.id))
|
|
66
|
+
.where(eq(User.id, 1))
|
|
123
67
|
```
|
|
124
68
|
|
|
125
69
|
Order, group, limit queries:
|
|
126
70
|
|
|
127
71
|
```ts
|
|
128
|
-
|
|
129
|
-
|
|
72
|
+
import {asc} from 'rado'
|
|
73
|
+
db.select().from(User)
|
|
74
|
+
.orderBy(asc(User.username))
|
|
130
75
|
.groupBy(User.id, User.username)
|
|
131
|
-
.
|
|
132
|
-
.take(10)
|
|
76
|
+
.limit(10, 20)
|
|
133
77
|
```
|
|
134
78
|
|
|
135
79
|
#### Insert
|
|
136
80
|
|
|
137
|
-
Insert a single row
|
|
81
|
+
Insert a single row:
|
|
138
82
|
|
|
139
83
|
```ts
|
|
140
|
-
User
|
|
84
|
+
db.insert(User).values({username: 'Mario'})
|
|
141
85
|
```
|
|
142
86
|
|
|
143
87
|
Insert multiple rows:
|
|
144
88
|
|
|
145
89
|
```ts
|
|
146
|
-
User
|
|
90
|
+
db.insert(User).values([{username: 'Mario'}, {username: 'Luigi'}])
|
|
147
91
|
```
|
|
148
92
|
|
|
149
93
|
#### Update
|
|
@@ -151,14 +95,17 @@ User().insertAll([{username: 'Mario'}, {username: 'Luigi'}])
|
|
|
151
95
|
Set values
|
|
152
96
|
|
|
153
97
|
```ts
|
|
154
|
-
|
|
98
|
+
db.update(User)
|
|
99
|
+
.set({username: 'Bowser'})
|
|
100
|
+
.where(eq(User.id, 1))
|
|
155
101
|
```
|
|
156
102
|
|
|
157
103
|
Use expressions to update complex values:
|
|
158
104
|
|
|
159
105
|
```ts
|
|
160
|
-
|
|
161
|
-
|
|
106
|
+
import {concat} from 'rado/sqlite'
|
|
107
|
+
db.update(User)
|
|
108
|
+
.set({username: concat(User.username, ' [removed]')})
|
|
162
109
|
.where(User.deleted)
|
|
163
110
|
```
|
|
164
111
|
|
|
@@ -167,62 +114,49 @@ User()
|
|
|
167
114
|
Delete rows
|
|
168
115
|
|
|
169
116
|
```ts
|
|
170
|
-
User(
|
|
117
|
+
db.delete(User).where(eq(User.id, 1))
|
|
171
118
|
```
|
|
172
119
|
|
|
173
120
|
## Schema definition
|
|
174
121
|
|
|
175
122
|
```ts
|
|
176
|
-
import
|
|
123
|
+
import * as sqlite from 'rado/sqlite'
|
|
124
|
+
import {sqliteTable as table} from 'rado/sqlite'
|
|
177
125
|
|
|
178
126
|
const User = table({
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
id = column.integer.primaryKey()
|
|
182
|
-
username = column.string
|
|
183
|
-
|
|
184
|
-
// Define helper methods directly on the model
|
|
185
|
-
posts() {
|
|
186
|
-
return Post({userId: this.id})
|
|
187
|
-
}
|
|
188
|
-
}
|
|
127
|
+
id: sqlite.integer().primaryKey(),
|
|
128
|
+
userName: sqlite.text('user') // Column names are optional
|
|
189
129
|
})
|
|
190
130
|
|
|
191
131
|
const Post = table({
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
content = column.string
|
|
196
|
-
|
|
197
|
-
author() {
|
|
198
|
-
return User({id: this.userId}).first()
|
|
199
|
-
}
|
|
200
|
-
|
|
201
|
-
tags() {
|
|
202
|
-
return Tag({id: PostTags.tagId}).innerJoin(PostTags({postId: this.id}))
|
|
203
|
-
}
|
|
204
|
-
}
|
|
132
|
+
id: sqlite.integer().primaryKey(),
|
|
133
|
+
userId: sqlite.integer().references(() => User.id),
|
|
134
|
+
content: sqlite.text()
|
|
205
135
|
})
|
|
206
136
|
|
|
207
137
|
const Tag = table({
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
name = column.string
|
|
211
|
-
}
|
|
138
|
+
id: sqlite.integer().primaryKey(),
|
|
139
|
+
name: sqlite.text()
|
|
212
140
|
})
|
|
213
141
|
|
|
214
142
|
const PostTags = table({
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
tagId = column.integer.references(() => Tag.id)
|
|
218
|
-
}
|
|
143
|
+
postId: sqlite.integer().references(() => Post.id),
|
|
144
|
+
tagId: sqlite.integer().references(() => Tag.id)
|
|
219
145
|
})
|
|
220
146
|
```
|
|
221
147
|
|
|
222
148
|
## Connections
|
|
223
149
|
|
|
224
|
-
Currently supported
|
|
225
|
-
|
|
150
|
+
Currently supported drivers:
|
|
151
|
+
|
|
152
|
+
| Driver | import |
|
|
153
|
+
| ---------------- | ------------------------------ |
|
|
154
|
+
| `better-sqlite3` | `'rado/driver/better-sqlite3'` |
|
|
155
|
+
| `bun-sqlite` | `'rado/driver/bun-sqlite'` |
|
|
156
|
+
| `mysql2` | `'rado/driver/mysql2'` |
|
|
157
|
+
| `pg` | `'rado/driver/pg'` |
|
|
158
|
+
| `pglite` | `'rado/driver/pglite'` |
|
|
159
|
+
| `sql.js` | `'rado/driver/sql.js'` |
|
|
226
160
|
|
|
227
161
|
Pass an instance of the database to the `connect` function to get started:
|
|
228
162
|
|
|
@@ -233,32 +167,16 @@ import {connect} from 'rado/driver/better-sqlite3'
|
|
|
233
167
|
const db = connect(new Database('foobar.db'))
|
|
234
168
|
```
|
|
235
169
|
|
|
236
|
-
Currently all drivers are synchronous except for the `sqlite3` driver which is
|
|
237
|
-
async.
|
|
238
170
|
|
|
239
171
|
#### Run queries
|
|
240
172
|
|
|
241
173
|
Call the connection with a query to retrieve its results:
|
|
242
174
|
|
|
243
175
|
```ts
|
|
244
|
-
|
|
176
|
+
// For sync drivers this will be an array:
|
|
177
|
+
const posts = db.select().from(Post).all()
|
|
245
178
|
// For async drivers this will be a Promise:
|
|
246
|
-
const posts = await db(Post
|
|
247
|
-
```
|
|
248
|
-
|
|
249
|
-
#### Iterate results
|
|
250
|
-
|
|
251
|
-
Iterate over query results:
|
|
252
|
-
|
|
253
|
-
```ts
|
|
254
|
-
const allPosts = Post()
|
|
255
|
-
for (const row of db.iterate(allPosts)) {
|
|
256
|
-
console.log(row)
|
|
257
|
-
}
|
|
258
|
-
// For async drivers:
|
|
259
|
-
for await (const row of db.iterate(allPosts)) {
|
|
260
|
-
console.log(row)
|
|
261
|
-
}
|
|
179
|
+
const posts = await db.select().from(Post)
|
|
262
180
|
```
|
|
263
181
|
|
|
264
182
|
#### Transactions
|
|
@@ -268,10 +186,9 @@ and can optionally return a result:
|
|
|
268
186
|
|
|
269
187
|
```ts
|
|
270
188
|
const firstUserId = db.transaction(tx => {
|
|
271
|
-
const createTable =
|
|
189
|
+
const createTable = db.create(User)
|
|
272
190
|
tx(createTable)
|
|
273
|
-
const insertUser = User
|
|
274
|
-
tx(insertUser)
|
|
275
|
-
return insertUser.id
|
|
191
|
+
const insertUser = db.insert(User).values({username: 'Mario'}).returning(User.id)
|
|
192
|
+
return tx(insertUser)
|
|
276
193
|
})
|
|
277
194
|
```
|
package/dist/compat.d.ts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { HasSql } from './core/Internal.js';
|
|
2
|
+
import type { Sql } from './core/Sql.js';
|
|
3
|
+
import type { Table, TableDefinition, TableFields, TableInsert, TableRow } from './core/Table.js';
|
|
4
|
+
export type InferSelectModel<T> = T extends Table<infer Definition> ? TableRow<Definition> : never;
|
|
5
|
+
export type InferInsertModel<T> = T extends Table<infer Definition> ? TableInsert<Definition> : never;
|
|
6
|
+
export type SQL<T = unknown> = Sql<T>;
|
|
7
|
+
export type SQLWrapper<T = unknown> = HasSql<T>;
|
|
8
|
+
export declare function getTableColumns<Definition extends TableDefinition>(table: Table<Definition>): TableFields<Definition>;
|
package/dist/compat.js
ADDED
package/dist/core/Builder.d.ts
CHANGED
|
@@ -1,19 +1,15 @@
|
|
|
1
1
|
import { internalData, type HasQuery, type HasSql, type HasTable, type HasTarget } from './Internal.js';
|
|
2
2
|
import type { IsPostgres, QueryMeta } from './MetaData.js';
|
|
3
3
|
import type { QueryData } from './Query.js';
|
|
4
|
-
import type
|
|
4
|
+
import { type SelectionInput } from './Selection.js';
|
|
5
5
|
import type { Table, TableDefinition } from './Table.js';
|
|
6
|
-
import { Create } from './query/CreateTable.js';
|
|
7
6
|
import { DeleteFrom } from './query/Delete.js';
|
|
8
|
-
import { Drop } from './query/DropTable.js';
|
|
9
7
|
import { InsertInto } from './query/Insert.js';
|
|
10
8
|
import type { SelectBase, WithSelection, WithoutSelection } from './query/Select.js';
|
|
11
9
|
import { UpdateTable } from './query/Update.js';
|
|
12
10
|
declare class BuilderBase<Meta extends QueryMeta> {
|
|
13
11
|
readonly [internalData]: QueryData<Meta>;
|
|
14
|
-
constructor(data
|
|
15
|
-
createTable<Definition extends TableDefinition>(table: Table<Definition>): Create<Meta>;
|
|
16
|
-
dropTable(table: HasTable): Drop<Meta>;
|
|
12
|
+
constructor(data?: QueryData<Meta>);
|
|
17
13
|
select(): WithoutSelection<Meta>;
|
|
18
14
|
select<Input extends SelectionInput>(selection: Input): WithSelection<Input, Meta>;
|
|
19
15
|
selectDistinct(): WithoutSelection<Meta>;
|
|
@@ -30,7 +26,5 @@ export declare class Builder<Meta extends QueryMeta> extends BuilderBase<Meta> {
|
|
|
30
26
|
as<Input extends SelectionInput>(query: SelectBase<Input, Meta>): CTE<Input>;
|
|
31
27
|
};
|
|
32
28
|
with(...cte: Array<CTE>): BuilderBase<Meta>;
|
|
33
|
-
createTable<Definition extends TableDefinition>(table: Table<Definition>): Create<Meta>;
|
|
34
|
-
dropTable(table: HasTable): Drop<Meta>;
|
|
35
29
|
}
|
|
36
30
|
export {};
|
package/dist/core/Builder.js
CHANGED
|
@@ -7,32 +7,24 @@ import {
|
|
|
7
7
|
internalQuery,
|
|
8
8
|
internalTarget
|
|
9
9
|
} from "./Internal.js";
|
|
10
|
+
import { selection } from "./Selection.js";
|
|
10
11
|
import { sql } from "./Sql.js";
|
|
11
|
-
import { Create } from "./query/CreateTable.js";
|
|
12
12
|
import { DeleteFrom } from "./query/Delete.js";
|
|
13
|
-
import { Drop } from "./query/DropTable.js";
|
|
14
13
|
import { InsertInto } from "./query/Insert.js";
|
|
15
14
|
import { Select } from "./query/Select.js";
|
|
16
15
|
import { UpdateTable } from "./query/Update.js";
|
|
17
16
|
var BuilderBase = class {
|
|
18
17
|
[internalData];
|
|
19
|
-
constructor(data) {
|
|
18
|
+
constructor(data = {}) {
|
|
20
19
|
this[internalData] = data;
|
|
21
20
|
}
|
|
22
|
-
createTable(table) {
|
|
23
|
-
return new Create({ ...getData(this), table });
|
|
24
|
-
}
|
|
25
|
-
dropTable(table) {
|
|
26
|
-
return new Drop({ ...getData(this), table });
|
|
27
|
-
}
|
|
28
21
|
select(input) {
|
|
29
22
|
return new Select({
|
|
30
23
|
...getData(this),
|
|
31
24
|
select: {
|
|
32
25
|
type: input ? "selection" : "allFrom",
|
|
33
|
-
input,
|
|
34
|
-
tables: []
|
|
35
|
-
nullable: []
|
|
26
|
+
selection: input && selection(input),
|
|
27
|
+
tables: []
|
|
36
28
|
}
|
|
37
29
|
});
|
|
38
30
|
}
|
|
@@ -41,21 +33,19 @@ var BuilderBase = class {
|
|
|
41
33
|
...getData(this),
|
|
42
34
|
select: {
|
|
43
35
|
type: input ? "selection" : "allFrom",
|
|
44
|
-
input,
|
|
45
|
-
tables: []
|
|
46
|
-
nullable: []
|
|
36
|
+
selection: input && selection(input),
|
|
37
|
+
tables: []
|
|
47
38
|
},
|
|
48
39
|
distinct: true
|
|
49
40
|
});
|
|
50
41
|
}
|
|
51
|
-
selectDistinctOn(columns,
|
|
42
|
+
selectDistinctOn(columns, input) {
|
|
52
43
|
return new Select({
|
|
53
44
|
...getData(this),
|
|
54
45
|
select: {
|
|
55
|
-
type:
|
|
56
|
-
|
|
57
|
-
tables: []
|
|
58
|
-
nullable: []
|
|
46
|
+
type: input ? "selection" : "allFrom",
|
|
47
|
+
selection: input && selection(input),
|
|
48
|
+
tables: []
|
|
59
49
|
},
|
|
60
50
|
distinctOn: columns
|
|
61
51
|
});
|
|
@@ -88,12 +78,6 @@ var Builder = class extends BuilderBase {
|
|
|
88
78
|
cte
|
|
89
79
|
});
|
|
90
80
|
}
|
|
91
|
-
createTable(table) {
|
|
92
|
-
return new Create({ ...getData(this), table });
|
|
93
|
-
}
|
|
94
|
-
dropTable(table) {
|
|
95
|
-
return new Drop({ ...getData(this), table });
|
|
96
|
-
}
|
|
97
81
|
};
|
|
98
82
|
export {
|
|
99
83
|
Builder
|
package/dist/core/Column.d.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
import {
|
|
3
|
-
import type
|
|
1
|
+
import type { DriverSpecs } from './Driver.js';
|
|
2
|
+
import { internalData } from './Internal.js';
|
|
3
|
+
import { type Sql } from './Sql.js';
|
|
4
4
|
import type { Field, FieldData } from './expr/Field.js';
|
|
5
5
|
import { type Input } from './expr/Input.js';
|
|
6
|
-
export
|
|
6
|
+
export interface ColumnData {
|
|
7
7
|
type: Sql;
|
|
8
8
|
name?: string;
|
|
9
9
|
json?: boolean;
|
|
@@ -11,28 +11,36 @@ export declare class ColumnData {
|
|
|
11
11
|
notNull?: boolean;
|
|
12
12
|
isUnique?: boolean;
|
|
13
13
|
autoIncrement?: boolean;
|
|
14
|
-
defaultValue?():
|
|
14
|
+
defaultValue?(): Sql;
|
|
15
15
|
references?(): FieldData;
|
|
16
|
-
onUpdate?:
|
|
17
|
-
onDelete?:
|
|
18
|
-
mapFromDriverValue?(value: unknown): unknown;
|
|
16
|
+
onUpdate?: Sql;
|
|
17
|
+
onDelete?: Sql;
|
|
18
|
+
mapFromDriverValue?(value: unknown, specs: DriverSpecs): unknown;
|
|
19
19
|
mapToDriverValue?(value: unknown): unknown;
|
|
20
20
|
}
|
|
21
|
-
|
|
22
|
-
|
|
21
|
+
type WithoutNull<Value> = Exclude<Value, null>;
|
|
22
|
+
export declare class Column<Value = unknown> {
|
|
23
|
+
readonly [internalData]: ColumnData;
|
|
23
24
|
constructor(data: ColumnData);
|
|
24
|
-
notNull(): RequiredColumn<
|
|
25
|
-
default(value: Input<
|
|
26
|
-
|
|
25
|
+
notNull(): RequiredColumn<Value>;
|
|
26
|
+
default(value: Input<WithoutNull<Value>> | (() => Input<WithoutNull<Value>>)): Column<WithoutNull<Value>>;
|
|
27
|
+
defaultNow(): Column<WithoutNull<Value>>;
|
|
28
|
+
primaryKey(): Column<WithoutNull<Value>>;
|
|
27
29
|
unique(name?: string): Column<Value>;
|
|
28
30
|
references(foreignField: Field | (() => Field)): Column<Value>;
|
|
31
|
+
$type<T>(): Column<null extends Value ? T | null : T>;
|
|
29
32
|
}
|
|
30
33
|
export declare class JsonColumn<Value = unknown> extends Column<Value> {
|
|
31
34
|
private brand;
|
|
32
35
|
constructor(data: ColumnData);
|
|
33
36
|
}
|
|
34
37
|
declare const required: unique symbol;
|
|
35
|
-
export interface RequiredColumn<Value = unknown> extends Column<Value
|
|
38
|
+
export interface RequiredColumn<Value = unknown> extends Column<WithoutNull<Value>> {
|
|
36
39
|
[required]: true;
|
|
37
40
|
}
|
|
41
|
+
export interface Columns {
|
|
42
|
+
<T>(data: ColumnData): Column<T>;
|
|
43
|
+
[key: string]: (...args: Array<Input<any>>) => Sql<any>;
|
|
44
|
+
}
|
|
45
|
+
export declare const column: Columns;
|
|
38
46
|
export {};
|
package/dist/core/Column.js
CHANGED
|
@@ -1,45 +1,43 @@
|
|
|
1
1
|
// src/core/Column.ts
|
|
2
|
-
import { getField,
|
|
2
|
+
import { getData, getField, internalData } from "./Internal.js";
|
|
3
|
+
import { sql } from "./Sql.js";
|
|
3
4
|
import { input } from "./expr/Input.js";
|
|
4
|
-
var ColumnData = class {
|
|
5
|
-
type;
|
|
6
|
-
name;
|
|
7
|
-
json;
|
|
8
|
-
primary;
|
|
9
|
-
notNull;
|
|
10
|
-
isUnique;
|
|
11
|
-
autoIncrement;
|
|
12
|
-
onUpdate;
|
|
13
|
-
onDelete;
|
|
14
|
-
};
|
|
15
5
|
var Column = class _Column {
|
|
16
|
-
[
|
|
6
|
+
[internalData];
|
|
17
7
|
constructor(data) {
|
|
18
|
-
this[
|
|
8
|
+
this[internalData] = data;
|
|
19
9
|
}
|
|
20
10
|
notNull() {
|
|
21
11
|
return new _Column({
|
|
22
|
-
...this
|
|
12
|
+
...getData(this),
|
|
23
13
|
notNull: true
|
|
24
14
|
});
|
|
25
15
|
}
|
|
26
16
|
default(value) {
|
|
27
17
|
return new _Column({
|
|
28
|
-
...this
|
|
18
|
+
...getData(this),
|
|
29
19
|
defaultValue() {
|
|
30
20
|
return input(value instanceof Function ? value() : value);
|
|
31
21
|
}
|
|
32
22
|
});
|
|
33
23
|
}
|
|
24
|
+
defaultNow() {
|
|
25
|
+
return new _Column({
|
|
26
|
+
...getData(this),
|
|
27
|
+
defaultValue() {
|
|
28
|
+
return sql.unsafe("now()");
|
|
29
|
+
}
|
|
30
|
+
});
|
|
31
|
+
}
|
|
34
32
|
primaryKey() {
|
|
35
|
-
return new _Column({ ...this
|
|
33
|
+
return new _Column({ ...getData(this), primary: true });
|
|
36
34
|
}
|
|
37
35
|
unique(name) {
|
|
38
|
-
return new _Column({ ...this
|
|
36
|
+
return new _Column({ ...getData(this), isUnique: true });
|
|
39
37
|
}
|
|
40
38
|
references(foreignField) {
|
|
41
39
|
return new _Column({
|
|
42
|
-
...this
|
|
40
|
+
...getData(this),
|
|
43
41
|
references() {
|
|
44
42
|
return getField(
|
|
45
43
|
typeof foreignField === "function" ? foreignField() : foreignField
|
|
@@ -47,14 +45,35 @@ var Column = class _Column {
|
|
|
47
45
|
}
|
|
48
46
|
});
|
|
49
47
|
}
|
|
48
|
+
$type() {
|
|
49
|
+
return this;
|
|
50
|
+
}
|
|
50
51
|
};
|
|
51
52
|
var JsonColumn = class extends Column {
|
|
52
53
|
constructor(data) {
|
|
53
54
|
super({ ...data, json: true });
|
|
54
55
|
}
|
|
55
56
|
};
|
|
57
|
+
function createColumn(data) {
|
|
58
|
+
return new Column(data);
|
|
59
|
+
}
|
|
60
|
+
var column = new Proxy(createColumn, {
|
|
61
|
+
get(target, method) {
|
|
62
|
+
return target[method] ??= (...args) => {
|
|
63
|
+
while (args.length > 0)
|
|
64
|
+
if (args.at(-1) === void 0)
|
|
65
|
+
args.pop();
|
|
66
|
+
if (args.length === 0)
|
|
67
|
+
return sql.unsafe(method);
|
|
68
|
+
return sql`${sql.unsafe(method)}(${sql.join(
|
|
69
|
+
args.map(sql.inline),
|
|
70
|
+
sql`, `
|
|
71
|
+
)})`;
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
});
|
|
56
75
|
export {
|
|
57
76
|
Column,
|
|
58
|
-
|
|
59
|
-
|
|
77
|
+
JsonColumn,
|
|
78
|
+
column
|
|
60
79
|
};
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { internalConstraint, internalData, type HasConstraint, type HasData } from './Internal.js';
|
|
2
|
+
import { type Sql } from './Sql.js';
|
|
2
3
|
import type { Field, FieldData } from './expr/Field.js';
|
|
3
4
|
export interface UniqueConstraintData {
|
|
4
5
|
fields: Array<FieldData>;
|
|
@@ -9,10 +10,10 @@ export declare class UniqueConstraint<TableName extends string = string> impleme
|
|
|
9
10
|
[internalData]: UniqueConstraintData;
|
|
10
11
|
constructor(data: UniqueConstraintData);
|
|
11
12
|
on<TableName extends string>(...columns: Array<Field<unknown, TableName>>): UniqueConstraint<TableName>;
|
|
12
|
-
nullsNotDistinct(): UniqueConstraint<
|
|
13
|
-
get [internalConstraint]():
|
|
13
|
+
nullsNotDistinct(): UniqueConstraint<TableName>;
|
|
14
|
+
get [internalConstraint](): Sql;
|
|
14
15
|
}
|
|
15
|
-
export declare function unique(): UniqueConstraint
|
|
16
|
+
export declare function unique(): UniqueConstraint;
|
|
16
17
|
export interface PrimaryKeyConstraintData {
|
|
17
18
|
fields: Array<FieldData>;
|
|
18
19
|
}
|
|
@@ -21,7 +22,7 @@ export declare class PrimaryKeyConstraint<TableName extends string = string> imp
|
|
|
21
22
|
private brand;
|
|
22
23
|
[internalData]: PrimaryKeyConstraintData;
|
|
23
24
|
constructor(data: PrimaryKeyConstraintData);
|
|
24
|
-
get [internalConstraint]():
|
|
25
|
+
get [internalConstraint](): Sql;
|
|
25
26
|
}
|
|
26
27
|
export declare function primaryKey<TableName extends string = string>(...fields: Array<Field<unknown, TableName>>): PrimaryKeyConstraint<TableName>;
|
|
27
28
|
export interface ForeignKeyConstraintData {
|
|
@@ -33,7 +34,7 @@ export declare class ForeignKeyConstraint<TableName extends string = string> imp
|
|
|
33
34
|
[internalData]: ForeignKeyConstraintData;
|
|
34
35
|
constructor(data: ForeignKeyConstraintData);
|
|
35
36
|
references<ForeignTable extends string>(...fields: Array<Field<unknown, ForeignTable>>): ForeignKeyConstraint<TableName>;
|
|
36
|
-
get [internalConstraint]():
|
|
37
|
+
get [internalConstraint](): Sql;
|
|
37
38
|
}
|
|
38
39
|
export declare function foreignKey<TableName extends string = string>(...fields: Array<Field<unknown, TableName>>): ForeignKeyConstraint<TableName>;
|
|
39
40
|
export type Constraint = UniqueConstraint | PrimaryKeyConstraint | ForeignKeyConstraint;
|