@airnauts/comments-adapter-mongo 0.1.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/LICENSE +21 -0
- package/README.md +24 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +182 -0
- package/dist/index.js.map +1 -0
- package/dist/indexes.d.ts +8 -0
- package/dist/indexes.d.ts.map +1 -0
- package/dist/repository.d.ts +22 -0
- package/dist/repository.d.ts.map +1 -0
- package/package.json +55 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Airnauts
|
|
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
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
# @airnauts/comments-adapter-mongo
|
|
2
|
+
|
|
3
|
+
MongoDB repository adapter for the [Airnauts commenting tool](https://github.com/Airnauts/commenting-tool) server.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
pnpm add @airnauts/comments-adapter-mongo mongodb
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
```ts
|
|
14
|
+
import { createMongoRepository, ensureIndexes } from '@airnauts/comments-adapter-mongo'
|
|
15
|
+
|
|
16
|
+
await ensureIndexes(db) // once at startup
|
|
17
|
+
const repository = createMongoRepository({ db })
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
Pass `repository` to `createCommentsServer` from `@airnauts/comments-server`.
|
|
21
|
+
|
|
22
|
+
## License
|
|
23
|
+
|
|
24
|
+
MIT © Airnauts
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,WAAW,CAAA;AACzC,OAAO,EAAE,qBAAqB,EAAE,eAAe,EAAE,MAAM,cAAc,CAAA"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
// src/repository.ts
|
|
2
|
+
import {
|
|
3
|
+
decodeCursor,
|
|
4
|
+
encodeCursor,
|
|
5
|
+
lazyRepository
|
|
6
|
+
} from "@airnauts/comments-server";
|
|
7
|
+
import { MongoClient } from "mongodb";
|
|
8
|
+
var COLLECTION = "threads";
|
|
9
|
+
var ATTACHMENTS_COLLECTION = "attachments";
|
|
10
|
+
function toAttachment(doc) {
|
|
11
|
+
const { _id, projectId: _p, env: _e, ...rest } = doc;
|
|
12
|
+
return { id: _id, ...rest };
|
|
13
|
+
}
|
|
14
|
+
function scopeFilter(scope) {
|
|
15
|
+
return { projectId: scope.projectId, env: scope.env ?? null };
|
|
16
|
+
}
|
|
17
|
+
function unresolvedCountOf(status) {
|
|
18
|
+
return status === "open" ? 1 : 0;
|
|
19
|
+
}
|
|
20
|
+
function toThread(doc) {
|
|
21
|
+
const { _id, projectId: _p, env: _e, ...rest } = doc;
|
|
22
|
+
return { id: _id, ...rest };
|
|
23
|
+
}
|
|
24
|
+
function toListItem(doc) {
|
|
25
|
+
const {
|
|
26
|
+
_id,
|
|
27
|
+
projectId: _p,
|
|
28
|
+
env: _e,
|
|
29
|
+
comments: _c,
|
|
30
|
+
captureContext: _cc,
|
|
31
|
+
provenance: _pr,
|
|
32
|
+
...rest
|
|
33
|
+
} = doc;
|
|
34
|
+
return { id: _id, ...rest };
|
|
35
|
+
}
|
|
36
|
+
function createMongoRepository({ db }) {
|
|
37
|
+
const col = db.collection(COLLECTION);
|
|
38
|
+
const attachmentsCol = db.collection(ATTACHMENTS_COLLECTION);
|
|
39
|
+
return {
|
|
40
|
+
async createThread(input) {
|
|
41
|
+
const doc = {
|
|
42
|
+
_id: input.id,
|
|
43
|
+
projectId: input.projectId,
|
|
44
|
+
env: input.env ?? null,
|
|
45
|
+
scope: input.scope,
|
|
46
|
+
pageKey: input.pageKey,
|
|
47
|
+
pageUrl: input.pageUrl,
|
|
48
|
+
...input.pageTitle !== void 0 ? { pageTitle: input.pageTitle } : {},
|
|
49
|
+
anchor: input.anchor,
|
|
50
|
+
status: input.status,
|
|
51
|
+
anchorState: input.anchorState,
|
|
52
|
+
...input.selectionLost !== void 0 ? { selectionLost: input.selectionLost } : {},
|
|
53
|
+
commentCount: 1,
|
|
54
|
+
unresolvedCount: unresolvedCountOf(input.status),
|
|
55
|
+
createdBy: input.createdBy,
|
|
56
|
+
createdAt: input.createdAt,
|
|
57
|
+
updatedAt: input.updatedAt,
|
|
58
|
+
lastActivityAt: input.lastActivityAt,
|
|
59
|
+
schemaVersion: input.schemaVersion,
|
|
60
|
+
comments: [input.firstComment],
|
|
61
|
+
captureContext: input.captureContext,
|
|
62
|
+
...input.provenance !== void 0 ? { provenance: input.provenance } : {}
|
|
63
|
+
};
|
|
64
|
+
await col.insertOne(doc);
|
|
65
|
+
return toThread(doc);
|
|
66
|
+
},
|
|
67
|
+
async getThread(scope, id) {
|
|
68
|
+
const doc = await col.findOne({ _id: id, ...scopeFilter(scope) });
|
|
69
|
+
return doc ? toThread(doc) : null;
|
|
70
|
+
},
|
|
71
|
+
async listThreads(query) {
|
|
72
|
+
const limit = Math.max(1, Math.min(query.limit, 200));
|
|
73
|
+
const filter = scopeFilter(query);
|
|
74
|
+
if (query.pageKey !== void 0) filter.pageKey = query.pageKey;
|
|
75
|
+
if (query.status !== void 0) filter.status = query.status;
|
|
76
|
+
const cursor = query.cursor ? decodeCursor(query.cursor) : void 0;
|
|
77
|
+
if (cursor) {
|
|
78
|
+
filter.$or = [
|
|
79
|
+
{ updatedAt: { $lt: cursor.updatedAt } },
|
|
80
|
+
{ updatedAt: cursor.updatedAt, _id: { $lt: cursor.id } }
|
|
81
|
+
];
|
|
82
|
+
}
|
|
83
|
+
const docs = await col.find(filter, {
|
|
84
|
+
projection: { comments: 0, captureContext: 0, provenance: 0 }
|
|
85
|
+
}).sort({ updatedAt: -1, _id: -1 }).limit(limit + 1).toArray();
|
|
86
|
+
const more = docs.length > limit;
|
|
87
|
+
const page = more ? docs.slice(0, limit) : docs;
|
|
88
|
+
const last = page[page.length - 1];
|
|
89
|
+
const nextCursor = more && last ? encodeCursor({ updatedAt: last.updatedAt, id: last._id }) : null;
|
|
90
|
+
return { threads: page.map((d) => toListItem(d)), nextCursor };
|
|
91
|
+
},
|
|
92
|
+
async addComment(scope, threadId, comment) {
|
|
93
|
+
const res = await col.updateOne(
|
|
94
|
+
{ _id: threadId, ...scopeFilter(scope) },
|
|
95
|
+
{
|
|
96
|
+
$push: { comments: comment },
|
|
97
|
+
$inc: { commentCount: 1 },
|
|
98
|
+
$set: { updatedAt: comment.createdAt, lastActivityAt: comment.createdAt }
|
|
99
|
+
}
|
|
100
|
+
);
|
|
101
|
+
if (res.matchedCount === 0) throw new Error("thread not found");
|
|
102
|
+
return comment;
|
|
103
|
+
},
|
|
104
|
+
async setStatus(scope, threadId, status, now) {
|
|
105
|
+
const doc = await col.findOneAndUpdate(
|
|
106
|
+
{ _id: threadId, ...scopeFilter(scope) },
|
|
107
|
+
{
|
|
108
|
+
$set: {
|
|
109
|
+
status,
|
|
110
|
+
updatedAt: now,
|
|
111
|
+
lastActivityAt: now,
|
|
112
|
+
unresolvedCount: unresolvedCountOf(status)
|
|
113
|
+
}
|
|
114
|
+
},
|
|
115
|
+
{ returnDocument: "after" }
|
|
116
|
+
);
|
|
117
|
+
if (!doc) throw new Error("thread not found");
|
|
118
|
+
return toThread(doc);
|
|
119
|
+
},
|
|
120
|
+
async updateAnchor(scope, threadId, patch, now) {
|
|
121
|
+
const set = {
|
|
122
|
+
anchorState: patch.anchorState,
|
|
123
|
+
updatedAt: now,
|
|
124
|
+
lastActivityAt: now
|
|
125
|
+
};
|
|
126
|
+
if (patch.selectors !== void 0) set["anchor.selectors"] = patch.selectors;
|
|
127
|
+
if (patch.signals !== void 0) set["anchor.signals"] = patch.signals;
|
|
128
|
+
if (patch.selectionLost !== void 0) set.selectionLost = patch.selectionLost;
|
|
129
|
+
const doc = await col.findOneAndUpdate(
|
|
130
|
+
{ _id: threadId, ...scopeFilter(scope) },
|
|
131
|
+
{ $set: set },
|
|
132
|
+
{ returnDocument: "after" }
|
|
133
|
+
);
|
|
134
|
+
if (!doc) throw new Error("thread not found");
|
|
135
|
+
return toListItem(doc);
|
|
136
|
+
},
|
|
137
|
+
async putAttachment(scope, attachment) {
|
|
138
|
+
const { id, ...rest } = attachment;
|
|
139
|
+
const doc = { _id: id, ...scopeFilter(scope), ...rest };
|
|
140
|
+
await attachmentsCol.replaceOne({ _id: id }, doc, { upsert: true });
|
|
141
|
+
},
|
|
142
|
+
async getAttachments(scope, ids) {
|
|
143
|
+
if (ids.length === 0) return [];
|
|
144
|
+
const docs = await attachmentsCol.find({ _id: { $in: ids }, ...scopeFilter(scope) }).toArray();
|
|
145
|
+
return docs.map((d) => toAttachment(d));
|
|
146
|
+
}
|
|
147
|
+
};
|
|
148
|
+
}
|
|
149
|
+
async function connectMongo(uri) {
|
|
150
|
+
const client = new MongoClient(uri);
|
|
151
|
+
try {
|
|
152
|
+
await client.connect();
|
|
153
|
+
} catch (err) {
|
|
154
|
+
await client.close().catch(() => {
|
|
155
|
+
});
|
|
156
|
+
throw err;
|
|
157
|
+
}
|
|
158
|
+
const db = client.db();
|
|
159
|
+
await ensureIndexes(db);
|
|
160
|
+
return createMongoRepository({ db });
|
|
161
|
+
}
|
|
162
|
+
function mongoRepository({
|
|
163
|
+
uri,
|
|
164
|
+
cacheKey = "mongo"
|
|
165
|
+
}) {
|
|
166
|
+
return lazyRepository(() => connectMongo(uri), { cacheKey });
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
// src/indexes.ts
|
|
170
|
+
async function ensureIndexes(db) {
|
|
171
|
+
await db.collection(COLLECTION).createIndexes([
|
|
172
|
+
{ key: { projectId: 1, pageKey: 1 }, name: "projectId_pageKey" },
|
|
173
|
+
{ key: { projectId: 1, updatedAt: -1 }, name: "projectId_updatedAt" },
|
|
174
|
+
{ key: { projectId: 1, status: 1 }, name: "projectId_status" }
|
|
175
|
+
]);
|
|
176
|
+
}
|
|
177
|
+
export {
|
|
178
|
+
createMongoRepository,
|
|
179
|
+
ensureIndexes,
|
|
180
|
+
mongoRepository
|
|
181
|
+
};
|
|
182
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/repository.ts","../src/indexes.ts"],"sourcesContent":["import type {\n Attachment,\n AttachmentId,\n Comment,\n Thread,\n ThreadId,\n ThreadListItem,\n ThreadStatus,\n} from '@airnauts/comments-core'\nimport {\n type AnchorPatch,\n decodeCursor,\n encodeCursor,\n type ListQuery,\n type ListResult,\n lazyRepository,\n type NewComment,\n type NewThread,\n type Repository,\n type Scope,\n} from '@airnauts/comments-server'\nimport { type Db, type Filter, MongoClient, type UpdateFilter } from 'mongodb'\nimport { ensureIndexes } from './indexes'\n\nexport const COLLECTION = 'threads'\nexport const ATTACHMENTS_COLLECTION = 'attachments'\n\n/** Stored shape: the wire Thread (minus its `id`) keyed by `_id`, plus server-only scope. */\ntype StoredThread = Omit<Thread, 'id'> & {\n _id: string\n projectId: string\n env: string | null\n}\n\n/** Stored shape: the wire Attachment (minus its `id`) keyed by `_id`, plus server-only scope. */\ntype StoredAttachment = Omit<Attachment, 'id'> & {\n _id: string\n projectId: string\n env: string | null\n}\n\nfunction toAttachment(doc: StoredAttachment): Attachment {\n const { _id, projectId: _p, env: _e, ...rest } = doc\n return { id: _id as AttachmentId, ...rest }\n}\n\nfunction scopeFilter(scope: Scope): { projectId: string; env: string | null } {\n return { projectId: scope.projectId, env: scope.env ?? null }\n}\n\nfunction unresolvedCountOf(status: ThreadStatus): number {\n return status === 'open' ? 1 : 0\n}\n\nfunction toThread(doc: StoredThread): Thread {\n const { _id, projectId: _p, env: _e, ...rest } = doc\n return { id: _id as ThreadId, ...rest }\n}\n\nfunction toListItem(doc: StoredThread): ThreadListItem {\n // Strip server-only scope + thread-only payload (also absent under the list projection).\n const {\n _id,\n projectId: _p,\n env: _e,\n comments: _c,\n captureContext: _cc,\n provenance: _pr,\n ...rest\n } = doc\n return { id: _id as ThreadId, ...rest }\n}\n\nexport function createMongoRepository({ db }: { db: Db }): Repository {\n const col = db.collection<StoredThread>(COLLECTION)\n const attachmentsCol = db.collection<StoredAttachment>(ATTACHMENTS_COLLECTION)\n\n return {\n async createThread(input: NewThread): Promise<Thread> {\n const doc: StoredThread = {\n _id: input.id,\n projectId: input.projectId,\n env: input.env ?? null,\n scope: input.scope,\n pageKey: input.pageKey,\n pageUrl: input.pageUrl,\n ...(input.pageTitle !== undefined ? { pageTitle: input.pageTitle } : {}),\n anchor: input.anchor,\n status: input.status,\n anchorState: input.anchorState,\n ...(input.selectionLost !== undefined ? { selectionLost: input.selectionLost } : {}),\n commentCount: 1,\n unresolvedCount: unresolvedCountOf(input.status),\n createdBy: input.createdBy,\n createdAt: input.createdAt,\n updatedAt: input.updatedAt,\n lastActivityAt: input.lastActivityAt,\n schemaVersion: input.schemaVersion,\n comments: [input.firstComment],\n captureContext: input.captureContext,\n ...(input.provenance !== undefined ? { provenance: input.provenance } : {}),\n }\n await col.insertOne(doc)\n return toThread(doc)\n },\n\n async getThread(scope: Scope, id: ThreadId): Promise<Thread | null> {\n const doc = await col.findOne({ _id: id, ...scopeFilter(scope) })\n return doc ? toThread(doc) : null\n },\n\n async listThreads(query: ListQuery): Promise<ListResult> {\n const limit = Math.max(1, Math.min(query.limit, 200))\n const filter: Record<string, unknown> = scopeFilter(query)\n if (query.pageKey !== undefined) filter.pageKey = query.pageKey\n if (query.status !== undefined) filter.status = query.status\n const cursor = query.cursor ? decodeCursor(query.cursor) : undefined\n if (cursor) {\n filter.$or = [\n { updatedAt: { $lt: cursor.updatedAt } },\n { updatedAt: cursor.updatedAt, _id: { $lt: cursor.id } },\n ]\n }\n const docs = await col\n .find(filter as Filter<StoredThread>, {\n projection: { comments: 0, captureContext: 0, provenance: 0 },\n })\n .sort({ updatedAt: -1, _id: -1 })\n .limit(limit + 1)\n .toArray()\n const more = docs.length > limit\n const page = more ? docs.slice(0, limit) : docs\n const last = page[page.length - 1]\n const nextCursor =\n more && last ? encodeCursor({ updatedAt: last.updatedAt, id: last._id }) : null\n return { threads: page.map((d) => toListItem(d as StoredThread)), nextCursor }\n },\n\n async addComment(scope: Scope, threadId: ThreadId, comment: NewComment): Promise<Comment> {\n // unresolvedCount is intentionally untouched: it tracks `status` only, and\n // addComment never changes status. Do NOT add a spurious $inc here.\n const res = await col.updateOne(\n { _id: threadId, ...scopeFilter(scope) },\n {\n $push: { comments: comment },\n $inc: { commentCount: 1 },\n $set: { updatedAt: comment.createdAt, lastActivityAt: comment.createdAt },\n },\n )\n if (res.matchedCount === 0) throw new Error('thread not found')\n return comment\n },\n\n async setStatus(\n scope: Scope,\n threadId: ThreadId,\n status: ThreadStatus,\n now: string,\n ): Promise<Thread> {\n const doc = await col.findOneAndUpdate(\n { _id: threadId, ...scopeFilter(scope) },\n {\n $set: {\n status,\n updatedAt: now,\n lastActivityAt: now,\n unresolvedCount: unresolvedCountOf(status),\n },\n },\n { returnDocument: 'after' },\n )\n if (!doc) throw new Error('thread not found')\n return toThread(doc)\n },\n\n async updateAnchor(\n scope: Scope,\n threadId: ThreadId,\n patch: AnchorPatch,\n now: string,\n ): Promise<ThreadListItem> {\n const set: Record<string, unknown> = {\n anchorState: patch.anchorState,\n updatedAt: now,\n lastActivityAt: now,\n }\n if (patch.selectors !== undefined) set['anchor.selectors'] = patch.selectors\n if (patch.signals !== undefined) set['anchor.signals'] = patch.signals\n if (patch.selectionLost !== undefined) set.selectionLost = patch.selectionLost\n const doc = await col.findOneAndUpdate(\n { _id: threadId, ...scopeFilter(scope) },\n { $set: set } as UpdateFilter<StoredThread>,\n { returnDocument: 'after' },\n )\n if (!doc) throw new Error('thread not found')\n return toListItem(doc)\n },\n\n async putAttachment(scope: Scope, attachment: Attachment): Promise<void> {\n const { id, ...rest } = attachment\n const doc: StoredAttachment = { _id: id, ...scopeFilter(scope), ...rest }\n await attachmentsCol.replaceOne({ _id: id }, doc, { upsert: true })\n },\n\n async getAttachments(scope: Scope, ids: AttachmentId[]): Promise<Attachment[]> {\n if (ids.length === 0) return []\n const docs = await attachmentsCol\n .find({ _id: { $in: ids as unknown as string[] }, ...scopeFilter(scope) })\n .toArray()\n return docs.map((d) => toAttachment(d as StoredAttachment))\n },\n }\n}\n\n/** Open one client, connect, ensure indexes, and build the repository. */\nasync function connectMongo(uri: string): Promise<Repository> {\n const client = new MongoClient(uri)\n try {\n await client.connect() // intentionally left open for the process lifetime on success\n } catch (err) {\n await client.close().catch(() => {}) // best-effort cleanup; swallow secondary errors\n throw err\n }\n const db = client.db() // database name comes from the connection string\n await ensureIndexes(db)\n return createMongoRepository({ db })\n}\n\n/**\n * Host-facing Mongo `Repository`: connects lazily on first use and memoizes the\n * connection (warm serverless / HMR reuse) under `cacheKey`. The single function\n * a host imports — `createMongoRepository`/`ensureIndexes` remain for callers\n * that own their own connection.\n *\n * `cacheKey` defaults to `'mongo'`. If you connect to more than one database in the\n * same process, pass a distinct `cacheKey` per connection — otherwise the second\n * call reuses the first connection under the shared default key.\n */\nexport function mongoRepository({\n uri,\n cacheKey = 'mongo',\n}: {\n uri: string\n cacheKey?: string\n}): Repository {\n return lazyRepository(() => connectMongo(uri), { cacheKey })\n}\n","import type { Db } from 'mongodb'\nimport { COLLECTION } from './repository'\n\n/**\n * Create the scoped indexes from architecture §5. Idempotent: MongoDB's\n * createIndexes is a no-op when an identical index already exists, so this is\n * safe to run on every startup.\n */\nexport async function ensureIndexes(db: Db): Promise<void> {\n await db.collection(COLLECTION).createIndexes([\n { key: { projectId: 1, pageKey: 1 }, name: 'projectId_pageKey' },\n { key: { projectId: 1, updatedAt: -1 }, name: 'projectId_updatedAt' },\n { key: { projectId: 1, status: 1 }, name: 'projectId_status' },\n ])\n}\n"],"mappings":";AASA;AAAA,EAEE;AAAA,EACA;AAAA,EAGA;AAAA,OAKK;AACP,SAA+B,mBAAsC;AAG9D,IAAM,aAAa;AACnB,IAAM,yBAAyB;AAgBtC,SAAS,aAAa,KAAmC;AACvD,QAAM,EAAE,KAAK,WAAW,IAAI,KAAK,IAAI,GAAG,KAAK,IAAI;AACjD,SAAO,EAAE,IAAI,KAAqB,GAAG,KAAK;AAC5C;AAEA,SAAS,YAAY,OAAyD;AAC5E,SAAO,EAAE,WAAW,MAAM,WAAW,KAAK,MAAM,OAAO,KAAK;AAC9D;AAEA,SAAS,kBAAkB,QAA8B;AACvD,SAAO,WAAW,SAAS,IAAI;AACjC;AAEA,SAAS,SAAS,KAA2B;AAC3C,QAAM,EAAE,KAAK,WAAW,IA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AAA,IACpE,EAAE,KAAK,EAAE,WAAW,GAAG,QAAQ,EAAE,GAAG,MAAM,mBAAmB;AAAA,EAC/D,CAAC;AACH;","names":[]}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { Db } from 'mongodb';
|
|
2
|
+
/**
|
|
3
|
+
* Create the scoped indexes from architecture §5. Idempotent: MongoDB's
|
|
4
|
+
* createIndexes is a no-op when an identical index already exists, so this is
|
|
5
|
+
* safe to run on every startup.
|
|
6
|
+
*/
|
|
7
|
+
export declare function ensureIndexes(db: Db): Promise<void>;
|
|
8
|
+
//# sourceMappingURL=indexes.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"indexes.d.ts","sourceRoot":"","sources":["../src/indexes.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,EAAE,EAAE,MAAM,SAAS,CAAA;AAGjC;;;;GAIG;AACH,wBAAsB,aAAa,CAAC,EAAE,EAAE,EAAE,GAAG,OAAO,CAAC,IAAI,CAAC,CAMzD"}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { type Repository } from '@airnauts/comments-server';
|
|
2
|
+
import { type Db } from 'mongodb';
|
|
3
|
+
export declare const COLLECTION = "threads";
|
|
4
|
+
export declare const ATTACHMENTS_COLLECTION = "attachments";
|
|
5
|
+
export declare function createMongoRepository({ db }: {
|
|
6
|
+
db: Db;
|
|
7
|
+
}): Repository;
|
|
8
|
+
/**
|
|
9
|
+
* Host-facing Mongo `Repository`: connects lazily on first use and memoizes the
|
|
10
|
+
* connection (warm serverless / HMR reuse) under `cacheKey`. The single function
|
|
11
|
+
* a host imports — `createMongoRepository`/`ensureIndexes` remain for callers
|
|
12
|
+
* that own their own connection.
|
|
13
|
+
*
|
|
14
|
+
* `cacheKey` defaults to `'mongo'`. If you connect to more than one database in the
|
|
15
|
+
* same process, pass a distinct `cacheKey` per connection — otherwise the second
|
|
16
|
+
* call reuses the first connection under the shared default key.
|
|
17
|
+
*/
|
|
18
|
+
export declare function mongoRepository({ uri, cacheKey, }: {
|
|
19
|
+
uri: string;
|
|
20
|
+
cacheKey?: string;
|
|
21
|
+
}): Repository;
|
|
22
|
+
//# sourceMappingURL=repository.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"repository.d.ts","sourceRoot":"","sources":["../src/repository.ts"],"names":[],"mappings":"AASA,OAAO,EASL,KAAK,UAAU,EAEhB,MAAM,2BAA2B,CAAA;AAClC,OAAO,EAAE,KAAK,EAAE,EAA+C,MAAM,SAAS,CAAA;AAG9E,eAAO,MAAM,UAAU,YAAY,CAAA;AACnC,eAAO,MAAM,sBAAsB,gBAAgB,CAAA;AAgDnD,wBAAgB,qBAAqB,CAAC,EAAE,EAAE,EAAE,EAAE;IAAE,EAAE,EAAE,EAAE,CAAA;CAAE,GAAG,UAAU,CA2IpE;AAgBD;;;;;;;;;GASG;AACH,wBAAgB,eAAe,CAAC,EAC9B,GAAG,EACH,QAAkB,GACnB,EAAE;IACD,GAAG,EAAE,MAAM,CAAA;IACX,QAAQ,CAAC,EAAE,MAAM,CAAA;CAClB,GAAG,UAAU,CAEb"}
|
package/package.json
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@airnauts/comments-adapter-mongo",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "MongoDB repository adapter for the Airnauts commenting tool server.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"comments",
|
|
7
|
+
"commenting",
|
|
8
|
+
"annotations",
|
|
9
|
+
"feedback",
|
|
10
|
+
"airnauts",
|
|
11
|
+
"mongodb",
|
|
12
|
+
"adapter"
|
|
13
|
+
],
|
|
14
|
+
"license": "MIT",
|
|
15
|
+
"author": "Airnauts",
|
|
16
|
+
"homepage": "https://github.com/Airnauts/commenting-tool#readme",
|
|
17
|
+
"repository": {
|
|
18
|
+
"type": "git",
|
|
19
|
+
"url": "git+https://github.com/Airnauts/commenting-tool.git",
|
|
20
|
+
"directory": "packages/adapter-mongo"
|
|
21
|
+
},
|
|
22
|
+
"bugs": {
|
|
23
|
+
"url": "https://github.com/Airnauts/commenting-tool/issues"
|
|
24
|
+
},
|
|
25
|
+
"type": "module",
|
|
26
|
+
"exports": {
|
|
27
|
+
".": {
|
|
28
|
+
"types": "./dist/index.d.ts",
|
|
29
|
+
"import": "./dist/index.js"
|
|
30
|
+
}
|
|
31
|
+
},
|
|
32
|
+
"files": [
|
|
33
|
+
"dist",
|
|
34
|
+
"!dist/.tsbuildinfo",
|
|
35
|
+
"README.md",
|
|
36
|
+
"LICENSE"
|
|
37
|
+
],
|
|
38
|
+
"publishConfig": {
|
|
39
|
+
"access": "public"
|
|
40
|
+
},
|
|
41
|
+
"dependencies": {
|
|
42
|
+
"mongodb": "^6.12.0",
|
|
43
|
+
"@airnauts/comments-core": "^0.1.0",
|
|
44
|
+
"@airnauts/comments-server": "^0.1.0"
|
|
45
|
+
},
|
|
46
|
+
"devDependencies": {
|
|
47
|
+
"mongodb-memory-server": "^10.1.2",
|
|
48
|
+
"@airnauts/comments-test-support": "0.0.0"
|
|
49
|
+
},
|
|
50
|
+
"scripts": {
|
|
51
|
+
"build": "tsup && tsc --build --force",
|
|
52
|
+
"typecheck": "tsc --build",
|
|
53
|
+
"test": "vitest run"
|
|
54
|
+
}
|
|
55
|
+
}
|