Merge pull request 'feat(share): custom /l/:alias pretty links (share_aliases table) (#205)' (#214) from feat/205-share-aliases into develop
Reviewed-on: #214
This commit was merged in pull request #214.
This commit is contained in:
@@ -23,6 +23,7 @@ import { UserTokenRepo } from './repos/user-token/user-token.repo';
|
||||
import { UserSessionRepo } from '@docmost/db/repos/session/user-session.repo';
|
||||
import { BacklinkRepo } from '@docmost/db/repos/backlink/backlink.repo';
|
||||
import { ShareRepo } from '@docmost/db/repos/share/share.repo';
|
||||
import { ShareAliasRepo } from '@docmost/db/repos/share-alias/share-alias.repo';
|
||||
import { NotificationRepo } from '@docmost/db/repos/notification/notification.repo';
|
||||
import { WatcherRepo } from '@docmost/db/repos/watcher/watcher.repo';
|
||||
import { LabelRepo } from '@docmost/db/repos/label/label.repo';
|
||||
@@ -96,6 +97,7 @@ import { normalizePostgresUrl } from '../common/helpers';
|
||||
UserSessionRepo,
|
||||
BacklinkRepo,
|
||||
ShareRepo,
|
||||
ShareAliasRepo,
|
||||
NotificationRepo,
|
||||
WatcherRepo,
|
||||
LabelRepo,
|
||||
@@ -128,6 +130,7 @@ import { normalizePostgresUrl } from '../common/helpers';
|
||||
UserSessionRepo,
|
||||
BacklinkRepo,
|
||||
ShareRepo,
|
||||
ShareAliasRepo,
|
||||
NotificationRepo,
|
||||
WatcherRepo,
|
||||
LabelRepo,
|
||||
|
||||
@@ -0,0 +1,54 @@
|
||||
import { type Kysely, sql } from 'kysely';
|
||||
|
||||
/**
|
||||
* Vanity share aliases: a retargetable, human-readable pointer (`/l/<alias>`)
|
||||
* that lives independently of any single `shares` row. The alias belongs to the
|
||||
* WORKSPACE (stable address), and `page_id` is nullable with ON DELETE SET NULL
|
||||
* so the address survives deletion of its current target (it 404s until
|
||||
* retargeted) rather than disappearing with the page.
|
||||
*/
|
||||
export async function up(db: Kysely<any>): Promise<void> {
|
||||
await db.schema
|
||||
.createTable('share_aliases')
|
||||
.addColumn('id', 'uuid', (col) =>
|
||||
col.primaryKey().defaultTo(sql`gen_uuid_v7()`),
|
||||
)
|
||||
.addColumn('workspace_id', 'uuid', (col) =>
|
||||
col.references('workspaces.id').onDelete('cascade').notNull(),
|
||||
)
|
||||
// Normalized ASCII, lowercase. Uniqueness is enforced per-workspace below.
|
||||
.addColumn('alias', 'varchar', (col) => col.notNull())
|
||||
// Nullable + SET NULL: the address outlives its target page.
|
||||
.addColumn('page_id', 'uuid', (col) =>
|
||||
col.references('pages.id').onDelete('set null'),
|
||||
)
|
||||
.addColumn('creator_id', 'uuid', (col) =>
|
||||
col.references('users.id').onDelete('set null'),
|
||||
)
|
||||
.addColumn('created_at', 'timestamptz', (col) =>
|
||||
col.notNull().defaultTo(sql`now()`),
|
||||
)
|
||||
.addColumn('updated_at', 'timestamptz', (col) =>
|
||||
col.notNull().defaultTo(sql`now()`),
|
||||
)
|
||||
.execute();
|
||||
|
||||
// The vanity name is unique within a workspace (mirrors shares.key scoping).
|
||||
await db.schema
|
||||
.createIndex('share_aliases_workspace_id_alias_unique')
|
||||
.on('share_aliases')
|
||||
.columns(['workspace_id', 'alias'])
|
||||
.unique()
|
||||
.execute();
|
||||
|
||||
// "Which alias targets this page?" lookup for the share modal.
|
||||
await db.schema
|
||||
.createIndex('share_aliases_page_id_idx')
|
||||
.on('share_aliases')
|
||||
.column('page_id')
|
||||
.execute();
|
||||
}
|
||||
|
||||
export async function down(db: Kysely<any>): Promise<void> {
|
||||
await db.schema.dropTable('share_aliases').execute();
|
||||
}
|
||||
@@ -0,0 +1,94 @@
|
||||
import * as migration from './20260626T130000-share-aliases';
|
||||
import type {
|
||||
InsertableShareAlias,
|
||||
ShareAlias,
|
||||
UpdatableShareAlias,
|
||||
} from '../types/entity.types';
|
||||
|
||||
/**
|
||||
* Sanity checks for the share_aliases migration + entity types. We don't run a
|
||||
* live Postgres here (that's the integration suite); instead we assert the
|
||||
* migration exposes the expected up/down contract and creates the table with
|
||||
* the unique (workspace_id, alias) constraint and the page_id index, and that
|
||||
* the generated entity types line up with the column set.
|
||||
*/
|
||||
describe('share-aliases migration', () => {
|
||||
it('up creates the table, the unique index and the page_id index', async () => {
|
||||
const calls: string[] = [];
|
||||
|
||||
const tableBuilder: any = new Proxy(
|
||||
{},
|
||||
{
|
||||
get(_t, prop: string) {
|
||||
if (prop === 'execute') return async () => undefined;
|
||||
// addColumn/addConstraint/etc. are chainable no-ops.
|
||||
return () => tableBuilder;
|
||||
},
|
||||
},
|
||||
);
|
||||
|
||||
const indexBuilder: any = new Proxy(
|
||||
{},
|
||||
{
|
||||
get(_t, prop: string) {
|
||||
if (prop === 'execute') return async () => undefined;
|
||||
return () => indexBuilder;
|
||||
},
|
||||
},
|
||||
);
|
||||
|
||||
const schema = {
|
||||
createTable: (name: string) => {
|
||||
calls.push(`createTable:${name}`);
|
||||
return tableBuilder;
|
||||
},
|
||||
createIndex: (name: string) => {
|
||||
calls.push(`createIndex:${name}`);
|
||||
return indexBuilder;
|
||||
},
|
||||
};
|
||||
|
||||
await migration.up({ schema } as any);
|
||||
|
||||
expect(calls).toContain('createTable:share_aliases');
|
||||
expect(calls).toContain(
|
||||
'createIndex:share_aliases_workspace_id_alias_unique',
|
||||
);
|
||||
expect(calls).toContain('createIndex:share_aliases_page_id_idx');
|
||||
});
|
||||
|
||||
it('down drops the table', async () => {
|
||||
const calls: string[] = [];
|
||||
const dropBuilder: any = { execute: async () => undefined };
|
||||
const schema = {
|
||||
dropTable: (name: string) => {
|
||||
calls.push(`dropTable:${name}`);
|
||||
return dropBuilder;
|
||||
},
|
||||
};
|
||||
await migration.down({ schema } as any);
|
||||
expect(calls).toContain('dropTable:share_aliases');
|
||||
});
|
||||
|
||||
it('entity types expose the alias columns', () => {
|
||||
// Compile-time only: these typed declarations fail `tsc` if the entity types
|
||||
// drift (missing/renamed columns, wrong nullability). The runtime assertions
|
||||
// would be tautological, so the value is purely in the type-check.
|
||||
const row: ShareAlias = {
|
||||
id: 'a-1',
|
||||
workspaceId: 'ws-1',
|
||||
alias: 'foo',
|
||||
pageId: 'p-1',
|
||||
creatorId: 'u-1',
|
||||
createdAt: new Date(),
|
||||
updatedAt: new Date(),
|
||||
};
|
||||
const insert: InsertableShareAlias = {
|
||||
workspaceId: 'ws-1',
|
||||
alias: 'foo',
|
||||
};
|
||||
const update: UpdatableShareAlias = { pageId: null };
|
||||
|
||||
expect([row, insert, update]).toHaveLength(3);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,120 @@
|
||||
import { ShareAliasRepo } from './share-alias.repo';
|
||||
import type { KyselyDB } from '../../types/kysely.types';
|
||||
|
||||
/**
|
||||
* SQL-shape unit tests for ShareAliasRepo. A live Postgres is out of scope;
|
||||
* instead we spy on the Kysely builder to assert each method pins the
|
||||
* workspace scope (so a name in one workspace can never resolve another's
|
||||
* page) and threads the right columns.
|
||||
*/
|
||||
describe('ShareAliasRepo', () => {
|
||||
function makeSelectRepo(result: unknown) {
|
||||
const where = jest.fn();
|
||||
const builder: any = {
|
||||
select: jest.fn(() => builder),
|
||||
where: jest.fn((...args: unknown[]) => {
|
||||
where(...args);
|
||||
return builder;
|
||||
}),
|
||||
executeTakeFirst: jest.fn().mockResolvedValue(result),
|
||||
};
|
||||
const db = { selectFrom: jest.fn(() => builder) } as unknown as KyselyDB;
|
||||
return { repo: new ShareAliasRepo(db), db, where, builder };
|
||||
}
|
||||
|
||||
it('findByAliasAndWorkspace scopes by alias AND workspace', async () => {
|
||||
const row = { id: 'a-1', alias: 'foo', workspaceId: 'ws-1' };
|
||||
const { repo, db, where } = makeSelectRepo(row);
|
||||
|
||||
const res = await repo.findByAliasAndWorkspace('foo', 'ws-1');
|
||||
|
||||
expect(res).toBe(row);
|
||||
expect(db.selectFrom).toHaveBeenCalledWith('shareAliases');
|
||||
expect(where).toHaveBeenCalledWith('alias', '=', 'foo');
|
||||
expect(where).toHaveBeenCalledWith('workspaceId', '=', 'ws-1');
|
||||
});
|
||||
|
||||
it('findByPageId scopes by page AND workspace', async () => {
|
||||
const { repo, where } = makeSelectRepo(undefined);
|
||||
await repo.findByPageId('p-1', 'ws-1');
|
||||
expect(where).toHaveBeenCalledWith('pageId', '=', 'p-1');
|
||||
expect(where).toHaveBeenCalledWith('workspaceId', '=', 'ws-1');
|
||||
});
|
||||
|
||||
it('insert writes the provided columns and returns the row', async () => {
|
||||
const values = jest.fn();
|
||||
const inserted = { id: 'a-1' };
|
||||
const builder: any = {
|
||||
values: jest.fn((v: unknown) => {
|
||||
values(v);
|
||||
return builder;
|
||||
}),
|
||||
returning: jest.fn(() => builder),
|
||||
executeTakeFirst: jest.fn().mockResolvedValue(inserted),
|
||||
};
|
||||
const db = { insertInto: jest.fn(() => builder) } as unknown as KyselyDB;
|
||||
const repo = new ShareAliasRepo(db);
|
||||
|
||||
const res = await repo.insert({
|
||||
workspaceId: 'ws-1',
|
||||
alias: 'foo',
|
||||
pageId: 'p-1',
|
||||
creatorId: 'u-1',
|
||||
});
|
||||
|
||||
expect(db.insertInto).toHaveBeenCalledWith('shareAliases');
|
||||
expect(values).toHaveBeenCalledWith({
|
||||
workspaceId: 'ws-1',
|
||||
alias: 'foo',
|
||||
pageId: 'p-1',
|
||||
creatorId: 'u-1',
|
||||
});
|
||||
expect(res).toBe(inserted);
|
||||
});
|
||||
|
||||
it('updatePageId retargets a single row scoped by id + workspace', async () => {
|
||||
const set = jest.fn();
|
||||
const where = jest.fn();
|
||||
const builder: any = {
|
||||
set: jest.fn((s: unknown) => {
|
||||
set(s);
|
||||
return builder;
|
||||
}),
|
||||
where: jest.fn((...args: unknown[]) => {
|
||||
where(...args);
|
||||
return builder;
|
||||
}),
|
||||
returning: jest.fn(() => builder),
|
||||
executeTakeFirst: jest.fn().mockResolvedValue({ id: 'a-1' }),
|
||||
};
|
||||
const db = { updateTable: jest.fn(() => builder) } as unknown as KyselyDB;
|
||||
const repo = new ShareAliasRepo(db);
|
||||
|
||||
await repo.updatePageId('a-1', 'p-2', 'ws-1');
|
||||
|
||||
expect(db.updateTable).toHaveBeenCalledWith('shareAliases');
|
||||
expect(set.mock.calls[0][0].pageId).toBe('p-2');
|
||||
expect(set.mock.calls[0][0].updatedAt).toBeInstanceOf(Date);
|
||||
expect(where).toHaveBeenCalledWith('id', '=', 'a-1');
|
||||
expect(where).toHaveBeenCalledWith('workspaceId', '=', 'ws-1');
|
||||
});
|
||||
|
||||
it('delete scopes by id + workspace', async () => {
|
||||
const where = jest.fn();
|
||||
const builder: any = {
|
||||
where: jest.fn((...args: unknown[]) => {
|
||||
where(...args);
|
||||
return builder;
|
||||
}),
|
||||
execute: jest.fn().mockResolvedValue(undefined),
|
||||
};
|
||||
const db = { deleteFrom: jest.fn(() => builder) } as unknown as KyselyDB;
|
||||
const repo = new ShareAliasRepo(db);
|
||||
|
||||
await repo.delete('a-1', 'ws-1');
|
||||
|
||||
expect(db.deleteFrom).toHaveBeenCalledWith('shareAliases');
|
||||
expect(where).toHaveBeenCalledWith('id', '=', 'a-1');
|
||||
expect(where).toHaveBeenCalledWith('workspaceId', '=', 'ws-1');
|
||||
});
|
||||
});
|
||||
109
apps/server/src/database/repos/share-alias/share-alias.repo.ts
Normal file
109
apps/server/src/database/repos/share-alias/share-alias.repo.ts
Normal file
@@ -0,0 +1,109 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { InjectKysely } from 'nestjs-kysely';
|
||||
import { KyselyDB, KyselyTransaction } from '../../types/kysely.types';
|
||||
import { dbOrTx } from '../../utils';
|
||||
import {
|
||||
InsertableShareAlias,
|
||||
ShareAlias,
|
||||
} from '@docmost/db/types/entity.types';
|
||||
|
||||
/**
|
||||
* Repository for vanity share aliases (`/l/:alias`). An alias is a long-lived,
|
||||
* workspace-scoped pointer to a page; retargeting is a single UPDATE of
|
||||
* `page_id`. All lookups are workspace-scoped so a name in one workspace can
|
||||
* never resolve a page in another.
|
||||
*/
|
||||
@Injectable()
|
||||
export class ShareAliasRepo {
|
||||
constructor(@InjectKysely() private readonly db: KyselyDB) {}
|
||||
|
||||
private baseFields: Array<keyof ShareAlias> = [
|
||||
'id',
|
||||
'workspaceId',
|
||||
'alias',
|
||||
'pageId',
|
||||
'creatorId',
|
||||
'createdAt',
|
||||
'updatedAt',
|
||||
];
|
||||
|
||||
/** Resolve a (normalized) alias within a workspace, or undefined. */
|
||||
async findByAliasAndWorkspace(
|
||||
alias: string,
|
||||
workspaceId: string,
|
||||
trx?: KyselyTransaction,
|
||||
): Promise<ShareAlias | undefined> {
|
||||
return dbOrTx(this.db, trx)
|
||||
.selectFrom('shareAliases')
|
||||
.select(this.baseFields)
|
||||
.where('alias', '=', alias)
|
||||
.where('workspaceId', '=', workspaceId)
|
||||
.executeTakeFirst();
|
||||
}
|
||||
|
||||
/** The alias currently pointing at a page (for the share modal). */
|
||||
async findByPageId(
|
||||
pageId: string,
|
||||
workspaceId: string,
|
||||
trx?: KyselyTransaction,
|
||||
): Promise<ShareAlias | undefined> {
|
||||
return dbOrTx(this.db, trx)
|
||||
.selectFrom('shareAliases')
|
||||
.select(this.baseFields)
|
||||
.where('pageId', '=', pageId)
|
||||
.where('workspaceId', '=', workspaceId)
|
||||
.executeTakeFirst();
|
||||
}
|
||||
|
||||
async findById(
|
||||
id: string,
|
||||
workspaceId: string,
|
||||
trx?: KyselyTransaction,
|
||||
): Promise<ShareAlias | undefined> {
|
||||
return dbOrTx(this.db, trx)
|
||||
.selectFrom('shareAliases')
|
||||
.select(this.baseFields)
|
||||
.where('id', '=', id)
|
||||
.where('workspaceId', '=', workspaceId)
|
||||
.executeTakeFirst();
|
||||
}
|
||||
|
||||
async insert(
|
||||
insertable: InsertableShareAlias,
|
||||
trx?: KyselyTransaction,
|
||||
): Promise<ShareAlias> {
|
||||
return dbOrTx(this.db, trx)
|
||||
.insertInto('shareAliases')
|
||||
.values(insertable)
|
||||
.returning(this.baseFields)
|
||||
.executeTakeFirst();
|
||||
}
|
||||
|
||||
/** Retarget an existing alias to a new page (the "swap" operation). */
|
||||
async updatePageId(
|
||||
id: string,
|
||||
pageId: string,
|
||||
workspaceId: string,
|
||||
trx?: KyselyTransaction,
|
||||
): Promise<ShareAlias> {
|
||||
return dbOrTx(this.db, trx)
|
||||
.updateTable('shareAliases')
|
||||
.set({ pageId, updatedAt: new Date() })
|
||||
.where('id', '=', id)
|
||||
.where('workspaceId', '=', workspaceId)
|
||||
.returning(this.baseFields)
|
||||
.executeTakeFirst();
|
||||
}
|
||||
|
||||
async delete(
|
||||
id: string,
|
||||
workspaceId: string,
|
||||
trx?: KyselyTransaction,
|
||||
): Promise<void> {
|
||||
await dbOrTx(this.db, trx)
|
||||
.deleteFrom('shareAliases')
|
||||
.where('id', '=', id)
|
||||
.where('workspaceId', '=', workspaceId)
|
||||
.execute();
|
||||
}
|
||||
}
|
||||
11
apps/server/src/database/types/db.d.ts
vendored
11
apps/server/src/database/types/db.d.ts
vendored
@@ -305,6 +305,16 @@ export interface Pages {
|
||||
ydoc: Buffer | null;
|
||||
}
|
||||
|
||||
export interface ShareAliases {
|
||||
alias: string;
|
||||
createdAt: Generated<Timestamp>;
|
||||
creatorId: string | null;
|
||||
id: Generated<string>;
|
||||
pageId: string | null;
|
||||
updatedAt: Generated<Timestamp>;
|
||||
workspaceId: string;
|
||||
}
|
||||
|
||||
export interface Shares {
|
||||
createdAt: Generated<Timestamp>;
|
||||
creatorId: string | null;
|
||||
@@ -674,6 +684,7 @@ export interface DB {
|
||||
pageVerifiers: PageVerifiers;
|
||||
pages: Pages;
|
||||
scimTokens: ScimTokens;
|
||||
shareAliases: ShareAliases;
|
||||
shares: Shares;
|
||||
spaceMembers: SpaceMembers;
|
||||
spaces: Spaces;
|
||||
|
||||
@@ -30,6 +30,7 @@ import {
|
||||
AuthProviders,
|
||||
AuthAccounts,
|
||||
Shares,
|
||||
ShareAliases,
|
||||
Favorites,
|
||||
FileTasks,
|
||||
UserMfa as _UserMFA,
|
||||
@@ -172,6 +173,11 @@ export type Share = Selectable<Shares>;
|
||||
export type InsertableShare = Insertable<Shares>;
|
||||
export type UpdatableShare = Updateable<Omit<Shares, 'id'>>;
|
||||
|
||||
// Share alias (vanity /l/:alias pointer)
|
||||
export type ShareAlias = Selectable<ShareAliases>;
|
||||
export type InsertableShareAlias = Insertable<ShareAliases>;
|
||||
export type UpdatableShareAlias = Updateable<Omit<ShareAliases, 'id'>>;
|
||||
|
||||
// Favorite
|
||||
export type Favorite = Selectable<Favorites>;
|
||||
export type InsertableFavorite = Insertable<Favorites>;
|
||||
|
||||
Reference in New Issue
Block a user