a tool for shared writing and social publishing
1import {
2 entities,
3 permission_tokens,
4 permission_token_rights,
5 entity_sets,
6 identities,
7} from "drizzle/schema";
8import { v7 } from "uuid";
9import { PgTransaction } from "drizzle-orm/pg-core";
10import { NodePgDatabase } from "drizzle-orm/node-postgres";
11import { Json } from "supabase/database.types";
12
13export async function createIdentity(
14 db: NodePgDatabase,
15 data?: { email?: string; atp_did?: string },
16) {
17 return db.transaction(async (tx) => {
18 // Create a new entity set
19 let [entity_set] = await tx.insert(entity_sets).values({}).returning();
20 // Create a root-entity
21 let [entity] = await tx
22 .insert(entities)
23 // And add it to that permission set
24 .values({ set: entity_set.id, id: v7() })
25 .returning();
26 //Create a new permission token
27 let [permissionToken] = await tx
28 .insert(permission_tokens)
29 .values({ root_entity: entity.id })
30 .returning();
31 //and give it all the permission on that entity set
32 let [rights] = await tx
33 .insert(permission_token_rights)
34 .values({
35 token: permissionToken.id,
36 entity_set: entity_set.id,
37 read: true,
38 write: true,
39 create_token: true,
40 change_entity_set: true,
41 })
42 .returning();
43 let [identity] = await tx
44 .insert(identities)
45 .values({ home_page: permissionToken.id, ...data })
46 .returning();
47 return identity as Omit<typeof identity, "interface_state"> & {
48 interface_state: Json;
49 };
50 });
51}