feat: uuid ids everywhere, pickers for linked tickets, @pi/ui checkboxes
- Postgres: every table has a uuid `id` (gen_random_uuid()); the event store keeps its relay order in `seq`, and the lookup tables their uniqueness in constraints of their own. - MongoDB: `_id` is Mongo's ObjectId; `id` is the domain id (the same uuid as the aggregate's stream in Postgres), unique, and every query goes by it. Production starts from empty databases. - Web: linking tickets and hanging one from a parent are picked from a searchable select (links across projects, parents within the project) instead of typing a key; every checkbox is @pi/ui's pi-checkbox. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -16,20 +16,20 @@ export class MongoUserRegistry extends UserRegistry {
|
||||
|
||||
async upsert(user: User): Promise<void> {
|
||||
await this.users.updateOne(
|
||||
{ _id: user.id },
|
||||
{ id: user.id },
|
||||
{ $set: { name: user.name, email: user.email, isActive: user.isActive } },
|
||||
{ upsert: true },
|
||||
);
|
||||
}
|
||||
|
||||
async findById(id: string): Promise<User | null> {
|
||||
const doc = await this.users.findById(id).lean();
|
||||
const doc = await this.users.findOne({ id }).lean();
|
||||
return doc ? this.toUser(doc) : null;
|
||||
}
|
||||
|
||||
async findByIds(ids: string[]): Promise<User[]> {
|
||||
if (ids.length === 0) return [];
|
||||
const docs = await this.users.find({ _id: { $in: ids } }).lean();
|
||||
const docs = await this.users.find({ id: { $in: ids } }).lean();
|
||||
return docs.map((d) => this.toUser(d));
|
||||
}
|
||||
|
||||
@@ -48,7 +48,7 @@ export class MongoUserRegistry extends UserRegistry {
|
||||
|
||||
private toUser(doc: UserDocument): User {
|
||||
return this.factory.restore({
|
||||
id: doc._id,
|
||||
id: doc.id,
|
||||
name: doc.name,
|
||||
email: doc.email,
|
||||
isActive: doc.isActive,
|
||||
|
||||
@@ -2,8 +2,9 @@ import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';
|
||||
|
||||
@Schema({ collection: 'users', timestamps: true, versionKey: false })
|
||||
export class UserDocument {
|
||||
@Prop({ type: String, required: true })
|
||||
_id!: string;
|
||||
/** The user's id at Pireon; `_id` is Mongo's own ObjectId. */
|
||||
@Prop({ type: String, required: true, unique: true })
|
||||
id!: string;
|
||||
|
||||
@Prop({ required: true })
|
||||
name!: string;
|
||||
|
||||
@@ -2,6 +2,7 @@ import {
|
||||
Column,
|
||||
CreateDateColumn,
|
||||
Entity,
|
||||
Generated,
|
||||
Index,
|
||||
PrimaryGeneratedColumn,
|
||||
Unique,
|
||||
@@ -13,12 +14,21 @@ import {
|
||||
*/
|
||||
@Entity('events')
|
||||
@Unique('uq_events_stream_position', ['streamId', 'position'])
|
||||
@Index('idx_events_unpublished', ['id'], { where: '"published_at" IS NULL' })
|
||||
@Index('idx_events_unpublished', ['seq'], { where: '"published_at" IS NULL' })
|
||||
export class EventEntity {
|
||||
/** Global order of the store, used to relay events in sequence. */
|
||||
@PrimaryGeneratedColumn('increment', { type: 'bigint' })
|
||||
/** The event's own id. */
|
||||
@PrimaryGeneratedColumn('uuid')
|
||||
id!: string;
|
||||
|
||||
/**
|
||||
* Global order of the store, used to relay events in sequence. A uuid has
|
||||
* no reliable order, so it is kept apart.
|
||||
*/
|
||||
@Index('uq_events_seq', { unique: true })
|
||||
@Column({ type: 'bigint' })
|
||||
@Generated('increment')
|
||||
seq!: string;
|
||||
|
||||
@Column({ name: 'stream_id', length: 64 })
|
||||
streamId!: string;
|
||||
|
||||
|
||||
@@ -73,7 +73,7 @@ export class EventStoreRelay
|
||||
.getRepository(EventEntity)
|
||||
.createQueryBuilder('e')
|
||||
.where('e.published_at IS NULL')
|
||||
.orderBy('e.id', 'ASC')
|
||||
.orderBy('e.seq', 'ASC')
|
||||
.limit(BATCH_SIZE)
|
||||
.setLock('pessimistic_write')
|
||||
.setOnLocked('skip_locked')
|
||||
|
||||
@@ -41,12 +41,14 @@ export class MongoAutomationsRepository extends AutomationsRepository {
|
||||
}
|
||||
|
||||
async findById(id: string): Promise<Automation | null> {
|
||||
const doc = await this.automations.findById(id).lean<AutomationDoc>();
|
||||
const doc = await this.automations
|
||||
.findOne({ id: id })
|
||||
.lean<AutomationDoc>();
|
||||
return doc ? (fromDoc(doc) as Automation) : null;
|
||||
}
|
||||
|
||||
async insert({ id, ...rest }: Automation): Promise<void> {
|
||||
await this.automations.create({ _id: id, ...rest });
|
||||
await this.automations.create({ id, ...rest });
|
||||
}
|
||||
|
||||
async update(
|
||||
@@ -54,16 +56,16 @@ export class MongoAutomationsRepository extends AutomationsRepository {
|
||||
changes: Partial<Pick<Automation, 'name' | 'active'>>,
|
||||
): Promise<void> {
|
||||
if (Object.keys(changes).length)
|
||||
await this.automations.updateOne({ _id: id }, { $set: changes });
|
||||
await this.automations.updateOne({ id }, { $set: changes });
|
||||
}
|
||||
|
||||
async delete(id: string): Promise<void> {
|
||||
await this.automations.deleteOne({ _id: id });
|
||||
await this.automations.deleteOne({ id });
|
||||
}
|
||||
|
||||
async claimRun(ruleId: string, eventKey: string): Promise<boolean> {
|
||||
try {
|
||||
await this.runs.create({ _id: `${ruleId}:${eventKey}`, at: new Date() });
|
||||
await this.runs.create({ id: `${ruleId}:${eventKey}`, at: new Date() });
|
||||
return true;
|
||||
} catch (error) {
|
||||
if ((error as { code?: number }).code === DUPLICATE_KEY) return false;
|
||||
@@ -73,7 +75,7 @@ export class MongoAutomationsRepository extends AutomationsRepository {
|
||||
|
||||
async recordRun(id: string, error: string | null): Promise<void> {
|
||||
await this.automations.updateOne(
|
||||
{ _id: id },
|
||||
{ id },
|
||||
{
|
||||
$inc: { runs: 1 },
|
||||
$set: { lastRunAt: new Date().toISOString(), lastError: error },
|
||||
|
||||
@@ -79,7 +79,7 @@ export class MongoFindProjectsRepository extends FindProjectsRepository {
|
||||
}
|
||||
|
||||
async findById(id: string): Promise<ProjectReadModel | null> {
|
||||
const doc = await this.projects.findById(id).lean<ProjectDoc>();
|
||||
const doc = await this.projects.findOne({ id: id }).lean<ProjectDoc>();
|
||||
return doc ? toProject(doc) : null;
|
||||
}
|
||||
|
||||
@@ -109,7 +109,7 @@ export class MongoFindProjectsRepository extends FindProjectsRepository {
|
||||
userId: string,
|
||||
): Promise<ProjectRole | null> {
|
||||
const doc = await this.projects
|
||||
.findOne({ _id: projectId, memberIds: userId }, { members: 1 })
|
||||
.findOne({ id: projectId, memberIds: userId }, { members: 1 })
|
||||
.lean<Pick<ProjectDoc, 'members'>>();
|
||||
return doc?.members.find((m) => m.user.id === userId)?.role ?? null;
|
||||
}
|
||||
@@ -124,7 +124,7 @@ export class MongoFindIssuesRepository extends FindIssuesRepository {
|
||||
}
|
||||
|
||||
async findById(id: string): Promise<IssueReadModel | null> {
|
||||
const doc = await this.issues.findById(id).lean<IssueDoc>();
|
||||
const doc = await this.issues.findOne({ id: id }).lean<IssueDoc>();
|
||||
return doc ? issueFromDoc(doc) : null;
|
||||
}
|
||||
|
||||
@@ -136,7 +136,7 @@ export class MongoFindIssuesRepository extends FindIssuesRepository {
|
||||
async findByIds(ids: string[]): Promise<IssueReadModel[]> {
|
||||
if (ids.length === 0) return [];
|
||||
const docs = await this.issues
|
||||
.find({ _id: { $in: ids } })
|
||||
.find({ id: { $in: ids } })
|
||||
.lean<IssueDoc[]>();
|
||||
return docs.map(issueFromDoc);
|
||||
}
|
||||
@@ -322,15 +322,15 @@ export class MongoFindIssuesRepository extends FindIssuesRepository {
|
||||
// Only the top of each tree: a ticket with subtasks under an epic is not one.
|
||||
const docs = await this.issues
|
||||
.find({
|
||||
_id: { $in: [...progress.keys()] },
|
||||
id: { $in: [...progress.keys()] },
|
||||
'parent.id': null,
|
||||
trashedAt: null,
|
||||
})
|
||||
.sort({ number: 1 })
|
||||
.lean<IssueDoc[]>();
|
||||
return docs.map((doc) => {
|
||||
const { _id: _ignored, ...row } = progress.get(doc._id) ?? {
|
||||
_id: doc._id,
|
||||
const { _id: _ignored, ...row } = progress.get(doc.id) ?? {
|
||||
_id: doc.id,
|
||||
total: 0,
|
||||
finished: 0,
|
||||
storyPoints: 0,
|
||||
@@ -456,7 +456,7 @@ export class MongoFindCommentsRepository extends FindCommentsRepository {
|
||||
}
|
||||
|
||||
async findById(id: string): Promise<CommentReadModel | null> {
|
||||
const doc = await this.comments.findById(id).lean<CommentDoc>();
|
||||
const doc = await this.comments.findOne({ id: id }).lean<CommentDoc>();
|
||||
return doc ? commentFromDoc(doc) : null;
|
||||
}
|
||||
|
||||
@@ -496,7 +496,7 @@ export class MongoFindReleasesRepository extends FindReleasesRepository {
|
||||
}
|
||||
|
||||
async findById(id: string): Promise<ReleaseReadModel | null> {
|
||||
const doc = await this.releases.findById(id).lean<ReleaseDoc>();
|
||||
const doc = await this.releases.findOne({ id: id }).lean<ReleaseDoc>();
|
||||
return doc ? fromDoc(doc) : null;
|
||||
}
|
||||
|
||||
|
||||
@@ -21,7 +21,7 @@ export class MongoGitIntegrationsRepository extends GitIntegrationsRepository {
|
||||
|
||||
async repositoriesOf(projectId: string): Promise<GitRepositoryConfig[]> {
|
||||
const doc = await this.integrations
|
||||
.findById(projectId)
|
||||
.findOne({ id: projectId })
|
||||
.lean<GitIntegrationDoc>();
|
||||
if (!doc) return [];
|
||||
if (doc.repositories) return doc.repositories;
|
||||
@@ -44,11 +44,11 @@ export class MongoGitIntegrationsRepository extends GitIntegrationsRepository {
|
||||
repositories: GitRepositoryConfig[],
|
||||
): Promise<void> {
|
||||
if (!repositories.length) {
|
||||
await this.integrations.deleteOne({ _id: projectId });
|
||||
await this.integrations.deleteOne({ id: projectId });
|
||||
return;
|
||||
}
|
||||
await this.integrations.updateOne(
|
||||
{ _id: projectId },
|
||||
{ id: projectId },
|
||||
{ $set: { repositories }, $unset: { secret: '', createdAt: '' } },
|
||||
{ upsert: true },
|
||||
);
|
||||
|
||||
@@ -26,7 +26,7 @@ export class MongoNotificationsRepository extends NotificationsRepository {
|
||||
|
||||
async insert({ id, ...rest }: NotificationReadModel): Promise<boolean> {
|
||||
const result = await this.notifications.updateOne(
|
||||
{ _id: id },
|
||||
{ id },
|
||||
{ $setOnInsert: rest },
|
||||
{ upsert: true },
|
||||
);
|
||||
@@ -51,21 +51,21 @@ export class MongoNotificationsRepository extends NotificationsRepository {
|
||||
|
||||
async markRead(userId: string, ids: string[] | null): Promise<void> {
|
||||
await this.notifications.updateMany(
|
||||
{ userId, readAt: null, ...(ids ? { _id: { $in: ids } } : {}) },
|
||||
{ userId, readAt: null, ...(ids ? { id: { $in: ids } } : {}) },
|
||||
{ $set: { readAt: new Date().toISOString() } },
|
||||
);
|
||||
}
|
||||
|
||||
async settings(userId: string): Promise<NotificationSettingsReadModel> {
|
||||
const doc = await this.settingsDocs
|
||||
.findById(userId)
|
||||
.findOne({ id: userId })
|
||||
.lean<NotificationSettingsDoc>();
|
||||
return { userId, email: doc?.email ?? false };
|
||||
}
|
||||
|
||||
async saveSettings(settings: NotificationSettingsReadModel): Promise<void> {
|
||||
await this.settingsDocs.updateOne(
|
||||
{ _id: settings.userId },
|
||||
{ id: settings.userId },
|
||||
{ $set: { email: settings.email } },
|
||||
{ upsert: true },
|
||||
);
|
||||
|
||||
@@ -33,12 +33,14 @@ export class MongoOutgoingWebhooksRepository extends OutgoingWebhooksRepository
|
||||
}
|
||||
|
||||
async findById(id: string): Promise<OutgoingWebhook | null> {
|
||||
const doc = await this.webhooks.findById(id).lean<OutgoingWebhookDoc>();
|
||||
const doc = await this.webhooks
|
||||
.findOne({ id: id })
|
||||
.lean<OutgoingWebhookDoc>();
|
||||
return doc ? (fromDoc(doc) as OutgoingWebhook) : null;
|
||||
}
|
||||
|
||||
async insert({ id, ...rest }: OutgoingWebhook): Promise<void> {
|
||||
await this.webhooks.create({ _id: id, ...rest });
|
||||
await this.webhooks.create({ id, ...rest });
|
||||
}
|
||||
|
||||
async update(
|
||||
@@ -46,17 +48,14 @@ export class MongoOutgoingWebhooksRepository extends OutgoingWebhooksRepository
|
||||
changes: Partial<Pick<OutgoingWebhook, 'url' | 'events' | 'active'>>,
|
||||
): Promise<void> {
|
||||
if (Object.keys(changes).length)
|
||||
await this.webhooks.updateOne({ _id: id }, { $set: changes });
|
||||
await this.webhooks.updateOne({ id }, { $set: changes });
|
||||
}
|
||||
|
||||
async delete(id: string): Promise<void> {
|
||||
await this.webhooks.deleteOne({ _id: id });
|
||||
await this.webhooks.deleteOne({ id });
|
||||
}
|
||||
|
||||
async recordDelivery(id: string, delivery: WebhookDelivery): Promise<void> {
|
||||
await this.webhooks.updateOne(
|
||||
{ _id: id },
|
||||
{ $set: { lastDelivery: delivery } },
|
||||
);
|
||||
await this.webhooks.updateOne({ id }, { $set: { lastDelivery: delivery } });
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,13 +18,13 @@ export class RankBackfill implements OnApplicationBootstrap {
|
||||
|
||||
async onApplicationBootstrap(): Promise<void> {
|
||||
const missing = await this.issues
|
||||
.find({ rank: { $exists: false } }, { number: 1 })
|
||||
.lean<Pick<IssueDoc, '_id' | 'number'>[]>();
|
||||
.find({ rank: { $exists: false } }, { id: 1, number: 1 })
|
||||
.lean<Pick<IssueDoc, 'id' | 'number'>[]>();
|
||||
if (missing.length === 0) return;
|
||||
const result = await this.issues.bulkWrite(
|
||||
missing.map((doc) => ({
|
||||
updateOne: {
|
||||
filter: { _id: doc._id, rank: { $exists: false } },
|
||||
filter: { id: doc.id, rank: { $exists: false } },
|
||||
update: { $set: { rank: implicitRank(doc.number) } },
|
||||
},
|
||||
})),
|
||||
|
||||
@@ -15,11 +15,16 @@ import {
|
||||
SprintReadModel,
|
||||
implicitRank,
|
||||
} from '@task-stream/api/tracker/domain';
|
||||
import { Schema } from 'mongoose';
|
||||
import { Schema, Types } from 'mongoose';
|
||||
|
||||
/* Read model (MongoDB): denormalised documents shaped for the GraphQL queries. */
|
||||
|
||||
type Doc<T extends { id: string }> = Omit<T, 'id'> & { _id: string };
|
||||
/**
|
||||
* Each document keeps the domain id (a uuid, the same one the event store
|
||||
* uses) in `id`, unique; `_id` is Mongo's own ObjectId and means nothing
|
||||
* outside the database.
|
||||
*/
|
||||
type Doc<T extends { id: string }> = T & { _id?: Types.ObjectId };
|
||||
|
||||
export type ProjectDoc = Doc<ProjectReadModel> & { memberIds: string[] };
|
||||
/** Links are split: outward ones come from the issue itself, inward ones from other issues' events. */
|
||||
@@ -34,11 +39,11 @@ export type ActivityDoc = Doc<ActivityReadModel>;
|
||||
export type TimelineDoc = Doc<IssueTimelineEntry>;
|
||||
export type NotificationDoc = Doc<NotificationReadModel>;
|
||||
export type SavedFilterDoc = Doc<SavedFilterReadModel>;
|
||||
export type OutgoingWebhookDoc = Omit<OutgoingWebhook, 'id'> & { _id: string };
|
||||
export type AutomationDoc = Omit<Automation, 'id'> & { _id: string };
|
||||
export type OutgoingWebhookDoc = Doc<OutgoingWebhook>;
|
||||
export type AutomationDoc = Doc<Automation>;
|
||||
export interface AutomationRunDoc {
|
||||
/** `ruleId:eventKey`: a rule runs once per event. */
|
||||
_id: string;
|
||||
id: string;
|
||||
at: Date;
|
||||
}
|
||||
export interface GitRepositoryDoc {
|
||||
@@ -52,13 +57,15 @@ export interface GitRepositoryDoc {
|
||||
* kept: it reads as a repository named "principal" until the list is saved.
|
||||
*/
|
||||
export interface GitIntegrationDoc {
|
||||
_id: string;
|
||||
/** The project's id. */
|
||||
id: string;
|
||||
repositories?: GitRepositoryDoc[];
|
||||
secret?: string;
|
||||
createdAt?: string;
|
||||
}
|
||||
export interface NotificationSettingsDoc {
|
||||
_id: string;
|
||||
/** The user's id. */
|
||||
id: string;
|
||||
email: boolean;
|
||||
}
|
||||
|
||||
@@ -88,11 +95,13 @@ const link = {
|
||||
issueId: String,
|
||||
issueKey: String,
|
||||
};
|
||||
const loose = { strict: false, versionKey: false, _id: false } as const;
|
||||
const loose = { strict: false, versionKey: false } as const;
|
||||
/** The domain id: what every query looks a document up by. */
|
||||
const domainId = { type: String, required: true, unique: true };
|
||||
|
||||
export const ProjectSchema = new Schema<ProjectDoc>(
|
||||
{
|
||||
_id: String,
|
||||
id: domainId,
|
||||
key: { type: String, unique: true },
|
||||
isPublic: { type: Boolean, index: true },
|
||||
memberIds: { type: [String], index: true },
|
||||
@@ -105,7 +114,7 @@ export const ProjectSchema = new Schema<ProjectDoc>(
|
||||
|
||||
export const IssueSchema = new Schema<IssueDoc>(
|
||||
{
|
||||
_id: String,
|
||||
id: domainId,
|
||||
projectId: { type: String, index: true },
|
||||
key: { type: String, unique: true },
|
||||
status: String,
|
||||
@@ -149,7 +158,7 @@ IssueSchema.index({ title: 'text', description: 'text', key: 'text' });
|
||||
|
||||
export const CommentSchema = new Schema<CommentDoc>(
|
||||
{
|
||||
_id: String,
|
||||
id: domainId,
|
||||
issueId: { type: String, index: true },
|
||||
replyTo: { type: String, index: true },
|
||||
author: userRef,
|
||||
@@ -160,13 +169,13 @@ export const CommentSchema = new Schema<CommentDoc>(
|
||||
CommentSchema.index({ issueId: 1, createdAt: 1 });
|
||||
|
||||
export const ReleaseSchema = new Schema<ReleaseDoc>(
|
||||
{ _id: String, projectId: { type: String, index: true }, version: Number },
|
||||
{ id: domainId, projectId: { type: String, index: true }, version: Number },
|
||||
{ ...loose, collection: 'releases_view' },
|
||||
);
|
||||
|
||||
export const SprintSchema = new Schema<SprintDoc>(
|
||||
{
|
||||
_id: String,
|
||||
id: domainId,
|
||||
projectId: { type: String, index: true },
|
||||
status: String,
|
||||
version: Number,
|
||||
@@ -176,7 +185,7 @@ export const SprintSchema = new Schema<SprintDoc>(
|
||||
|
||||
export const ActivitySchema = new Schema<ActivityDoc>(
|
||||
{
|
||||
_id: String,
|
||||
id: domainId,
|
||||
projectId: String,
|
||||
issueId: String,
|
||||
actor: userRef,
|
||||
@@ -188,14 +197,14 @@ ActivitySchema.index({ issueId: 1, occurredAt: -1 });
|
||||
|
||||
/** Every status and release step of every ticket (statistics). */
|
||||
export const TimelineSchema = new Schema<TimelineDoc>(
|
||||
{ _id: String, projectId: String, issueId: String, at: String },
|
||||
{ id: domainId, projectId: String, issueId: String, at: String },
|
||||
{ ...loose, collection: 'issue_timeline' },
|
||||
);
|
||||
TimelineSchema.index({ projectId: 1, at: 1 });
|
||||
|
||||
export const NotificationSchema = new Schema<NotificationDoc>(
|
||||
{
|
||||
_id: String,
|
||||
id: domainId,
|
||||
userId: String,
|
||||
readAt: String,
|
||||
createdAt: String,
|
||||
@@ -207,13 +216,13 @@ NotificationSchema.index({ userId: 1, createdAt: -1 });
|
||||
NotificationSchema.index({ userId: 1, readAt: 1 });
|
||||
|
||||
export const NotificationSettingsSchema = new Schema<NotificationSettingsDoc>(
|
||||
{ _id: String, email: Boolean },
|
||||
{ id: domainId, email: Boolean },
|
||||
{ ...loose, collection: 'notification_settings' },
|
||||
);
|
||||
|
||||
export const SavedFilterSchema = new Schema<SavedFilterDoc>(
|
||||
{
|
||||
_id: String,
|
||||
id: domainId,
|
||||
projectId: String,
|
||||
owner: userRef,
|
||||
name: String,
|
||||
@@ -226,13 +235,13 @@ SavedFilterSchema.index({ projectId: 1, 'owner.id': 1 });
|
||||
SavedFilterSchema.index({ projectId: 1, shared: 1 });
|
||||
|
||||
export const GitIntegrationSchema = new Schema<GitIntegrationDoc>(
|
||||
{ _id: String, repositories: [Object], secret: String, createdAt: String },
|
||||
{ id: domainId, repositories: [Object], secret: String, createdAt: String },
|
||||
{ ...loose, collection: 'git_integrations' },
|
||||
);
|
||||
|
||||
export const OutgoingWebhookSchema = new Schema<OutgoingWebhookDoc>(
|
||||
{
|
||||
_id: String,
|
||||
id: domainId,
|
||||
projectId: { type: String, index: true },
|
||||
active: Boolean,
|
||||
events: [String],
|
||||
@@ -244,7 +253,7 @@ export const OutgoingWebhookSchema = new Schema<OutgoingWebhookDoc>(
|
||||
|
||||
export const AutomationSchema = new Schema<AutomationDoc>(
|
||||
{
|
||||
_id: String,
|
||||
id: domainId,
|
||||
projectId: { type: String, index: true },
|
||||
active: Boolean,
|
||||
createdBy: userRef,
|
||||
@@ -254,7 +263,7 @@ export const AutomationSchema = new Schema<AutomationDoc>(
|
||||
);
|
||||
|
||||
export const AutomationRunSchema = new Schema<AutomationRunDoc>(
|
||||
{ _id: String, at: Date },
|
||||
{ id: domainId, at: Date },
|
||||
{ ...loose, collection: 'automation_runs' },
|
||||
);
|
||||
// Redeliveries happen within minutes; a month of memory is plenty.
|
||||
@@ -277,17 +286,17 @@ export const READ_MODEL_SCHEMAS = [
|
||||
{ name: ReadModels.SavedFilter, schema: SavedFilterSchema },
|
||||
];
|
||||
|
||||
export function fromDoc<T extends { _id: string }>(
|
||||
doc: T,
|
||||
): Omit<T, '_id'> & { id: string } {
|
||||
const { _id, ...rest } = doc;
|
||||
return { id: _id, ...rest };
|
||||
/** A document as the model it holds: Mongo's own `_id` left out. */
|
||||
export function fromDoc<T extends { id: string }>(
|
||||
doc: T & { _id?: unknown },
|
||||
): T {
|
||||
const { _id: _ignored, ...rest } = doc;
|
||||
return rest as T;
|
||||
}
|
||||
|
||||
export function issueFromDoc(doc: IssueDoc): IssueReadModel {
|
||||
const { _id, outwardLinks, inwardLinks, ...rest } = doc;
|
||||
const { _id: _ignored, outwardLinks, inwardLinks, ...rest } = doc;
|
||||
return {
|
||||
id: _id,
|
||||
...rest,
|
||||
links: [...(outwardLinks ?? []), ...(inwardLinks ?? [])],
|
||||
// Tickets projected before attachments existed have none.
|
||||
|
||||
@@ -26,7 +26,7 @@ export class MongoSavedFiltersRepository extends SavedFiltersRepository {
|
||||
}
|
||||
|
||||
async findById(id: string): Promise<SavedFilterReadModel | null> {
|
||||
const doc = await this.filters.findById(id).lean<SavedFilterDoc>();
|
||||
const doc = await this.filters.findOne({ id: id }).lean<SavedFilterDoc>();
|
||||
return doc ? toFilter(doc) : null;
|
||||
}
|
||||
|
||||
@@ -35,11 +35,11 @@ export class MongoSavedFiltersRepository extends SavedFiltersRepository {
|
||||
}
|
||||
|
||||
async insert({ id, ...rest }: SavedFilterReadModel): Promise<void> {
|
||||
await this.filters.create({ _id: id, ...rest });
|
||||
await this.filters.create({ id, ...rest });
|
||||
}
|
||||
|
||||
async delete(id: string): Promise<void> {
|
||||
await this.filters.deleteOne({ _id: id });
|
||||
await this.filters.deleteOne({ id });
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -24,7 +24,7 @@ export class MongoFindSprintsRepository extends FindSprintsRepository {
|
||||
}
|
||||
|
||||
async findById(id: string): Promise<SprintReadModel | null> {
|
||||
const doc = await this.sprints.findById(id).lean<SprintDoc>();
|
||||
const doc = await this.sprints.findOne({ id: id }).lean<SprintDoc>();
|
||||
return doc ? fromDoc(doc) : null;
|
||||
}
|
||||
|
||||
@@ -67,6 +67,6 @@ export class MongoUpsertMaterializedSprintRepository extends UpsertMaterializedS
|
||||
}
|
||||
|
||||
async remove(sprintId: string) {
|
||||
await this.sprints.deleteOne({ _id: sprintId });
|
||||
await this.sprints.deleteOne({ id: sprintId });
|
||||
}
|
||||
}
|
||||
|
||||
@@ -30,11 +30,11 @@ export class StatusCategoryBackfill implements OnApplicationBootstrap {
|
||||
|
||||
const projectIds = [...new Set(groups.map((g) => g._id.projectId))];
|
||||
const projects = await this.projects
|
||||
.find({ _id: { $in: projectIds } }, { settings: 1 })
|
||||
.lean<Pick<ProjectDoc, '_id' | 'settings'>[]>();
|
||||
.find({ id: { $in: projectIds } }, { id: 1, settings: 1 })
|
||||
.lean<Pick<ProjectDoc, 'id' | 'settings'>[]>();
|
||||
const workflows = new Map(
|
||||
projects.map((p) => [
|
||||
p._id,
|
||||
p.id,
|
||||
p.settings
|
||||
? new ProjectWorkflow(p.settings)
|
||||
: ProjectWorkflow.default(),
|
||||
|
||||
@@ -4,7 +4,7 @@ import { Model } from 'mongoose';
|
||||
* Idempotent upsert: only applies a change carrying a newer aggregate
|
||||
* version (event position). Redelivered events become no-ops.
|
||||
*/
|
||||
export async function upsertIfNewer<T extends { _id: string }>(
|
||||
export async function upsertIfNewer<T extends { id: string }>(
|
||||
model: Model<T>,
|
||||
id: string,
|
||||
version: number,
|
||||
@@ -13,13 +13,13 @@ export async function upsertIfNewer<T extends { _id: string }>(
|
||||
): Promise<boolean> {
|
||||
try {
|
||||
const result = await model.updateOne(
|
||||
{ _id: id, version: { $lt: version } } as never,
|
||||
{ id, version: { $lt: version } } as never,
|
||||
{ $set: { ...set, version }, $setOnInsert: setOnInsert } as never,
|
||||
{ upsert: true },
|
||||
);
|
||||
return result.modifiedCount + result.upsertedCount > 0;
|
||||
} catch (error) {
|
||||
// Duplicate _id on upsert: the stored document is already newer.
|
||||
// Duplicate id on upsert: the stored document is already newer.
|
||||
if ((error as { code?: number }).code === 11000) return false;
|
||||
throw error;
|
||||
}
|
||||
|
||||
@@ -64,12 +64,12 @@ export class MongoUpsertMaterializedProjectRepository extends UpsertMaterialized
|
||||
|
||||
async remove(projectId: string) {
|
||||
await Promise.all([
|
||||
this.projects.deleteOne({ _id: projectId }),
|
||||
this.projects.deleteOne({ id: projectId }),
|
||||
this.activity.deleteMany({ projectId }),
|
||||
this.timeline.deleteMany({ projectId }),
|
||||
// Views of the board of a project that is gone.
|
||||
this.savedFilters.deleteMany({ projectId }),
|
||||
this.gitIntegrations.deleteOne({ _id: projectId }),
|
||||
this.gitIntegrations.deleteOne({ id: projectId }),
|
||||
this.outgoingWebhooks.deleteMany({ projectId }),
|
||||
this.automations.deleteMany({ projectId }),
|
||||
]);
|
||||
@@ -102,7 +102,7 @@ export class MongoUpsertMaterializedIssueRepository extends UpsertMaterializedIs
|
||||
|
||||
async remove(issueId: string) {
|
||||
await Promise.all([
|
||||
this.issues.deleteOne({ _id: issueId }),
|
||||
this.issues.deleteOne({ id: issueId }),
|
||||
this.issues.updateMany(
|
||||
{
|
||||
$or: [
|
||||
@@ -138,21 +138,21 @@ export class MongoUpsertMaterializedIssueRepository extends UpsertMaterializedIs
|
||||
|
||||
async addWatcher(issueId: string, user: UserRefReadModel) {
|
||||
await this.issues.updateOne(
|
||||
{ _id: issueId, 'watchers.id': { $ne: user.id } },
|
||||
{ id: issueId, 'watchers.id': { $ne: user.id } },
|
||||
{ $push: { watchers: user } },
|
||||
);
|
||||
}
|
||||
|
||||
async removeWatcher(issueId: string, userId: string) {
|
||||
await this.issues.updateOne(
|
||||
{ _id: issueId },
|
||||
{ id: issueId },
|
||||
{ $pull: { watchers: { id: userId } } },
|
||||
);
|
||||
}
|
||||
|
||||
async addChecklistItem(issueId: string, item: ChecklistItemReadModel) {
|
||||
await this.issues.updateOne(
|
||||
{ _id: issueId, 'checklist.id': { $ne: item.id } },
|
||||
{ id: issueId, 'checklist.id': { $ne: item.id } },
|
||||
{ $push: { checklist: item } },
|
||||
);
|
||||
}
|
||||
@@ -170,14 +170,14 @@ export class MongoUpsertMaterializedIssueRepository extends UpsertMaterializedIs
|
||||
);
|
||||
if (Object.keys(set).length === 0) return;
|
||||
await this.issues.updateOne(
|
||||
{ _id: issueId, 'checklist.id': itemId },
|
||||
{ id: issueId, 'checklist.id': itemId },
|
||||
{ $set: set },
|
||||
);
|
||||
}
|
||||
|
||||
async removeChecklistItem(issueId: string, itemId: string) {
|
||||
await this.issues.updateOne(
|
||||
{ _id: issueId },
|
||||
{ id: issueId },
|
||||
{ $pull: { checklist: { id: itemId } } },
|
||||
);
|
||||
}
|
||||
@@ -185,11 +185,11 @@ export class MongoUpsertMaterializedIssueRepository extends UpsertMaterializedIs
|
||||
async putDevLink(issueId: string, link: DevLink) {
|
||||
// Same URL: the newer report replaces the older one.
|
||||
await this.issues.updateOne(
|
||||
{ _id: issueId },
|
||||
{ id: issueId },
|
||||
{ $pull: { devLinks: { url: link.url } } },
|
||||
);
|
||||
await this.issues.updateOne(
|
||||
{ _id: issueId },
|
||||
{ id: issueId },
|
||||
{ $push: { devLinks: { $each: [link], $slice: -MAX_DEV_LINKS } } },
|
||||
);
|
||||
}
|
||||
@@ -203,14 +203,14 @@ export class MongoUpsertMaterializedIssueRepository extends UpsertMaterializedIs
|
||||
|
||||
async addAttachment(issueId: string, attachment: AttachmentReadModel) {
|
||||
await this.issues.updateOne(
|
||||
{ _id: issueId, 'attachments.id': { $ne: attachment.id } },
|
||||
{ id: issueId, 'attachments.id': { $ne: attachment.id } },
|
||||
{ $push: { attachments: attachment } },
|
||||
);
|
||||
}
|
||||
|
||||
async removeAttachment(issueId: string, attachmentId: string) {
|
||||
await this.issues.updateOne(
|
||||
{ _id: issueId },
|
||||
{ id: issueId },
|
||||
{ $pull: { attachments: { id: attachmentId } } },
|
||||
);
|
||||
}
|
||||
@@ -219,7 +219,7 @@ export class MongoUpsertMaterializedIssueRepository extends UpsertMaterializedIs
|
||||
const field = link.direction === 'OUTWARD' ? 'outwardLinks' : 'inwardLinks';
|
||||
await this.issues.updateOne(
|
||||
{
|
||||
_id: issueId,
|
||||
id: issueId,
|
||||
[field]: {
|
||||
$not: { $elemMatch: { type: link.type, issueId: link.issueId } },
|
||||
},
|
||||
@@ -234,13 +234,13 @@ export class MongoUpsertMaterializedIssueRepository extends UpsertMaterializedIs
|
||||
) {
|
||||
const field = link.direction === 'OUTWARD' ? 'outwardLinks' : 'inwardLinks';
|
||||
await this.issues.updateOne(
|
||||
{ _id: issueId },
|
||||
{ id: issueId },
|
||||
{ $pull: { [field]: { type: link.type, issueId: link.issueId } } },
|
||||
);
|
||||
}
|
||||
|
||||
async setCommentCount(issueId: string, commentCount: number) {
|
||||
await this.issues.updateOne({ _id: issueId }, { $set: { commentCount } });
|
||||
await this.issues.updateOne({ id: issueId }, { $set: { commentCount } });
|
||||
}
|
||||
|
||||
async recategorize(
|
||||
@@ -289,7 +289,7 @@ export class MongoUpsertMaterializedCommentRepository extends UpsertMaterialized
|
||||
}
|
||||
|
||||
async remove(id: string) {
|
||||
await this.comments.deleteOne({ _id: id });
|
||||
await this.comments.deleteOne({ id });
|
||||
}
|
||||
}
|
||||
|
||||
@@ -311,7 +311,7 @@ export class MongoUpsertMaterializedReleaseRepository extends UpsertMaterialized
|
||||
}
|
||||
|
||||
async remove(releaseId: string) {
|
||||
await this.releases.deleteOne({ _id: releaseId });
|
||||
await this.releases.deleteOne({ id: releaseId });
|
||||
}
|
||||
}
|
||||
|
||||
@@ -326,7 +326,7 @@ export class MongoUpsertMaterializedActivityRepository extends UpsertMaterialize
|
||||
|
||||
async insert({ id, ...fields }: ActivityReadModel): Promise<boolean> {
|
||||
const result = await this.activity.updateOne(
|
||||
{ _id: id },
|
||||
{ id },
|
||||
{ $setOnInsert: fields },
|
||||
{ upsert: true },
|
||||
);
|
||||
@@ -345,7 +345,7 @@ export class MongoUpsertMaterializedTimelineRepository extends UpsertMaterialize
|
||||
|
||||
async record({ id, ...fields }: IssueTimelineEntry): Promise<void> {
|
||||
await this.timeline.updateOne(
|
||||
{ _id: id },
|
||||
{ id },
|
||||
{ $setOnInsert: fields },
|
||||
{ upsert: true },
|
||||
);
|
||||
|
||||
@@ -1,13 +1,18 @@
|
||||
import { Column, Entity, PrimaryColumn } from 'typeorm';
|
||||
import { Column, Entity, Index, PrimaryGeneratedColumn, Unique } from 'typeorm';
|
||||
|
||||
/*
|
||||
* Write-side lookup tables. The event store holds the truth; these only
|
||||
* enforce uniqueness and sequences that cannot be checked per aggregate.
|
||||
* Each row has a uuid `id`; what makes it unique is a constraint apart.
|
||||
*/
|
||||
|
||||
@Entity('project_keys')
|
||||
export class ProjectKeyEntity {
|
||||
@PrimaryColumn({ length: 10 })
|
||||
@PrimaryGeneratedColumn('uuid')
|
||||
id!: string;
|
||||
|
||||
@Index('uq_project_keys_key', { unique: true })
|
||||
@Column({ length: 10 })
|
||||
key!: string;
|
||||
|
||||
@Column({ name: 'project_id', length: 64 })
|
||||
@@ -16,7 +21,12 @@ export class ProjectKeyEntity {
|
||||
|
||||
@Entity('issue_sequences')
|
||||
export class IssueSequenceEntity {
|
||||
@PrimaryColumn({ name: 'project_id', length: 64 })
|
||||
@PrimaryGeneratedColumn('uuid')
|
||||
id!: string;
|
||||
|
||||
/** `ON CONFLICT (project_id)` counts on it. */
|
||||
@Index('uq_issue_sequences_project', { unique: true })
|
||||
@Column({ name: 'project_id', length: 64 })
|
||||
projectId!: string;
|
||||
|
||||
@Column({ name: 'last_number', type: 'int', default: 0 })
|
||||
@@ -24,11 +34,15 @@ export class IssueSequenceEntity {
|
||||
}
|
||||
|
||||
@Entity('release_names')
|
||||
@Unique('uq_release_names_project_name', ['projectId', 'name'])
|
||||
export class ReleaseNameEntity {
|
||||
@PrimaryColumn({ name: 'project_id', length: 64 })
|
||||
@PrimaryGeneratedColumn('uuid')
|
||||
id!: string;
|
||||
|
||||
@Column({ name: 'project_id', length: 64 })
|
||||
projectId!: string;
|
||||
|
||||
@PrimaryColumn({ length: 60 })
|
||||
@Column({ length: 60 })
|
||||
name!: string;
|
||||
|
||||
@Column({ name: 'release_id', length: 64 })
|
||||
|
||||
Reference in New Issue
Block a user