@toride/drizzle 0.3.0 → 0.4.0
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/index.d.ts +8 -3
- package/dist/index.js +19 -0
- package/package.json +2 -2
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { TorideSchema, DefaultSchema, VirtualFieldsConfig, ConstraintAdapter, ResourceRef } from 'toride';
|
|
2
2
|
|
|
3
3
|
declare const VERSION = "0.0.1";
|
|
4
4
|
|
|
@@ -23,11 +23,13 @@ interface RoleAssignmentConfig {
|
|
|
23
23
|
roleColumn: string;
|
|
24
24
|
}
|
|
25
25
|
/** Options for createDrizzleAdapter. */
|
|
26
|
-
interface DrizzleAdapterOptions {
|
|
26
|
+
interface DrizzleAdapterOptions<S extends TorideSchema = DefaultSchema, TModelMap = never> {
|
|
27
27
|
/** Maps relation fields to Drizzle table references and foreign keys. */
|
|
28
28
|
relations?: Record<string, RelationConfig>;
|
|
29
29
|
/** Role assignment table configuration. */
|
|
30
30
|
roleAssignments?: RoleAssignmentConfig;
|
|
31
|
+
/** Maps virtual fields to their relation queries for field_includes constraints. */
|
|
32
|
+
virtualFields?: VirtualFieldsConfig<S, TModelMap>;
|
|
31
33
|
}
|
|
32
34
|
/**
|
|
33
35
|
* Create a Drizzle constraint adapter for a specific table.
|
|
@@ -36,10 +38,13 @@ interface DrizzleAdapterOptions {
|
|
|
36
38
|
* operation type, field, and value. These can be used with drizzle-orm
|
|
37
39
|
* operators or processed by custom query builders.
|
|
38
40
|
*
|
|
41
|
+
* @typeParam S - The Toride schema type. Defaults to `DefaultSchema` for backward compatibility.
|
|
42
|
+
* @typeParam TModelMap - Maps resource type names to their model field shapes. Used for model-aware
|
|
43
|
+
* virtual field detection. Defaults to `never` for backward compatibility.
|
|
39
44
|
* @typeParam TQueryMap - Maps resource type names to their Drizzle query types.
|
|
40
45
|
* Defaults to `Record<string, DrizzleQuery>` for backward compatibility.
|
|
41
46
|
*/
|
|
42
|
-
declare function createDrizzleAdapter<TQueryMap extends Record<string, DrizzleQuery> = Record<string, DrizzleQuery>>(table: AnyTable, options?: DrizzleAdapterOptions): ConstraintAdapter<TQueryMap>;
|
|
47
|
+
declare function createDrizzleAdapter<S extends TorideSchema = DefaultSchema, TModelMap = never, TQueryMap extends Record<string, DrizzleQuery> = Record<string, DrizzleQuery>>(table: AnyTable, options?: DrizzleAdapterOptions<S, TModelMap>): ConstraintAdapter<TQueryMap>;
|
|
43
48
|
/** Options for createDrizzleResolver. */
|
|
44
49
|
interface DrizzleResolverOptions {
|
|
45
50
|
/** Column used as the resource ID. Defaults to "id". */
|
package/dist/index.js
CHANGED
|
@@ -3,8 +3,27 @@ var VERSION = "0.0.1";
|
|
|
3
3
|
function createDrizzleAdapter(table, options) {
|
|
4
4
|
const relations = options?.relations ?? {};
|
|
5
5
|
const roleAssignments = options?.roleAssignments;
|
|
6
|
+
const flatVirtualFields = {};
|
|
7
|
+
if (options?.virtualFields) {
|
|
8
|
+
for (const [_resource, resourceFields] of Object.entries(options.virtualFields)) {
|
|
9
|
+
if (resourceFields) {
|
|
10
|
+
for (const field of Object.keys(resourceFields)) {
|
|
11
|
+
if (flatVirtualFields[field]) {
|
|
12
|
+
throw new Error(
|
|
13
|
+
`Virtual field "${field}" is defined in multiple resources. The adapter cannot disambiguate at translation time. Use unique field names per resource.`
|
|
14
|
+
);
|
|
15
|
+
}
|
|
16
|
+
flatVirtualFields[field] = resourceFields[field];
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
}
|
|
6
21
|
return {
|
|
7
22
|
translate(constraint) {
|
|
23
|
+
const vf = flatVirtualFields[constraint.field];
|
|
24
|
+
if (vf && constraint.type === "field_includes") {
|
|
25
|
+
return { _op: "relation", field: vf.relation, matchField: vf.matchField, value: constraint.value, filter: vf.filter, table };
|
|
26
|
+
}
|
|
8
27
|
switch (constraint.type) {
|
|
9
28
|
case "field_eq":
|
|
10
29
|
return { _op: "eq", field: constraint.field, value: constraint.value, table };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@toride/drizzle",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.0",
|
|
4
4
|
"description": "Drizzle ORM integration for Toride authorization engine",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"exports": {
|
|
@@ -20,7 +20,7 @@
|
|
|
20
20
|
]
|
|
21
21
|
},
|
|
22
22
|
"dependencies": {
|
|
23
|
-
"toride": "0.
|
|
23
|
+
"toride": "0.4.0"
|
|
24
24
|
},
|
|
25
25
|
"peerDependencies": {
|
|
26
26
|
"drizzle-orm": ">=0.29.0"
|