@eightyfourthousand/data-access 2026.3.1 → 2026.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.
@@ -1,147 +0,0 @@
1
- import {
2
- DataClient,
3
- GlossaryDetailDTO,
4
- GlossaryEntityDTO,
5
- GlossaryInstanceDTO,
6
- GlossaryTermInstanceDTO,
7
- GlossaryLandingItem,
8
- GlossaryLandingItemDTO,
9
- glossaryTermInstanceFromDTO,
10
- glossaryLandingItemFromDTO,
11
- glossaryPageItemFromDTO,
12
- GlossaryTermInstancesDTO,
13
- } from './types';
14
-
15
- export const getAllGlossaryTerms = async ({
16
- client,
17
- uuids,
18
- }: {
19
- client: DataClient;
20
- uuids?: string[];
21
- }): Promise<GlossaryLandingItem[]> => {
22
- const pageSize = 1000;
23
- let start = 0;
24
- let end = pageSize - 1;
25
- let count = pageSize;
26
- const terms: GlossaryLandingItem[] = [];
27
-
28
- while (count === pageSize) {
29
- let rpc = client.rpc('scholar_glossary_get_all');
30
-
31
- if (uuids?.length) {
32
- rpc = rpc.in('authority_uuid', uuids);
33
- }
34
-
35
- const { data, error } = await rpc.range(start, end);
36
-
37
- if (error) {
38
- console.error('Error fetching glossary terms:', error);
39
- return [];
40
- }
41
- if (!data || !data.length) {
42
- break;
43
- }
44
-
45
- const dtos = data as GlossaryLandingItemDTO[];
46
- const items = dtos
47
- .map((item) => glossaryLandingItemFromDTO(item as GlossaryLandingItemDTO))
48
- .flatMap((item) => (item ? [item] : []));
49
-
50
- terms.push(...items);
51
- start += pageSize;
52
- end += pageSize;
53
- count = data.length;
54
- }
55
-
56
- return terms;
57
- };
58
-
59
- export const getGlossaryInstances = async ({
60
- client,
61
- uuid,
62
- withAttestations = false,
63
- }: {
64
- client: DataClient;
65
- uuid: string;
66
- withAttestations?: boolean;
67
- }) => {
68
- const { data, error } = await client.rpc('show_glossary_entries', {
69
- v_work_uuid: uuid,
70
- v_with_attestation: withAttestations,
71
- });
72
- if (error) {
73
- console.error(
74
- `Error fetching glossary instances for work: ${uuid} `,
75
- error,
76
- );
77
- return [];
78
- }
79
-
80
- const dto = data as GlossaryTermInstancesDTO;
81
- return dto.glossary_entries.map(glossaryTermInstanceFromDTO).sort((a, b) => {
82
- const nameA = a.names.english || '';
83
- const nameB = b.names.english || '';
84
- return nameA.localeCompare(nameB);
85
- });
86
- };
87
-
88
- export const getGlossaryInstance = async ({
89
- client,
90
- uuid,
91
- }: {
92
- client: DataClient;
93
- uuid: string;
94
- }) => {
95
- const { data, error } = await client.rpc('show_glossary_entry', {
96
- v_glossary_uuid: uuid,
97
- });
98
-
99
- if (error) {
100
- console.error(`Error fetching glossary instance: ${uuid} `, error);
101
- return undefined;
102
- }
103
-
104
- return glossaryTermInstanceFromDTO(data as GlossaryTermInstanceDTO);
105
- };
106
-
107
- export const getGlossaryEntry = async ({
108
- client,
109
- uuid,
110
- }: {
111
- client: DataClient;
112
- uuid: string;
113
- }) => {
114
- const { data: details = [], error: detailError } = await client.rpc(
115
- 'scholar_glossary_detail',
116
- { v_uuid: uuid },
117
- );
118
-
119
- if (detailError || !details?.length) {
120
- console.error('Error fetching glossary item:', detailError);
121
- return null;
122
- }
123
-
124
- const detail = details[0];
125
-
126
- const { data: entities = [], error: entitiesError } = await client.rpc(
127
- 'scholar_glossary_detail_related_entities',
128
- { v_uuid: uuid },
129
- );
130
- if (entitiesError) {
131
- console.error('Error fetching related entities:', entitiesError);
132
- }
133
-
134
- const { data: glossaries = [], error: glossariesError } = await client.rpc(
135
- 'scholar_glossary_detail_related_glossaries',
136
- { v_uuid: uuid },
137
- );
138
- if (glossariesError) {
139
- console.error('Error fetching related glossaries:', glossariesError);
140
- }
141
-
142
- return glossaryPageItemFromDTO(
143
- detail as GlossaryDetailDTO,
144
- glossaries as GlossaryInstanceDTO[],
145
- entities as GlossaryEntityDTO[],
146
- );
147
- };
@@ -1,122 +0,0 @@
1
- import {
2
- ANNOTATIONS_TO_IGNORE,
3
- DataClient,
4
- Passage,
5
- PassageDTO,
6
- annotationsFromDTO,
7
- passageFromDTO,
8
- passagesToDTO,
9
- passagesToRowDTO,
10
- } from './types';
11
-
12
- export const getPassage = async ({
13
- client,
14
- uuid,
15
- }: {
16
- client: DataClient;
17
- uuid: string;
18
- }) => {
19
- const { data } = await client
20
- .rpc('get_passage_with_annotations', {
21
- uuid_input: uuid,
22
- })
23
- .single();
24
-
25
- if (!data) {
26
- console.warn(`No passage found for uuid: ${uuid}`);
27
- return undefined;
28
- }
29
-
30
- const dto = data as PassageDTO;
31
- return passageFromDTO(
32
- dto,
33
- annotationsFromDTO(dto?.annotations || [], dto?.content.length || 0),
34
- );
35
- };
36
-
37
- export const getPassageUuidByXmlId = async ({
38
- client,
39
- xmlId,
40
- }: {
41
- client: DataClient;
42
- xmlId: string;
43
- }) => {
44
- const { data, error } = await client
45
- .from('passages')
46
- .select('uuid, workUuid:work_uuid')
47
- .eq('xmlId', xmlId)
48
- .single();
49
-
50
- if (error) {
51
- console.error(`Error fetching passage uuid for xmlId: ${xmlId}`, error);
52
- return;
53
- }
54
-
55
- return data?.uuid;
56
- };
57
-
58
- export const savePassages = async ({
59
- client,
60
- passages,
61
- }: {
62
- client: DataClient;
63
- passages: Passage[];
64
- }) => {
65
- /**
66
- * 1. Extract all annotations from savePassages
67
- * 2. Query the database for all current annotations for the savePassages
68
- * 3. Determine which annotations need to be deleted or upserted
69
- * 4. Upsert the passages
70
- * 5. Upsert the annotations
71
- * 6. Delete the annotations that are no longer present
72
- */
73
- const dtos = passagesToDTO(passages);
74
- const passageRowDtos = passagesToRowDTO(passages);
75
- const passageUuids = passages.map((p) => p.uuid);
76
- const annotations = dtos.flatMap((p) => p.annotations || []);
77
-
78
- const { data: existingAnnotations } = await client
79
- .from('passage_annotations')
80
- .select(`uuid`)
81
- .in('passage_uuid', passageUuids)
82
- .not('type', 'in', `(${ANNOTATIONS_TO_IGNORE.join(',')})`);
83
-
84
- const annotationsToDelete = existingAnnotations?.filter(
85
- (ea) => !annotations.find((a) => a.uuid === ea.uuid),
86
- );
87
-
88
- const { error: passageError } = await client
89
- .from('passages')
90
- .upsert(passageRowDtos, { onConflict: 'uuid' });
91
-
92
- if (passageError) {
93
- console.error('Error saving passages:', passageError);
94
- return;
95
- }
96
-
97
- if (annotations.length > 0) {
98
- const { error: annotationError } = await client
99
- .from('passage_annotations')
100
- .upsert(annotations, { onConflict: 'uuid' });
101
-
102
- if (annotationError) {
103
- console.error('Error saving annotations:', annotationError);
104
- throw annotationError;
105
- }
106
- }
107
-
108
- if (annotationsToDelete && annotationsToDelete.length > 0) {
109
- const { error: deleteError } = await client
110
- .from('passage_annotations')
111
- .delete()
112
- .in(
113
- 'uuid',
114
- annotationsToDelete.map((a) => a.uuid),
115
- );
116
-
117
- if (deleteError) {
118
- console.error('Error deleting annotations:', deleteError);
119
- throw deleteError;
120
- }
121
- }
122
- };
@@ -1,107 +0,0 @@
1
- import {
2
- DataClient,
3
- ProjectContributor,
4
- ProjectContributorsDTO,
5
- projectContributorsFromDTO,
6
- } from './types';
7
- import {
8
- Project,
9
- ProjectAsset,
10
- ProjectStageDetail,
11
- ProjectStageDetailsDTO,
12
- ProjectTableDTO,
13
- ProjectViewDTO,
14
- projectFromTableDTO,
15
- projectFromViewDTO,
16
- projectStageDetailsFromDTO,
17
- } from './types/project';
18
-
19
- export const getProjects = async ({ client }: { client: DataClient }) => {
20
- const { data } = await client.rpc('get_projects');
21
-
22
- const dtos = (data as ProjectViewDTO[]) || [];
23
- const projects = dtos.map(projectFromViewDTO);
24
-
25
- return { projects };
26
- };
27
-
28
- export const getProjectByUuid = async ({
29
- client,
30
- uuid,
31
- }: {
32
- client: DataClient;
33
- uuid: string;
34
- }): Promise<Project> => {
35
- const { data } = await client.rpc('get_project', { uuid_input: uuid });
36
-
37
- return projectFromTableDTO(data as ProjectTableDTO);
38
- };
39
-
40
- export const getProjectStages = async ({
41
- client,
42
- uuid,
43
- }: {
44
- client: DataClient;
45
- uuid: string;
46
- }): Promise<ProjectStageDetail[]> => {
47
- const { data } = await client.rpc('get_project_stages', { uuid_input: uuid });
48
- return projectStageDetailsFromDTO(data as ProjectStageDetailsDTO[]);
49
- };
50
-
51
- export const getProjectAssets = async ({
52
- client,
53
- projectUuid,
54
- stageUuid,
55
- }: {
56
- client: DataClient;
57
- projectUuid?: string;
58
- stageUuid?: string;
59
- }): Promise<ProjectAsset[]> => {
60
- if (!projectUuid && !stageUuid) {
61
- console.error('One of projectUuid or stageUuid is required');
62
- return [];
63
- }
64
-
65
- const { data } = await client.rpc('get_project_assets', {
66
- project_uuid_input: projectUuid,
67
- stage_uuid_input: stageUuid,
68
- });
69
- return data || [];
70
- };
71
-
72
- export const getProjectContributors = async ({
73
- client,
74
- projectUuid,
75
- stageUuid,
76
- }: {
77
- client: DataClient;
78
- projectUuid?: string;
79
- stageUuid?: string;
80
- }): Promise<ProjectContributor[]> => {
81
- if (!projectUuid && !stageUuid) {
82
- console.error('One of projectUuid or stageUuid is required');
83
- return [];
84
- }
85
-
86
- const uuid = stageUuid ? stageUuid : projectUuid;
87
- const key = stageUuid ? 'stage_uuid' : 'project_uuid';
88
-
89
- const { data } = await client
90
- .from('project_contributors')
91
- .select(
92
- `
93
- uuid,
94
- stageUuid:stage_uuid,
95
- projectUuid:project_uuid,
96
- ...authorities!contributor_uuid(
97
- contributorUuid:uuid,
98
- name:headword
99
- ),
100
- startDate:start_date,
101
- endDate:end_date,
102
- role:type
103
- `,
104
- )
105
- .eq(key, uuid);
106
- return projectContributorsFromDTO(data as ProjectContributorsDTO);
107
- };
@@ -1,148 +0,0 @@
1
- import { TohokuCatalogEntry } from './toh';
2
- import { ExtendedTranslationLanguage } from './language';
3
-
4
- export type CanonNodeType =
5
- | 'root'
6
- | 'canonicalSection'
7
- | 'nonCanonicalSection'
8
- | 'textGrouping';
9
-
10
- export type CanonDTO = {
11
- uuid: string;
12
- created_at: string;
13
- label: string;
14
- parent_uuid?: string;
15
- xmlId: string;
16
- parent_xmlId?: string;
17
- sort: number;
18
- description: string;
19
- type: CanonNodeType;
20
- public_description?: string;
21
- article_uuid?: string;
22
- };
23
-
24
- export type CanonHead = {
25
- children: CanonNode[];
26
- };
27
-
28
- export type CanonNode = {
29
- uuid: string;
30
- label?: string;
31
- xmlId?: string;
32
- parentXmlId?: string;
33
- parentUuid?: string;
34
- sort: number;
35
- description?: string;
36
- type: CanonNodeType;
37
- publicDescription?: string;
38
- articleUuid?: string;
39
- children?: CanonNode[];
40
- createdAt?: string;
41
- };
42
-
43
- export type CanonArticleDTO = {
44
- uuid: string;
45
- title: string;
46
- body: string;
47
- image?: string;
48
- caption?: string;
49
- };
50
-
51
- export type CanonArticle = CanonArticleDTO;
52
-
53
- export type CanonArticleTitleDTO = {
54
- title: string;
55
- language: ExtendedTranslationLanguage;
56
- };
57
-
58
- export type CanonArticleTitle = CanonArticleTitleDTO;
59
-
60
- export type CanonDetailDTO = {
61
- uuid: string;
62
- label: string;
63
- description: string;
64
- type: CanonNodeType;
65
- article?: CanonArticleDTO;
66
- titles: CanonArticleTitleDTO[];
67
- };
68
- export type CanonDetail = CanonDetailDTO & {
69
- article?: CanonArticle;
70
- titles: CanonArticleTitle[];
71
- };
72
-
73
- export type CanonWorkStatus = 'published' | 'in-progress' | 'not-started';
74
-
75
- export type CanonWorkDTO = {
76
- uuid: string;
77
- toh: TohokuCatalogEntry;
78
- title: string;
79
- published: boolean;
80
- status: CanonWorkStatus;
81
- pages: number;
82
- };
83
-
84
- export type CanonWork = CanonWorkDTO;
85
-
86
- export type CanonWorksDTO = {
87
- works: CanonWorkDTO[];
88
- };
89
-
90
- export const canonNodeFromDTO = (dto: CanonDTO): CanonNode => {
91
- return {
92
- uuid: dto.uuid,
93
- label: dto.label,
94
- xmlId: dto.xmlId,
95
- parentXmlId: dto.parent_xmlId,
96
- parentUuid: dto.parent_uuid,
97
- sort: dto.sort,
98
- description: dto.description,
99
- type: dto.type,
100
- publicDescription: dto.public_description,
101
- articleUuid: dto.article_uuid,
102
- createdAt: dto.created_at,
103
- };
104
- };
105
-
106
- export const canonTreeFromDTOs = (dtos: CanonDTO[]): CanonHead => {
107
- const nodes: Record<string, CanonNode> = {};
108
- dtos.forEach((dto) => {
109
- nodes[dto.uuid] = canonNodeFromDTO(dto);
110
- });
111
-
112
- const tree: CanonNode[] = [];
113
-
114
- Object.values(nodes).forEach((node) => {
115
- if (node.parentUuid) {
116
- const parent = nodes[node.parentUuid];
117
- if (parent) {
118
- parent.children = parent.children || [];
119
- parent.children.push(node);
120
- }
121
- } else {
122
- tree.push(node);
123
- }
124
- });
125
-
126
- // Sort children by their sort order
127
- const sortChildren = (children: CanonNode[]) => {
128
- children.sort((a, b) => a.sort - b.sort);
129
- children.forEach((child) => {
130
- if (child.children) {
131
- sortChildren(child.children);
132
- }
133
- });
134
- };
135
- sortChildren(tree);
136
-
137
- return {
138
- children: tree,
139
- };
140
- };
141
-
142
- export const canonDetailFromDTO = (dto: CanonDetailDTO): CanonDetail => {
143
- return dto as CanonDetail;
144
- };
145
-
146
- export const caononWorksFromDTO = (dto: CanonWorksDTO): CanonWork[] => {
147
- return dto.works as CanonWork[];
148
- };
@@ -1,84 +0,0 @@
1
- export type ContributorRole =
2
- | 'englishAdvisor'
3
- | 'englishAssociateEditor'
4
- | 'englishCopyEditor'
5
- | 'englishDharmaMaster'
6
- | 'englishFinalReviewer'
7
- | 'englishMarkup'
8
- | 'englishProjectEditor'
9
- | 'englishProjectManager'
10
- | 'englishProofReader'
11
- | 'englishReviser'
12
- | 'englishTranslator'
13
- | 'englishTranslationSponsor'
14
- | 'englishTranslationTeam'
15
- | 'tibetanTranslator';
16
-
17
- export type Contributor = {
18
- name: string;
19
- role: ContributorRole;
20
- };
21
-
22
- export type ProjectContributor = Contributor & {
23
- uuid: string;
24
- contributorUuid: string;
25
- startDate?: Date;
26
- endDate?: Date;
27
- projectUuid: string;
28
- stageUuid?: string;
29
- };
30
-
31
- export type Contributors = Contributor[];
32
- export type ProjectContributors = ProjectContributor[];
33
-
34
- export type ContributorDTO = {
35
- name: string;
36
- role: ContributorRole;
37
- };
38
-
39
- export type ContributorsDTO = ContributorDTO[];
40
-
41
- export type ProjectContributorDTO = {
42
- uuid: string;
43
- projectUuid: string;
44
- stageUuid?: string;
45
- contributorUuid: string;
46
- name: string;
47
- role: ContributorRole;
48
- startDate: string;
49
- endDate: string;
50
- };
51
-
52
- export type ProjectContributorsDTO = ProjectContributorDTO[];
53
-
54
- export const contributorFromDTO = (dto: ContributorDTO): Contributor => {
55
- return {
56
- name: dto.name,
57
- role: dto.role,
58
- };
59
- };
60
-
61
- export const contributorsFromDTO = (dto?: ContributorsDTO): Contributors => {
62
- return dto?.map(contributorFromDTO) || [];
63
- };
64
-
65
- export const projectContributorFromDTO = (
66
- dto: ProjectContributorDTO,
67
- ): ProjectContributor => {
68
- return {
69
- uuid: dto.uuid,
70
- name: dto.name,
71
- contributorUuid: dto.contributorUuid,
72
- role: dto.role,
73
- startDate: new Date(dto.startDate),
74
- endDate: new Date(dto.endDate),
75
- projectUuid: dto.projectUuid,
76
- stageUuid: dto.stageUuid,
77
- };
78
- };
79
-
80
- export const projectContributorsFromDTO = (
81
- dto?: ProjectContributorsDTO,
82
- ): ProjectContributors => {
83
- return dto?.map(projectContributorFromDTO) || [];
84
- };