@liminalfunctions/framework 1.0.74 → 1.0.75
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/dist/F_Collection.d.ts +4 -4
- package/dist/F_Collection.js +33 -21
- package/dist/F_Collection.js.map +1 -1
- package/package.json +1 -1
- package/src/F_Collection.ts +40 -27
package/dist/F_Collection.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import * as z from "zod/v4";
|
|
2
|
-
import mongoose, { Model } from "mongoose";
|
|
2
|
+
import mongoose, { ClientSession, Model } from "mongoose";
|
|
3
3
|
import { F_Security_Model } from "./F_Security_Models/F_Security_Model.js";
|
|
4
4
|
export type CollectionType<Col extends F_Collection<string, Validator>, Validator extends z.ZodObject> = z.output<Col['validator']>;
|
|
5
5
|
export type F_Layer<Collection_ID extends string, ZodSchema extends z.ZodObject> = {
|
|
@@ -40,8 +40,8 @@ export declare class F_Collection<Collection_ID extends string, ZodSchema extend
|
|
|
40
40
|
after_create(hook: (created_document: z.output<ZodSchema>) => Promise<void>): void;
|
|
41
41
|
after_update(hook: (updated_document: z.output<ZodSchema>) => Promise<void>): void;
|
|
42
42
|
after_delete(hook: (deleted_document: z.output<ZodSchema>) => Promise<void>): void;
|
|
43
|
-
perform_create_and_side_effects(data: z.output<this['post_validator']
|
|
44
|
-
perform_update_and_side_effects(find: any, data: z.output<this['put_validator']
|
|
45
|
-
perform_delete_and_side_effects(find: any): Promise<z.output<ZodSchema>>;
|
|
43
|
+
perform_create_and_side_effects(data: z.output<this['post_validator']>, session?: ClientSession): Promise<z.output<ZodSchema>>;
|
|
44
|
+
perform_update_and_side_effects(find: any, data: z.output<this['put_validator']>, session?: ClientSession): Promise<z.output<ZodSchema>>;
|
|
45
|
+
perform_delete_and_side_effects(find: any, session?: ClientSession): Promise<z.output<ZodSchema>>;
|
|
46
46
|
}
|
|
47
47
|
export {};
|
package/dist/F_Collection.js
CHANGED
|
@@ -85,16 +85,16 @@ export class F_Collection {
|
|
|
85
85
|
after_delete(hook) {
|
|
86
86
|
this.post_delete_hooks.push(hook);
|
|
87
87
|
}
|
|
88
|
-
async perform_create_and_side_effects(data) {
|
|
88
|
+
async perform_create_and_side_effects(data, session) {
|
|
89
89
|
let created_document_data;
|
|
90
|
-
if (this.create_hooks.length > 0) {
|
|
91
|
-
await
|
|
90
|
+
if (this.create_hooks.length > 0 || session) {
|
|
91
|
+
await create_or_use_session(async (session) => {
|
|
92
92
|
let [created_document] = await this.mongoose_model.create([data], { session: session, lean: true });
|
|
93
93
|
created_document_data = created_document;
|
|
94
94
|
for (let hook of this.create_hooks) {
|
|
95
95
|
await hook(session, created_document);
|
|
96
96
|
}
|
|
97
|
-
});
|
|
97
|
+
}, session);
|
|
98
98
|
}
|
|
99
99
|
else {
|
|
100
100
|
created_document_data = await this.mongoose_model.create(data);
|
|
@@ -110,16 +110,16 @@ export class F_Collection {
|
|
|
110
110
|
}
|
|
111
111
|
return created_document_data;
|
|
112
112
|
}
|
|
113
|
-
async perform_update_and_side_effects(find, data) {
|
|
113
|
+
async perform_update_and_side_effects(find, data, session) {
|
|
114
114
|
let update_document_data;
|
|
115
|
-
if (this.update_hooks.length > 0) {
|
|
116
|
-
await
|
|
115
|
+
if (this.update_hooks.length > 0 || session) {
|
|
116
|
+
await create_or_use_session(async (session) => {
|
|
117
117
|
let updated_document = await this.mongoose_model.findOneAndUpdate(find, data, { returnDocument: 'after', session: session, lean: true });
|
|
118
118
|
update_document_data = updated_document;
|
|
119
119
|
for (let hook of this.update_hooks) {
|
|
120
120
|
await hook(session, updated_document);
|
|
121
121
|
}
|
|
122
|
-
});
|
|
122
|
+
}, session);
|
|
123
123
|
}
|
|
124
124
|
else {
|
|
125
125
|
update_document_data = await this.mongoose_model.findOneAndUpdate(find, data, { returnDocument: 'after', lean: true });
|
|
@@ -135,30 +135,42 @@ export class F_Collection {
|
|
|
135
135
|
}
|
|
136
136
|
return update_document_data;
|
|
137
137
|
}
|
|
138
|
-
async perform_delete_and_side_effects(find) {
|
|
138
|
+
async perform_delete_and_side_effects(find, session) {
|
|
139
139
|
let deleted_document_data;
|
|
140
|
-
if (this.delete_hooks.length > 0) {
|
|
141
|
-
await
|
|
140
|
+
if (this.delete_hooks.length > 0 || session) {
|
|
141
|
+
await create_or_use_session(async (session) => {
|
|
142
142
|
let deleted_document = await this.mongoose_model.findOneAndDelete(find, { returnDocument: 'after', session: session, lean: true });
|
|
143
143
|
deleted_document_data = deleted_document;
|
|
144
|
-
|
|
145
|
-
|
|
144
|
+
if (deleted_document_data) {
|
|
145
|
+
for (let hook of this.delete_hooks) {
|
|
146
|
+
await hook(session, deleted_document);
|
|
147
|
+
}
|
|
146
148
|
}
|
|
147
|
-
});
|
|
149
|
+
}, session);
|
|
148
150
|
}
|
|
149
151
|
else {
|
|
150
152
|
deleted_document_data = await this.mongoose_model.findOneAndDelete(find, { returnDocument: 'after', lean: true });
|
|
151
153
|
}
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
154
|
+
if (deleted_document_data) {
|
|
155
|
+
for (let hook of this.post_delete_hooks) {
|
|
156
|
+
try {
|
|
157
|
+
await hook(deleted_document_data);
|
|
158
|
+
}
|
|
159
|
+
catch (err) {
|
|
160
|
+
console.error(`Error in ${this.collection_id} after_delete:`);
|
|
161
|
+
console.error(err);
|
|
162
|
+
}
|
|
159
163
|
}
|
|
160
164
|
}
|
|
161
165
|
return deleted_document_data;
|
|
162
166
|
}
|
|
163
167
|
}
|
|
168
|
+
async function create_or_use_session(callback, session) {
|
|
169
|
+
if (session) {
|
|
170
|
+
return await callback(session);
|
|
171
|
+
}
|
|
172
|
+
else {
|
|
173
|
+
return await mongoose.connection.transaction(callback);
|
|
174
|
+
}
|
|
175
|
+
}
|
|
164
176
|
//# sourceMappingURL=F_Collection.js.map
|
package/dist/F_Collection.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"F_Collection.js","sourceRoot":"","sources":["../src/F_Collection.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,iBAAiB,EAAE,eAAe,EAAE,MAAM,8BAA8B,CAAC;AAClF,OAAO,
|
|
1
|
+
{"version":3,"file":"F_Collection.js","sourceRoot":"","sources":["../src/F_Collection.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,iBAAiB,EAAE,eAAe,EAAE,MAAM,8BAA8B,CAAC;AAClF,OAAO,QAAwD,MAAM,UAAU,CAAC;AAEhF,OAAO,EAAE,wBAAwB,EAAE,MAAM,qCAAqC,CAAC;AAC/E,OAAO,EAAE,uBAAuB,EAAE,MAAM,oCAAoC,CAAC;AAC7E,OAAO,EAAE,gCAAgC,EAAE,MAAM,6CAA6C,CAAC;AAY/F,MAAM,OAAO,YAAY;IACrB,aAAa,CAAgB;IAC7B,sBAAsB,CAAS;IAC/B,SAAS,CAAY;IACrB,eAAe,CAAM;IACrB,cAAc,CAA4B;IAE1C,sBAAsB,CAAY;IAClC,sBAAsB,CAAY;IAClC,+BAA+B,CAAY;IAC3C,+BAA+B,CAAY;IAC3C,aAAa,CAAmC;IAGhD,cAAc,CAA+C;IAC7D,WAAW,CAAU;IAErB,kBAAkB,CAAyB;IAC3C,uBAAuB,CAAyB;IAEhD,aAAa,CAAsC;IACnD,YAAY,CAAsG;IAClH,YAAY,CAAsG;IAClH,YAAY,CAAsG;IAClH,iBAAiB,CAA+D;IAChF,iBAAiB,CAA+D;IAChF,iBAAiB,CAA+D;IAEhF,YAAY,eAA8B,EAAE,sBAA8B,EAAE,SAAoB,EAAE,WAA4B,QAAQ;QAClI,IAAI,CAAC,aAAa,GAAG,eAAe,CAAC;QACrC,IAAI,CAAC,sBAAsB,GAAG,sBAAsB,CAAC;QACrD,IAAI,CAAC,SAAS,GAAG,SAAS,CAAC;QAC3B,IAAI,CAAC,eAAe,GAAG,eAAe,CAAC,SAAS,CAAC,CAAC;QAClD,IAAI,CAAC,cAAc,GAAG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BACvB,CAAC;YACL,CAAC;QACL,CAAC;QAED,OAAO,qBAAqB,CAAC;IACjC,CAAC;CACJ;AAED,KAAK,UAAU,qBAAqB,CAAC,QAAmD,EAAE,OAAuB;IAC7G,IAAG,OAAO,EAAE,CAAC;QACT,OAAO,MAAM,QAAQ,CAAC,OAAO,CAAC,CAAC;IACnC,CAAC;SAAM,CAAC;QACL,OAAO,MAAM,QAAQ,CAAC,UAAU,CAAC,WAAW,CAAC,QAAQ,CAAC,CAAC;IAC1D,CAAC;AACL,CAAC"}
|
package/package.json
CHANGED
package/src/F_Collection.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import * as z from "zod/v4";
|
|
2
2
|
import { mongoose_from_zod, schema_from_zod } from "./utils/mongoose_from_zod.js";
|
|
3
|
-
import mongoose, { Collection, Model, ObjectId } from "mongoose";
|
|
3
|
+
import mongoose, { ClientSession, Collection, Model, ObjectId } from "mongoose";
|
|
4
4
|
import { F_Security_Model } from "./F_Security_Models/F_Security_Model.js";
|
|
5
5
|
import { query_validator_from_zod } from "./utils/query_validator_from_zod.js";
|
|
6
6
|
import { array_children_from_zod } from "./utils/array_children_from_zod.js";
|
|
@@ -160,12 +160,12 @@ export class F_Collection<Collection_ID extends string, ZodSchema extends z.ZodO
|
|
|
160
160
|
* If there are no side-effects specified by the on_create method, runs a normal non-transaction create to avoid
|
|
161
161
|
* the performance impacts of a transaction.
|
|
162
162
|
*/
|
|
163
|
-
async perform_create_and_side_effects(data: z.output<this['post_validator']
|
|
163
|
+
async perform_create_and_side_effects(data: z.output<this['post_validator']>, session?: ClientSession): Promise<z.output<ZodSchema>> {
|
|
164
164
|
let created_document_data;
|
|
165
165
|
|
|
166
166
|
// if we have any create hooks, run the create operation in a transaction
|
|
167
|
-
if(this.create_hooks.length > 0){
|
|
168
|
-
await
|
|
167
|
+
if(this.create_hooks.length > 0 || session){
|
|
168
|
+
await create_or_use_session(async (session) => {
|
|
169
169
|
// create the document
|
|
170
170
|
//@ts-expect-error
|
|
171
171
|
let [created_document] = await this.mongoose_model.create([data], {session: session, lean: true});
|
|
@@ -176,7 +176,7 @@ export class F_Collection<Collection_ID extends string, ZodSchema extends z.ZodO
|
|
|
176
176
|
for(let hook of this.create_hooks){
|
|
177
177
|
await hook(session, created_document);
|
|
178
178
|
}
|
|
179
|
-
});
|
|
179
|
+
}, session);
|
|
180
180
|
} else {// if we don't have any post create hooks, run the create operation normally
|
|
181
181
|
//@ts-expect-error
|
|
182
182
|
created_document_data = await this.mongoose_model.create(data);
|
|
@@ -204,12 +204,12 @@ export class F_Collection<Collection_ID extends string, ZodSchema extends z.ZodO
|
|
|
204
204
|
* If there are no side-effects specified by the on_update method, runs a normal non-transaction update to avoid
|
|
205
205
|
* the performance impacts of a transaction.
|
|
206
206
|
*/
|
|
207
|
-
async perform_update_and_side_effects(find: any, data: z.output<this['put_validator']
|
|
207
|
+
async perform_update_and_side_effects(find: any, data: z.output<this['put_validator']>, session?: ClientSession): Promise<z.output<ZodSchema>> {
|
|
208
208
|
let update_document_data;
|
|
209
209
|
|
|
210
210
|
// if we have any update hooks, run the update operation in a transaction
|
|
211
|
-
if(this.update_hooks.length > 0){
|
|
212
|
-
await
|
|
211
|
+
if(this.update_hooks.length > 0 || session){
|
|
212
|
+
await create_or_use_session(async (session) => {
|
|
213
213
|
// update the document
|
|
214
214
|
let updated_document = await this.mongoose_model.findOneAndUpdate(find, data, {returnDocument: 'after', session: session, lean: true})
|
|
215
215
|
update_document_data = updated_document;
|
|
@@ -220,7 +220,7 @@ export class F_Collection<Collection_ID extends string, ZodSchema extends z.ZodO
|
|
|
220
220
|
//@ts-expect-error
|
|
221
221
|
await hook(session, updated_document);
|
|
222
222
|
}
|
|
223
|
-
});
|
|
223
|
+
}, session);
|
|
224
224
|
} else {// if we don't have any post update hooks, run the update operation normally
|
|
225
225
|
update_document_data = await this.mongoose_model.findOneAndUpdate(find, data, {returnDocument: 'after', lean: true})
|
|
226
226
|
}
|
|
@@ -248,36 +248,49 @@ export class F_Collection<Collection_ID extends string, ZodSchema extends z.ZodO
|
|
|
248
248
|
* If there are no side-effects specified by the on_delete method, runs a normal non-transaction delete to avoid
|
|
249
249
|
* the performance impacts of a transaction.
|
|
250
250
|
*/
|
|
251
|
-
async perform_delete_and_side_effects(find: any): Promise<z.output<ZodSchema>> {
|
|
251
|
+
async perform_delete_and_side_effects(find: any, session?: ClientSession): Promise<z.output<ZodSchema>> {
|
|
252
252
|
let deleted_document_data;
|
|
253
253
|
|
|
254
|
-
// if we have any
|
|
255
|
-
if(this.delete_hooks.length > 0){
|
|
256
|
-
await
|
|
257
|
-
//
|
|
254
|
+
// if we have any delete hooks, run the delete operation in a transaction
|
|
255
|
+
if(this.delete_hooks.length > 0 || session){
|
|
256
|
+
await create_or_use_session(async (session) => {
|
|
257
|
+
// delete the document
|
|
258
258
|
let deleted_document = await this.mongoose_model.findOneAndDelete(find, {returnDocument: 'after', session: session, lean: true})
|
|
259
259
|
deleted_document_data = deleted_document;
|
|
260
260
|
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
261
|
+
if(deleted_document_data) {
|
|
262
|
+
// run each hook one-by-one because running them in parallell is verboten
|
|
263
|
+
// https://mongoosejs.com/docs/transactions.html
|
|
264
|
+
for(let hook of this.delete_hooks){
|
|
265
|
+
await hook(session, deleted_document);
|
|
266
|
+
}
|
|
265
267
|
}
|
|
266
|
-
|
|
267
|
-
|
|
268
|
+
|
|
269
|
+
}, session);
|
|
270
|
+
} else {// if we don't have any post delete hooks, run the delete operation normally
|
|
268
271
|
deleted_document_data = await this.mongoose_model.findOneAndDelete(find, {returnDocument: 'after', lean: true})
|
|
269
272
|
}
|
|
270
273
|
|
|
271
|
-
// run the post-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
274
|
+
// run the post-delete hooks, which should not make DB changes.
|
|
275
|
+
if(deleted_document_data) {
|
|
276
|
+
for(let hook of this.post_delete_hooks) {
|
|
277
|
+
try {
|
|
278
|
+
await hook(deleted_document_data);
|
|
279
|
+
} catch(err) {
|
|
280
|
+
console.error(`Error in ${this.collection_id} after_delete:`)
|
|
281
|
+
console.error(err);
|
|
282
|
+
}
|
|
278
283
|
}
|
|
279
284
|
}
|
|
280
285
|
|
|
281
286
|
return deleted_document_data;
|
|
282
287
|
}
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
async function create_or_use_session(callback: (session: ClientSession) => Promise<void>, session?: ClientSession, ) {
|
|
291
|
+
if(session) {
|
|
292
|
+
return await callback(session);
|
|
293
|
+
} else {
|
|
294
|
+
return await mongoose.connection.transaction(callback);
|
|
295
|
+
}
|
|
283
296
|
}
|