@nxgt/mongo 0.2.0 → 0.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +89 -26
- package/dist/collection/context.d.ts +64 -0
- package/dist/collection/context.d.ts.map +1 -0
- package/dist/collection/documents.d.ts +21 -0
- package/dist/collection/documents.d.ts.map +1 -0
- package/dist/collection/filters.d.ts +15 -0
- package/dist/collection/filters.d.ts.map +1 -0
- package/dist/collection/get-collection.d.ts +26 -0
- package/dist/collection/get-collection.d.ts.map +1 -0
- package/dist/collection/paginate.d.ts +6 -0
- package/dist/collection/paginate.d.ts.map +1 -0
- package/dist/collection/reads.d.ts +19 -0
- package/dist/collection/reads.d.ts.map +1 -0
- package/dist/collection/types.d.ts +270 -0
- package/dist/collection/types.d.ts.map +1 -0
- package/dist/collection/writes.d.ts +12 -0
- package/dist/collection/writes.d.ts.map +1 -0
- package/dist/definition/define-collection.d.ts +2 -2
- package/dist/definition/fields.d.ts +5 -5
- package/dist/index.d.ts +2 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +664 -589
- package/dist/index.js.map +17 -11
- package/dist/transaction/with-transaction.d.ts +4 -4
- package/package.json +1 -1
- package/dist/repository/create-repository.d.ts +0 -21
- package/dist/repository/create-repository.d.ts.map +0 -1
- package/dist/repository/types.d.ts +0 -147
- package/dist/repository/types.d.ts.map +0 -1
|
@@ -1,147 +0,0 @@
|
|
|
1
|
-
import type { ClientSession, Collection, Db, Filter, Sort, UpdateFilter } from 'mongodb';
|
|
2
|
-
import type { DocumentOf, FieldOf, IdOf, NewDocumentOf, ReadDocumentOf } from '../definition/define-collection';
|
|
3
|
-
import type { CursorPage, Page, PageOptions } from '../pagination/page';
|
|
4
|
-
import type { SyncOptions, SyncReport } from '../sync/sync-collection';
|
|
5
|
-
export type OrderDirection = 'asc' | 'desc';
|
|
6
|
-
export interface ReadOptions {
|
|
7
|
-
/** Include soft-deleted documents. Ignored without a `deletedAt` field. */
|
|
8
|
-
withDeleted?: boolean;
|
|
9
|
-
}
|
|
10
|
-
export interface FindFirstOptions<Def> extends ReadOptions {
|
|
11
|
-
sort?: Sort;
|
|
12
|
-
projection?: Record<FieldOf<Def>, 0 | 1> | Record<string, unknown>;
|
|
13
|
-
}
|
|
14
|
-
export interface FindManyOptions<Def> extends FindFirstOptions<Def> {
|
|
15
|
-
filter?: Filter<DocumentOf<Def>>;
|
|
16
|
-
limit?: number;
|
|
17
|
-
skip?: number;
|
|
18
|
-
}
|
|
19
|
-
export interface PaginateOptions<Def> extends PageOptions, ReadOptions {
|
|
20
|
-
filter?: Filter<DocumentOf<Def>>;
|
|
21
|
-
/** Default `{ _id: 1 }`, so that pages are stable. */
|
|
22
|
-
sort?: Sort;
|
|
23
|
-
}
|
|
24
|
-
export interface CursorPaginateOptions<Def> extends ReadOptions {
|
|
25
|
-
/** The `nextCursor` of the previous page. Omit it for the first page. */
|
|
26
|
-
after?: string | null | undefined;
|
|
27
|
-
/** Documents per page. Default `20`, at most `maxPageSize`. */
|
|
28
|
-
limit?: number;
|
|
29
|
-
filter?: Filter<DocumentOf<Def>>;
|
|
30
|
-
/**
|
|
31
|
-
* The field to page along. Default `_id`. Any other is followed by `_id`,
|
|
32
|
-
* which breaks its ties, and must be set on every document.
|
|
33
|
-
*/
|
|
34
|
-
orderBy?: FieldOf<Def>;
|
|
35
|
-
/** Default `'asc'`. */
|
|
36
|
-
direction?: OrderDirection;
|
|
37
|
-
}
|
|
38
|
-
/**
|
|
39
|
-
* What an update writes: the document's own fields, checked against the
|
|
40
|
-
* schema, or MongoDB's operators for anything they cannot say.
|
|
41
|
-
*
|
|
42
|
-
* The driver's `UpdateFilter` is intersected with `Document`, so it accepts
|
|
43
|
-
* any key whatsoever; a plain patch is what gets checked.
|
|
44
|
-
*/
|
|
45
|
-
export type Patch<Def> = Partial<DocumentOf<Def>> | (UpdateFilter<DocumentOf<Def>> & {
|
|
46
|
-
[K in keyof DocumentOf<Def>]?: never;
|
|
47
|
-
} & {
|
|
48
|
-
id?: never;
|
|
49
|
-
});
|
|
50
|
-
export interface UpdateOptions {
|
|
51
|
-
/**
|
|
52
|
-
* Only update the document while its `version` is still this one. When it
|
|
53
|
-
* is not, nothing is written and `OptimisticLockError` is thrown with the
|
|
54
|
-
* version the document has now.
|
|
55
|
-
*/
|
|
56
|
-
expectedVersion?: number;
|
|
57
|
-
}
|
|
58
|
-
export interface RepositoryOptions {
|
|
59
|
-
/**
|
|
60
|
-
* Soft delete through the `deletedAt` field. Default: on when the schema
|
|
61
|
-
* has one. `false` makes `delete` a real delete.
|
|
62
|
-
*/
|
|
63
|
-
softDelete?: boolean;
|
|
64
|
-
/**
|
|
65
|
-
* Set `updatedAt` on every update that does not set it. Default: on when
|
|
66
|
-
* the schema has the field.
|
|
67
|
-
*/
|
|
68
|
-
touchUpdatedAt?: boolean;
|
|
69
|
-
/**
|
|
70
|
-
* Raise `version` by one on every update. Default: on when the schema has
|
|
71
|
-
* the field. `expectedVersion` needs it.
|
|
72
|
-
*/
|
|
73
|
-
optimisticLock?: boolean;
|
|
74
|
-
/**
|
|
75
|
-
* Check documents against the schema before writing them, which is also
|
|
76
|
-
* what fills their defaults. Default `'parse'`. `'off'` sends them as they
|
|
77
|
-
* are — and then nothing fills `_id`, `createdAt` or `version`.
|
|
78
|
-
*/
|
|
79
|
-
validate?: 'parse' | 'off';
|
|
80
|
-
/** The largest `pageSize` or `limit` a page may ask for. Default `100`. */
|
|
81
|
-
maxPageSize?: number;
|
|
82
|
-
/** The session every operation runs in. `with(session)` is how it is set. */
|
|
83
|
-
session?: ClientSession;
|
|
84
|
-
/** Who is writing, stamped into `createdBy`, `updatedBy` and `deletedBy`. */
|
|
85
|
-
actor?: unknown;
|
|
86
|
-
}
|
|
87
|
-
/**
|
|
88
|
-
* A repository over one collection: typed reads and writes by `_id` or by
|
|
89
|
-
* filter, pagination, soft delete, optimistic locking, audit stamps, and
|
|
90
|
-
* MongoDB errors turned into this package's.
|
|
91
|
-
*/
|
|
92
|
-
export interface Repository<Def> {
|
|
93
|
-
readonly definition: Def;
|
|
94
|
-
readonly db: Db;
|
|
95
|
-
/** The driver's collection, for anything this does not wrap. */
|
|
96
|
-
readonly collection: Collection<DocumentOf<Def> & Document>;
|
|
97
|
-
/** The session every operation of this repository runs in, if any. */
|
|
98
|
-
readonly session: ClientSession | undefined;
|
|
99
|
-
/**
|
|
100
|
-
* The same repository, bound to a session. MongoDB has no ambient
|
|
101
|
-
* session: without this, an operation inside a transaction runs outside
|
|
102
|
-
* it.
|
|
103
|
-
*/
|
|
104
|
-
with(session: ClientSession | undefined): Repository<Def>;
|
|
105
|
-
/** The same repository, stamping this actor into the `*By` fields. */
|
|
106
|
-
as(actor: unknown): Repository<Def>;
|
|
107
|
-
/** Creates the collection, its validator and its indexes. See `syncCollection`. */
|
|
108
|
-
sync(options?: SyncOptions): Promise<SyncReport>;
|
|
109
|
-
/** The document with this `_id`, or `undefined`. */
|
|
110
|
-
findById(id: IdOf<Def>, options?: ReadOptions): Promise<ReadDocumentOf<Def> | undefined>;
|
|
111
|
-
/** The document with this `_id`. Throws `NotFoundError`. */
|
|
112
|
-
getById(id: IdOf<Def>, options?: ReadOptions): Promise<ReadDocumentOf<Def>>;
|
|
113
|
-
/** The first document that matches, or `undefined`. */
|
|
114
|
-
findFirst(filter?: Filter<DocumentOf<Def>>, options?: FindFirstOptions<Def>): Promise<ReadDocumentOf<Def> | undefined>;
|
|
115
|
-
/** Every document that matches. */
|
|
116
|
-
findMany(options?: FindManyOptions<Def>): Promise<ReadDocumentOf<Def>[]>;
|
|
117
|
-
/** Checks the document against the schema, fills its defaults, inserts it. */
|
|
118
|
-
create(values: NewDocumentOf<Def>): Promise<ReadDocumentOf<Def>>;
|
|
119
|
-
/** The same, in one insert. `[]` sends nothing. */
|
|
120
|
-
createMany(values: readonly NewDocumentOf<Def>[]): Promise<ReadDocumentOf<Def>[]>;
|
|
121
|
-
/** Updates the document with this `_id` and returns it. Throws `NotFoundError`. */
|
|
122
|
-
update(id: IdOf<Def>, patch: Patch<Def>, options?: UpdateOptions): Promise<ReadDocumentOf<Def>>;
|
|
123
|
-
/** Updates every document that matches, and returns how many changed. */
|
|
124
|
-
updateMany(filter: Filter<DocumentOf<Def>>, patch: Patch<Def>): Promise<number>;
|
|
125
|
-
/**
|
|
126
|
-
* Deletes the document with this `_id` and returns it: a soft delete on a
|
|
127
|
-
* collection with `deletedAt`. Throws `NotFoundError`.
|
|
128
|
-
*/
|
|
129
|
-
delete(id: IdOf<Def>): Promise<ReadDocumentOf<Def>>;
|
|
130
|
-
/** Deletes every document that matches, and returns how many. */
|
|
131
|
-
deleteMany(filter: Filter<DocumentOf<Def>>): Promise<number>;
|
|
132
|
-
/** A real delete, of a live or a soft-deleted document. */
|
|
133
|
-
hardDelete(id: IdOf<Def>): Promise<ReadDocumentOf<Def>>;
|
|
134
|
-
/** A real delete of every document that matches, soft-deleted ones included. */
|
|
135
|
-
hardDeleteMany(filter: Filter<DocumentOf<Def>>): Promise<number>;
|
|
136
|
-
/** Clears `deletedAt` and returns the document. Throws `NotFoundError`. */
|
|
137
|
-
restore(id: IdOf<Def>): Promise<ReadDocumentOf<Def>>;
|
|
138
|
-
/** How many documents match. */
|
|
139
|
-
count(filter?: Filter<DocumentOf<Def>>, options?: ReadOptions): Promise<number>;
|
|
140
|
-
/** Whether any document matches. */
|
|
141
|
-
exists(filter: Filter<DocumentOf<Def>>, options?: ReadOptions): Promise<boolean>;
|
|
142
|
-
/** One page of the documents that match, and how many there are. */
|
|
143
|
-
paginate(options?: PaginateOptions<Def>): Promise<Page<ReadDocumentOf<Def>>>;
|
|
144
|
-
/** One page of the documents that match, after a cursor. */
|
|
145
|
-
paginateByCursor(options?: CursorPaginateOptions<Def>): Promise<CursorPage<ReadDocumentOf<Def>>>;
|
|
146
|
-
}
|
|
147
|
-
//# sourceMappingURL=types.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":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