casbin-knex-adapter
Advanced tools
Comparing version 0.4.0 to 0.5.0
import type Knex from 'knex'; | ||
import type { Adapter, BatchAdapter, Model } from 'casbin'; | ||
export declare type KnexAdapterOptions = { | ||
tableName?: string; | ||
chunkSize?: number; | ||
}; | ||
export declare class KnexAdapter implements Adapter, BatchAdapter { | ||
private readonly knex; | ||
private readonly tableName; | ||
constructor(knex: Knex, tableName?: string); | ||
static newAdapter(knex: Knex): Promise<KnexAdapter>; | ||
private readonly chunkSize; | ||
constructor(knex: Knex, options?: KnexAdapterOptions); | ||
static newAdapter(knex: Knex, options?: KnexAdapterOptions): Promise<KnexAdapter>; | ||
private get policiesTable(); | ||
@@ -9,0 +14,0 @@ createTable(): Promise<void>; |
@@ -5,9 +5,11 @@ "use strict"; | ||
const casbin_1 = require("casbin"); | ||
const utils_1 = require("./utils"); | ||
class KnexAdapter { | ||
constructor(knex, tableName = 'policies') { | ||
constructor(knex, options) { | ||
this.knex = knex; | ||
this.tableName = tableName; | ||
this.tableName = (options === null || options === void 0 ? void 0 : options.tableName) || 'policies'; | ||
this.chunkSize = (options === null || options === void 0 ? void 0 : options.chunkSize) || 100; | ||
} | ||
static async newAdapter(knex) { | ||
const adapter = new KnexAdapter(knex); | ||
static async newAdapter(knex, options) { | ||
const adapter = new KnexAdapter(knex, options); | ||
await adapter.createTable(); | ||
@@ -70,3 +72,6 @@ return adapter; | ||
}); | ||
await this.policiesTable.insert(policies); | ||
const insertChunks = utils_1.chunk(policies, this.chunkSize); | ||
for (const insertChunk of insertChunks) { | ||
await this.policiesTable.insert(insertChunk); | ||
} | ||
} | ||
@@ -78,5 +83,5 @@ async removePolicy(sec, ptype, rule) { | ||
async removePolicies(sec, ptype, rules) { | ||
// ToDo Execute this in chunks | ||
for (const rule of rules) { | ||
await this.removePolicy(sec, ptype, rule); | ||
const deleteChunks = utils_1.chunk(rules, this.chunkSize); | ||
for (const deleteChunk of deleteChunks) { | ||
await this.removePolicy(sec, ptype, deleteChunk); | ||
} | ||
@@ -83,0 +88,0 @@ } |
{ | ||
"name": "casbin-knex-adapter", | ||
"version": "0.4.0", | ||
"version": "0.5.0", | ||
"description": "Knex adapter for Casbin", | ||
@@ -26,2 +26,5 @@ "main": "dist/index.js", | ||
}, | ||
"dependencies": { | ||
"lodash.chunk": "^4.2.0" | ||
}, | ||
"peerDependencies": { | ||
@@ -28,0 +31,0 @@ "knex": ">=0.16.5", |
@@ -30,3 +30,3 @@ # Knex Adapter | ||
// Create adapter | ||
const adapter = await KnexAdapter.newAdapter(knex); | ||
const adapter = await KnexAdapter.newAdapter({ knex }); | ||
@@ -33,0 +33,0 @@ // Create casbin enforcer |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
27918
12
189
3
+ Addedlodash.chunk@^4.2.0
+ Addedlodash.chunk@4.2.0(transitive)