@radatek/microserver 2.3.0 → 2.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/microserver.d.ts +9 -13
- package/microserver.js +73 -81
- package/package.json +1 -1
package/microserver.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* MicroServer
|
|
3
|
-
* @version 2.3.
|
|
3
|
+
* @version 2.3.1
|
|
4
4
|
* @package @radatek/microserver
|
|
5
5
|
* @copyright Darius Kisonas 2022
|
|
6
6
|
* @license MIT
|
|
@@ -739,9 +739,9 @@ export declare class Model<TSchema extends ModelSchema> {
|
|
|
739
739
|
/** Find many documents */
|
|
740
740
|
findMany(query: Query, options?: ModelContextOptions): Promise<ModelDocument<TSchema>[]>;
|
|
741
741
|
/** Insert a new document */
|
|
742
|
-
insert(data: Record<string, any>, options?: ModelContextOptions): Promise<
|
|
742
|
+
insert(data: Record<string, any>, options?: ModelContextOptions): Promise<ModelDocument<TSchema>>;
|
|
743
743
|
/** Update one matching document */
|
|
744
|
-
update(query: Record<string, any>, options?: ModelContextOptions): Promise<
|
|
744
|
+
update(query: Record<string, any>, options?: ModelContextOptions): Promise<ModelDocument<TSchema>>;
|
|
745
745
|
/** Delete one matching document */
|
|
746
746
|
delete(query: Query, options?: ModelContextOptions): Promise<void>;
|
|
747
747
|
/** Microserver middleware */
|
|
@@ -750,10 +750,6 @@ export declare class Model<TSchema extends ModelSchema> {
|
|
|
750
750
|
export declare interface MicroCollectionOptions<T extends ModelSchema = any> {
|
|
751
751
|
/** Collection name */
|
|
752
752
|
name?: string;
|
|
753
|
-
/** Collection persistent store */
|
|
754
|
-
store?: FileStore;
|
|
755
|
-
/** Custom data loader */
|
|
756
|
-
load?: (col: MicroCollection) => Promise<object>;
|
|
757
753
|
/** Custom data saver */
|
|
758
754
|
save?: (id: string, doc: ModelDocument<T> | undefined, col: MicroCollection) => Promise<ModelDocument<T>>;
|
|
759
755
|
/** Preloaded data object */
|
|
@@ -773,8 +769,8 @@ export declare interface FindOptions {
|
|
|
773
769
|
query?: Query;
|
|
774
770
|
/** is upsert */
|
|
775
771
|
upsert?: boolean;
|
|
776
|
-
/** is
|
|
777
|
-
|
|
772
|
+
/** is upsert */
|
|
773
|
+
delete?: boolean;
|
|
778
774
|
/** update object */
|
|
779
775
|
update?: Query;
|
|
780
776
|
/** maximum number of hits */
|
|
@@ -802,20 +798,20 @@ export declare class MicroCollection<TSchema extends ModelSchema = any> {
|
|
|
802
798
|
/** Find all matching documents */
|
|
803
799
|
find(query: Query): Cursor<TSchema>;
|
|
804
800
|
/** Find and modify one matching document */
|
|
805
|
-
findAndModify(options: FindOptions): Promise<
|
|
801
|
+
findAndModify(options: FindOptions): Promise<ModelDocument<TSchema> | undefined>;
|
|
806
802
|
/** Insert one document */
|
|
807
803
|
insertOne(doc: ModelDocument<TSchema>): Promise<ModelDocument<TSchema>>;
|
|
808
804
|
/** Insert multiple documents */
|
|
809
805
|
insertMany(docs: ModelDocument<TSchema>[]): Promise<ModelDocument<TSchema>[]>;
|
|
810
806
|
/** Delete one matching document */
|
|
811
|
-
deleteOne(query: Query): Promise<
|
|
807
|
+
deleteOne(query: Query): Promise<number>;
|
|
812
808
|
/** Delete all matching documents */
|
|
813
809
|
deleteMany(query: Query): Promise<number>;
|
|
814
|
-
updateOne(query: Query,
|
|
810
|
+
updateOne(query: Query, update: Record<string, any>, options?: FindOptions): Promise<{
|
|
815
811
|
upsertedId: any;
|
|
816
812
|
modifiedCount: number;
|
|
817
813
|
}>;
|
|
818
|
-
updateMany(query: Query, update:
|
|
814
|
+
updateMany(query: Query, update: Record<string, any>, options?: FindOptions): Promise<{
|
|
819
815
|
upsertedId: any;
|
|
820
816
|
modifiedCount: number;
|
|
821
817
|
}>;
|
package/microserver.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* MicroServer
|
|
3
|
-
* @version 2.3.
|
|
3
|
+
* @version 2.3.1
|
|
4
4
|
* @package @radatek/microserver
|
|
5
5
|
* @copyright Darius Kisonas 2022
|
|
6
6
|
* @license MIT
|
|
@@ -2916,6 +2916,9 @@ export class Model {
|
|
|
2916
2916
|
}
|
|
2917
2917
|
}
|
|
2918
2918
|
const res = await this.collection.findAndModify({ query: this.getFilter(query, { required: true, validate: false, default: false }), update: query, upsert: options?.insert });
|
|
2919
|
+
if (!res)
|
|
2920
|
+
throw new NotFound('Document not found');
|
|
2921
|
+
return res;
|
|
2919
2922
|
}
|
|
2920
2923
|
/** Delete one matching document */
|
|
2921
2924
|
async delete(query, options) {
|
|
@@ -3035,54 +3038,20 @@ export class MicroCollection {
|
|
|
3035
3038
|
}
|
|
3036
3039
|
/** Find and modify one matching document */
|
|
3037
3040
|
async findAndModify(options) {
|
|
3038
|
-
|
|
3039
|
-
|
|
3040
|
-
|
|
3041
|
-
|
|
3042
|
-
|
|
3043
|
-
|
|
3044
|
-
|
|
3045
|
-
|
|
3046
|
-
Object.assign(doc, options.update);
|
|
3047
|
-
if (this._save) {
|
|
3048
|
-
promise = promise.then(async () => {
|
|
3049
|
-
if (!doc._id)
|
|
3050
|
-
throw new Error('Internal error: missing _id in document');
|
|
3051
|
-
this.data[doc._id] = await this._save?.(doc._id, doc, this) || this.data[doc._id];
|
|
3052
|
-
});
|
|
3053
|
-
}
|
|
3054
|
-
count++;
|
|
3055
|
-
if (options.limit && count >= options.limit)
|
|
3056
|
-
return false;
|
|
3057
|
-
}
|
|
3058
|
-
});
|
|
3059
|
-
await promise;
|
|
3060
|
-
return count;
|
|
3061
|
-
}
|
|
3062
|
-
let doc = this.queryDocument(options.query, this.data[id]);
|
|
3063
|
-
if (!doc) {
|
|
3064
|
-
if (!options.upsert && !options.new)
|
|
3065
|
-
throw new InvalidData(`Document not found`);
|
|
3066
|
-
doc = { _id: id };
|
|
3067
|
-
this.data[id] = doc;
|
|
3068
|
-
}
|
|
3069
|
-
else {
|
|
3070
|
-
if (options.new)
|
|
3071
|
-
throw new InvalidData(`Document dupplicate`);
|
|
3072
|
-
}
|
|
3073
|
-
if (options.update) {
|
|
3074
|
-
for (const n in options.update) {
|
|
3041
|
+
const res = await this.findOne(options.query || {});
|
|
3042
|
+
if (res?._id) {
|
|
3043
|
+
await this.updateOne({ _id: res._id }, options.update || {}, options);
|
|
3044
|
+
return this.data[res._id];
|
|
3045
|
+
}
|
|
3046
|
+
if (options.upsert) {
|
|
3047
|
+
const doc = { ...options.query };
|
|
3048
|
+
for (const n in options.update)
|
|
3075
3049
|
if (!n.startsWith('$'))
|
|
3076
3050
|
doc[n] = options.update[n];
|
|
3077
|
-
|
|
3078
|
-
|
|
3079
|
-
|
|
3080
|
-
delete doc[n];
|
|
3081
|
-
}
|
|
3051
|
+
if (options.update?.$set)
|
|
3052
|
+
Object.assign(doc, options.update.$set);
|
|
3053
|
+
return this.insertOne(doc);
|
|
3082
3054
|
}
|
|
3083
|
-
if (this._save)
|
|
3084
|
-
this.data[id] = await this._save(id, doc, this) || doc;
|
|
3085
|
-
return 1;
|
|
3086
3055
|
}
|
|
3087
3056
|
/** Insert one document */
|
|
3088
3057
|
async insertOne(doc) {
|
|
@@ -3108,49 +3077,72 @@ export class MicroCollection {
|
|
|
3108
3077
|
}
|
|
3109
3078
|
/** Delete one matching document */
|
|
3110
3079
|
async deleteOne(query) {
|
|
3111
|
-
|
|
3112
|
-
if (!id)
|
|
3113
|
-
return;
|
|
3114
|
-
delete this.data[id];
|
|
3080
|
+
return (await this.updateMany(query, {}, { delete: true, limit: 1 })).modifiedCount;
|
|
3115
3081
|
}
|
|
3116
3082
|
/** Delete all matching documents */
|
|
3117
3083
|
async deleteMany(query) {
|
|
3118
|
-
|
|
3084
|
+
return (await this.updateMany(query, {}, { delete: true })).modifiedCount;
|
|
3085
|
+
}
|
|
3086
|
+
async updateOne(query, update, options) {
|
|
3087
|
+
const res = await this.updateMany(query, update, { ...options, limit: 1 });
|
|
3088
|
+
return res;
|
|
3089
|
+
}
|
|
3090
|
+
async updateMany(query, update, options) {
|
|
3091
|
+
let res = { upsertedId: undefined, modifiedCount: 0 };
|
|
3092
|
+
if (!query)
|
|
3093
|
+
return res;
|
|
3119
3094
|
let promise = Promise.resolve();
|
|
3120
3095
|
this.find(query).forEach((doc) => {
|
|
3121
3096
|
if (this.queryDocument(query, doc)) {
|
|
3122
|
-
|
|
3123
|
-
|
|
3124
|
-
|
|
3125
|
-
|
|
3126
|
-
|
|
3127
|
-
|
|
3097
|
+
if (options?.delete) {
|
|
3098
|
+
if (!doc._id)
|
|
3099
|
+
return;
|
|
3100
|
+
res.modifiedCount++;
|
|
3101
|
+
if (this._save)
|
|
3102
|
+
promise = promise.then(async () => { doc._id && this._save?.(doc._id, undefined, this); });
|
|
3103
|
+
delete this.data[doc._id];
|
|
3104
|
+
}
|
|
3105
|
+
else {
|
|
3106
|
+
Object.assign(doc, update);
|
|
3107
|
+
res.modifiedCount++;
|
|
3108
|
+
if (this._save) {
|
|
3109
|
+
promise = promise.then(async () => {
|
|
3110
|
+
if (!doc._id)
|
|
3111
|
+
throw new Error('Internal error: missing _id in document');
|
|
3112
|
+
this.data[doc._id] = await this._save?.(doc._id, doc, this) || this.data[doc._id];
|
|
3113
|
+
});
|
|
3114
|
+
}
|
|
3115
|
+
}
|
|
3116
|
+
if (options?.limit && res.modifiedCount >= options?.limit)
|
|
3117
|
+
return false;
|
|
3128
3118
|
}
|
|
3129
3119
|
});
|
|
3130
3120
|
await promise;
|
|
3131
|
-
|
|
3132
|
-
|
|
3133
|
-
|
|
3134
|
-
|
|
3135
|
-
|
|
3136
|
-
|
|
3137
|
-
|
|
3138
|
-
|
|
3139
|
-
|
|
3140
|
-
|
|
3141
|
-
|
|
3142
|
-
|
|
3143
|
-
|
|
3144
|
-
|
|
3145
|
-
|
|
3146
|
-
|
|
3147
|
-
|
|
3148
|
-
|
|
3149
|
-
update
|
|
3150
|
-
|
|
3151
|
-
|
|
3152
|
-
|
|
3153
|
-
|
|
3154
|
-
|
|
3121
|
+
if (res.modifiedCount || !options?.upsert || options?.delete)
|
|
3122
|
+
return res;
|
|
3123
|
+
if (!query._id)
|
|
3124
|
+
query._id = res.upsertedId = newObjectId();
|
|
3125
|
+
let doc = this.queryDocument(options.query, this.data[query._id]);
|
|
3126
|
+
if (doc)
|
|
3127
|
+
throw new InvalidData(`Document dupplicate`);
|
|
3128
|
+
doc = { _id: query._id, ...query };
|
|
3129
|
+
this.data[query._id] = doc;
|
|
3130
|
+
if (update) {
|
|
3131
|
+
for (const n in update) {
|
|
3132
|
+
if (!n.startsWith('$'))
|
|
3133
|
+
doc[n] = update[n];
|
|
3134
|
+
}
|
|
3135
|
+
if (update.$set) {
|
|
3136
|
+
for (const n in update.$set)
|
|
3137
|
+
doc[n] = update.$set[n];
|
|
3138
|
+
}
|
|
3139
|
+
if (update.$unset) {
|
|
3140
|
+
for (const n in update.$unset)
|
|
3141
|
+
delete doc[n];
|
|
3142
|
+
}
|
|
3143
|
+
}
|
|
3144
|
+
if (this._save)
|
|
3145
|
+
this.data[query._id] = await this._save(query._id, doc, this) || doc;
|
|
3146
|
+
return res;
|
|
3155
3147
|
}
|
|
3156
3148
|
}
|