@knymbus/firestoredb 1.0.13 → 1.0.15
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/cjs/FirestoreDB.d.ts +4 -0
- package/dist/cjs/FirestoreDB.d.ts.map +1 -1
- package/dist/cjs/FirestoreDB.js +19 -4
- package/dist/cjs/utils/createFirestoreDB.d.ts +1 -1
- package/dist/cjs/utils/createFirestoreDB.d.ts.map +1 -1
- package/dist/cjs/utils/createFirestoreDB.js +2 -2
- package/dist/esm/FirestoreDB.d.ts +4 -0
- package/dist/esm/FirestoreDB.d.ts.map +1 -1
- package/dist/esm/FirestoreDB.js +19 -4
- package/dist/esm/utils/createFirestoreDB.d.ts +1 -1
- package/dist/esm/utils/createFirestoreDB.d.ts.map +1 -1
- package/dist/esm/utils/createFirestoreDB.js +2 -2
- package/package.json +1 -1
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"FirestoreDB.d.ts","sourceRoot":"","sources":["../../src/FirestoreDB.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,cAAc,EAAmB,WAAW,EAAE,MAAM,oBAAoB,CAAC;AAClG,OAAO,EAAE,cAAc,EAAE,MAAM,kBAAkB,CAAC;AAClD,OAAO,EAAE,SAAS,EAAE,aAAa,EAAE,YAAY,EAAE,WAAW,EAAkB,cAAc,EAAE,gBAAgB,EAAE,MAAM,SAAS,CAAC;AAIhI,qBAAa,WAAW,CAAC,CAAC;IAWV,OAAO,CAAC,GAAG;IAAa,OAAO,CAAC,eAAe;IAAU,OAAO,CAAC,IAAI;IAAgB,OAAO,CAAC,QAAQ;IAVjH,OAAO,CAAC,cAAc,CAAM;IAC5B,OAAO,CAAC,oBAAoB,CAAU;IACtC,OAAO,CAAC,SAAS,CAAC,CAAiB;IACnC,OAAO,CAAC,YAAY,CAAS;IAE7B;;;;OAIG;gBACiB,GAAG,EAAE,SAAS,EAAU,eAAe,EAAE,MAAM,E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cxB;;;;OAIG;IACH,OAAO,CAAC,UAAU,CAIjB;IAED,OAAO,CAAC,gBAAgB;IAYxB;;OAEG;IACH,OAAO,CAAC,iBAAiB;CAO5B"}
|
package/dist/cjs/FirestoreDB.js
CHANGED
|
@@ -79,6 +79,7 @@ class FirestoreDB {
|
|
|
79
79
|
* Basic implementation. For production, you'd expand the 'filter' to handle where clauses.
|
|
80
80
|
*/
|
|
81
81
|
this.find = (filter = {}) => {
|
|
82
|
+
this._checkPermission('read', null, filter);
|
|
82
83
|
return new FirestoreQuery_1.FirestoreQuery(this._db, this._collectionName, this._collectionRef, filter, this._sdk, this._buildConstraints.bind(this), //Pass the private helper
|
|
83
84
|
this.countDocuments.bind(this), // pass the count helper
|
|
84
85
|
this._isSoftDeleteEnabled);
|
|
@@ -91,7 +92,7 @@ class FirestoreDB {
|
|
|
91
92
|
this._checkPermission('create', null, entity);
|
|
92
93
|
const docRef = this._getDocRef(entity._id);
|
|
93
94
|
// Using 'set' with { merge: false } acts like an insert/overwrite
|
|
94
|
-
yield this._sdk.setDoc(docRef, Object.assign(Object.assign({}, entity), { _id: docRef.id, isDeleted: false, createdAt: this._sdk.serverTimestamp(), updatedAt: this._sdk.serverTimestamp() }));
|
|
95
|
+
yield this._sdk.setDoc(docRef, this._applyTenantStamp(Object.assign(Object.assign({}, entity), { _id: docRef.id, isDeleted: false, createdAt: this._sdk.serverTimestamp(), updatedAt: this._sdk.serverTimestamp() })));
|
|
95
96
|
// 2. TRIGGER INVALIDATION: Any cached 'find' results for this
|
|
96
97
|
// collection are now potentially stale.
|
|
97
98
|
this._invalidateCache();
|
|
@@ -114,9 +115,11 @@ class FirestoreDB {
|
|
|
114
115
|
const chunk = entities.slice(i, i + CHUNK_SIZE);
|
|
115
116
|
// Inner loop: Set the item to be created
|
|
116
117
|
chunk.forEach(entity => {
|
|
118
|
+
// 1. Client-side security check (if enabled)
|
|
119
|
+
this._checkPermission('create', null, entity);
|
|
117
120
|
const docRef = this._getDocRef(entity._id);
|
|
118
|
-
const finalDoc = Object.assign(Object.assign({}, entity), { _id: docRef.id, isDeleted: false, createdAt: this._sdk.serverTimestamp(), updatedAt: this._sdk.serverTimestamp() // Added
|
|
119
|
-
});
|
|
121
|
+
const finalDoc = this._applyTenantStamp(Object.assign(Object.assign({}, entity), { _id: docRef.id, isDeleted: false, createdAt: this._sdk.serverTimestamp(), updatedAt: this._sdk.serverTimestamp() // Added
|
|
122
|
+
}));
|
|
120
123
|
// Add operation to the current batch
|
|
121
124
|
batch.set(docRef, finalDoc);
|
|
122
125
|
// Track the processed doc to return to the user
|
|
@@ -191,7 +194,7 @@ class FirestoreDB {
|
|
|
191
194
|
const { docId, entity } = item;
|
|
192
195
|
const docRef = this._getDocRef(docId);
|
|
193
196
|
// Inject updatedAt into the update payload
|
|
194
|
-
const updateData = Object.assign(Object.assign({}, entity), { updatedAt: this._sdk.serverTimestamp() });
|
|
197
|
+
const updateData = this._applyTenantStamp(Object.assign(Object.assign({}, entity), { updatedAt: this._sdk.serverTimestamp() }));
|
|
195
198
|
if (options.upsert) {
|
|
196
199
|
// UPSERT: Create if missing, merge if exists
|
|
197
200
|
// We ensure _id is included in the document for consistency
|
|
@@ -391,6 +394,8 @@ class FirestoreDB {
|
|
|
391
394
|
* pushes updates as they happen in the database.
|
|
392
395
|
*/
|
|
393
396
|
watch(filter = {}, callback, onError) {
|
|
397
|
+
// 1. Security: Pre-check read permission
|
|
398
|
+
this._checkPermission('read');
|
|
394
399
|
const constraints = this._buildConstraints(filter);
|
|
395
400
|
const q = this._sdk.query(this._collectionRef, ...constraints);
|
|
396
401
|
// Returns the unsubscribe function
|
|
@@ -453,5 +458,15 @@ class FirestoreDB {
|
|
|
453
458
|
}
|
|
454
459
|
return true;
|
|
455
460
|
}
|
|
461
|
+
/**
|
|
462
|
+
* PRIVATE: Helper to stamp tenant info on new data
|
|
463
|
+
*/
|
|
464
|
+
_applyTenantStamp(data) {
|
|
465
|
+
var _a;
|
|
466
|
+
if ((_a = this._options.tenant) === null || _a === void 0 ? void 0 : _a.enabled) {
|
|
467
|
+
return Object.assign(Object.assign({}, data), { [this._tenantField]: this._options.tenant.id });
|
|
468
|
+
}
|
|
469
|
+
return data;
|
|
470
|
+
}
|
|
456
471
|
}
|
|
457
472
|
exports.FirestoreDB = FirestoreDB;
|
|
@@ -6,5 +6,5 @@ import { DBOptions, FirestoreSDK } from "../types";
|
|
|
6
6
|
*
|
|
7
7
|
* @returns Returns a generator function to create collection-specific instances.
|
|
8
8
|
*/
|
|
9
|
-
export declare const createFirestoreDB: (db: Firestore, sdk: FirestoreSDK, FirestoreOptions?: DBOptions) => (collectionName: string, options?: DBOptions) => FirestoreDB<
|
|
9
|
+
export declare const createFirestoreDB: <T>(db: Firestore, sdk: FirestoreSDK, FirestoreOptions?: DBOptions) => (collectionName: string, options?: DBOptions) => FirestoreDB<T>;
|
|
10
10
|
//# sourceMappingURL=createFirestoreDB.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"createFirestoreDB.d.ts","sourceRoot":"","sources":["../../../src/utils/createFirestoreDB.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,oBAAoB,CAAC;AACpD,OAAO,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAC;AAC7C,OAAO,EAAE,SAAS,EAAE,YAAY,EAAE,MAAM,UAAU,CAAC;AAEnD;;;;GAIG;AACH,eAAO,MAAM,iBAAiB,GAAI,IAAI,SAAS,EAAE,KAAK,YAAY,EAAE,mBAAkB,SAAc,
|
|
1
|
+
{"version":3,"file":"createFirestoreDB.d.ts","sourceRoot":"","sources":["../../../src/utils/createFirestoreDB.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,oBAAoB,CAAC;AACpD,OAAO,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAC;AAC7C,OAAO,EAAE,SAAS,EAAE,YAAY,EAAE,MAAM,UAAU,CAAC;AAEnD;;;;GAIG;AACH,eAAO,MAAM,iBAAiB,GAAI,CAAC,EAAE,IAAI,SAAS,EAAE,KAAK,YAAY,EAAE,mBAAkB,SAAc,MAM3F,gBAAgB,MAAM,EAAE,UAAS,SAAc,mBAG1D,CAAC"}
|
|
@@ -12,8 +12,8 @@ const createFirestoreDB = (db, sdk, FirestoreOptions = {}) => {
|
|
|
12
12
|
* @param collectionName - The name of the Firestore collection
|
|
13
13
|
* @param options - Optional: { softDelete: true }
|
|
14
14
|
*/
|
|
15
|
-
return (collectionName, options =
|
|
16
|
-
return new FirestoreDB_1.FirestoreDB(db, collectionName, sdk, options);
|
|
15
|
+
return (collectionName, options = {}) => {
|
|
16
|
+
return new FirestoreDB_1.FirestoreDB(db, collectionName, sdk, Object.assign(options, FirestoreOptions));
|
|
17
17
|
};
|
|
18
18
|
};
|
|
19
19
|
exports.createFirestoreDB = createFirestoreDB;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"FirestoreDB.d.ts","sourceRoot":"","sources":["../../src/FirestoreDB.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,cAAc,EAAmB,WAAW,EAAE,MAAM,oBAAoB,CAAC;AAClG,OAAO,EAAE,cAAc,EAAE,MAAM,kBAAkB,CAAC;AAClD,OAAO,EAAE,SAAS,EAAE,aAAa,EAAE,YAAY,EAAE,WAAW,EAAkB,cAAc,EAAE,gBAAgB,EAAE,MAAM,SAAS,CAAC;AAIhI,qBAAa,WAAW,CAAC,CAAC;IAWV,OAAO,CAAC,GAAG;IAAa,OAAO,CAAC,eAAe;IAAU,OAAO,CAAC,IAAI;IAAgB,OAAO,CAAC,QAAQ;IAVjH,OAAO,CAAC,cAAc,CAAM;IAC5B,OAAO,CAAC,oBAAoB,CAAU;IACtC,OAAO,CAAC,SAAS,CAAC,CAAiB;IACnC,OAAO,CAAC,YAAY,CAAS;IAE7B;;;;OAIG;gBACiB,GAAG,EAAE,SAAS,EAAU,eAAe,EAAE,M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gB;IAcxB;;;;OAIG;IACH,OAAO,CAAC,UAAU,CAIjB;IAED,OAAO,CAAC,gBAAgB;IAYxB;;OAEG;IACH,OAAO,CAAC,iBAAiB;CAO5B"}
|
package/dist/esm/FirestoreDB.js
CHANGED
|
@@ -79,6 +79,7 @@ class FirestoreDB {
|
|
|
79
79
|
* Basic implementation. For production, you'd expand the 'filter' to handle where clauses.
|
|
80
80
|
*/
|
|
81
81
|
this.find = (filter = {}) => {
|
|
82
|
+
this._checkPermission('read', null, filter);
|
|
82
83
|
return new FirestoreQuery_1.FirestoreQuery(this._db, this._collectionName, this._collectionRef, filter, this._sdk, this._buildConstraints.bind(this), //Pass the private helper
|
|
83
84
|
this.countDocuments.bind(this), // pass the count helper
|
|
84
85
|
this._isSoftDeleteEnabled);
|
|
@@ -91,7 +92,7 @@ class FirestoreDB {
|
|
|
91
92
|
this._checkPermission('create', null, entity);
|
|
92
93
|
const docRef = this._getDocRef(entity._id);
|
|
93
94
|
// Using 'set' with { merge: false } acts like an insert/overwrite
|
|
94
|
-
yield this._sdk.setDoc(docRef, Object.assign(Object.assign({}, entity), { _id: docRef.id, isDeleted: false, createdAt: this._sdk.serverTimestamp(), updatedAt: this._sdk.serverTimestamp() }));
|
|
95
|
+
yield this._sdk.setDoc(docRef, this._applyTenantStamp(Object.assign(Object.assign({}, entity), { _id: docRef.id, isDeleted: false, createdAt: this._sdk.serverTimestamp(), updatedAt: this._sdk.serverTimestamp() })));
|
|
95
96
|
// 2. TRIGGER INVALIDATION: Any cached 'find' results for this
|
|
96
97
|
// collection are now potentially stale.
|
|
97
98
|
this._invalidateCache();
|
|
@@ -114,9 +115,11 @@ class FirestoreDB {
|
|
|
114
115
|
const chunk = entities.slice(i, i + CHUNK_SIZE);
|
|
115
116
|
// Inner loop: Set the item to be created
|
|
116
117
|
chunk.forEach(entity => {
|
|
118
|
+
// 1. Client-side security check (if enabled)
|
|
119
|
+
this._checkPermission('create', null, entity);
|
|
117
120
|
const docRef = this._getDocRef(entity._id);
|
|
118
|
-
const finalDoc = Object.assign(Object.assign({}, entity), { _id: docRef.id, isDeleted: false, createdAt: this._sdk.serverTimestamp(), updatedAt: this._sdk.serverTimestamp() // Added
|
|
119
|
-
});
|
|
121
|
+
const finalDoc = this._applyTenantStamp(Object.assign(Object.assign({}, entity), { _id: docRef.id, isDeleted: false, createdAt: this._sdk.serverTimestamp(), updatedAt: this._sdk.serverTimestamp() // Added
|
|
122
|
+
}));
|
|
120
123
|
// Add operation to the current batch
|
|
121
124
|
batch.set(docRef, finalDoc);
|
|
122
125
|
// Track the processed doc to return to the user
|
|
@@ -191,7 +194,7 @@ class FirestoreDB {
|
|
|
191
194
|
const { docId, entity } = item;
|
|
192
195
|
const docRef = this._getDocRef(docId);
|
|
193
196
|
// Inject updatedAt into the update payload
|
|
194
|
-
const updateData = Object.assign(Object.assign({}, entity), { updatedAt: this._sdk.serverTimestamp() });
|
|
197
|
+
const updateData = this._applyTenantStamp(Object.assign(Object.assign({}, entity), { updatedAt: this._sdk.serverTimestamp() }));
|
|
195
198
|
if (options.upsert) {
|
|
196
199
|
// UPSERT: Create if missing, merge if exists
|
|
197
200
|
// We ensure _id is included in the document for consistency
|
|
@@ -391,6 +394,8 @@ class FirestoreDB {
|
|
|
391
394
|
* pushes updates as they happen in the database.
|
|
392
395
|
*/
|
|
393
396
|
watch(filter = {}, callback, onError) {
|
|
397
|
+
// 1. Security: Pre-check read permission
|
|
398
|
+
this._checkPermission('read');
|
|
394
399
|
const constraints = this._buildConstraints(filter);
|
|
395
400
|
const q = this._sdk.query(this._collectionRef, ...constraints);
|
|
396
401
|
// Returns the unsubscribe function
|
|
@@ -453,5 +458,15 @@ class FirestoreDB {
|
|
|
453
458
|
}
|
|
454
459
|
return true;
|
|
455
460
|
}
|
|
461
|
+
/**
|
|
462
|
+
* PRIVATE: Helper to stamp tenant info on new data
|
|
463
|
+
*/
|
|
464
|
+
_applyTenantStamp(data) {
|
|
465
|
+
var _a;
|
|
466
|
+
if ((_a = this._options.tenant) === null || _a === void 0 ? void 0 : _a.enabled) {
|
|
467
|
+
return Object.assign(Object.assign({}, data), { [this._tenantField]: this._options.tenant.id });
|
|
468
|
+
}
|
|
469
|
+
return data;
|
|
470
|
+
}
|
|
456
471
|
}
|
|
457
472
|
exports.FirestoreDB = FirestoreDB;
|
|
@@ -6,5 +6,5 @@ import { DBOptions, FirestoreSDK } from "../types";
|
|
|
6
6
|
*
|
|
7
7
|
* @returns Returns a generator function to create collection-specific instances.
|
|
8
8
|
*/
|
|
9
|
-
export declare const createFirestoreDB: (db: Firestore, sdk: FirestoreSDK, FirestoreOptions?: DBOptions) => (collectionName: string, options?: DBOptions) => FirestoreDB<
|
|
9
|
+
export declare const createFirestoreDB: <T>(db: Firestore, sdk: FirestoreSDK, FirestoreOptions?: DBOptions) => (collectionName: string, options?: DBOptions) => FirestoreDB<T>;
|
|
10
10
|
//# sourceMappingURL=createFirestoreDB.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"createFirestoreDB.d.ts","sourceRoot":"","sources":["../../../src/utils/createFirestoreDB.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,oBAAoB,CAAC;AACpD,OAAO,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAC;AAC7C,OAAO,EAAE,SAAS,EAAE,YAAY,EAAE,MAAM,UAAU,CAAC;AAEnD;;;;GAIG;AACH,eAAO,MAAM,iBAAiB,GAAI,IAAI,SAAS,EAAE,KAAK,YAAY,EAAE,mBAAkB,SAAc,
|
|
1
|
+
{"version":3,"file":"createFirestoreDB.d.ts","sourceRoot":"","sources":["../../../src/utils/createFirestoreDB.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,oBAAoB,CAAC;AACpD,OAAO,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAC;AAC7C,OAAO,EAAE,SAAS,EAAE,YAAY,EAAE,MAAM,UAAU,CAAC;AAEnD;;;;GAIG;AACH,eAAO,MAAM,iBAAiB,GAAI,CAAC,EAAE,IAAI,SAAS,EAAE,KAAK,YAAY,EAAE,mBAAkB,SAAc,MAM3F,gBAAgB,MAAM,EAAE,UAAS,SAAc,mBAG1D,CAAC"}
|
|
@@ -12,8 +12,8 @@ const createFirestoreDB = (db, sdk, FirestoreOptions = {}) => {
|
|
|
12
12
|
* @param collectionName - The name of the Firestore collection
|
|
13
13
|
* @param options - Optional: { softDelete: true }
|
|
14
14
|
*/
|
|
15
|
-
return (collectionName, options =
|
|
16
|
-
return new FirestoreDB_1.FirestoreDB(db, collectionName, sdk, options);
|
|
15
|
+
return (collectionName, options = {}) => {
|
|
16
|
+
return new FirestoreDB_1.FirestoreDB(db, collectionName, sdk, Object.assign(options, FirestoreOptions));
|
|
17
17
|
};
|
|
18
18
|
};
|
|
19
19
|
exports.createFirestoreDB = createFirestoreDB;
|
package/package.json
CHANGED