evikit 2.10.1 → 2.11.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 +7 -6
- package/db.d.ts +14 -1
- package/package.json +1 -1
- package/sqlite.js +22 -7
package/README.md
CHANGED
|
@@ -218,7 +218,7 @@ export function api() {
|
|
|
218
218
|
|
|
219
219
|
#### Page router
|
|
220
220
|
|
|
221
|
-
Add a Preact router at `src/
|
|
221
|
+
Add a Preact router at `src/prerender.js` (uncomment the routes one-by-one when we add them later):
|
|
222
222
|
|
|
223
223
|
```js
|
|
224
224
|
import { route, ssr } from "evikit";
|
|
@@ -252,7 +252,7 @@ Make an HTML template `index.html`, leaving placeholders for server-rendered con
|
|
|
252
252
|
</head>
|
|
253
253
|
<body>
|
|
254
254
|
<div id="app"><!--app-html--></div>
|
|
255
|
-
<script prerender type="module" src="/src/
|
|
255
|
+
<script prerender type="module" src="/src/prerender.js"></script>
|
|
256
256
|
</body>
|
|
257
257
|
</html>
|
|
258
258
|
```
|
|
@@ -308,7 +308,7 @@ import { readFile } from "node:fs/promises";
|
|
|
308
308
|
import swaggerUi from "swagger-ui-express";
|
|
309
309
|
|
|
310
310
|
import { api } from "./src/api.js";
|
|
311
|
-
import { prerender } from "./src/
|
|
311
|
+
import { prerender } from "./src/prerender.js";
|
|
312
312
|
|
|
313
313
|
const app = express();
|
|
314
314
|
app.use(api().onRequest);
|
|
@@ -433,7 +433,7 @@ export function HomePage() {
|
|
|
433
433
|
}
|
|
434
434
|
```
|
|
435
435
|
|
|
436
|
-
Uncomment the `HomePage` import and `/` route in `src/
|
|
436
|
+
Uncomment the `HomePage` import and `/` route in `src/prerender.js`.
|
|
437
437
|
|
|
438
438
|
#### Forms
|
|
439
439
|
|
|
@@ -524,7 +524,7 @@ export function QuestionIdPage() {
|
|
|
524
524
|
}
|
|
525
525
|
```
|
|
526
526
|
|
|
527
|
-
Uncomment the `QuestionIdPage` import and `/question/:id` route in `src/
|
|
527
|
+
Uncomment the `QuestionIdPage` import and `/question/:id` route in `src/prerender.js`.
|
|
528
528
|
|
|
529
529
|
##### Suggest own option
|
|
530
530
|
|
|
@@ -789,8 +789,9 @@ import { ok } from "node:assert/strict";
|
|
|
789
789
|
|
|
790
790
|
// ...
|
|
791
791
|
|
|
792
|
-
const
|
|
792
|
+
const [question] = db.Question.select({
|
|
793
793
|
with: { Choice: { with: { iconStorage: true } } },
|
|
794
|
+
where: { id: input.id },
|
|
794
795
|
});
|
|
795
796
|
|
|
796
797
|
// ...
|
package/db.d.ts
CHANGED
|
@@ -17,7 +17,12 @@ export type Db<
|
|
|
17
17
|
delete(where: Where<Schema, Table>): void;
|
|
18
18
|
|
|
19
19
|
select<
|
|
20
|
-
W extends Partial<Join<R, Table>> & {
|
|
20
|
+
W extends Partial<Join<R, Table>> & {
|
|
21
|
+
limit?: number;
|
|
22
|
+
offset?: number;
|
|
23
|
+
order?: Order<Schema, Table>;
|
|
24
|
+
where?: Where<Schema, Table>;
|
|
25
|
+
},
|
|
21
26
|
>(
|
|
22
27
|
props?: W,
|
|
23
28
|
): Select<Schema, R, F, Table, W>[];
|
|
@@ -93,6 +98,14 @@ export type Join<Relations, Self extends keyof Relations> = UnionToIntersection<
|
|
|
93
98
|
}[keyof Relations[Self]]
|
|
94
99
|
>;
|
|
95
100
|
|
|
101
|
+
export type Order<
|
|
102
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
103
|
+
Schema extends Record<string, ObjectSchema<any, any>>,
|
|
104
|
+
Table extends keyof Schema,
|
|
105
|
+
> =
|
|
106
|
+
| { [K in keyof Schema[Table]["entries"]]?: null | { desc: true } }
|
|
107
|
+
| { [K in keyof Schema[Table]["entries"]]?: null | { desc: true } }[];
|
|
108
|
+
|
|
96
109
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
97
110
|
export type Relations<Schema extends Record<string, ObjectSchema<any, any>>> = {
|
|
98
111
|
[Self in keyof Schema]: Self extends string
|
package/package.json
CHANGED
package/sqlite.js
CHANGED
|
@@ -391,17 +391,35 @@ export function sqlite({ pkg, schema }) {
|
|
|
391
391
|
};
|
|
392
392
|
}
|
|
393
393
|
|
|
394
|
-
/** @type {<Schema extends Record<string, import("valibot").ObjectSchema<any, any>>, R extends import("./db.d.ts").Relations<Schema> = import("./db.d.ts").Relations<Schema>, F extends import("./db.d.ts").Flatten<Schema> = import("./db.d.ts").Flatten<Schema>, Table extends keyof F & keyof Schema = keyof F & keyof Schema >(props: { database: DatabaseSync; flat: F; join?: import("./db.d.ts").Join<R, Table> & { where?: import("./db.d.ts").Where<Schema, Table> }; schema: Schema; table: Table; }) => any[]} */
|
|
394
|
+
/** @type {<Schema extends Record<string, import("valibot").ObjectSchema<any, any>>, R extends import("./db.d.ts").Relations<Schema> = import("./db.d.ts").Relations<Schema>, F extends import("./db.d.ts").Flatten<Schema> = import("./db.d.ts").Flatten<Schema>, Table extends keyof F & keyof Schema = keyof F & keyof Schema >(props: { database: DatabaseSync; flat: F; join?: import("./db.d.ts").Join<R, Table> & { limit?: number, offset?: number, order?: import("./db.d.ts").Order<Schema, Table>, where?: import("./db.d.ts").Where<Schema, Table> }; schema: Schema; table: Table; }) => any[]} */
|
|
395
395
|
function select(props) {
|
|
396
396
|
ok(typeof props.table == "string");
|
|
397
|
-
let
|
|
398
|
-
|
|
397
|
+
let sql =
|
|
398
|
+
`select * from ${props.table}` +
|
|
399
|
+
whereQuery({ where: props.join?.where }) +
|
|
400
|
+
(props.join?.order
|
|
401
|
+
? "\norder by " +
|
|
402
|
+
(Array.isArray(props.join.order)
|
|
403
|
+
? props.join.order
|
|
404
|
+
: [props.join.order]
|
|
405
|
+
)
|
|
406
|
+
.flatMap((column) =>
|
|
407
|
+
Object.entries(column).map(([name, props]) =>
|
|
408
|
+
props?.desc ? `${name} desc` : name,
|
|
409
|
+
),
|
|
410
|
+
)
|
|
411
|
+
.join(", ")
|
|
412
|
+
: "") +
|
|
413
|
+
(props.join?.offset ? `\noffset ${props.join.offset}` : "") +
|
|
414
|
+
(props.join?.limit ? `\nlimit ${props.join.limit}` : "");
|
|
399
415
|
|
|
416
|
+
let alias = `${props.table}_0`;
|
|
400
417
|
let columns = Object.keys(
|
|
401
418
|
/** @type {any} */ (props.schema)[props.table].entries,
|
|
402
419
|
);
|
|
403
420
|
if (/** @type {any} */ (props.join)?.with) {
|
|
404
421
|
columns = columns.map((name) => `${alias}.${name} ${alias}_${name}`);
|
|
422
|
+
sql = `select COLUMNS\nfrom (${sql}) ${alias}`;
|
|
405
423
|
}
|
|
406
424
|
|
|
407
425
|
/** @type {Record<string, { entities: any[]; table: string }>} */
|
|
@@ -446,11 +464,8 @@ function select(props) {
|
|
|
446
464
|
}
|
|
447
465
|
}
|
|
448
466
|
}
|
|
449
|
-
|
|
450
467
|
join(0, props.table, alias, /** @type {any} */ (props.join));
|
|
451
|
-
sql =
|
|
452
|
-
`select ${columns.join(",\n")} ${sql}` +
|
|
453
|
-
whereQuery({ alias, where: props.join?.where });
|
|
468
|
+
sql = sql.replace("COLUMNS", columns.join(",\n "));
|
|
454
469
|
|
|
455
470
|
const rows = props.database
|
|
456
471
|
.prepare(sql)
|