Skip to content

Commit 9301274

Browse files
committed
Implement types and api on JS side for new Authorizer API
1 parent 1757ac0 commit 9301274

File tree

7 files changed

+727
-46
lines changed

7 files changed

+727
-46
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ dist
111111
.vscode-test
112112

113113
# End of https://www.toptal.com/developers/gitignore/api/node
114-
114+
integration-tests/*.db
115115
# Created by https://www.toptal.com/developers/gitignore/api/macos
116116
# Edit at https://www.toptal.com/developers/gitignore?templates=macos
117117

auth.js

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
* @enum {number}
66
* @property {number} ALLOW - Allow access to a resource.
77
* @property {number} DENY - Deny access to a resource and throw an error.
8+
* @property {number} IGNORE - For READ: return NULL instead of the column value. For other actions: equivalent to DENY.
89
*/
910
const Authorization = {
1011
/**
@@ -18,5 +19,55 @@ const Authorization = {
1819
* @type {number}
1920
*/
2021
DENY: 1,
22+
23+
/**
24+
* For READ: return NULL instead of the actual column value.
25+
* For other actions: equivalent to DENY.
26+
* @type {number}
27+
*/
28+
IGNORE: 2,
29+
};
30+
31+
/**
32+
* SQLite authorizer action codes.
33+
*
34+
* @readonly
35+
* @enum {number}
36+
*/
37+
const Action = {
38+
CREATE_INDEX: 1,
39+
CREATE_TABLE: 2,
40+
CREATE_TEMP_INDEX: 3,
41+
CREATE_TEMP_TABLE: 4,
42+
CREATE_TEMP_TRIGGER: 5,
43+
CREATE_TEMP_VIEW: 6,
44+
CREATE_TRIGGER: 7,
45+
CREATE_VIEW: 8,
46+
DELETE: 9,
47+
DROP_INDEX: 10,
48+
DROP_TABLE: 11,
49+
DROP_TEMP_INDEX: 12,
50+
DROP_TEMP_TABLE: 13,
51+
DROP_TEMP_TRIGGER: 14,
52+
DROP_TEMP_VIEW: 15,
53+
DROP_TRIGGER: 16,
54+
DROP_VIEW: 17,
55+
INSERT: 18,
56+
PRAGMA: 19,
57+
READ: 20,
58+
SELECT: 21,
59+
TRANSACTION: 22,
60+
UPDATE: 23,
61+
ATTACH: 24,
62+
DETACH: 25,
63+
ALTER_TABLE: 26,
64+
REINDEX: 27,
65+
ANALYZE: 28,
66+
CREATE_VTABLE: 29,
67+
DROP_VTABLE: 30,
68+
FUNCTION: 31,
69+
SAVEPOINT: 32,
70+
RECURSIVE: 33,
2171
};
22-
module.exports = Authorization;
72+
73+
module.exports = { Authorization, Action };

compat.js

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
const { Database: NativeDb, databasePrepareSync, databaseSyncSync, databaseExecSync, statementRunSync, statementGetSync, statementIterateSync, iteratorNextSync } = require("./index.js");
44
const SqliteError = require("./sqlite-error.js");
5-
const Authorization = require("./auth");
5+
const { Authorization, Action } = require("./auth");
66

77
function convertError(err) {
88
// Handle errors from Rust with JSON-encoded message
@@ -167,14 +167,6 @@ class Database {
167167
throw new Error("not implemented");
168168
}
169169

170-
authorizer(rules) {
171-
try {
172-
this.db.authorizer(rules);
173-
} catch (err) {
174-
throw convertError(err);
175-
}
176-
}
177-
178170
loadExtension(...args) {
179171
try {
180172
this.db.loadExtension(...args);
@@ -218,8 +210,12 @@ class Database {
218210
this.db.close();
219211
}
220212

221-
authorizer(hook) {
222-
this.db.authorizer(hook);
213+
authorizer(config) {
214+
try {
215+
this.db.authorizer(config);
216+
} catch (err) {
217+
throw convertError(err);
218+
}
223219
return this;
224220
}
225221

@@ -372,3 +368,4 @@ class Statement {
372368
module.exports = Database;
373369
module.exports.SqliteError = SqliteError;
374370
module.exports.Authorization = Authorization;
371+
module.exports.Action = Action;

index.d.ts

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -76,25 +76,12 @@ export declare class Database {
7676
/**
7777
* Sets the authorizer for the database.
7878
*
79-
* # Arguments
80-
*
81-
* * `env` - The environment.
82-
* * `rules_obj` - The rules object.
83-
*
84-
* The `rules_obj` is a JavaScript object with the following properties:
85-
*
86-
* * `Authorization.ALLOW` - Allow access to the table.
87-
* * `Authorization.DENY` - Deny access to the table.
88-
*
89-
* Example:
90-
*
91-
* ```javascript
92-
* db.authorizer({
93-
* "users": Authorization.ALLOW
94-
* });
95-
* ```
79+
* Accepts either:
80+
* - Legacy format: `{ [tableName: string]: 0 | 1 }`
81+
* - Full format: `{ rules: AuthRule[], defaultPolicy?: 0 | 1 | 2 }`
82+
* - `null` to remove the authorizer
9683
*/
97-
authorizer(rulesObj: object): void
84+
authorizer(config: import('./auth').AuthorizerConfig | { [tableName: string]: import('./auth').AuthorizationValue } | null): void
9885
/**
9986
* Loads an extension into the database.
10087
*

0 commit comments

Comments
 (0)