akanjs 2.3.11-rc.0 → 2.3.11-rc.2

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.
@@ -53,21 +53,67 @@ export type DocumentQueryValue =
53
53
  | undefined
54
54
  | Record<string, unknown>;
55
55
 
56
- export interface DocumentUpdate {
57
- [key: string]: unknown;
58
- set?: Record<string, unknown>;
59
- unset?: Record<string, unknown> | string[];
60
- addToSet?: Record<string, unknown>;
61
- pull?: Record<string, unknown>;
62
- push?: Record<string, unknown>;
63
- inc?: Record<string, number>;
64
- setOnInsert?: Record<string, unknown>;
56
+ export type DocumentUpdateOperator =
57
+ | "set"
58
+ | "unset"
59
+ | "inc"
60
+ | "mul"
61
+ | "min"
62
+ | "max"
63
+ | "push"
64
+ | "pull"
65
+ | "addToSet"
66
+ | "setOnInsert";
67
+
68
+ export interface DocumentUpdateNode {
69
+ kind: "update";
70
+ op: DocumentUpdateOperator;
71
+ value?: unknown;
65
72
  }
66
73
 
74
+ export type DocumentUpdateValue =
75
+ | DocumentUpdateNode
76
+ | DocumentPrimitive
77
+ | DocumentPrimitive[]
78
+ | Record<string, unknown>
79
+ | undefined;
80
+
81
+ export type DocumentUpdate<T = any> = {
82
+ [K in DocumentPath<T>]?: DocumentUpdateValue;
83
+ };
84
+
67
85
  export interface DocumentUpdateOptions {
68
86
  upsert?: boolean;
69
87
  }
70
88
 
89
+ const updateOp = (op: DocumentUpdateOperator, value?: unknown): DocumentUpdateNode => ({ kind: "update", op, value });
90
+
91
+ export const createDocumentUpdateHelper = () => ({
92
+ set: (value: unknown) => updateOp("set", value),
93
+ unset: () => updateOp("unset"),
94
+ inc: (by = 1) => updateOp("inc", by),
95
+ mul: (by: number) => updateOp("mul", by),
96
+ min: (value: unknown) => updateOp("min", value),
97
+ max: (value: unknown) => updateOp("max", value),
98
+ push: (value: unknown) => updateOp("push", value),
99
+ pull: (value: unknown) => updateOp("pull", value),
100
+ addToSet: (value: unknown) => updateOp("addToSet", value),
101
+ setOnInsert: (value: unknown) => updateOp("setOnInsert", value),
102
+ });
103
+
104
+ export type DocumentUpdateHelper = ReturnType<typeof createDocumentUpdateHelper>;
105
+
106
+ export const documentUpdateHelper = createDocumentUpdateHelper();
107
+
108
+ export type DocumentUpdateBuilder<T = any> = (helper: DocumentUpdateHelper) => DocumentUpdate<T>;
109
+ export type DocumentUpdateInput<T = any> = DocumentUpdate<T> | DocumentUpdateBuilder<T>;
110
+
111
+ export const resolveDocumentUpdate = <T>(update: DocumentUpdateInput<T>): DocumentUpdate<T> =>
112
+ typeof update === "function" ? update(documentUpdateHelper) : update;
113
+
114
+ export const isDocumentUpdateNode = (value: unknown): value is DocumentUpdateNode =>
115
+ !!value && typeof value === "object" && (value as { kind?: unknown }).kind === "update";
116
+
71
117
  export type DocumentQuery<T = any> =
72
118
  | DocumentQueryNode
73
119
  | {
package/document/into.ts CHANGED
@@ -4,7 +4,7 @@ import type { DocumentModel, QueryOf } from "akanjs/constant";
4
4
  import type { FilterCls, FilterQueryOf, FilterSortOf, SchemaOf } from ".";
5
5
  import type { CacheDatabase, QueryMethodPart } from "./database";
6
6
  import type { DataLoader } from "./dataLoader";
7
- import type { DocumentUpdate, DocumentUpdateOptions } from "./documentQuery";
7
+ import type { DocumentQuery, DocumentUpdateInput, DocumentUpdateOptions } from "./documentQuery";
8
8
  import { type LoaderBuilder, type ModelCls, makeLoaderBuilder } from "./loaderInfo";
9
9
  import type { DocumentProjection } from "./types";
10
10
 
@@ -15,7 +15,7 @@ interface DefaultMdlStats<
15
15
  TDocument,
16
16
  TSchema,
17
17
  _Partial extends Partial<TSchema> = Partial<TSchema>,
18
- _FilterQuery extends QueryOf<TSchema> = QueryOf<TSchema>,
18
+ _FilterQuery extends DocumentQuery<TSchema> = DocumentQuery<TSchema>,
19
19
  _Projection = DocumentProjection<TSchema>,
20
20
  > {
21
21
  pickOneAndWrite: (query: _FilterQuery, rawData: _Partial) => Promise<TDocument>;
@@ -47,10 +47,10 @@ export interface UpdateResult {
47
47
  modifiedCount: number;
48
48
  upsertedId?: string | null;
49
49
  }
50
- export interface BulkWriteOperation<Raw, _RawDoc = DocumentModel<Raw>, _RawQuery = QueryOf<_RawDoc>> {
50
+ export interface BulkWriteOperation<Raw, _RawDoc = DocumentModel<Raw>, _RawQuery = DocumentQuery<_RawDoc>> {
51
51
  updateOne: {
52
52
  filter: _RawQuery;
53
- update: DocumentUpdate;
53
+ update: DocumentUpdateInput<_RawDoc>;
54
54
  upsert?: boolean;
55
55
  };
56
56
  }
@@ -69,7 +69,7 @@ export type Mdl<
69
69
  Doc,
70
70
  Raw,
71
71
  _RawDoc = DocumentModel<Raw>,
72
- _RawQuery = QueryOf<_RawDoc>,
72
+ _RawQuery extends DocumentQuery<_RawDoc> = DocumentQuery<_RawDoc>,
73
73
  _Projection extends DocumentProjection<Raw> = DocumentProjection<Raw>,
74
74
  > = DefaultMdlStats<Doc, _RawDoc, Partial<_RawDoc>, _RawQuery, _Projection> & {
75
75
  refName: string;
@@ -79,8 +79,12 @@ export type Mdl<
79
79
  findById(id: string | undefined, projection?: _Projection): Promise<Doc | null>;
80
80
  countDocuments(query: _RawQuery): Promise<number>;
81
81
  exists(query: _RawQuery): Promise<string | null>;
82
- updateOne(query: _RawQuery, update: DocumentUpdate, options?: DocumentUpdateOptions): Promise<UpdateResult>;
83
- updateMany(query: _RawQuery, update: DocumentUpdate): Promise<UpdateResult>;
82
+ updateOne(
83
+ query: _RawQuery,
84
+ update: DocumentUpdateInput<_RawDoc>,
85
+ options?: DocumentUpdateOptions,
86
+ ): Promise<UpdateResult>;
87
+ updateMany(query: _RawQuery, update: DocumentUpdateInput<_RawDoc>): Promise<UpdateResult>;
84
88
  deleteMany(query: _RawQuery): Promise<UpdateResult>;
85
89
  bulkWrite(operations: BulkWriteOperation<Raw, _RawDoc, _RawQuery>[]): Promise<UpdateResult>;
86
90
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "akanjs",
3
- "version": "2.3.11-rc.0",
3
+ "version": "2.3.11-rc.2",
4
4
  "sourceType": "module",
5
5
  "type": "module",
6
6
  "publishConfig": {
@@ -9,6 +9,7 @@ import {
9
9
  type DataInputOf,
10
10
  DataLoader,
11
11
  DocumentSchema,
12
+ type DocumentUpdateInput,
12
13
  documentQueryHelper,
13
14
  type FindQueryOption,
14
15
  fillMissingFilterArgs,
@@ -219,12 +220,12 @@ export class DatabaseResolver {
219
220
  findOne: (query: QueryOf<any>) => createFindOneChain(query),
220
221
  findById: (id: string | undefined) => (id ? store.findOne({ id }) : Promise.resolve(null)),
221
222
  countDocuments: (query: QueryOf<any>) => store.count(query),
222
- updateOne: (query: QueryOf<any>, update: Record<string, unknown>, options?: { upsert?: boolean }) =>
223
+ updateOne: (query: QueryOf<any>, update: DocumentUpdateInput, options?: { upsert?: boolean }) =>
223
224
  store.updateOneByQuery(query, update, options),
224
- updateMany: (query: QueryOf<any>, update: Record<string, unknown>) => store.updateManyByQuery(query, update),
225
+ updateMany: (query: QueryOf<any>, update: DocumentUpdateInput) => store.updateManyByQuery(query, update),
225
226
  deleteMany: (query: QueryOf<any>) => store.deleteManyByQuery(query),
226
227
  bulkWrite: (
227
- operations: { updateOne: { filter: QueryOf<any>; update: Record<string, unknown>; upsert?: boolean } }[],
228
+ operations: { updateOne: { filter: QueryOf<any>; update: DocumentUpdateInput; upsert?: boolean } }[],
228
229
  ) => store.bulkWrite(operations),
229
230
  listenPre: (type: SaveEventType, listener: (doc: any, type: CRUDEventType) => PromiseOrObject<void>) =>
230
231
  schema.pre(type, function (this: any, _next, crudType) {