@shivaedev/identity 0.39.0 → 0.41.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/CHANGELOG.md +17 -0
- package/README.md +37 -28
- package/dist/accountPreferenceHeaders.d.ts +5 -0
- package/dist/accountPreferenceHeaders.d.ts.map +1 -0
- package/dist/accountPreferenceHeaders.js +5 -0
- package/dist/accountPreferenceHeaders.js.map +1 -0
- package/dist/client/preferencesPlugin.d.ts +17 -0
- package/dist/client/preferencesPlugin.d.ts.map +1 -0
- package/dist/{server/better-auth/preferencesClient.js → client/preferencesPlugin.js} +7 -7
- package/dist/client/preferencesPlugin.js.map +1 -0
- package/dist/server/account-data/OwnedModelsUncollected.d.ts +7 -0
- package/dist/server/account-data/OwnedModelsUncollected.d.ts.map +1 -0
- package/dist/server/account-data/OwnedModelsUncollected.js +7 -0
- package/dist/server/account-data/OwnedModelsUncollected.js.map +1 -0
- package/dist/server/account-data/OwnedTableCycle.d.ts +9 -0
- package/dist/server/account-data/OwnedTableCycle.d.ts.map +1 -0
- package/dist/server/account-data/OwnedTableCycle.js +9 -0
- package/dist/server/account-data/OwnedTableCycle.js.map +1 -0
- package/dist/server/account-data/UncoveredUserColumns.d.ts +19 -0
- package/dist/server/account-data/UncoveredUserColumns.d.ts.map +1 -0
- package/dist/server/account-data/UncoveredUserColumns.js +11 -0
- package/dist/server/account-data/UncoveredUserColumns.js.map +1 -0
- package/dist/server/account-data/checkCoverage.d.ts +8 -0
- package/dist/server/account-data/checkCoverage.d.ts.map +1 -0
- package/dist/server/account-data/checkCoverage.js +25 -0
- package/dist/server/account-data/checkCoverage.js.map +1 -0
- package/dist/server/account-data/eraseOrder.d.ts +7 -0
- package/dist/server/account-data/eraseOrder.d.ts.map +1 -0
- package/dist/server/account-data/eraseOrder.js +24 -0
- package/dist/server/account-data/eraseOrder.js.map +1 -0
- package/dist/server/account-data/eraseOwnedRows.d.ts +5 -0
- package/dist/server/account-data/eraseOwnedRows.d.ts.map +1 -0
- package/dist/server/account-data/eraseOwnedRows.js +27 -0
- package/dist/server/account-data/eraseOwnedRows.js.map +1 -0
- package/dist/server/account-data/exportOwnedRows.d.ts +7 -0
- package/dist/server/account-data/exportOwnedRows.d.ts.map +1 -0
- package/dist/server/account-data/exportOwnedRows.js +36 -0
- package/dist/server/account-data/exportOwnedRows.js.map +1 -0
- package/dist/server/account-data/foreignKeys.d.ts +27 -0
- package/dist/server/account-data/foreignKeys.d.ts.map +1 -0
- package/dist/server/account-data/foreignKeys.js +32 -0
- package/dist/server/account-data/foreignKeys.js.map +1 -0
- package/dist/server/account-data/ownedTables.d.ts +11 -0
- package/dist/server/account-data/ownedTables.d.ts.map +1 -0
- package/dist/server/account-data/ownedTables.js +41 -0
- package/dist/server/account-data/ownedTables.js.map +1 -0
- package/dist/server/account-preferences/Rows.d.ts.map +1 -1
- package/dist/server/account-preferences/Rows.js +2 -1
- package/dist/server/account-preferences/Rows.js.map +1 -1
- package/dist/server/better-auth/accountData.d.ts +42 -0
- package/dist/server/better-auth/accountData.d.ts.map +1 -0
- package/dist/server/better-auth/accountData.js +24 -0
- package/dist/server/better-auth/accountData.js.map +1 -0
- package/dist/server/better-auth/adapters/accountPreferenceOptions.d.ts.map +1 -1
- package/dist/server/better-auth/adapters/accountPreferenceOptions.js +3 -2
- package/dist/server/better-auth/adapters/accountPreferenceOptions.js.map +1 -1
- package/dist/server/better-auth/profileExport.d.ts +32 -0
- package/dist/server/better-auth/profileExport.d.ts.map +1 -0
- package/dist/server/better-auth/profileExport.js +53 -0
- package/dist/server/better-auth/profileExport.js.map +1 -0
- package/package.json +10 -10
- package/src/accountPreferenceHeaders.ts +4 -0
- package/src/client/preferencesPlugin.ts +40 -0
- package/src/server/account-data/OwnedModelsUncollected.ts +7 -0
- package/src/server/account-data/OwnedTableCycle.ts +9 -0
- package/src/server/account-data/UncoveredUserColumns.ts +14 -0
- package/src/server/account-data/checkCoverage.ts +28 -0
- package/src/server/account-data/eraseOrder.ts +36 -0
- package/src/server/account-data/eraseOwnedRows.ts +28 -0
- package/src/server/account-data/exportOwnedRows.ts +45 -0
- package/src/server/account-data/foreignKeys.ts +35 -0
- package/src/server/account-data/ownedTables.ts +43 -0
- package/src/server/account-preferences/Rows.ts +2 -1
- package/src/server/better-auth/accountData.ts +26 -0
- package/src/server/better-auth/adapters/accountPreferenceOptions.ts +3 -2
- package/src/server/better-auth/profileExport.ts +55 -0
- package/dist/server/account-export/check.d.ts +0 -15
- package/dist/server/account-export/check.d.ts.map +0 -1
- package/dist/server/account-export/check.js +0 -33
- package/dist/server/account-export/check.js.map +0 -1
- package/dist/server/accountExport.d.ts +0 -14
- package/dist/server/accountExport.d.ts.map +0 -1
- package/dist/server/accountExport.js +0 -15
- package/dist/server/accountExport.js.map +0 -1
- package/dist/server/better-auth/preferencesClient.d.ts +0 -3
- package/dist/server/better-auth/preferencesClient.d.ts.map +0 -1
- package/dist/server/better-auth/preferencesClient.js.map +0 -1
- package/dist/server/better-auth/userDataExport.d.ts +0 -38
- package/dist/server/better-auth/userDataExport.d.ts.map +0 -1
- package/dist/server/better-auth/userDataExport.js +0 -55
- package/dist/server/better-auth/userDataExport.js.map +0 -1
- package/dist/server/userDataExport.d.ts +0 -13
- package/dist/server/userDataExport.d.ts.map +0 -1
- package/dist/server/userDataExport.js +0 -2
- package/dist/server/userDataExport.js.map +0 -1
- package/src/server/account-export/check.ts +0 -38
- package/src/server/accountExport.ts +0 -40
- package/src/server/better-auth/preferencesClient.ts +0 -25
- package/src/server/better-auth/userDataExport.ts +0 -57
- package/src/server/userDataExport.ts +0 -15
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { Effect, Schema } from "effect";
|
|
2
|
+
import { SqlClient, SqlSchema } from "effect/sql";
|
|
3
|
+
const ForeignKeyColumn = Schema.Struct({
|
|
4
|
+
column: Schema.String,
|
|
5
|
+
onDelete: Schema.String,
|
|
6
|
+
schema: Schema.String,
|
|
7
|
+
table: Schema.String,
|
|
8
|
+
target: Schema.Struct({ column: Schema.String, schema: Schema.String, table: Schema.String }),
|
|
9
|
+
});
|
|
10
|
+
// One row per column pair, so a composite key such as (id, owner_id) shows which column points at a person.
|
|
11
|
+
export const foreignKeys = Effect.fn("Identity.foreignKeys")(function* () {
|
|
12
|
+
const sql = (yield* SqlClient.SqlClient).withoutTransforms();
|
|
13
|
+
return yield* SqlSchema.findAll({
|
|
14
|
+
execute: () => sql `
|
|
15
|
+
select source_schema.nspname as schema, source.relname as table, source_column.attname as column,
|
|
16
|
+
fk.confdeltype::text as "onDelete",
|
|
17
|
+
json_build_object('schema', target_schema.nspname, 'table', target.relname, 'column', target_column.attname) as target
|
|
18
|
+
from pg_catalog.pg_constraint fk
|
|
19
|
+
cross join lateral unnest(fk.conkey, fk.confkey) as pair(source_key, target_key)
|
|
20
|
+
join pg_catalog.pg_class source on source.oid = fk.conrelid
|
|
21
|
+
join pg_catalog.pg_namespace source_schema on source_schema.oid = source.relnamespace
|
|
22
|
+
join pg_catalog.pg_attribute source_column on source_column.attrelid = source.oid and source_column.attnum = pair.source_key
|
|
23
|
+
join pg_catalog.pg_class target on target.oid = fk.confrelid
|
|
24
|
+
join pg_catalog.pg_namespace target_schema on target_schema.oid = target.relnamespace
|
|
25
|
+
join pg_catalog.pg_attribute target_column on target_column.attrelid = target.oid and target_column.attnum = pair.target_key
|
|
26
|
+
where fk.contype = 'f'
|
|
27
|
+
order by schema, "table", "column"`,
|
|
28
|
+
Request: Schema.Void,
|
|
29
|
+
Result: ForeignKeyColumn,
|
|
30
|
+
})(undefined);
|
|
31
|
+
});
|
|
32
|
+
//# sourceMappingURL=foreignKeys.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"foreignKeys.js","sourceRoot":"","sources":["../../../src/server/account-data/foreignKeys.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AACxC,OAAO,EAAE,SAAS,EAAE,SAAS,EAAE,MAAM,YAAY,CAAC;AAElD,MAAM,gBAAgB,GAAG,MAAM,CAAC,MAAM,CAAC;IACtC,MAAM,EAAE,MAAM,CAAC,MAAM;IACrB,QAAQ,EAAE,MAAM,CAAC,MAAM;IACvB,MAAM,EAAE,MAAM,CAAC,MAAM;IACrB,KAAK,EAAE,MAAM,CAAC,MAAM;IACpB,MAAM,EAAE,MAAM,CAAC,MAAM,CAAC,EAAE,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,KAAK,EAAE,MAAM,CAAC,MAAM,EAAE,CAAC;CAC7F,CAAC,CAAC;AAIH,4GAA4G;AAC5G,MAAM,CAAC,MAAM,WAAW,GAAG,MAAM,CAAC,EAAE,CAAC,sBAAsB,CAAC,CAAC,QAAQ,CAAC;IACrE,MAAM,GAAG,GAAG,CAAC,KAAK,CAAC,CAAC,SAAS,CAAC,SAAS,CAAC,CAAC,iBAAiB,EAAE,CAAC;IAC7D,OAAO,KAAK,CAAC,CAAC,SAAS,CAAC,OAAO,CAAC;QAC/B,OAAO,EAAE,GAAG,EAAE,CAAC,GAAG,CAAA;;;;;;;;;;;;;qCAaiB;QACnC,OAAO,EAAE,MAAM,CAAC,IAAI;QACpB,MAAM,EAAE,gBAAgB;KACxB,CAAC,CAAC,SAAS,CAAC,CAAC;AACf,CAAC,CAAC,CAAC"}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { Effect, Schema } from "effect";
|
|
2
|
+
import { SqlClient } from "effect/sql";
|
|
3
|
+
import { OwnedModelsUncollected } from "#server/account-data/OwnedModelsUncollected.ts";
|
|
4
|
+
export declare const ownedTables: () => Effect.Effect<{
|
|
5
|
+
model: import("effect/schema/Model").Any;
|
|
6
|
+
table: string;
|
|
7
|
+
owner: import("@shivaedev/sql/RepositoryModels.ts").ModelOwner;
|
|
8
|
+
ownerKey: string;
|
|
9
|
+
relation: string;
|
|
10
|
+
}[], OwnedModelsUncollected | Schema.SchemaError | import("effect/sql/SqlError").SqlError, SqlClient.SqlClient>;
|
|
11
|
+
//# sourceMappingURL=ownedTables.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ownedTables.d.ts","sourceRoot":"","sources":["../../../src/server/account-data/ownedTables.ts"],"names":[],"mappings":"AAAA,OAAO,EAAW,MAAM,EAAU,MAAM,EAAE,MAAM,QAAQ,CAAC;AACzD,OAAO,EAAE,SAAS,EAAa,MAAM,YAAY,CAAC;AAElD,OAAO,EAAE,sBAAsB,EAAE,MAAM,gDAAgD,CAAC;AAYxF,eAAO,MAAM,WAAW;;;;;;+GA2BtB,CAAC"}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { Context, Effect, Option, Schema } from "effect";
|
|
2
|
+
import { SqlClient, SqlSchema } from "effect/sql";
|
|
3
|
+
import { RepositoryModels } from "@shivaedev/sql/RepositoryModels.ts";
|
|
4
|
+
import { OwnedModelsUncollected } from "#server/account-data/OwnedModelsUncollected.ts";
|
|
5
|
+
const ResolvedTable = Schema.Struct({ name: Schema.String, schema: Schema.String, table: Schema.String });
|
|
6
|
+
// Quoted the way a repository's statement names its table, so the catalog resolves the same relation through the search path.
|
|
7
|
+
function quotedName(name) {
|
|
8
|
+
return name
|
|
9
|
+
.split(".")
|
|
10
|
+
.map((part) => `"${part.replaceAll('"', '""')}"`)
|
|
11
|
+
.join(".");
|
|
12
|
+
}
|
|
13
|
+
export const ownedTables = Effect.fn("Identity.ownedTables")(function* () {
|
|
14
|
+
const models = yield* RepositoryModels;
|
|
15
|
+
// Without a collector the Reference holds its default, which records nothing, so an empty list would read as "nothing owned".
|
|
16
|
+
if (Option.getOrUndefined(Context.getOption(Context.empty(), RepositoryModels)) === models) {
|
|
17
|
+
return yield* new OwnedModelsUncollected();
|
|
18
|
+
}
|
|
19
|
+
const owned = yield* models.owned;
|
|
20
|
+
const names = [...new Set(owned.map(({ table }) => table))];
|
|
21
|
+
if (names.length === 0) {
|
|
22
|
+
return [];
|
|
23
|
+
}
|
|
24
|
+
const sql = (yield* SqlClient.SqlClient).withoutTransforms();
|
|
25
|
+
const resolved = yield* SqlSchema.findAll({
|
|
26
|
+
execute: () => sql `
|
|
27
|
+
select owned.name, namespace.nspname as schema, relation.relname as table
|
|
28
|
+
from (values ${sql.csv(names.map((name) => sql `(${name}, ${quotedName(name)})`))}) as owned(name, quoted)
|
|
29
|
+
join pg_catalog.pg_class relation on relation.oid = to_regclass(owned.quoted)
|
|
30
|
+
join pg_catalog.pg_namespace namespace on namespace.oid = relation.relnamespace`,
|
|
31
|
+
Request: Schema.Void,
|
|
32
|
+
Result: ResolvedTable,
|
|
33
|
+
})(undefined);
|
|
34
|
+
return owned.flatMap((model) => {
|
|
35
|
+
const table = resolved.find(({ name }) => name === model.table);
|
|
36
|
+
return table === undefined
|
|
37
|
+
? []
|
|
38
|
+
: [{ ...model, ownerKey: `${table.schema}.${table.table}.${model.owner.column}`, relation: `${table.schema}.${table.table}` }];
|
|
39
|
+
});
|
|
40
|
+
});
|
|
41
|
+
//# sourceMappingURL=ownedTables.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ownedTables.js","sourceRoot":"","sources":["../../../src/server/account-data/ownedTables.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AACzD,OAAO,EAAE,SAAS,EAAE,SAAS,EAAE,MAAM,YAAY,CAAC;AAClD,OAAO,EAAE,gBAAgB,EAAE,MAAM,oCAAoC,CAAC;AACtE,OAAO,EAAE,sBAAsB,EAAE,MAAM,gDAAgD,CAAC;AAExF,MAAM,aAAa,GAAG,MAAM,CAAC,MAAM,CAAC,EAAE,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,KAAK,EAAE,MAAM,CAAC,MAAM,EAAE,CAAC,CAAC;AAE1G,8HAA8H;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I,IAAI,KAAK,UAAU,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;;kFAEA;QAChF,OAAO,EAAE,MAAM,CAAC,IAAI;QACpB,MAAM,EAAE,aAAa;KACrB,CAAC,CAAC,SAAS,CAAC,CAAC;IACd,OAAO,KAAK,CAAC,OAAO,CAAC,CAAC,KAAK,EAAE,EAAE;QAC9B,MAAM,KAAK,GAAG,QAAQ,CAAC,IAAI,CAAC,CAAC,EAAE,IAAI,EAAE,EAAE,EAAE,CAAC,IAAI,KAAK,KAAK,CAAC,KAAK,CAAC,CAAC;QAChE,OAAO,KAAK,KAAK,SAAS;YACzB,CAAC,CAAC,EAAE;YACJ,CAAC,CAAC,CAAC,EAAE,GAAG,KAAK,EAAE,QAAQ,EAAE,GAAG,KAAK,CAAC,MAAM,IAAI,KAAK,CAAC,KAAK,IAAI,KAAK,CAAC,KAAK,CAAC,MAAM,EAAE,EAAE,QAAQ,EAAE,GAAG,KAAK,CAAC,MAAM,IAAI,KAAK,CAAC,KAAK,EAAE,EAAE,CAAC,CAAC;IACjI,CAAC,CAAC,CAAC;AACJ,CAAC,CAAC,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Rows.d.ts","sourceRoot":"","sources":["../../../src/server/account-preferences/Rows.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
1
|
+
{"version":3,"file":"Rows.d.ts","sourceRoot":"","sources":["../../../src/server/account-preferences/Rows.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAOhC,cAAM,iBAAkB,SAAQ,sBAG9B;CAAG;AASL,eAAO,MAAM,qBAAqB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CAGhC,CAAC"}
|
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
import { Schema } from "effect";
|
|
2
2
|
import { Model } from "effect/schema";
|
|
3
3
|
import { defineRepository } from "@shivaedev/sql/defineRepository.ts";
|
|
4
|
+
import { owner } from "@shivaedev/sql/owner.ts";
|
|
4
5
|
import { makeRepository } from "@shivaedev/sql/repository.ts";
|
|
5
6
|
import { AccountPreferenceValues } from "#accountPreferenceValues.ts";
|
|
6
7
|
class StoredPreferences extends Model.Class("AccountPreferences")({
|
|
7
|
-
id: Schema.String,
|
|
8
|
+
id: owner(Schema.String),
|
|
8
9
|
...AccountPreferenceValues.fields,
|
|
9
10
|
}) {
|
|
10
11
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Rows.js","sourceRoot":"","sources":["../../../src/server/account-preferences/Rows.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAChC,OAAO,EAAE,KAAK,EAAE,MAAM,eAAe,CAAC;AACtC,OAAO,EAAE,gBAAgB,EAAE,MAAM,oCAAoC,CAAC;AACtE,OAAO,EAAE,cAAc,EAAE,MAAM,8BAA8B,CAAC;AAC9D,OAAO,EAAE,uBAAuB,EAAE,MAAM,6BAA6B,CAAC;AAEtE,MAAM,iBAAkB,SAAQ,KAAK,CAAC,KAAK,CAAoB,oBAAoB,CAAC,CAAC;IACpF,EAAE,EAAE,MAAM,CAAC,MAAM;
|
|
1
|
+
{"version":3,"file":"Rows.js","sourceRoot":"","sources":["../../../src/server/account-preferences/Rows.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAChC,OAAO,EAAE,KAAK,EAAE,MAAM,eAAe,CAAC;AACtC,OAAO,EAAE,gBAAgB,EAAE,MAAM,oCAAoC,CAAC;AACtE,OAAO,EAAE,KAAK,EAAE,MAAM,yBAAyB,CAAC;AAChD,OAAO,EAAE,cAAc,EAAE,MAAM,8BAA8B,CAAC;AAC9D,OAAO,EAAE,uBAAuB,EAAE,MAAM,6BAA6B,CAAC;AAEtE,MAAM,iBAAkB,SAAQ,KAAK,CAAC,KAAK,CAAoB,oBAAoB,CAAC,CAAC;IACpF,EAAE,EAAE,KAAK,CAAC,MAAM,CAAC,MAAM,CAAC;IACxB,GAAG,uBAAuB,CAAC,MAAM;CACjC,CAAC;CAAG;AAEL,MAAM,UAAU,GAAG,cAAc,CAAC,iBAAiB,EAAE;IACpD,eAAe,EAAE,CAAC,IAAI,CAAC;IACvB,QAAQ,EAAE,IAAI;IACd,UAAU,EAAE,oBAAoB;IAChC,SAAS,EAAE,8BAA8B;CACzC,CAAC,CAAC;AAEH,MAAM,CAAC,MAAM,qBAAqB,GAAG,gBAAgB,CAAC;IACrD,EAAE,EAAE,2CAA2C;IAC/C,UAAU;CACV,CAAC,CAAC"}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { Effect } from "effect";
|
|
2
|
+
import type { TableColumn } from "#server/account-data/UncoveredUserColumns.ts";
|
|
3
|
+
import type { AuthSchemaDeclaration } from "#server/better-auth/define.ts";
|
|
4
|
+
export declare function accountData(auth: Pick<AuthSchemaDeclaration, "options">, reference?: TableColumn): {
|
|
5
|
+
check: Effect.Effect<undefined, import("../account-data/OwnedModelsUncollected.ts").OwnedModelsUncollected | import("effect/Schema").SchemaError | import("effect/sql/SqlError").SqlError | import("#server/account-data/UncoveredUserColumns.ts").UncoveredUserColumns, import("effect/sql/SqlClient").SqlClient>;
|
|
6
|
+
eraseOwnedRows: (userId: string) => Effect.Effect<void, import("../account-data/OwnedModelsUncollected.ts").OwnedModelsUncollected | import("../account-data/OwnedTableCycle.ts").OwnedTableCycle | import("effect/Schema").SchemaError | import("effect/sql/SqlError").SqlError | import("#server/account-data/UncoveredUserColumns.ts").UncoveredUserColumns, import("effect/sql/SqlClient").SqlClient>;
|
|
7
|
+
exportAccount: (userId: string) => Effect.Effect<{
|
|
8
|
+
account: {
|
|
9
|
+
accounts: {
|
|
10
|
+
readonly createdAt: import("effect/DateTime").Utc;
|
|
11
|
+
readonly updatedAt: import("effect/DateTime").Utc;
|
|
12
|
+
readonly accountId: string;
|
|
13
|
+
readonly id: string;
|
|
14
|
+
readonly providerId: string;
|
|
15
|
+
readonly userId: string;
|
|
16
|
+
}[];
|
|
17
|
+
sessions: {
|
|
18
|
+
readonly createdAt: import("effect/DateTime").Utc;
|
|
19
|
+
readonly updatedAt: import("effect/DateTime").Utc;
|
|
20
|
+
readonly expiresAt: import("effect/DateTime").Utc;
|
|
21
|
+
readonly id: string;
|
|
22
|
+
readonly ipAddress: string | null;
|
|
23
|
+
readonly userAgent: string | null;
|
|
24
|
+
readonly userId: string;
|
|
25
|
+
}[];
|
|
26
|
+
users: {
|
|
27
|
+
readonly createdAt: import("effect/DateTime").Utc;
|
|
28
|
+
readonly updatedAt: import("effect/DateTime").Utc;
|
|
29
|
+
readonly email: string;
|
|
30
|
+
readonly emailVerified: boolean;
|
|
31
|
+
readonly id: string;
|
|
32
|
+
readonly image: string | null;
|
|
33
|
+
readonly name: string;
|
|
34
|
+
}[];
|
|
35
|
+
};
|
|
36
|
+
owned: {
|
|
37
|
+
rows: readonly unknown[];
|
|
38
|
+
table: string;
|
|
39
|
+
}[];
|
|
40
|
+
}, import("../account-data/OwnedModelsUncollected.ts").OwnedModelsUncollected | import("effect/Schema").SchemaError | import("effect/sql/SqlError").SqlError | import("#server/account-data/UncoveredUserColumns.ts").UncoveredUserColumns, import("effect/sql/SqlClient").SqlClient>;
|
|
41
|
+
};
|
|
42
|
+
//# sourceMappingURL=accountData.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"accountData.d.ts","sourceRoot":"","sources":["../../../src/server/better-auth/accountData.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAIhC,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,8CAA8C,CAAC;AAChF,OAAO,KAAK,EAAE,qBAAqB,EAAE,MAAM,+BAA+B,CAAC;AAG3E,wBAAgB,WAAW,CAAC,IAAI,EAAE,IAAI,CAAC,qBAAqB,EAAE,SAAS,CAAC,EAAE,SAAS,CAAC,EAAE,WAAW;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;EAgBhG"}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { getAuthTables } from "better-auth/db";
|
|
2
|
+
import { Effect } from "effect";
|
|
3
|
+
import { checkCoverage } from "#server/account-data/checkCoverage.ts";
|
|
4
|
+
import { eraseOwnedRows } from "#server/account-data/eraseOwnedRows.ts";
|
|
5
|
+
import { exportOwnedRows } from "#server/account-data/exportOwnedRows.ts";
|
|
6
|
+
import { profileExport } from "#server/better-auth/profileExport.ts";
|
|
7
|
+
export function accountData(auth, reference) {
|
|
8
|
+
const tables = getAuthTables(auth.options);
|
|
9
|
+
const user = reference ?? { column: "id", schema: "public", table: tables.user?.modelName ?? "user" };
|
|
10
|
+
const check = checkCoverage(user, new Set(Object.values(tables).map(({ modelName }) => modelName)));
|
|
11
|
+
const exportProfile = profileExport(auth.options, user.schema);
|
|
12
|
+
return {
|
|
13
|
+
check,
|
|
14
|
+
eraseOwnedRows: Effect.fn("AccountData.eraseOwnedRows")(function* (userId) {
|
|
15
|
+
yield* check;
|
|
16
|
+
yield* eraseOwnedRows(userId);
|
|
17
|
+
}),
|
|
18
|
+
exportAccount: Effect.fn("AccountData.exportAccount")(function* (userId) {
|
|
19
|
+
yield* check;
|
|
20
|
+
return { account: yield* exportProfile(userId), owned: yield* exportOwnedRows(userId) };
|
|
21
|
+
}),
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
//# sourceMappingURL=accountData.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"accountData.js","sourceRoot":"","sources":["../../../src/server/better-auth/accountData.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,gBAAgB,CA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AE,KAAK,EAAE,KAAK,CAAC,CAAC,eAAe,CAAC,MAAM,CAAC,EAAE,CAAC;QACzF,CAAC,CAAC;KACF,CAAC;AACH,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"accountPreferenceOptions.d.ts","sourceRoot":"","sources":["../../../../src/server/better-auth/adapters/accountPreferenceOptions.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,iBAAiB,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAC;AAEvE,OAAO,EAAE,MAAM,EAAkB,MAAM,QAAQ,CAAC;
|
|
1
|
+
{"version":3,"file":"accountPreferenceOptions.d.ts","sourceRoot":"","sources":["../../../../src/server/better-auth/adapters/accountPreferenceOptions.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,iBAAiB,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAC;AAEvE,OAAO,EAAE,MAAM,EAAkB,MAAM,QAAQ,CAAC;AAKhD,eAAO,MAAM,wBAAwB,GAAgC,QAAQ,SAAS,iBAAiB;;;;;;;;;;wDA0BrG,CAAC"}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { createAuthMiddleware } from "better-auth/api";
|
|
2
2
|
import { Effect, Option, Schema } from "effect";
|
|
3
|
+
import { accountPreferenceHeaders } from "#accountPreferenceHeaders.ts";
|
|
3
4
|
import { AccountPreferenceValues } from "#accountPreferenceValues.ts";
|
|
4
5
|
import { AccountPreferencesStore } from "#server/account-preferences/service.ts";
|
|
5
6
|
export const accountPreferenceOptions = Effect.fnUntraced(function* (options) {
|
|
@@ -8,8 +9,8 @@ export const accountPreferenceOptions = Effect.fnUntraced(function* (options) {
|
|
|
8
9
|
const capture = createAuthMiddleware(async (context) => {
|
|
9
10
|
const session = context.context.newSession ?? context.context.session;
|
|
10
11
|
const values = Schema.decodeUnknownOption(AccountPreferenceValues)({
|
|
11
|
-
locale: context.headers?.get(
|
|
12
|
-
timezone: context.headers?.get(
|
|
12
|
+
locale: context.headers?.get(accountPreferenceHeaders.locale),
|
|
13
|
+
timezone: context.headers?.get(accountPreferenceHeaders.timezone),
|
|
13
14
|
units: "metric",
|
|
14
15
|
});
|
|
15
16
|
if (session !== null && session !== undefined && Option.isSome(values)) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"accountPreferenceOptions.js","sourceRoot":"","sources":["../../../../src/server/better-auth/adapters/accountPreferenceOptions.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,oBAAoB,EAAE,MAAM,iBAAiB,CAAC;AACvD,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAChD,OAAO,EAAE,uBAAuB,EAAE,MAAM,6BAA6B,CAAC;AACtE,OAAO,EAAE,uBAAuB,EAAE,MAAM,wCAAwC,CAAC;AAEjF,MAAM,CAAC,MAAM,wBAAwB,GAAG,MAAM,CAAC,UAAU,CAAC,QAAQ,CAAC,EAAsC,OAAiB;IACzH,MAAM,KAAK,GAAG,KAAK,CAAC,CAAC,uBAAuB,CAAC;IAC7C,MAAM,QAAQ,GAAG,KAAK,CAAC,CAAC,MAAM,CAAC,OAAO,EAAS,CAAC;IAChD,MAAM,OAAO,GAAG,oBAAoB,CAAC,KAAK,EAAE,OAAO,EAAE,EAAE;QACtD,MAAM,OAAO,GAAG,OAAO,CAAC,OAAO,CAAC,UAAU,IAAI,OAAO,CAAC,OAAO,CAAC,OAAO,CAAC;QACtE,MAAM,MAAM,GAAG,MAAM,CAAC,mBAAmB,CAAC,uBAAuB,CAAC,CAAC;YAClE,MAAM,EAAE,OAAO,CAAC,OAAO,EAAE,GAAG,CAAC,
|
|
1
|
+
{"version":3,"file":"accountPreferenceOptions.js","sourceRoot":"","sources":["../../../../src/server/better-auth/adapters/accountPreferenceOptions.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,oBAAoB,EAAE,MAAM,iBAAiB,CAAC;AACvD,OAAO,EAAE,MAAM,EAAE,MAAM,EA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AAG;QACd,KAAK,EAAE;YACN,KAAK,EAAE;gBACN;oBACC,OAAO,EAAE,OAAO;oBAChB,OAAO,EAAE,CAAC,OAAO,EAAE,EAAE,CAAC,OAAO,CAAC,IAAI,KAAK,cAAc,IAAI,CAAC,OAAO,CAAC,OAAO,CAAC,UAAU,KAAK,IAAI,IAAI,OAAO,CAAC,OAAO,CAAC,UAAU,KAAK,SAAS,CAAC;iBAC1I;aACD;SACD;QACD,EAAE,EAAE,8BAA8B;KACP,CAAC;IAC7B,OAAO,EAAE,GAAG,OAAO,EAAE,OAAO,EAAE,CAAC,GAAG,CAAC,OAAO,CAAC,OAAO,IAAI,EAAE,CAAC,EAAE,MAAM,CAAC,EAA8B,CAAC;AAClG,CAAC,CAAC,CAAC"}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import type { BetterAuthOptions } from "better-auth";
|
|
2
|
+
import { Effect, Schema } from "effect";
|
|
3
|
+
import { SqlClient } from "effect/sql";
|
|
4
|
+
export declare function profileExport(options: Omit<BetterAuthOptions, "database">, schema: string): (userId: string) => Effect.Effect<{
|
|
5
|
+
accounts: {
|
|
6
|
+
readonly createdAt: import("effect/DateTime").Utc;
|
|
7
|
+
readonly updatedAt: import("effect/DateTime").Utc;
|
|
8
|
+
readonly accountId: string;
|
|
9
|
+
readonly id: string;
|
|
10
|
+
readonly providerId: string;
|
|
11
|
+
readonly userId: string;
|
|
12
|
+
}[];
|
|
13
|
+
sessions: {
|
|
14
|
+
readonly createdAt: import("effect/DateTime").Utc;
|
|
15
|
+
readonly updatedAt: import("effect/DateTime").Utc;
|
|
16
|
+
readonly expiresAt: import("effect/DateTime").Utc;
|
|
17
|
+
readonly id: string;
|
|
18
|
+
readonly ipAddress: string | null;
|
|
19
|
+
readonly userAgent: string | null;
|
|
20
|
+
readonly userId: string;
|
|
21
|
+
}[];
|
|
22
|
+
users: {
|
|
23
|
+
readonly createdAt: import("effect/DateTime").Utc;
|
|
24
|
+
readonly updatedAt: import("effect/DateTime").Utc;
|
|
25
|
+
readonly email: string;
|
|
26
|
+
readonly emailVerified: boolean;
|
|
27
|
+
readonly id: string;
|
|
28
|
+
readonly image: string | null;
|
|
29
|
+
readonly name: string;
|
|
30
|
+
}[];
|
|
31
|
+
}, Schema.SchemaError | import("effect/sql/SqlError").SqlError, SqlClient.SqlClient>;
|
|
32
|
+
//# sourceMappingURL=profileExport.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"profileExport.d.ts","sourceRoot":"","sources":["../../../src/server/better-auth/profileExport.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,aAAa,CAAC;AAErD,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AACxC,OAAO,EAAE,SAAS,EAAa,MAAM,YAAY,CAAC;AAsBlD,wBAAgB,aAAa,CAAC,OAAO,EAAE,IAAI,CAAC,iBAAiB,EAAE,UAAU,CAAC,EAAE,MAAM,EAAE,MAAM;;;;;;;;;;;;;;;;;;;;;;;;;;;qFA6BzF"}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { getAuthTables } from "better-auth/db";
|
|
2
|
+
import { Effect, Schema } from "effect";
|
|
3
|
+
import { SqlClient, SqlSchema } from "effect/sql";
|
|
4
|
+
const Timestamps = { createdAt: Schema.DateTimeUtcFromDate, updatedAt: Schema.DateTimeUtcFromDate };
|
|
5
|
+
const User = Schema.Struct({
|
|
6
|
+
...Timestamps,
|
|
7
|
+
email: Schema.String,
|
|
8
|
+
emailVerified: Schema.Boolean,
|
|
9
|
+
id: Schema.String,
|
|
10
|
+
image: Schema.NullOr(Schema.String),
|
|
11
|
+
name: Schema.String,
|
|
12
|
+
});
|
|
13
|
+
const Account = Schema.Struct({ ...Timestamps, accountId: Schema.String, id: Schema.String, providerId: Schema.String, userId: Schema.String });
|
|
14
|
+
const Session = Schema.Struct({
|
|
15
|
+
...Timestamps,
|
|
16
|
+
expiresAt: Schema.DateTimeUtcFromDate,
|
|
17
|
+
id: Schema.String,
|
|
18
|
+
ipAddress: Schema.NullOr(Schema.String),
|
|
19
|
+
userAgent: Schema.NullOr(Schema.String),
|
|
20
|
+
userId: Schema.String,
|
|
21
|
+
});
|
|
22
|
+
// Selects each field it exports, so passwords and provider or session tokens stay out of a person's export.
|
|
23
|
+
export function profileExport(options, schema) {
|
|
24
|
+
const models = getAuthTables(options);
|
|
25
|
+
return Effect.fn("BetterAuth.profileExport")(function* (userId) {
|
|
26
|
+
const sql = (yield* SqlClient.SqlClient).withoutTransforms();
|
|
27
|
+
function select(model, fields, owner, id) {
|
|
28
|
+
const table = models[model];
|
|
29
|
+
function column(name) {
|
|
30
|
+
return table?.fields[name]?.fieldName ?? name;
|
|
31
|
+
}
|
|
32
|
+
const columns = sql.csv(fields.map((name) => sql `${sql(column(name))} as ${sql(name)}`));
|
|
33
|
+
return sql `select ${columns} from ${sql(`${schema}.${table?.modelName ?? model}`)} where ${sql(column(owner))} = ${id} order by id`;
|
|
34
|
+
}
|
|
35
|
+
const users = yield* SqlSchema.findAll({
|
|
36
|
+
execute: (id) => select("user", Object.keys(User.fields), "id", id),
|
|
37
|
+
Request: Schema.String,
|
|
38
|
+
Result: User,
|
|
39
|
+
})(userId);
|
|
40
|
+
const accounts = yield* SqlSchema.findAll({
|
|
41
|
+
execute: (id) => select("account", Object.keys(Account.fields), "userId", id),
|
|
42
|
+
Request: Schema.String,
|
|
43
|
+
Result: Account,
|
|
44
|
+
})(userId);
|
|
45
|
+
const sessions = yield* SqlSchema.findAll({
|
|
46
|
+
execute: (id) => select("session", Object.keys(Session.fields), "userId", id),
|
|
47
|
+
Request: Schema.String,
|
|
48
|
+
Result: Session,
|
|
49
|
+
})(userId);
|
|
50
|
+
return { accounts, sessions, users };
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
//# sourceMappingURL=profileExport.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"profileExport.js","s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ACX,MAAM,QAAQ,GAAG,KAAK,CAAC,CAAC,SAAS,CAAC,OAAO,CAAC;YACzC,OAAO,EAAE,CAAC,EAAE,EAAE,EAAE,CAAC,MAAM,CAAC,SAAS,EAAE,MAAM,CAAC,IAAI,CAAC,OAAO,CAAC,MAAM,CAAC,EAAE,QAAQ,EAAE,EAAE,CAAC;YAC7E,OAAO,EAAE,MAAM,CAAC,MAAM;YACtB,MAAM,EAAE,OAAO;SACf,CAAC,CAAC,MAAM,CAAC,CAAC;QACX,MAAM,QAAQ,GAAG,KAAK,CAAC,CAAC,SAAS,CAAC,OAAO,CAAC;YACzC,OAAO,EAAE,CAAC,EAAE,EAAE,EAAE,CAAC,MAAM,CAAC,SAAS,EAAE,MAAM,CAAC,IAAI,CAAC,OAAO,CAAC,MAAM,CAAC,EAAE,QAAQ,EAAE,EAAE,CAAC;YAC7E,OAAO,EAAE,MAAM,CAAC,MAAM;YACtB,MAAM,EAAE,OAAO;SACf,CAAC,CAAC,MAAM,CAAC,CAAC;QACX,OAAO,EAAE,QAAQ,EAAE,QAAQ,EAAE,KAAK,EAAE,CAAC;IACtC,CAAC,CAAC,CAAC;AACJ,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@shivaedev/identity",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.41.0",
|
|
4
4
|
"description": "Request identity, sessions, transport trust and permission enforcement for Effect RPC servers",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"effect"
|
|
@@ -50,21 +50,21 @@
|
|
|
50
50
|
"@shivaedev/changes": "0.14.1",
|
|
51
51
|
"@shivaedev/contract": "0.58.0",
|
|
52
52
|
"@shivaedev/errors": "0.14.1",
|
|
53
|
-
"@shivaedev/rate-limit": "0.8.
|
|
53
|
+
"@shivaedev/rate-limit": "0.8.1",
|
|
54
54
|
"@shivaedev/service": "0.9.2",
|
|
55
|
-
"@shivaedev/sql": "0.
|
|
55
|
+
"@shivaedev/sql": "0.55.0"
|
|
56
56
|
},
|
|
57
57
|
"devDependencies": {
|
|
58
58
|
"@better-auth/oauth-provider": "1.7.23",
|
|
59
59
|
"@effect/platform-node": "4.1.4",
|
|
60
60
|
"@effect/sql-pg": "4.1.4",
|
|
61
|
-
"@shivaedev/admin": "0.29.
|
|
62
|
-
"@shivaedev/build": "0.
|
|
63
|
-
"@shivaedev/quality": "0.
|
|
64
|
-
"@shivaedev/server": "0.
|
|
65
|
-
"@shivaedev/test-kits": "0.16.
|
|
66
|
-
"@shivaedev/test-sql": "0.26.
|
|
67
|
-
"@shivaedev/test-story": "0.52.
|
|
61
|
+
"@shivaedev/admin": "0.29.3",
|
|
62
|
+
"@shivaedev/build": "0.8.0",
|
|
63
|
+
"@shivaedev/quality": "0.84.0",
|
|
64
|
+
"@shivaedev/server": "0.44.0",
|
|
65
|
+
"@shivaedev/test-kits": "0.16.6",
|
|
66
|
+
"@shivaedev/test-sql": "0.26.1",
|
|
67
|
+
"@shivaedev/test-story": "0.52.5",
|
|
68
68
|
"@types/node": "24.19.2",
|
|
69
69
|
"better-auth": "1.7.23",
|
|
70
70
|
"effect": "4.1.4",
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { accountPreferenceHeaders } from "#accountPreferenceHeaders.ts";
|
|
2
|
+
|
|
3
|
+
// Structural copies of the Better Auth client plugin shapes: the client core must not reach the optional better-auth peer.
|
|
4
|
+
interface PreferenceRequest {
|
|
5
|
+
readonly headers: Headers;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
interface PreferenceFetchPlugin {
|
|
9
|
+
readonly hooks: { readonly onRequest: (context: PreferenceRequest) => void };
|
|
10
|
+
readonly id: string;
|
|
11
|
+
readonly name: string;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export interface PreferencesPlugin {
|
|
15
|
+
readonly fetchPlugins: PreferenceFetchPlugin[];
|
|
16
|
+
readonly id: "identity-account-preferences";
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export function preferencesPlugin(): PreferencesPlugin {
|
|
20
|
+
return {
|
|
21
|
+
fetchPlugins: [
|
|
22
|
+
{
|
|
23
|
+
hooks: {
|
|
24
|
+
onRequest(context) {
|
|
25
|
+
// A server-rendered auth call would send the server's zone, so only a page or app shell sends one.
|
|
26
|
+
if (!("window" in globalThis)) {
|
|
27
|
+
return;
|
|
28
|
+
}
|
|
29
|
+
const { locale, timeZone } = new Intl.DateTimeFormat().resolvedOptions();
|
|
30
|
+
context.headers.set(accountPreferenceHeaders.timezone, timeZone);
|
|
31
|
+
context.headers.set(accountPreferenceHeaders.locale, locale);
|
|
32
|
+
},
|
|
33
|
+
},
|
|
34
|
+
id: "identity-account-preferences",
|
|
35
|
+
name: "identity-account-preferences",
|
|
36
|
+
},
|
|
37
|
+
],
|
|
38
|
+
id: "identity-account-preferences",
|
|
39
|
+
};
|
|
40
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { Schema } from "effect";
|
|
2
|
+
|
|
3
|
+
export class OwnedModelsUncollected extends Schema.TaggedError<OwnedModelsUncollected>()("OwnedModelsUncollected", {}) {
|
|
4
|
+
override get message() {
|
|
5
|
+
return "No collector receives the owned models that repositories report, so identity cannot see what to export, erase or count as covered: provide collectRepositoryModels from @shivaedev/sql/RepositoryModels.ts before the repositories are built, as appHost does";
|
|
6
|
+
}
|
|
7
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { Schema } from "effect";
|
|
2
|
+
|
|
3
|
+
export class OwnedTableCycle extends Schema.TaggedError<OwnedTableCycle>()("OwnedTableCycle", {
|
|
4
|
+
tables: Schema.Array(Schema.String),
|
|
5
|
+
}) {
|
|
6
|
+
override get message() {
|
|
7
|
+
return `Owned tables reference each other through RESTRICT or NO ACTION foreign keys in a cycle, so no order can erase them: ${this.tables.join(", ")}`;
|
|
8
|
+
}
|
|
9
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { Schema } from "effect";
|
|
2
|
+
|
|
3
|
+
const TableColumn = Schema.Struct({ column: Schema.String, schema: Schema.String, table: Schema.String });
|
|
4
|
+
|
|
5
|
+
export type TableColumn = typeof TableColumn.Type;
|
|
6
|
+
|
|
7
|
+
export class UncoveredUserColumns extends Schema.TaggedError<UncoveredUserColumns>()("UncoveredUserColumns", {
|
|
8
|
+
columns: Schema.Array(TableColumn),
|
|
9
|
+
}) {
|
|
10
|
+
override get message() {
|
|
11
|
+
const columns = this.columns.map(({ column, schema, table }) => `${schema}.${table}.${column}`).join(", ");
|
|
12
|
+
return `Columns refer to a person, but no owned model covers them: ${columns}. Mark the column's field with owner from @shivaedev/sql/owner.ts on a model that has a repository, or declare its foreign key ON DELETE SET NULL when the row stays without the person`;
|
|
13
|
+
}
|
|
14
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { Effect } from "effect";
|
|
2
|
+
import { foreignKeys } from "#server/account-data/foreignKeys.ts";
|
|
3
|
+
import { ownedTables } from "#server/account-data/ownedTables.ts";
|
|
4
|
+
import { type TableColumn, UncoveredUserColumns } from "#server/account-data/UncoveredUserColumns.ts";
|
|
5
|
+
|
|
6
|
+
const SET_NULL = "n";
|
|
7
|
+
|
|
8
|
+
function key({ column, schema, table }: TableColumn) {
|
|
9
|
+
return `${schema}.${table}.${column}`;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
// Identity's own tables are covered because the person's auth user row goes with the account and they reference it.
|
|
13
|
+
export const checkCoverage = Effect.fn("Identity.checkCoverage")(function* (reference: TableColumn, authTables: ReadonlySet<string>) {
|
|
14
|
+
const owners = new Set((yield* ownedTables()).map(({ ownerKey }) => ownerKey));
|
|
15
|
+
const user = key(reference);
|
|
16
|
+
const uncovered = new Map<string, TableColumn>();
|
|
17
|
+
for (const foreignKey of yield* foreignKeys()) {
|
|
18
|
+
const refersToPerson = key(foreignKey.target) === user || owners.has(key(foreignKey.target));
|
|
19
|
+
const covered =
|
|
20
|
+
owners.has(key(foreignKey)) || foreignKey.onDelete === SET_NULL || (foreignKey.schema === reference.schema && authTables.has(foreignKey.table));
|
|
21
|
+
if (refersToPerson && !covered) {
|
|
22
|
+
uncovered.set(key(foreignKey), { column: foreignKey.column, schema: foreignKey.schema, table: foreignKey.table });
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
if (uncovered.size > 0) {
|
|
26
|
+
return yield* new UncoveredUserColumns({ columns: [...uncovered.values()] });
|
|
27
|
+
}
|
|
28
|
+
});
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { Result } from "effect";
|
|
2
|
+
|
|
3
|
+
export interface Reference {
|
|
4
|
+
readonly source: string;
|
|
5
|
+
readonly target: string;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
interface Remaining {
|
|
9
|
+
readonly left: readonly string[];
|
|
10
|
+
readonly references: readonly Reference[];
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
// A table goes once nothing left references it, so the rows referencing a table are deleted before the table's rows.
|
|
14
|
+
function peel({ left, references }: Remaining, order: readonly string[]): Remaining & { readonly order: readonly string[] } {
|
|
15
|
+
const next = left.find((table) => !references.some(({ target }) => target === table));
|
|
16
|
+
if (next === undefined) {
|
|
17
|
+
return { left, order, references };
|
|
18
|
+
}
|
|
19
|
+
const remaining = { left: left.filter((table) => table !== next), references: references.filter(({ source }) => source !== next) };
|
|
20
|
+
return peel(remaining, [...order, next]);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
// Dropping what references nothing left keeps the tables on a cycle and those between its tables.
|
|
24
|
+
function cycle({ left, references }: Remaining): readonly string[] {
|
|
25
|
+
const sink = left.find((table) => !references.some(({ source }) => source === table));
|
|
26
|
+
if (sink === undefined) {
|
|
27
|
+
return left;
|
|
28
|
+
}
|
|
29
|
+
return cycle({ left: left.filter((table) => table !== sink), references: references.filter(({ target }) => target !== sink) });
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export function eraseOrder(tables: readonly string[], references: readonly Reference[]): Result.Result<readonly string[], readonly string[]> {
|
|
33
|
+
const between = references.filter(({ source, target }) => source !== target && tables.includes(source) && tables.includes(target));
|
|
34
|
+
const peeled = peel({ left: tables, references: between }, []);
|
|
35
|
+
return peeled.left.length === 0 ? Result.succeed(peeled.order) : Result.fail(cycle(peeled));
|
|
36
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { Effect, Result } from "effect";
|
|
2
|
+
import { SqlClient } from "effect/sql";
|
|
3
|
+
import { eraseOrder } from "#server/account-data/eraseOrder.ts";
|
|
4
|
+
import { foreignKeys } from "#server/account-data/foreignKeys.ts";
|
|
5
|
+
import { OwnedTableCycle } from "#server/account-data/OwnedTableCycle.ts";
|
|
6
|
+
import { ownedTables } from "#server/account-data/ownedTables.ts";
|
|
7
|
+
|
|
8
|
+
// A cascading or nulling key orders nothing: PostgreSQL resolves it whichever row goes first.
|
|
9
|
+
const ORDERING_ACTIONS = new Set(["a", "r"]);
|
|
10
|
+
|
|
11
|
+
// Runs on the caller's connection, so a surrounding transact erases everything or nothing.
|
|
12
|
+
export const eraseOwnedRows = Effect.fn("Identity.eraseOwnedRows")(function* (userId: string) {
|
|
13
|
+
const owned = yield* ownedTables();
|
|
14
|
+
const relations = [...new Set(owned.map(({ relation }) => relation))].sort((left, right) => left.localeCompare(right));
|
|
15
|
+
const references = (yield* foreignKeys())
|
|
16
|
+
.filter(({ onDelete }) => ORDERING_ACTIONS.has(onDelete))
|
|
17
|
+
.map(({ schema, table, target }) => ({ source: `${schema}.${table}`, target: `${target.schema}.${target.table}` }));
|
|
18
|
+
const order = eraseOrder(relations, references);
|
|
19
|
+
if (Result.isFailure(order)) {
|
|
20
|
+
return yield* new OwnedTableCycle({ tables: order.failure });
|
|
21
|
+
}
|
|
22
|
+
const sql = yield* SqlClient.SqlClient;
|
|
23
|
+
for (const relation of order.success) {
|
|
24
|
+
for (const { owner, table } of owned.filter((model) => model.relation === relation)) {
|
|
25
|
+
yield* sql`delete from ${sql(table)} where ${sql(owner.field)} = ${userId}`;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
});
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { Effect, Schema, SchemaAST } from "effect";
|
|
2
|
+
import { SqlClient } from "effect/sql";
|
|
3
|
+
import { groupColumns } from "@shivaedev/sql/adapters/groupColumns.ts";
|
|
4
|
+
import type { OwnedModel } from "@shivaedev/sql/RepositoryModels.ts";
|
|
5
|
+
import { sqlNameTransforms } from "@shivaedev/sql/sqlNameTransforms.ts";
|
|
6
|
+
import { storageSchema } from "@shivaedev/sql/storageSchema.ts";
|
|
7
|
+
import { ownedTables } from "#server/account-data/ownedTables.ts";
|
|
8
|
+
|
|
9
|
+
// The host reports models with their types erased; the JSON variant's fields are the ones a client may already
|
|
10
|
+
// receive, so reading and encoding them needs no service. A server-only or encrypted field has no JSON variant and
|
|
11
|
+
// is never read.
|
|
12
|
+
type Exported = Schema.ConstraintCodec<unknown, unknown, never, never>;
|
|
13
|
+
|
|
14
|
+
function exportedSchemas({ model }: OwnedModel, names: Effect.Success<typeof sqlNameTransforms>) {
|
|
15
|
+
const json = model.json.ast;
|
|
16
|
+
const properties: SchemaAST.PropertySignature[] = [];
|
|
17
|
+
const fields: Record<string, Schema.Constraint> = {};
|
|
18
|
+
for (const property of SchemaAST.isObjects(json) ? json.propertySignatures : []) {
|
|
19
|
+
const field = typeof property.name === "string" ? model.fields[property.name] : undefined;
|
|
20
|
+
if (typeof property.name === "string" && field !== undefined) {
|
|
21
|
+
properties.push(property);
|
|
22
|
+
fields[property.name] = field;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
return {
|
|
26
|
+
columns: groupColumns(fields),
|
|
27
|
+
json: Schema.make<Exported>(new SchemaAST.Objects(properties, [])),
|
|
28
|
+
stored: Schema.make<Exported>(storageSchema(Schema.Struct(fields), fields, names).ast),
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export const exportOwnedRows = Effect.fn("Identity.exportOwnedRows")(function* (userId: string) {
|
|
33
|
+
const sql = yield* SqlClient.SqlClient;
|
|
34
|
+
const names = yield* sqlNameTransforms;
|
|
35
|
+
const owned = [...(yield* ownedTables())].sort((left, right) => left.table.localeCompare(right.table));
|
|
36
|
+
return yield* Effect.forEach(owned, (model) =>
|
|
37
|
+
Effect.gen(function* () {
|
|
38
|
+
const { columns, json, stored } = exportedSchemas(model, names);
|
|
39
|
+
const selected = sql.csv(columns.map(({ column }) => sql`${sql(column)}`));
|
|
40
|
+
const rows = yield* sql`select ${selected} from ${sql(model.table)} where ${sql(model.owner.field)} = ${userId}`;
|
|
41
|
+
const decoded = yield* Schema.decodeUnknownEffect(Schema.Array(stored))(rows);
|
|
42
|
+
return { rows: yield* Schema.encodeUnknownEffect(Schema.Array(json))(decoded), table: model.table };
|
|
43
|
+
}),
|
|
44
|
+
);
|
|
45
|
+
});
|