@axium/server 0.11.5 → 0.12.1

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/dist/auth.d.ts CHANGED
@@ -1,8 +1,8 @@
1
1
  import type { Passkey, Session, Verification } from '@axium/core/api';
2
2
  import type { User } from '@axium/core/user';
3
3
  import type { RequestEvent } from '@sveltejs/kit';
4
- import { type Schema } from './database.js';
5
4
  import type { Insertable } from 'kysely';
5
+ import { type Schema } from './database.js';
6
6
  export interface UserInternal extends User {
7
7
  isAdmin: boolean;
8
8
  /** Tags are internal, roles are public */
package/dist/auth.js CHANGED
@@ -1,6 +1,5 @@
1
- import { jsonObjectFrom } from 'kysely/helpers/postgres';
2
1
  import { randomBytes, randomUUID } from 'node:crypto';
3
- import { connect, database as db } from './database.js';
2
+ import { connect, database as db, userFromId } from './database.js';
4
3
  export async function getUser(id) {
5
4
  connect();
6
5
  return await db.selectFrom('users').selectAll().where('id', '=', id).executeTakeFirstOrThrow();
@@ -30,7 +29,7 @@ export async function getSessionAndUser(token) {
30
29
  const result = await db
31
30
  .selectFrom('sessions')
32
31
  .selectAll()
33
- .select(eb => jsonObjectFrom(eb.selectFrom('users').selectAll().whereRef('users.id', '=', 'sessions.userId')).as('user'))
32
+ .select(userFromId)
34
33
  .where('sessions.token', '=', token)
35
34
  .where('sessions.expires', '>', new Date())
36
35
  .executeTakeFirstOrThrow();
@@ -1,7 +1,8 @@
1
1
  import type { Preferences } from '@axium/core';
2
2
  import type { AuthenticatorTransportFuture, CredentialDeviceType } from '@simplewebauthn/server';
3
- import { Kysely, type GeneratedAlways } from 'kysely';
4
- import type { VerificationRole } from './auth.js';
3
+ import type * as kysely from 'kysely';
4
+ import { Kysely } from 'kysely';
5
+ import type { UserInternal, VerificationRole } from './auth.js';
5
6
  export interface Schema {
6
7
  users: {
7
8
  id: string;
@@ -13,11 +14,11 @@ export interface Schema {
13
14
  isAdmin: boolean;
14
15
  roles: string[];
15
16
  tags: string[];
16
- registeredAt: GeneratedAlways<Date>;
17
+ registeredAt: kysely.GeneratedAlways<Date>;
17
18
  };
18
19
  sessions: {
19
- id: GeneratedAlways<string>;
20
- created: GeneratedAlways<Date>;
20
+ id: kysely.GeneratedAlways<string>;
21
+ created: kysely.GeneratedAlways<Date>;
21
22
  userId: string;
22
23
  token: string;
23
24
  expires: Date;
@@ -32,7 +33,7 @@ export interface Schema {
32
33
  passkeys: {
33
34
  id: string;
34
35
  name: string | null;
35
- createdAt: GeneratedAlways<Date>;
36
+ createdAt: kysely.GeneratedAlways<Date>;
36
37
  userId: string;
37
38
  publicKey: Uint8Array;
38
39
  counter: number;
@@ -44,12 +45,19 @@ export interface Schema {
44
45
  export type Database = Kysely<Schema> & AsyncDisposable;
45
46
  export declare let database: Database;
46
47
  export declare function connect(): Database;
48
+ export declare function count<const T extends keyof Schema>(table: T): Promise<number>;
49
+ export type TablesMatching<T> = (string & keyof Schema) & keyof {
50
+ [K in keyof Schema as Schema[K] extends T ? K : never]: null;
51
+ };
52
+ /**
53
+ * Select the user with the id from the userId column of a table, placing it in the `user` property.
54
+ */
55
+ export declare function userFromId(eb: kysely.ExpressionBuilder<Schema, keyof Schema>): kysely.AliasedRawBuilder<UserInternal, 'user'>;
47
56
  export interface Stats {
48
57
  users: number;
49
58
  passkeys: number;
50
59
  sessions: number;
51
60
  }
52
- export declare function count<const T extends keyof Schema>(table: T): Promise<number>;
53
61
  export declare function status(): Promise<Stats>;
54
62
  export declare function statusText(): Promise<string>;
55
63
  export interface OpOptions {
package/dist/database.js CHANGED
@@ -51,6 +51,7 @@ var __disposeResources = (this && this.__disposeResources) || (function (Suppres
51
51
  return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
52
52
  });
53
53
  import { Kysely, PostgresDialect, sql } from 'kysely';
54
+ import { jsonObjectFrom } from 'kysely/helpers/postgres';
54
55
  import { randomBytes } from 'node:crypto';
55
56
  import { readFileSync, writeFileSync } from 'node:fs';
56
57
  import pg from 'pg';
@@ -75,12 +76,22 @@ export function connect() {
75
76
  globalThis[sym] = database;
76
77
  return database;
77
78
  }
79
+ // Helpers
78
80
  export async function count(table) {
79
81
  const db = connect();
80
82
  return (await db.selectFrom(table)
81
83
  .select(db.fn.countAll().as('count'))
82
84
  .executeTakeFirstOrThrow()).count;
83
85
  }
86
+ /**
87
+ * Select the user with the id from the userId column of a table, placing it in the `user` property.
88
+ */
89
+ export function userFromId(eb) {
90
+ return jsonObjectFrom(eb.selectFrom('users').selectAll().whereRef('id', '=', 'userId'))
91
+ .$notNull()
92
+ .$castTo()
93
+ .as('user');
94
+ }
84
95
  export async function status() {
85
96
  return {
86
97
  users: await count('users'),
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@axium/server",
3
- "version": "0.11.5",
3
+ "version": "0.12.1",
4
4
  "author": "James Prevett <axium@jamespre.dev> (https://jamespre.dev)",
5
5
  "funding": {
6
6
  "type": "individual",