Skip to content

Commit 1167bed

Browse files
fix: change p_type to ptype (#61)
BREAKING CHANGE: we will finally move to `ptype`, as discussed one year ago: pycasbin/sqlalchemy-adapter#26 (comment) . It is also officially documented in the official site: https://casbin.org/docs/en/adapters#:~:text=Ptype%20Column.%20Name%20of%20this%20column%20should%20be%20ptype%20instead%20of%20p_type%20or%20Ptype Co-authored-by: Shivansh Yadav <[email protected]>
1 parent 26c051c commit 1167bed

File tree

4 files changed

+4146
-4061
lines changed

4 files changed

+4146
-4061
lines changed

src/adapter.ts

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export interface MongooseAdapterOptions {
2626
}
2727

2828
export interface policyLine {
29-
p_type?: string,
29+
ptype?: string,
3030
v0?: string,
3131
v1?: string,
3232
v2?: string,
@@ -272,7 +272,7 @@ export class MongooseAdapter implements BatchAdapter, FilteredAdapter, Updatable
272272
* @param {Object} model Casbin model to which policy rule must be loaded
273273
*/
274274
loadPolicyLine(line: policyLine, model: Model) {
275-
let lineText = `${line.p_type!}`;
275+
let lineText = `${line.ptype!}`;
276276

277277
for (const word of [line.v0, line.v1, line.v2, line.v3, line.v4, line.v5]) {
278278
if (word !== undefined) {
@@ -327,12 +327,12 @@ export class MongooseAdapter implements BatchAdapter, FilteredAdapter, Updatable
327327
* This method is used by casbin to generate Mongoose Model Object for single policy
328328
* and should not be called by user.
329329
*
330-
* @param {String} ptype Policy type to save into MongoDB
330+
* @param {String} pType Policy type to save into MongoDB
331331
* @param {Array<String>} rule An array which consists of policy rule elements to store
332332
* @returns {Object} Returns a created CasbinRule record for MongoDB
333333
*/
334-
savePolicyLine(ptype: string, rule: string[]) {
335-
const model = new CasbinRule({p_type: ptype});
334+
savePolicyLine(pType: string, rule: string[]) {
335+
const model = new CasbinRule({ptype: pType});
336336

337337
if (rule.length > 0) {
338338
model.v0 = rule[0];
@@ -412,16 +412,16 @@ export class MongooseAdapter implements BatchAdapter, FilteredAdapter, Updatable
412412
* This method is used by casbin and should not be called by user.
413413
*
414414
* @param {String} sec Section of the policy
415-
* @param {String} ptype Type of the policy (e.g. "p" or "g")
415+
* @param {String} pType Type of the policy (e.g. "p" or "g")
416416
* @param {Array<String>} rule Policy rule to add into enforcer
417417
* @returns {Promise<void>}
418418
*/
419-
async addPolicy(sec: string, ptype: string, rule: string[]) {
419+
async addPolicy(sec: string, pType: string, rule: string[]) {
420420
const options: sessionOption = {};
421421
try {
422422
if (this.isSynced) options.session = await this.getTransaction();
423423

424-
const line = this.savePolicyLine(ptype, rule);
424+
const line = this.savePolicyLine(pType, rule);
425425
await line.save(options);
426426

427427
this.autoCommit && options.session && await options.session.commitTransaction();
@@ -436,16 +436,16 @@ export class MongooseAdapter implements BatchAdapter, FilteredAdapter, Updatable
436436
* This method is used by casbin and should not be called by user.
437437
*
438438
* @param {String} sec Section of the policy
439-
* @param {String} ptype Type of the policy (e.g. "p" or "g")
439+
* @param {String} pType Type of the policy (e.g. "p" or "g")
440440
* @param {Array<String>} rules Policy rule to add into enforcer
441441
* @returns {Promise<void>}
442442
*/
443-
async addPolicies(sec: string, ptype: string, rules: Array<string[]>) {
443+
async addPolicies(sec: string, pType: string, rules: Array<string[]>) {
444444
const options: sessionOption = {};
445445
if (this.isSynced) options.session = await this.getTransaction();
446446
else throw new InvalidAdapterTypeError('addPolicies is only supported by SyncedAdapter. See newSyncedAdapter');
447447
try {
448-
const promises = rules.map(async rule => this.addPolicy(sec, ptype, rule));
448+
const promises = rules.map(async rule => this.addPolicy(sec, pType, rule));
449449
await Promise.all(promises);
450450

451451
this.autoCommit && options.session && await options.session.commitTransaction();
@@ -460,19 +460,19 @@ export class MongooseAdapter implements BatchAdapter, FilteredAdapter, Updatable
460460
* This method is used by casbin and should not be called by user.
461461
*
462462
* @param {String} sec Section of the policy
463-
* @param {String} ptype Type of the policy (e.g. "p" or "g")
463+
* @param {String} pType Type of the policy (e.g. "p" or "g")
464464
* @param {Array<String>} oldRule Policy rule to remove from enforcer
465465
* @param {Array<String>} newRule Policy rule to add into enforcer
466466
* @returns {Promise<void>}
467467
*/
468-
async updatePolicy(sec: string, ptype: string, oldRule: string[], newRule: string[]) {
468+
async updatePolicy(sec: string, pType: string, oldRule: string[], newRule: string[]) {
469469
const options: sessionOption = {};
470470
try {
471471
if (this.isSynced) options.session = await this.getTransaction();
472-
const {p_type, v0, v1, v2, v3, v4, v5} = this.savePolicyLine(ptype, oldRule);
473-
const newRuleLine = this.savePolicyLine(ptype, newRule);
472+
const {ptype, v0, v1, v2, v3, v4, v5} = this.savePolicyLine(pType, oldRule);
473+
const newRuleLine = this.savePolicyLine(pType, newRule);
474474
const newModel = {
475-
p_type: newRuleLine.p_type,
475+
ptype: newRuleLine.ptype,
476476
v0: newRuleLine.v0,
477477
v1: newRuleLine.v1,
478478
v2: newRuleLine.v2,
@@ -481,7 +481,7 @@ export class MongooseAdapter implements BatchAdapter, FilteredAdapter, Updatable
481481
v5: newRuleLine.v5
482482
}
483483

484-
await CasbinRule.updateOne({p_type, v0, v1, v2, v3, v4, v5}, newModel, options);
484+
await CasbinRule.updateOne({ptype, v0, v1, v2, v3, v4, v5}, newModel, options);
485485

486486
this.autoCommit && options.session && await options.session.commitTransaction();
487487
} catch (err) {
@@ -495,18 +495,18 @@ export class MongooseAdapter implements BatchAdapter, FilteredAdapter, Updatable
495495
* This method is used by casbin and should not be called by user.
496496
*
497497
* @param {String} sec Section of the policy
498-
* @param {String} ptype Type of the policy (e.g. "p" or "g")
498+
* @param {String} pType Type of the policy (e.g. "p" or "g")
499499
* @param {Array<String>} rule Policy rule to remove from enforcer
500500
* @returns {Promise<void>}
501501
*/
502-
async removePolicy(sec: string, ptype: string, rule: string[]) {
502+
async removePolicy(sec: string, pType: string, rule: string[]) {
503503
const options: sessionOption = {};
504504
try {
505505
if (this.isSynced) options.session = await this.getTransaction();
506506

507-
const {p_type, v0, v1, v2, v3, v4, v5} = this.savePolicyLine(ptype, rule);
507+
const {ptype, v0, v1, v2, v3, v4, v5} = this.savePolicyLine(pType, rule);
508508

509-
await CasbinRule.deleteMany({p_type, v0, v1, v2, v3, v4, v5}, options);
509+
await CasbinRule.deleteMany({ptype, v0, v1, v2, v3, v4, v5}, options);
510510

511511
this.autoCommit && options.session && await options.session.commitTransaction();
512512
} catch (err) {
@@ -520,17 +520,17 @@ export class MongooseAdapter implements BatchAdapter, FilteredAdapter, Updatable
520520
* This method is used by casbin and should not be called by user.
521521
*
522522
* @param {String} sec Section of the policy
523-
* @param {String} ptype Type of the policy (e.g. "p" or "g")
523+
* @param {String} pType Type of the policy (e.g. "p" or "g")
524524
* @param {Array<String>} rules Policy rule to remove from enforcer
525525
* @returns {Promise<void>}
526526
*/
527-
async removePolicies(sec: string, ptype: string, rules: Array<string[]>) {
527+
async removePolicies(sec: string, pType: string, rules: Array<string[]>) {
528528
const options: sessionOption = {};
529529
try {
530530
if (this.isSynced) options.session = await this.getTransaction();
531531
else throw new InvalidAdapterTypeError('removePolicies is only supported by SyncedAdapter. See newSyncedAdapter');
532532

533-
const promises = rules.map(async rule => this.removePolicy(sec, ptype, rule));
533+
const promises = rules.map(async rule => this.removePolicy(sec, pType, rule));
534534
await Promise.all(promises);
535535

536536
this.autoCommit && options.session && await options.session.commitTransaction();
@@ -545,16 +545,16 @@ export class MongooseAdapter implements BatchAdapter, FilteredAdapter, Updatable
545545
* This method is used by casbin and should not be called by user.
546546
*
547547
* @param {String} sec Section of the policy
548-
* @param {String} ptype Type of the policy (e.g. "p" or "g")
548+
* @param {String} pType Type of the policy (e.g. "p" or "g")
549549
* @param {Number} fieldIndex Index of the field to start filtering from
550550
* @param {...String} fieldValues Policy rule to match when removing (starting from fieldIndex)
551551
* @returns {Promise<void>}
552552
*/
553-
async removeFilteredPolicy(sec: string, ptype: string, fieldIndex: number, ...fieldValues: string[]) {
553+
async removeFilteredPolicy(sec: string, pType: string, fieldIndex: number, ...fieldValues: string[]) {
554554
const options: sessionOption = {};
555555
try {
556556
if (this.isSynced) options.session = await this.getTransaction();
557-
const where: policyLine = ptype ? {p_type: ptype} : {};
557+
const where: policyLine = pType ? {ptype: pType} : {};
558558

559559
if (fieldIndex <= 0 && fieldIndex + fieldValues.length > 0 && fieldValues[0 - fieldIndex]) {
560560
where.v0 = fieldValues[0 - fieldIndex];

src/model.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
import {Schema, Document, model} from 'mongoose';
1616

1717
export interface IModel extends Document {
18-
p_type: string;
18+
ptype: string;
1919
v0: string;
2020
v1: string;
2121
v2: string;
@@ -25,7 +25,7 @@ export interface IModel extends Document {
2525
}
2626

2727
const schema = new Schema({
28-
p_type: {
28+
ptype: {
2929
type: Schema.Types.String,
3030
required: true,
3131
index: true

0 commit comments

Comments
 (0)