@sfxcode/nuxt-mongocamp-server 2.3.0 → 2.4.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 +24 -2
- package/dist/module.json +1 -1
- package/dist/module.mjs +6 -2
- package/dist/runtime/api/.openapi-generator/FILES +4 -0
- package/dist/runtime/api/apis/CollectionApi.d.ts +6 -6
- package/dist/runtime/api/apis/CollectionApi.js +4 -4
- package/dist/runtime/api/apis/DocumentApi.d.ts +42 -14
- package/dist/runtime/api/apis/DocumentApi.js +27 -6
- package/dist/runtime/api/docs/CollectionApi.md +6 -6
- package/dist/runtime/api/docs/CollectionDeclaration.md +4 -0
- package/dist/runtime/api/docs/CollectionDeclarationIndexesInner.md +38 -0
- package/dist/runtime/api/docs/CollectionDeclarationRefsInner.md +38 -0
- package/dist/runtime/api/docs/DocumentApi.md +47 -26
- package/dist/runtime/api/docs/ExportRequest.md +2 -0
- package/dist/runtime/api/docs/MongoFindRequest.md +2 -0
- package/dist/runtime/api/docs/SearchRequest.md +2 -0
- package/dist/runtime/api/models/CollectionDeclaration.d.ts +10 -0
- package/dist/runtime/api/models/CollectionDeclaration.js +14 -0
- package/dist/runtime/api/models/CollectionDeclarationIndexesInner.d.ts +40 -0
- package/dist/runtime/api/models/CollectionDeclarationIndexesInner.js +32 -0
- package/dist/runtime/api/models/CollectionDeclarationRefsInner.d.ts +38 -0
- package/dist/runtime/api/models/CollectionDeclarationRefsInner.js +31 -0
- package/dist/runtime/api/models/ErrorDescription.d.ts +1 -1
- package/dist/runtime/api/models/ExportRequest.d.ts +4 -0
- package/dist/runtime/api/models/ExportRequest.js +4 -2
- package/dist/runtime/api/models/MongoFindRequest.d.ts +4 -0
- package/dist/runtime/api/models/MongoFindRequest.js +4 -2
- package/dist/runtime/api/models/SearchRequest.d.ts +4 -0
- package/dist/runtime/api/models/SearchRequest.js +4 -2
- package/dist/runtime/api/models/index.d.ts +2 -0
- package/dist/runtime/api/models/index.js +2 -0
- package/dist/runtime/composables/mongocampCollection.d.ts +48 -10
- package/dist/runtime/composables/mongocampCollection.js +50 -9
- package/dist/runtime/composables/mongocampLive.d.ts +5 -2
- package/dist/runtime/composables/mongocampLive.js +5 -2
- package/dist/runtime/server/composables/mongocampServerCollection.d.ts +20 -0
- package/dist/runtime/server/composables/mongocampServerCollection.js +12 -0
- package/dist/runtime/types/collections.d.ts +10 -0
- package/dist/runtime/utils/sse.d.ts +1 -1
- package/package.json +2 -2
|
@@ -7,7 +7,9 @@ export * from './ClusterMemberInformation.js';
|
|
|
7
7
|
export * from './CollectionCollation.js';
|
|
8
8
|
export * from './CollectionDeclaration.js';
|
|
9
9
|
export * from './CollectionDeclarationAudit.js';
|
|
10
|
+
export * from './CollectionDeclarationIndexesInner.js';
|
|
10
11
|
export * from './CollectionDeclarationOwner.js';
|
|
12
|
+
export * from './CollectionDeclarationRefsInner.js';
|
|
11
13
|
export * from './CollectionDeclarationSoftDelete.js';
|
|
12
14
|
export * from './CollectionDeclarationTimestamps.js';
|
|
13
15
|
export * from './CollectionDeclarationVersioned.js';
|
|
@@ -7,7 +7,9 @@ export * from "./ClusterMemberInformation.js";
|
|
|
7
7
|
export * from "./CollectionCollation.js";
|
|
8
8
|
export * from "./CollectionDeclaration.js";
|
|
9
9
|
export * from "./CollectionDeclarationAudit.js";
|
|
10
|
+
export * from "./CollectionDeclarationIndexesInner.js";
|
|
10
11
|
export * from "./CollectionDeclarationOwner.js";
|
|
12
|
+
export * from "./CollectionDeclarationRefsInner.js";
|
|
11
13
|
export * from "./CollectionDeclarationSoftDelete.js";
|
|
12
14
|
export * from "./CollectionDeclarationTimestamps.js";
|
|
13
15
|
export * from "./CollectionDeclarationVersioned.js";
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import type { Ref } from 'vue';
|
|
2
2
|
import type { DeleteResponse, InsertResponse, UpdateResponse } from '../api/index.js';
|
|
3
|
-
import type {
|
|
3
|
+
import type { ExpandedDocument } from '@sfxcode/mongocamp-server-core';
|
|
4
|
+
import type { MongocampCollectionName, MongocampDocument, MongocampDocumentInput, MongocampRefs } from '../types/collections.js';
|
|
4
5
|
import type { MongocampError } from '../utils/toMongocampError.js';
|
|
5
6
|
/**
|
|
6
7
|
* The documents of one collection with their types (2.3.0): `useMongocampCollection('orders')` for a declared collection
|
|
@@ -10,7 +11,7 @@ import type { MongocampError } from '../utils/toMongocampError.js';
|
|
|
10
11
|
*/
|
|
11
12
|
/**
|
|
12
13
|
* An error of a request: an `Error` with the status and the fields of the error response (`issues` for documents that are not valid).
|
|
13
|
-
* `isConflict` (2.
|
|
14
|
+
* `isConflict` (2.3.0): a change based on an outdated version; `current` is the stored document then (decoded), to show or merge.
|
|
14
15
|
*/
|
|
15
16
|
export type MongocampRequestError = Error & Omit<MongocampError, 'current'> & {
|
|
16
17
|
isConflict: boolean;
|
|
@@ -29,7 +30,26 @@ export interface CollectionFindOptions {
|
|
|
29
30
|
filterCaseInsensitive?: boolean;
|
|
30
31
|
/** Soft-deleted documents too (collections declared with `softDelete`, administrators of the collection only). */
|
|
31
32
|
withDeleted?: boolean;
|
|
33
|
+
/**
|
|
34
|
+
* References to expand (2.4.0): declared `refs` fields become the referenced documents, read with your grants on their collection
|
|
35
|
+
* (`null`, or left out of an array, when you cannot read one).
|
|
36
|
+
*/
|
|
37
|
+
expand?: readonly string[];
|
|
32
38
|
}
|
|
39
|
+
/** The options of a find that expands the references `K`. */
|
|
40
|
+
export type ExpandingFindOptions<K extends string> = Omit<CollectionFindOptions, 'expand'> & {
|
|
41
|
+
expand: readonly K[];
|
|
42
|
+
};
|
|
43
|
+
/** The options of a find: `expand` only with the references of the collection. */
|
|
44
|
+
export type FindOptionsOf<Refs> = Omit<CollectionFindOptions, 'expand'> & {
|
|
45
|
+
expand?: readonly (keyof Refs & string)[];
|
|
46
|
+
};
|
|
47
|
+
/** A document of `T` with the references `K` of `Refs` expanded to the documents of the declared collections. */
|
|
48
|
+
export type Expanded<T, Refs, K extends keyof Refs & string> = ExpandedDocument<T, Refs, K, MongocampCollections>;
|
|
49
|
+
/** Marks a document that was read with `expand`: its expanded fields (an enumerable symbol, kept by spreads, never sent). */
|
|
50
|
+
export declare const ExpandedFields: unique symbol;
|
|
51
|
+
/** Marks the documents of a read with `expand` (`ExpandedFields`), for writing them back. */
|
|
52
|
+
export declare function markExpanded<D extends Record<string, unknown>>(document: D, fields: readonly string[] | undefined): D;
|
|
33
53
|
export interface UpdateOptions {
|
|
34
54
|
/** On a version conflict, apply the changes once more to the stored document (its version) instead of failing. */
|
|
35
55
|
retryOnConflict?: boolean;
|
|
@@ -42,7 +62,7 @@ export interface CollectionPage<T> {
|
|
|
42
62
|
countRows?: number;
|
|
43
63
|
countPages?: number;
|
|
44
64
|
}
|
|
45
|
-
/** One change of a document from the audit records (collections declared with `audit`, 2.
|
|
65
|
+
/** One change of a document from the audit records (collections declared with `audit`, 2.3.0); before and after decoded and typed. */
|
|
46
66
|
export interface AuditEntry<T> {
|
|
47
67
|
operation: 'insert' | 'replace' | 'update' | 'delete' | 'softDelete' | 'restore';
|
|
48
68
|
user: string;
|
|
@@ -57,18 +77,33 @@ export type DocumentIdentity = string | number | {
|
|
|
57
77
|
} | {
|
|
58
78
|
_id: unknown;
|
|
59
79
|
};
|
|
60
|
-
export interface TypedCollection<T, I = T
|
|
80
|
+
export interface TypedCollection<T, I = T, Refs = Record<string, string>> {
|
|
61
81
|
readonly name: string;
|
|
62
|
-
find:
|
|
63
|
-
|
|
82
|
+
find: {
|
|
83
|
+
<K extends keyof Refs & string>(options: ExpandingFindOptions<K>): Promise<Expanded<T, Refs, K>[]>;
|
|
84
|
+
(options?: FindOptionsOf<Refs>): Promise<T[]>;
|
|
85
|
+
};
|
|
86
|
+
findPage: {
|
|
87
|
+
<K extends keyof Refs & string>(options: ExpandingFindOptions<K>): Promise<CollectionPage<Expanded<T, Refs, K>>>;
|
|
88
|
+
(options?: FindOptionsOf<Refs>): Promise<CollectionPage<T>>;
|
|
89
|
+
};
|
|
64
90
|
/** The document, `null` when there is none with the id. */
|
|
65
|
-
findById:
|
|
91
|
+
findById: {
|
|
92
|
+
<K extends keyof Refs & string>(id: DocumentIdentity, options: {
|
|
93
|
+
expand: readonly K[];
|
|
94
|
+
}): Promise<Expanded<T, Refs, K> | null>;
|
|
95
|
+
(id: DocumentIdentity, options?: {
|
|
96
|
+
expand?: readonly (keyof Refs & string)[];
|
|
97
|
+
}): Promise<T | null>;
|
|
98
|
+
};
|
|
66
99
|
/** Inserts a document; fields the server writes (timestamps, owner, version) and protected fields are left out. */
|
|
67
100
|
insert: (document: I) => Promise<InsertResponse>;
|
|
68
101
|
insertMany: (documents: I[]) => Promise<InsertResponse>;
|
|
69
102
|
/**
|
|
70
103
|
* Replaces the whole document. Given the document that was read (not only its id), its version goes with the request: a change of
|
|
71
|
-
* someone else in the meantime answers 409 (`isConflict`) instead of being overwritten.
|
|
104
|
+
* someone else in the meantime answers 409 (`isConflict`) instead of being overwritten. Expanded references are sent as their ids; a
|
|
105
|
+
* document read with `expand` whose reference is `null` or an array is refused (a reference you cannot read is unknown here) — use
|
|
106
|
+
* `update` with the changed fields.
|
|
72
107
|
*/
|
|
73
108
|
replace: (target: DocumentIdentity, document: I, options?: UpdateOptions) => Promise<UpdateResponse>;
|
|
74
109
|
/** Changes the given fields (`$set`); with the document that was read, its version goes with the request (see `replace`). */
|
|
@@ -82,7 +117,10 @@ export interface TypedCollection<T, I = T> {
|
|
|
82
117
|
*/
|
|
83
118
|
isMine: (document: T | null | undefined) => boolean;
|
|
84
119
|
/** The pages of a find as reactive state. */
|
|
85
|
-
paginated:
|
|
120
|
+
paginated: {
|
|
121
|
+
<K extends keyof Refs & string>(options: Omit<ExpandingFindOptions<K>, 'page'>): PaginatedCollection<Expanded<T, Refs, K>>;
|
|
122
|
+
(options?: Omit<FindOptionsOf<Refs>, 'page'>): PaginatedCollection<T>;
|
|
123
|
+
};
|
|
86
124
|
/** The changes of a document, newest first (collections declared with `audit`). */
|
|
87
125
|
history: (id: DocumentIdentity, options?: {
|
|
88
126
|
page?: number;
|
|
@@ -102,5 +140,5 @@ export interface PaginatedCollection<T> {
|
|
|
102
140
|
nextPage: () => Promise<void>;
|
|
103
141
|
previousPage: () => Promise<void>;
|
|
104
142
|
}
|
|
105
|
-
export declare function useMongocampCollection<N extends MongocampCollectionName>(name: N): TypedCollection<MongocampDocument<N>, MongocampDocumentInput<N>>;
|
|
143
|
+
export declare function useMongocampCollection<N extends MongocampCollectionName>(name: N): TypedCollection<MongocampDocument<N>, MongocampDocumentInput<N>, MongocampRefs<N>>;
|
|
106
144
|
export declare function useMongocampCollection<T extends object = Record<string, unknown>, I = T>(name: string): TypedCollection<T, I>;
|
|
@@ -6,6 +6,13 @@ import { useMongocampApi } from "./mongocampApi.js";
|
|
|
6
6
|
import { useMongocampStorage } from "./mongocampStorage.js";
|
|
7
7
|
import { useMongocampUrl } from "./mongocampUrl.js";
|
|
8
8
|
import { useRuntimeConfig } from "#app";
|
|
9
|
+
export const ExpandedFields = Symbol.for("mongocamp.expanded");
|
|
10
|
+
export function markExpanded(document, fields) {
|
|
11
|
+
if (fields !== void 0 && fields.length > 0) {
|
|
12
|
+
Object.defineProperty(document, ExpandedFields, { value: [...fields], enumerable: true, configurable: true, writable: true });
|
|
13
|
+
}
|
|
14
|
+
return document;
|
|
15
|
+
}
|
|
9
16
|
const VersionConflict = 4092;
|
|
10
17
|
async function requestError(error) {
|
|
11
18
|
const { current, ...described } = await toMongocampError(error);
|
|
@@ -43,6 +50,21 @@ function withoutPaths(document, paths) {
|
|
|
43
50
|
}
|
|
44
51
|
return copy;
|
|
45
52
|
}
|
|
53
|
+
function setPathOf(document, path, value) {
|
|
54
|
+
const segments = path.split(".");
|
|
55
|
+
let current = document;
|
|
56
|
+
for (const segment of segments.slice(0, -1)) {
|
|
57
|
+
const next = current[segment];
|
|
58
|
+
if (next === null || typeof next !== "object") {
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
current = next;
|
|
62
|
+
}
|
|
63
|
+
current[segments.at(-1)] = value;
|
|
64
|
+
}
|
|
65
|
+
function idOfReference(value) {
|
|
66
|
+
return isDocument(value) ? value._id : value;
|
|
67
|
+
}
|
|
46
68
|
function isDocument(target) {
|
|
47
69
|
return target !== null && typeof target === "object" && "_id" in target && typeof target.toHexString !== "function";
|
|
48
70
|
}
|
|
@@ -72,7 +94,24 @@ export function useMongocampCollection(name) {
|
|
|
72
94
|
};
|
|
73
95
|
const writable = async (document) => {
|
|
74
96
|
const loaded = await loadDeclaration();
|
|
75
|
-
|
|
97
|
+
if (loaded === null) {
|
|
98
|
+
return { ...document };
|
|
99
|
+
}
|
|
100
|
+
const body = withoutPaths(document, [...loaded.systemFields, ...loaded.protectedFields]);
|
|
101
|
+
for (const reference of loaded.refs ?? []) {
|
|
102
|
+
const value = getPath(body, reference.field);
|
|
103
|
+
if (value !== null && typeof value === "object") {
|
|
104
|
+
setPathOf(body, reference.field, Array.isArray(value) ? value.map(idOfReference) : idOfReference(value));
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
return body;
|
|
108
|
+
};
|
|
109
|
+
const requireComplete = (document) => {
|
|
110
|
+
const fields = document?.[ExpandedFields];
|
|
111
|
+
const lossy = (fields ?? []).find((field) => getPath(document, field) === null || Array.isArray(getPath(document, field)));
|
|
112
|
+
if (lossy !== void 0) {
|
|
113
|
+
throw Object.assign(new Error(`replace: ${lossy} was read with expand, references you cannot read are not in it; use update with the changed fields`), { status: 400, isConflict: false });
|
|
114
|
+
}
|
|
76
115
|
};
|
|
77
116
|
const versionOf = async (target) => {
|
|
78
117
|
const loaded = await loadDeclaration();
|
|
@@ -100,7 +139,7 @@ export function useMongocampCollection(name) {
|
|
|
100
139
|
throw await requestError(error);
|
|
101
140
|
}
|
|
102
141
|
};
|
|
103
|
-
const decode = (documents) => documents.map((document) => fromExtendedJson(document));
|
|
142
|
+
const decode = (documents, expand) => documents.map((document) => markExpanded(fromExtendedJson(document), expand));
|
|
104
143
|
const listParameters = (options) => ({
|
|
105
144
|
collectionName: name,
|
|
106
145
|
filter: options.filter,
|
|
@@ -109,14 +148,15 @@ export function useMongocampCollection(name) {
|
|
|
109
148
|
page: options.page ?? 1,
|
|
110
149
|
rowsPerPage: options.rowsPerPage ?? defaultRows(),
|
|
111
150
|
filterCaseInsensitive: options.filterCaseInsensitive,
|
|
112
|
-
...options.withDeleted ? { withDeleted: true } : {}
|
|
151
|
+
...options.withDeleted ? { withDeleted: true } : {},
|
|
152
|
+
...options.expand !== void 0 && options.expand.length > 0 ? { expand: [...options.expand] } : {}
|
|
113
153
|
});
|
|
114
154
|
const findPage = (options = {}) => call(async () => {
|
|
115
155
|
const parameters = listParameters(options);
|
|
116
156
|
const response = await documentApi.listDocumentsRaw(parameters);
|
|
117
157
|
const headers = response.raw.headers;
|
|
118
158
|
return {
|
|
119
|
-
documents: decode(await response.value()),
|
|
159
|
+
documents: decode(await response.value(), options.expand),
|
|
120
160
|
page: parameters.page,
|
|
121
161
|
rowsPerPage: parameters.rowsPerPage,
|
|
122
162
|
countRows: headerNumber(headers, "x-pagination-count-rows"),
|
|
@@ -125,11 +165,12 @@ export function useMongocampCollection(name) {
|
|
|
125
165
|
});
|
|
126
166
|
const collection = {
|
|
127
167
|
name,
|
|
128
|
-
find: (options) => call(async () => decode(await documentApi.listDocuments(listParameters(options ?? {})))),
|
|
168
|
+
find: (options) => call(async () => decode(await documentApi.listDocuments(listParameters(options ?? {})), options?.expand)),
|
|
129
169
|
findPage,
|
|
130
|
-
findById: async (id) => {
|
|
170
|
+
findById: async (id, options = {}) => {
|
|
171
|
+
const expand = options.expand !== void 0 && options.expand.length > 0 ? { expand: [...options.expand] } : {};
|
|
131
172
|
try {
|
|
132
|
-
return fromExtendedJson(await documentApi.getDocument({ collectionName: name, documentId: documentId(id) }));
|
|
173
|
+
return markExpanded(fromExtendedJson(await documentApi.getDocument({ collectionName: name, documentId: documentId(id), ...expand })), options.expand);
|
|
133
174
|
} catch (error) {
|
|
134
175
|
if (error instanceof ResponseError && error.response.status === 404) {
|
|
135
176
|
return null;
|
|
@@ -139,11 +180,11 @@ export function useMongocampCollection(name) {
|
|
|
139
180
|
},
|
|
140
181
|
insert: (document) => call(async () => documentApi.insert({ collectionName: name, requestBody: toExtendedJson(await writable(document)) })),
|
|
141
182
|
insertMany: (documents) => call(async () => documentApi.insertMany({ collectionName: name, requestBody: await Promise.all(documents.map(async (document) => toExtendedJson(await writable(document)))) })),
|
|
142
|
-
replace: (target, document, options) => withRetry(target, options, (current) => call(async () => documentApi.update({
|
|
183
|
+
replace: (target, document, options) => withRetry(target, options, (current) => call(async () => (requireComplete(document), documentApi.update({
|
|
143
184
|
collectionName: name,
|
|
144
185
|
documentId: documentId(current),
|
|
145
186
|
requestBody: toExtendedJson({ ...await writable(document), ...await versionOf(current) })
|
|
146
|
-
}))),
|
|
187
|
+
})))),
|
|
147
188
|
update: (target, changes, options) => withRetry(target, options, (current) => call(async () => documentApi.updateDocumentPartial({
|
|
148
189
|
collectionName: name,
|
|
149
190
|
documentId: documentId(current),
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import type { Ref } from 'vue';
|
|
2
|
-
import type { MongocampCollectionName, MongocampDocument } from '../types/collections.js';
|
|
2
|
+
import type { MongocampCollectionName, MongocampDocument, MongocampExpandedDocument, MongocampRefName } from '../types/collections.js';
|
|
3
3
|
import type { CollectionFindOptions, MongocampRequestError } from './mongocampCollection.js';
|
|
4
4
|
/**
|
|
5
|
-
* A live list in one line (2.
|
|
5
|
+
* A live list in one line (2.3.0): `const { documents, status } = useMongocampLive('orders', { filter: 'status:open' })`. The documents
|
|
6
6
|
* are loaded with the typed composable and kept in sync with the live updates of the server (Server-Sent Events on change streams):
|
|
7
7
|
*
|
|
8
8
|
* - mode `patch` (default without `sort` and `page`): an update changes the document in the list, a new one is added, a removed one goes;
|
|
@@ -29,5 +29,8 @@ export interface LiveState<T> {
|
|
|
29
29
|
stop: () => void;
|
|
30
30
|
reload: () => Promise<void>;
|
|
31
31
|
}
|
|
32
|
+
export declare function useMongocampLive<N extends MongocampCollectionName, K extends MongocampRefName<N>>(name: N, options: Omit<LiveOptions, 'expand'> & {
|
|
33
|
+
expand: readonly K[];
|
|
34
|
+
}): LiveState<MongocampExpandedDocument<N, K>>;
|
|
32
35
|
export declare function useMongocampLive<N extends MongocampCollectionName>(name: N, options?: LiveOptions): LiveState<MongocampDocument<N>>;
|
|
33
36
|
export declare function useMongocampLive<T extends object = Record<string, unknown>>(name: string, options?: LiveOptions): LiveState<T>;
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { getCurrentInstance, getCurrentScope, onMounted, onScopeDispose, ref } from "vue";
|
|
2
2
|
import { documentId, fromExtendedJson } from "../utils/extendedJson.js";
|
|
3
3
|
import { createSseParser } from "../utils/sse.js";
|
|
4
|
-
import { useMongocampCollection } from "./mongocampCollection.js";
|
|
4
|
+
import { markExpanded, useMongocampCollection } from "./mongocampCollection.js";
|
|
5
5
|
import { useMongocampStorage } from "./mongocampStorage.js";
|
|
6
6
|
import { useMongocampUrl } from "./mongocampUrl.js";
|
|
7
7
|
const MaxBackoff = 3e4;
|
|
@@ -45,7 +45,7 @@ export function useMongocampLive(name, options = {}) {
|
|
|
45
45
|
if (event === "remove") {
|
|
46
46
|
documents.value = others;
|
|
47
47
|
} else if (event === "upsert" && data.document !== void 0) {
|
|
48
|
-
const document = fromExtendedJson(data.document);
|
|
48
|
+
const document = markExpanded(fromExtendedJson(data.document), options.expand);
|
|
49
49
|
const index = documents.value.findIndex((entry) => documentId(entry) === id);
|
|
50
50
|
documents.value = index < 0 ? [...documents.value, document] : documents.value.map((entry, position) => position === index ? document : entry);
|
|
51
51
|
}
|
|
@@ -69,6 +69,9 @@ export function useMongocampLive(name, options = {}) {
|
|
|
69
69
|
if (options.filter !== void 0 && options.filter !== "") {
|
|
70
70
|
query.set("filter", options.filter);
|
|
71
71
|
}
|
|
72
|
+
for (const field of options.expand ?? []) {
|
|
73
|
+
query.append("expand", field);
|
|
74
|
+
}
|
|
72
75
|
if (options.filterCaseInsensitive) {
|
|
73
76
|
query.set("filterCaseInsensitive", "true");
|
|
74
77
|
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { AuthorizedUser, ServerCollection } from '@sfxcode/mongocamp-server-core';
|
|
2
|
+
import type { H3Event } from 'h3';
|
|
3
|
+
import type { MongocampCollectionName, MongocampDocument, MongocampDocumentInput } from '../../types/collections.js';
|
|
4
|
+
/**
|
|
5
|
+
* The documents of a collection in a Nitro route (2.4.0), typed like `useMongocampCollection` in the browser and with the same rules as
|
|
6
|
+
* the API: grants, read scope (owner, soft delete, hooks), validation, behaviours (timestamps, version, audit) and events.
|
|
7
|
+
*
|
|
8
|
+
* export default defineEventHandler(async (event) => {
|
|
9
|
+
* const orders = useMongocampServerCollection('orders', { user: event }) // the caller of the request
|
|
10
|
+
* return orders.insert({ customer: 'Ada' })
|
|
11
|
+
* })
|
|
12
|
+
*
|
|
13
|
+
* `user`: the event (its credentials), a user id, an authorized user, or `'system'` (an administrator without grant checks and owner rules,
|
|
14
|
+
* for jobs and seeds). Needs the embedded server.
|
|
15
|
+
*/
|
|
16
|
+
export interface ServerCollectionUserOptions {
|
|
17
|
+
user: H3Event | string | AuthorizedUser | 'system';
|
|
18
|
+
}
|
|
19
|
+
export declare function useMongocampServerCollection<N extends MongocampCollectionName>(name: N, options: ServerCollectionUserOptions): ServerCollection<MongocampDocument<N>, MongocampDocumentInput<N>>;
|
|
20
|
+
export declare function useMongocampServerCollection<T = Record<string, unknown>, I = T>(name: string, options: ServerCollectionUserOptions): ServerCollection<T, I>;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { useMongocampCore } from "../mongocamp/core.js";
|
|
2
|
+
const methods = ["find", "findPage", "findById", "count", "insert", "insertMany", "replace", "update", "updateMany", "remove", "removeMany", "restore", "history"];
|
|
3
|
+
function isEvent(user) {
|
|
4
|
+
return user !== null && typeof user === "object" && "req" in user && user.req instanceof Request;
|
|
5
|
+
}
|
|
6
|
+
export function useMongocampServerCollection(name, options) {
|
|
7
|
+
const user = isEvent(options.user) ? options.user.req : options.user;
|
|
8
|
+
let collection;
|
|
9
|
+
const get = () => collection ??= useMongocampCore().then((core) => core.collection(name, { user }));
|
|
10
|
+
const lazy = Object.fromEntries(methods.map((method) => [method, (...args) => get().then((target) => target[method](...args))]));
|
|
11
|
+
return { name, ...lazy };
|
|
12
|
+
}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { ExpandedDocument } from '@sfxcode/mongocamp-server-core';
|
|
1
2
|
/**
|
|
2
3
|
* The documents of the declared collections by name (2.3.0). Empty here; the module adds the declarations of `server/mongocamp/*.ts`
|
|
3
4
|
* (`.nuxt/types/mongocamp-collections.d.ts`), an application without the module's scan (external server) can add its own:
|
|
@@ -11,6 +12,9 @@ declare global {
|
|
|
11
12
|
/** The documents as they are written (the input of the schema: fields with a default are optional); falls back to the stored type. */
|
|
12
13
|
interface MongocampCollectionInputs {
|
|
13
14
|
}
|
|
15
|
+
/** The references of the declared collections (2.4.0): `{ orders: { customerId: 'customers' } }`. */
|
|
16
|
+
interface MongocampCollectionRefs {
|
|
17
|
+
}
|
|
14
18
|
}
|
|
15
19
|
/** The names of the declared collections. */
|
|
16
20
|
export type MongocampCollectionName = keyof MongocampCollections & string;
|
|
@@ -18,3 +22,9 @@ export type MongocampCollectionName = keyof MongocampCollections & string;
|
|
|
18
22
|
export type MongocampDocument<N extends MongocampCollectionName> = MongocampCollections[N];
|
|
19
23
|
/** The written document type of a declared collection. */
|
|
20
24
|
export type MongocampDocumentInput<N extends MongocampCollectionName> = N extends keyof MongocampCollectionInputs ? MongocampCollectionInputs[N] : MongocampCollections[N];
|
|
25
|
+
/** The references of a declared collection: field → target collection name (`{}` without refs). */
|
|
26
|
+
export type MongocampRefs<N extends MongocampCollectionName> = N extends keyof MongocampCollectionRefs ? MongocampCollectionRefs[N] : Record<never, never>;
|
|
27
|
+
/** The fields of a declared collection that a read can expand. */
|
|
28
|
+
export type MongocampRefName<N extends MongocampCollectionName> = keyof MongocampRefs<N> & string;
|
|
29
|
+
/** A document of a declared collection with the references `K` expanded (target document or `null`, arrays of documents). */
|
|
30
|
+
export type MongocampExpandedDocument<N extends MongocampCollectionName, K extends MongocampRefName<N>> = ExpandedDocument<MongocampDocument<N>, MongocampRefs<N>, K, MongocampCollections>;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* A parser of Server-Sent Events for a stream read with `fetch` (2.
|
|
2
|
+
* A parser of Server-Sent Events for a stream read with `fetch` (2.3.0, live updates): `EventSource` cannot send the bearer token of the
|
|
3
3
|
* session. Feed it the decoded text as it arrives; it calls `onEvent` for every complete event (comments are skipped).
|
|
4
4
|
*/
|
|
5
5
|
export interface SseEvent {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sfxcode/nuxt-mongocamp-server",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.4.0",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"exports": {
|
|
@@ -29,7 +29,7 @@
|
|
|
29
29
|
},
|
|
30
30
|
"dependencies": {
|
|
31
31
|
"@nuxt/kit": "^4.7.0",
|
|
32
|
-
"@sfxcode/mongocamp-server-core": "2.
|
|
32
|
+
"@sfxcode/mongocamp-server-core": "2.4.0",
|
|
33
33
|
"@vueuse/nuxt": "^15.1.0",
|
|
34
34
|
"bson": "^7.3.4",
|
|
35
35
|
"consola": "^3.4.2",
|