You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+60Lines changed: 60 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -59,6 +59,66 @@ async function myFunction() {
59
59
awaite.savePolicy();
60
60
```
61
61
62
+
## Custom Table Name Example
63
+
64
+
By default, each adapter creates its own table in the database. While this might seem convenient, it poses a significant drawback when multiple adapters need to share the same database: policies cannot be shared across adapters if they are stored in separate tables.
65
+
66
+
### Why Customization is Important
67
+
68
+
**Compliance with Naming Conventions:**
69
+
Many production databases follow strict naming conventions. By customizing the table name, you can ensure that your adapter’s tables comply with these conventions, which is essential for production environments.
70
+
71
+
**Shared Policies:**
72
+
When the same set of policies should be accessible by different adapters or services, having them in separate tables prevents a unified view. Customizing the table name allows you to consolidate policies into a single table that can be shared across adapters.
73
+
74
+
**Avoiding Duplication:**
75
+
If each adapter creates its own table by default, it can lead to unnecessary duplication of data. This not only wastes storage space but can also lead to inconsistent policy enforcement across different parts of your application.
76
+
77
+
**Simplified Management:**
78
+
With a single, customized table for all policies, managing, updating, and querying policy data becomes much easier. This is especially beneficial in environments where policies need to be audited or maintained centrally.
79
+
80
+
### How to Customize the Table Name
81
+
82
+
Most adapters offer a configuration option to set the table name explicitly. For example:
const tableName ='dbo.policies'// instead of the default 'casbin_rule'
90
+
91
+
asyncfunction myFunction() {
92
+
const a =awaitBasicAdapter.newAdapter('mssql',
93
+
newConnectionPool({
94
+
server: 'localhost',
95
+
port: 1433,
96
+
user: 'usr',
97
+
password: 'pwd',
98
+
database: 'casbin',
99
+
options: {
100
+
encrypt: true,
101
+
trustServerCertificate: true,
102
+
},
103
+
}),
104
+
tableName
105
+
);
106
+
107
+
const e =awaitnewEnforcer('examples/rbac_model.conf', a);
108
+
109
+
// Check the permission.
110
+
e.enforce('alice', 'data1', 'read');
111
+
112
+
// Modify the policy.
113
+
// await e.addPolicy(...);
114
+
// await e.removePolicy(...);
115
+
116
+
// Save the policy back to DB.
117
+
awaite.savePolicy();
118
+
```
119
+
120
+
By setting the `tableName` option, you ensure that all adapters point to the same table, enabling seamless policy sharing and compliance with any required naming conventions.
0 commit comments