@seedprotocol/sdk 0.1.47 → 0.1.49
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/bin.js.map +1 -1
- package/dist/constants-BLctWkrn.js.map +1 -1
- package/dist/{index-DWf9Ls94.js → index-DMIKRod-.js} +3995 -3995
- package/dist/index-DMIKRod-.js.map +1 -0
- package/dist/{index-B2WbNudj.js → index-wKss7188.js} +10 -10
- package/dist/index-wKss7188.js.map +1 -0
- package/dist/main.js +7 -7
- package/dist/{seed.schema.config-mBqth17w.js → seed.schema.config-C0M8Rcti.js} +7 -7
- package/dist/seed.schema.config-C0M8Rcti.js.map +1 -0
- package/dist/src/AppStateSchema.ts +10 -0
- package/dist/src/Attestation.ts +21 -0
- package/dist/src/ConfigSchema.ts +15 -0
- package/dist/src/ItemProperty.ts +383 -0
- package/dist/src/MetadataSchema.ts +28 -0
- package/dist/src/ModelSchema.ts +46 -0
- package/dist/src/ModelUidSchema.ts +16 -0
- package/dist/src/PropertyUidSchema.ts +16 -0
- package/dist/src/Schema.ts +17 -0
- package/dist/src/SeedSchema.ts +29 -0
- package/dist/src/VersionSchema.ts +16 -0
- package/dist/src/actors.ts +280 -0
- package/dist/src/addModelsToDb.ts +150 -0
- package/dist/src/allItems.ts +23 -0
- package/dist/src/arweave.ts +37 -0
- package/dist/src/browser.app.db.config.ts +27 -0
- package/dist/src/browser.seed.db.config.ts +33 -0
- package/dist/src/browser.ts +30 -0
- package/dist/src/checkStatus.ts +50 -0
- package/dist/src/client.ts +85 -0
- package/dist/src/configureFs.ts +81 -0
- package/dist/src/connectToDb.ts +74 -0
- package/dist/src/connectionManager.ts +67 -0
- package/dist/src/constants.ts +118 -0
- package/dist/src/create.ts +39 -0
- package/dist/src/createItem.ts +15 -0
- package/dist/src/createItemMachine.ts +37 -0
- package/dist/src/createPublishAttempt.ts +16 -0
- package/dist/src/createSeeds.ts +24 -0
- package/dist/src/createVersion.ts +33 -0
- package/dist/src/db.ts +247 -0
- package/dist/src/dbMachine.ts +181 -0
- package/dist/src/deleteItem.ts +19 -0
- package/dist/src/download.ts +289 -0
- package/dist/src/drizzle.ts +82 -0
- package/dist/src/environment.ts +15 -0
- package/dist/src/eventBus.ts +5 -0
- package/dist/src/events.ts +14 -0
- package/dist/src/fetchDataFromEas.ts +90 -0
- package/dist/src/fetchDbData.ts +16 -0
- package/dist/src/fetchRelatedItems.ts +179 -0
- package/dist/src/fetchSeeds.ts +44 -0
- package/dist/src/fetchVersions.ts +41 -0
- package/dist/src/files.ts +16 -0
- package/dist/src/fragment-masking.ts +87 -0
- package/dist/src/fsProxy.ts +36 -0
- package/dist/src/getItem.ts +189 -0
- package/dist/src/getItemProperties.ts +162 -0
- package/dist/src/getItems.ts +75 -0
- package/dist/src/getMetadata.ts +40 -0
- package/dist/src/getModelSchemas.ts +88 -0
- package/dist/src/getSchemaForModel.ts +42 -0
- package/dist/src/getSeedData.ts +34 -0
- package/dist/src/getVersionData.ts +58 -0
- package/dist/src/getVersionsForVersionUids.ts +39 -0
- package/dist/src/globalMachine.ts +259 -0
- package/dist/src/gql.ts +113 -0
- package/dist/src/graphql.ts +3201 -0
- package/dist/src/helpers.ts +150 -0
- package/dist/src/hydrateExistingItem.ts +137 -0
- package/dist/src/hydrateFromDb.ts +254 -0
- package/dist/src/hydrateNewItem.ts +34 -0
- package/dist/src/index.d.ts +5 -0
- package/dist/src/index.ts +73 -0
- package/dist/src/init.ts +61 -0
- package/dist/src/initialize.ts +127 -0
- package/dist/src/internalMachine.ts +220 -0
- package/dist/src/item.ts +324 -0
- package/dist/src/itemMachineAll.ts +158 -0
- package/dist/src/itemMachineSingle.ts +175 -0
- package/dist/src/loadAppDb.ts +47 -0
- package/dist/src/logger.ts +33 -0
- package/dist/src/machine.ts +55 -0
- package/dist/src/machines.ts +58 -0
- package/dist/src/migrate.ts +288 -0
- package/dist/src/model.ts +71 -0
- package/dist/src/modelClass.ts +19 -0
- package/dist/src/node.app.db.config.ts +40 -0
- package/dist/src/prepareDb.ts +34 -0
- package/dist/src/preparePublishRequestData.ts +81 -0
- package/dist/src/processItems.ts +71 -0
- package/dist/src/property.ts +161 -0
- package/dist/src/propertyMachine.ts +154 -0
- package/dist/src/publish.ts +31 -0
- package/dist/src/publishMachine.ts +77 -0
- package/dist/src/queries.ts +13 -0
- package/dist/src/read.ts +174 -0
- package/dist/src/recoverDeletedItem.ts +14 -0
- package/dist/src/request.ts +54 -0
- package/dist/src/requestAll.ts +157 -0
- package/dist/src/resolveRelatedValue.ts +348 -0
- package/dist/src/resolveRemoteStorage.ts +87 -0
- package/dist/src/save.ts +183 -0
- package/dist/src/saveConfig.ts +79 -0
- package/dist/src/saveDataToDb.ts +145 -0
- package/dist/src/saveMetadata.ts +18 -0
- package/dist/src/saveValueToDb.ts +94 -0
- package/dist/src/seed.schema.config.ts +25 -0
- package/dist/src/seed.ts +37 -0
- package/dist/src/seedData.ts +0 -0
- package/dist/src/seedProtocol.ts +17 -0
- package/dist/src/services.ts +359 -0
- package/dist/src/sqlWasmClient.ts +88 -0
- package/dist/src/syncDbWithEas.ts +686 -0
- package/dist/src/trash.ts +29 -0
- package/dist/src/ts-to-proto.ts +101 -0
- package/dist/src/types.ts +12 -0
- package/dist/src/upload.ts +86 -0
- package/dist/src/validate.ts +42 -0
- package/dist/src/validateInput.ts +33 -0
- package/dist/src/validateItemData.ts +20 -0
- package/dist/src/waitForDb.ts +23 -0
- package/dist/src/wasm.d.ts +8300 -0
- package/dist/src/write.ts +366 -0
- package/dist/types/src/browser/db/read/getModelSchemas.d.ts.map +1 -1
- package/dist/types/src/browser/item/single/actors/hydrateExistingItem.d.ts +3 -1
- package/dist/types/src/browser/item/single/actors/hydrateExistingItem.d.ts.map +1 -1
- package/dist/types/src/browser/item/single/actors/saveDataToDb.d.ts +3 -3
- package/dist/types/src/browser/item/single/itemMachineSingle.d.ts +3 -3
- package/dist/types/src/browser/property/ItemProperty.d.ts +7 -7
- package/dist/types/src/browser/property/ItemProperty.d.ts.map +1 -1
- package/dist/types/src/browser/property/actors/resolveRemoteStorage.d.ts +3 -3
- package/dist/types/src/browser/react/trash.d.ts +1 -1
- package/dist/types/src/browser/react/trash.d.ts.map +1 -1
- package/dist/types/src/browser/services/db/dbMachine.d.ts +6 -6
- package/dist/types/src/browser/services/publish/publishMachine.d.ts +17 -17
- package/dist/types/src/types/machines.d.ts +4 -0
- package/dist/types/src/types/machines.d.ts.map +1 -1
- package/package.json +2 -2
- package/dist/index-B2WbNudj.js.map +0 -1
- package/dist/index-DWf9Ls94.js.map +0 -1
- package/dist/seed.schema.config-mBqth17w.js.map +0 -1
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { EventObject, fromCallback } from 'xstate'
|
|
2
|
+
import { GET_SEEDS } from '@/browser/item/queries'
|
|
3
|
+
import { itemMachineAll } from '@/browser/item/all/itemMachineAll'
|
|
4
|
+
import { Attestation } from '@/browser/gql/graphql'
|
|
5
|
+
import { easClient, queryClient } from '@/browser/helpers'
|
|
6
|
+
import debug from 'debug'
|
|
7
|
+
|
|
8
|
+
const logger = debug('app:allItemsActors:fetchSeeds')
|
|
9
|
+
|
|
10
|
+
export const fetchSeeds = fromCallback<EventObject, typeof itemMachineAll>(
|
|
11
|
+
({ sendBack, input: { context } }) => {
|
|
12
|
+
const { queryVariables, modelName } = context
|
|
13
|
+
|
|
14
|
+
if (!queryVariables) {
|
|
15
|
+
throw new Error('No queryVariables found')
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
let itemSeeds: Attestation[] | undefined
|
|
19
|
+
|
|
20
|
+
const _fetchSeeds = async () => {
|
|
21
|
+
const queryKey = [`getSeeds${modelName}`]
|
|
22
|
+
|
|
23
|
+
const cachedResults = queryClient.getQueryData(queryKey)
|
|
24
|
+
|
|
25
|
+
logger(
|
|
26
|
+
`[allItemsActors] [fetchSeeds] cachedResults ${Date.now()}`,
|
|
27
|
+
cachedResults,
|
|
28
|
+
)
|
|
29
|
+
|
|
30
|
+
const results = await queryClient.fetchQuery({
|
|
31
|
+
queryKey,
|
|
32
|
+
queryFn: async () => easClient.request(GET_SEEDS, queryVariables),
|
|
33
|
+
})
|
|
34
|
+
|
|
35
|
+
itemSeeds = results.itemSeeds
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
_fetchSeeds().then(() => {
|
|
39
|
+
sendBack({ type: 'fetchSeedsSuccess', itemSeeds })
|
|
40
|
+
})
|
|
41
|
+
|
|
42
|
+
return () => {}
|
|
43
|
+
},
|
|
44
|
+
)
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { EventObject, fromCallback } from 'xstate'
|
|
2
|
+
import { GET_VERSIONS } from '@/browser/item/queries'
|
|
3
|
+
import { itemMachineAll } from '@/browser/item/all/itemMachineAll'
|
|
4
|
+
import { Attestation } from '@/browser/gql/graphql'
|
|
5
|
+
import { easClient, queryClient } from '@/browser/helpers'
|
|
6
|
+
|
|
7
|
+
export const fetchVersions = fromCallback<EventObject, typeof itemMachineAll>(
|
|
8
|
+
({ sendBack, input: { context } }) => {
|
|
9
|
+
const { itemSeeds, modelName } = context
|
|
10
|
+
|
|
11
|
+
if (!itemSeeds) {
|
|
12
|
+
throw new Error('No queryVariables found')
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
let itemVersions: Attestation[] | undefined
|
|
16
|
+
|
|
17
|
+
const _fetchVersions = async () => {
|
|
18
|
+
const seedIds = itemSeeds.map((seed) => seed.id)
|
|
19
|
+
|
|
20
|
+
const results = await queryClient.fetchQuery({
|
|
21
|
+
queryKey: [`getVersions${modelName}`],
|
|
22
|
+
queryFn: async () =>
|
|
23
|
+
easClient.request(GET_VERSIONS, {
|
|
24
|
+
where: {
|
|
25
|
+
refUID: {
|
|
26
|
+
in: seedIds,
|
|
27
|
+
},
|
|
28
|
+
},
|
|
29
|
+
}),
|
|
30
|
+
})
|
|
31
|
+
|
|
32
|
+
itemVersions = results.itemVersions
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
_fetchVersions().then(() => {
|
|
36
|
+
sendBack({ type: 'fetchVersionsSuccess', itemVersions })
|
|
37
|
+
})
|
|
38
|
+
|
|
39
|
+
return () => {}
|
|
40
|
+
},
|
|
41
|
+
)
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export const listFilesInOPFSRoot = async () => {
|
|
2
|
+
// Get the root directory handle
|
|
3
|
+
const rootDirHandle = await navigator.storage.getDirectory()
|
|
4
|
+
|
|
5
|
+
// Initialize an array to hold the file names
|
|
6
|
+
let fileNames = []
|
|
7
|
+
|
|
8
|
+
// Create an async iterator to loop through directory entries
|
|
9
|
+
for await (const entry of rootDirHandle.values()) {
|
|
10
|
+
if (entry.kind === 'file') {
|
|
11
|
+
fileNames.push(entry.name)
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
return fileNames
|
|
16
|
+
}
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
/* eslint-disable */
|
|
2
|
+
import { ResultOf, DocumentTypeDecoration, TypedDocumentNode } from '@graphql-typed-document-node/core';
|
|
3
|
+
import { FragmentDefinitionNode } from 'graphql';
|
|
4
|
+
import { Incremental } from './graphql';
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
export type FragmentType<TDocumentType extends DocumentTypeDecoration<any, any>> = TDocumentType extends DocumentTypeDecoration<
|
|
8
|
+
infer TType,
|
|
9
|
+
any
|
|
10
|
+
>
|
|
11
|
+
? [TType] extends [{ ' $fragmentName'?: infer TKey }]
|
|
12
|
+
? TKey extends string
|
|
13
|
+
? { ' $fragmentRefs'?: { [key in TKey]: TType } }
|
|
14
|
+
: never
|
|
15
|
+
: never
|
|
16
|
+
: never;
|
|
17
|
+
|
|
18
|
+
// return non-nullable if `fragmentType` is non-nullable
|
|
19
|
+
export function useFragment<TType>(
|
|
20
|
+
_documentNode: DocumentTypeDecoration<TType, any>,
|
|
21
|
+
fragmentType: FragmentType<DocumentTypeDecoration<TType, any>>
|
|
22
|
+
): TType;
|
|
23
|
+
// return nullable if `fragmentType` is undefined
|
|
24
|
+
export function useFragment<TType>(
|
|
25
|
+
_documentNode: DocumentTypeDecoration<TType, any>,
|
|
26
|
+
fragmentType: FragmentType<DocumentTypeDecoration<TType, any>> | undefined
|
|
27
|
+
): TType | undefined;
|
|
28
|
+
// return nullable if `fragmentType` is nullable
|
|
29
|
+
export function useFragment<TType>(
|
|
30
|
+
_documentNode: DocumentTypeDecoration<TType, any>,
|
|
31
|
+
fragmentType: FragmentType<DocumentTypeDecoration<TType, any>> | null
|
|
32
|
+
): TType | null;
|
|
33
|
+
// return nullable if `fragmentType` is nullable or undefined
|
|
34
|
+
export function useFragment<TType>(
|
|
35
|
+
_documentNode: DocumentTypeDecoration<TType, any>,
|
|
36
|
+
fragmentType: FragmentType<DocumentTypeDecoration<TType, any>> | null | undefined
|
|
37
|
+
): TType | null | undefined;
|
|
38
|
+
// return array of non-nullable if `fragmentType` is array of non-nullable
|
|
39
|
+
export function useFragment<TType>(
|
|
40
|
+
_documentNode: DocumentTypeDecoration<TType, any>,
|
|
41
|
+
fragmentType: Array<FragmentType<DocumentTypeDecoration<TType, any>>>
|
|
42
|
+
): Array<TType>;
|
|
43
|
+
// return array of nullable if `fragmentType` is array of nullable
|
|
44
|
+
export function useFragment<TType>(
|
|
45
|
+
_documentNode: DocumentTypeDecoration<TType, any>,
|
|
46
|
+
fragmentType: Array<FragmentType<DocumentTypeDecoration<TType, any>>> | null | undefined
|
|
47
|
+
): Array<TType> | null | undefined;
|
|
48
|
+
// return readonly array of non-nullable if `fragmentType` is array of non-nullable
|
|
49
|
+
export function useFragment<TType>(
|
|
50
|
+
_documentNode: DocumentTypeDecoration<TType, any>,
|
|
51
|
+
fragmentType: ReadonlyArray<FragmentType<DocumentTypeDecoration<TType, any>>>
|
|
52
|
+
): ReadonlyArray<TType>;
|
|
53
|
+
// return readonly array of nullable if `fragmentType` is array of nullable
|
|
54
|
+
export function useFragment<TType>(
|
|
55
|
+
_documentNode: DocumentTypeDecoration<TType, any>,
|
|
56
|
+
fragmentType: ReadonlyArray<FragmentType<DocumentTypeDecoration<TType, any>>> | null | undefined
|
|
57
|
+
): ReadonlyArray<TType> | null | undefined;
|
|
58
|
+
export function useFragment<TType>(
|
|
59
|
+
_documentNode: DocumentTypeDecoration<TType, any>,
|
|
60
|
+
fragmentType: FragmentType<DocumentTypeDecoration<TType, any>> | Array<FragmentType<DocumentTypeDecoration<TType, any>>> | ReadonlyArray<FragmentType<DocumentTypeDecoration<TType, any>>> | null | undefined
|
|
61
|
+
): TType | Array<TType> | ReadonlyArray<TType> | null | undefined {
|
|
62
|
+
return fragmentType as any;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
export function makeFragmentData<
|
|
67
|
+
F extends DocumentTypeDecoration<any, any>,
|
|
68
|
+
FT extends ResultOf<F>
|
|
69
|
+
>(data: FT, _fragment: F): FragmentType<F> {
|
|
70
|
+
return data as FragmentType<F>;
|
|
71
|
+
}
|
|
72
|
+
export function isFragmentReady<TQuery, TFrag>(
|
|
73
|
+
queryNode: DocumentTypeDecoration<TQuery, any>,
|
|
74
|
+
fragmentNode: TypedDocumentNode<TFrag>,
|
|
75
|
+
data: FragmentType<TypedDocumentNode<Incremental<TFrag>, any>> | null | undefined
|
|
76
|
+
): data is FragmentType<typeof fragmentNode> {
|
|
77
|
+
const deferredFields = (queryNode as { __meta__?: { deferredFields: Record<string, (keyof TFrag)[]> } }).__meta__
|
|
78
|
+
?.deferredFields;
|
|
79
|
+
|
|
80
|
+
if (!deferredFields) return true;
|
|
81
|
+
|
|
82
|
+
const fragDef = fragmentNode.definitions[0] as FragmentDefinitionNode | undefined;
|
|
83
|
+
const fragName = fragDef?.name?.value;
|
|
84
|
+
|
|
85
|
+
const fields = (fragName && deferredFields[fragName]) || [];
|
|
86
|
+
return fields.length > 0 && fields.every(field => data && field in data);
|
|
87
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
// File: fsProxy.js
|
|
2
|
+
let actualFs = {} // Empty placeholder for the real implementation
|
|
3
|
+
|
|
4
|
+
let resolveFsReady
|
|
5
|
+
const fsReady = new Promise((resolve) => {
|
|
6
|
+
resolveFsReady = resolve
|
|
7
|
+
})
|
|
8
|
+
|
|
9
|
+
// Create a proxy object that intercepts all accesses to `fs`
|
|
10
|
+
const fsProxy = new Proxy(actualFs, {
|
|
11
|
+
get(target, prop) {
|
|
12
|
+
if (prop.endsWith('Sync')) {
|
|
13
|
+
return (...args) => {
|
|
14
|
+
actualFs[prop](...args)
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
return async (...args) => {
|
|
19
|
+
await fsReady
|
|
20
|
+
if (typeof actualFs[prop] === 'function') {
|
|
21
|
+
return actualFs[prop](...args)
|
|
22
|
+
} else {
|
|
23
|
+
return actualFs[prop]
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
},
|
|
27
|
+
})
|
|
28
|
+
|
|
29
|
+
const setFsImplementation = (configuredFs) => {
|
|
30
|
+
Object.assign(actualFs, configuredFs)
|
|
31
|
+
resolveFsReady() // Resolve the promise to signal fs is ready
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export { setFsImplementation }
|
|
35
|
+
|
|
36
|
+
export default fsProxy
|
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
import { Item } from '@/browser/item'
|
|
2
|
+
|
|
3
|
+
import { getItemProperties } from '@/browser/db/read/getItemProperties'
|
|
4
|
+
import { getSeedData } from '@/browser/db/read/getSeedData'
|
|
5
|
+
import debug from 'debug'
|
|
6
|
+
import { ItemData } from '@/types'
|
|
7
|
+
import { getAppDb } from '../sqlWasmClient'
|
|
8
|
+
import { seeds, versions } from '@/shared/seedSchema'
|
|
9
|
+
import {
|
|
10
|
+
and,
|
|
11
|
+
count,
|
|
12
|
+
eq,
|
|
13
|
+
getTableColumns,
|
|
14
|
+
isNull,
|
|
15
|
+
max,
|
|
16
|
+
or,
|
|
17
|
+
SQL,
|
|
18
|
+
} from 'drizzle-orm'
|
|
19
|
+
|
|
20
|
+
const logger = debug('app:db:queries:getItem')
|
|
21
|
+
|
|
22
|
+
type GetItemDataFromDbParams = {
|
|
23
|
+
modelName?: string
|
|
24
|
+
seedLocalId?: string
|
|
25
|
+
seedUid?: string
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
type GetItemDataFromDb = (
|
|
29
|
+
params: GetItemDataFromDbParams,
|
|
30
|
+
) => Promise<ItemData | undefined>
|
|
31
|
+
|
|
32
|
+
export const getItemDataFromDb: GetItemDataFromDb = async ({
|
|
33
|
+
modelName,
|
|
34
|
+
seedLocalId,
|
|
35
|
+
seedUid,
|
|
36
|
+
}) => {
|
|
37
|
+
if (!seedLocalId && !seedUid) {
|
|
38
|
+
throw new Error('[db/queries] [getItem] no seedLocalId or seedUid')
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
if (seedUid && !seedLocalId) {
|
|
42
|
+
const seedData = await getSeedData({ seedUid })
|
|
43
|
+
if (!seedData) {
|
|
44
|
+
logger('[db/queries] [getItem] no seedData seedUid', seedUid)
|
|
45
|
+
return
|
|
46
|
+
}
|
|
47
|
+
seedLocalId = seedData.localId
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
const appDb = getAppDb()
|
|
51
|
+
|
|
52
|
+
const { localId, uid, ...rest } = getTableColumns(seeds)
|
|
53
|
+
|
|
54
|
+
const whereClauses: SQL[] = []
|
|
55
|
+
|
|
56
|
+
if (modelName) {
|
|
57
|
+
whereClauses.push(eq(seeds.type, modelName.toLowerCase()))
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
const uidWhereClause: SQL = seedUid
|
|
61
|
+
? eq(seeds.uid, seedUid)
|
|
62
|
+
: isNull(seeds.uid)
|
|
63
|
+
const localWhereClause: SQL = seedLocalId
|
|
64
|
+
? eq(seeds.localId, seedLocalId)
|
|
65
|
+
: isNull(seeds.localId)
|
|
66
|
+
|
|
67
|
+
whereClauses.push(or(localWhereClause, uidWhereClause) as SQL)
|
|
68
|
+
|
|
69
|
+
const latestVersions = appDb.$with('latestVersions').as(
|
|
70
|
+
appDb
|
|
71
|
+
.select({
|
|
72
|
+
localId: versions.localId,
|
|
73
|
+
uid: versions.uid,
|
|
74
|
+
seedLocalId: versions.seedLocalId,
|
|
75
|
+
seedUid: versions.seedUid,
|
|
76
|
+
})
|
|
77
|
+
.from(versions)
|
|
78
|
+
.groupBy(versions.seedLocalId),
|
|
79
|
+
)
|
|
80
|
+
|
|
81
|
+
const itemDataRows = await appDb
|
|
82
|
+
.with(latestVersions)
|
|
83
|
+
.select({
|
|
84
|
+
...rest,
|
|
85
|
+
seedLocalId: seeds.localId,
|
|
86
|
+
seedUid: seeds.uid,
|
|
87
|
+
versionsCount: count(versions.localId),
|
|
88
|
+
lastVersionPublishedAt: max(versions.attestationCreatedAt),
|
|
89
|
+
latestVersionUid: latestVersions.uid,
|
|
90
|
+
})
|
|
91
|
+
.from(seeds)
|
|
92
|
+
.leftJoin(versions, eq(versions.seedLocalId, seeds.localId))
|
|
93
|
+
.leftJoin(
|
|
94
|
+
latestVersions,
|
|
95
|
+
eq(versions.seedLocalId, latestVersions.seedLocalId),
|
|
96
|
+
)
|
|
97
|
+
.where(and(...whereClauses))
|
|
98
|
+
.groupBy(seeds.localId)
|
|
99
|
+
|
|
100
|
+
if (!itemDataRows || itemDataRows.length === 0) {
|
|
101
|
+
logger('[db/queries] [getItemDataFromDb] no itemDataRows')
|
|
102
|
+
return
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
let itemData = itemDataRows[0]
|
|
106
|
+
|
|
107
|
+
const propertiesData = await getItemProperties({ seedLocalId, seedUid })
|
|
108
|
+
|
|
109
|
+
// const initObj: ItemData = {
|
|
110
|
+
// seedLocalId,
|
|
111
|
+
// seedUid,
|
|
112
|
+
// modelName,
|
|
113
|
+
// }
|
|
114
|
+
|
|
115
|
+
if (!propertiesData || propertiesData.length === 0) {
|
|
116
|
+
return itemData
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
const firstPropertyData = propertiesData[0]
|
|
120
|
+
|
|
121
|
+
for (const propertyData of propertiesData) {
|
|
122
|
+
const propertyName = propertyData.propertyName
|
|
123
|
+
|
|
124
|
+
const propertyValue = propertyData.propertyValue
|
|
125
|
+
|
|
126
|
+
// TODO: Find a better place for the property data below
|
|
127
|
+
// Since initObj is used to initialize an Item, the following values
|
|
128
|
+
// just overwrite each other for each property since they are Property
|
|
129
|
+
// specific.
|
|
130
|
+
|
|
131
|
+
// const refSeedType = propertyDbValues[11]
|
|
132
|
+
// if (refSeedType) {
|
|
133
|
+
// initObj.refSeedType = refSeedType
|
|
134
|
+
// }
|
|
135
|
+
// const refValueType = propertyDbValues[12]
|
|
136
|
+
// if (refValueType) {
|
|
137
|
+
// initObj.refValueType = refValueType
|
|
138
|
+
// }
|
|
139
|
+
//
|
|
140
|
+
// if (
|
|
141
|
+
// refSeedType &&
|
|
142
|
+
// refValueType === 'list' &&
|
|
143
|
+
// propertyName.endsWith('Ids')
|
|
144
|
+
// ) {
|
|
145
|
+
// logger('[db/queries] [getItemDataFromDb] propertyName', propertyName)
|
|
146
|
+
// }
|
|
147
|
+
|
|
148
|
+
itemData[propertyName] = propertyValue
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
return itemData
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
type GetItemParams = {
|
|
155
|
+
modelName?: string
|
|
156
|
+
seedLocalId?: string
|
|
157
|
+
seedUid?: string
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
type GetItem = (params: GetItemParams) => Promise<Item<any> | undefined>
|
|
161
|
+
|
|
162
|
+
export const getItem: GetItem = async ({ modelName, seedLocalId, seedUid }) => {
|
|
163
|
+
const itemInitObj = await getItemDataFromDb({
|
|
164
|
+
modelName,
|
|
165
|
+
seedLocalId,
|
|
166
|
+
seedUid,
|
|
167
|
+
})
|
|
168
|
+
|
|
169
|
+
if (!itemInitObj) {
|
|
170
|
+
console.error(
|
|
171
|
+
`[db/queries] [getItem] no itemInitObj modelName: ${modelName} seedLocalId: ${seedLocalId} seedUid: ${seedUid}`,
|
|
172
|
+
)
|
|
173
|
+
return
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
if (!itemInitObj.seedLocalId) {
|
|
177
|
+
console.error(
|
|
178
|
+
`[db/queries] [getItem] no itemInitObj.seedLocalId modelName: ${modelName} seedLocalId: ${seedLocalId} seedUid: ${seedUid}`,
|
|
179
|
+
)
|
|
180
|
+
return
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
console.log(
|
|
184
|
+
`[${modelName}.${seedLocalId}] [getItem] Creating Item`,
|
|
185
|
+
itemInitObj,
|
|
186
|
+
)
|
|
187
|
+
|
|
188
|
+
return Item.create(itemInitObj)
|
|
189
|
+
}
|
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
import { PropertyData } from '@/types'
|
|
2
|
+
import { getAppDb } from '@/browser/db/sqlWasmClient'
|
|
3
|
+
import { metadata, seeds, versions } from '@/shared/seedSchema'
|
|
4
|
+
import {
|
|
5
|
+
and,
|
|
6
|
+
eq,
|
|
7
|
+
getTableColumns,
|
|
8
|
+
isNotNull,
|
|
9
|
+
isNull,
|
|
10
|
+
or,
|
|
11
|
+
SQL,
|
|
12
|
+
} from 'drizzle-orm'
|
|
13
|
+
|
|
14
|
+
type GetPropertiesForSeedProps = {
|
|
15
|
+
seedLocalId?: string
|
|
16
|
+
seedUid?: string
|
|
17
|
+
edited?: boolean
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
type GetItemProperties = (
|
|
21
|
+
props: GetPropertiesForSeedProps,
|
|
22
|
+
) => Promise<PropertyData[]>
|
|
23
|
+
|
|
24
|
+
export const getItemProperties: GetItemProperties = async ({
|
|
25
|
+
seedLocalId,
|
|
26
|
+
seedUid,
|
|
27
|
+
edited,
|
|
28
|
+
}) => {
|
|
29
|
+
const appDb = getAppDb()
|
|
30
|
+
|
|
31
|
+
const whereClauses: SQL[] = [isNotNull(metadata.propertyName)]
|
|
32
|
+
|
|
33
|
+
const uidWhereClause: SQL = seedUid
|
|
34
|
+
? eq(seeds.uid, seedUid)
|
|
35
|
+
: isNull(seeds.uid)
|
|
36
|
+
const localWhereClause: SQL = seedLocalId
|
|
37
|
+
? eq(seeds.localId, seedLocalId)
|
|
38
|
+
: isNull(seeds.localId)
|
|
39
|
+
|
|
40
|
+
whereClauses.push(or(localWhereClause, uidWhereClause) as SQL)
|
|
41
|
+
whereClauses.push(isNotNull(metadata.propertyName))
|
|
42
|
+
whereClauses.push(isNotNull(metadata.easDataType))
|
|
43
|
+
|
|
44
|
+
if (typeof edited !== 'undefined') {
|
|
45
|
+
if (edited) {
|
|
46
|
+
whereClauses.push(isNull(metadata.uid))
|
|
47
|
+
}
|
|
48
|
+
if (!edited) {
|
|
49
|
+
whereClauses.push(isNotNull(metadata.uid))
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
// if (!seedLocalId || !seedUid) {
|
|
54
|
+
// const seedRows = await appDb
|
|
55
|
+
// .select({
|
|
56
|
+
// localId: seeds.localId,
|
|
57
|
+
// uid: seeds.uid,
|
|
58
|
+
// })
|
|
59
|
+
// .from(seeds)
|
|
60
|
+
// .where(or(localWhereClause, uidWhereClause))
|
|
61
|
+
//
|
|
62
|
+
// if (seedRows && seedRows.length > 0) {
|
|
63
|
+
// seedUid = seedRows[0].uid as string
|
|
64
|
+
// seedLocalId = seedRows[0].localId as string
|
|
65
|
+
// }
|
|
66
|
+
// }
|
|
67
|
+
|
|
68
|
+
const metadataColumns = getTableColumns(metadata)
|
|
69
|
+
|
|
70
|
+
const propertiesData = await appDb
|
|
71
|
+
.select({
|
|
72
|
+
...metadataColumns,
|
|
73
|
+
})
|
|
74
|
+
.from(seeds)
|
|
75
|
+
.leftJoin(metadata, eq(metadata.seedLocalId, seeds.localId))
|
|
76
|
+
.leftJoin(versions, eq(versions.localId, seeds.localId))
|
|
77
|
+
.where(and(...whereClauses))
|
|
78
|
+
.groupBy(metadata.propertyName)
|
|
79
|
+
|
|
80
|
+
return propertiesData
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
// const localIdWhere = seedLocalId
|
|
84
|
+
// ? `s.local_id = '${seedLocalId}'`
|
|
85
|
+
// : 's.local_id IS NULL'
|
|
86
|
+
// const uidWhere = seedUid ? `s.uid = '${seedUid}'` : 's.uid IS NULL'
|
|
87
|
+
//
|
|
88
|
+
// const queryStatement = `WITH LatestMetadata as (SELECT m.property_name,
|
|
89
|
+
// m.property_value,
|
|
90
|
+
// m.version_local_id,
|
|
91
|
+
// MAX(m.attestation_created_at),
|
|
92
|
+
// m.uid,
|
|
93
|
+
// m.seed_local_id,
|
|
94
|
+
// seed_uid
|
|
95
|
+
// FROM metadata m
|
|
96
|
+
// JOIN seeds s ON s.local_id = m.seed_local_id
|
|
97
|
+
// GROUP BY m.property_name),
|
|
98
|
+
//
|
|
99
|
+
// LatestVersion as (SELECT v.local_id,
|
|
100
|
+
// MAX(v.attestation_created_at) as attestation_created_at,
|
|
101
|
+
// v.uid,
|
|
102
|
+
// v.seed_local_id,
|
|
103
|
+
// v.seed_uid
|
|
104
|
+
// FROM versions v
|
|
105
|
+
// JOIN seeds s ON s.local_id = v.seed_local_id
|
|
106
|
+
// GROUP BY s.local_id)
|
|
107
|
+
//
|
|
108
|
+
//
|
|
109
|
+
// SELECT s.local_id,
|
|
110
|
+
// s.uid,
|
|
111
|
+
// s.schema_uid,
|
|
112
|
+
// m.property_name,
|
|
113
|
+
// m.property_value,
|
|
114
|
+
// COUNT(v.local_id) as versions_count,
|
|
115
|
+
// m.model_type,
|
|
116
|
+
// lv.attestation_created_at,
|
|
117
|
+
// m.local_id,
|
|
118
|
+
// m.uid,
|
|
119
|
+
// MAX(m.attestation_created_at),
|
|
120
|
+
// m.ref_seed_type,
|
|
121
|
+
// m.ref_value_type,
|
|
122
|
+
// m.seed_local_id,
|
|
123
|
+
// m.seed_uid,
|
|
124
|
+
// m.created_at,
|
|
125
|
+
// m.updated_at,
|
|
126
|
+
// m.version_uid
|
|
127
|
+
// FROM seeds s
|
|
128
|
+
// JOIN LatestMetadata lm ON s.local_id = m.seed_local_id
|
|
129
|
+
// JOIN LatestVersion lv ON lv.seed_local_id = m.seed_local_id
|
|
130
|
+
// JOIN metadata m ON m.property_name = lm.property_name OR lm.property_value = s.uid
|
|
131
|
+
// JOIN versions v ON s.local_id = v.seed_local_id
|
|
132
|
+
// WHERE ${localIdWhere}
|
|
133
|
+
// OR ${uidWhere}
|
|
134
|
+
// GROUP BY m.property_name;
|
|
135
|
+
// `
|
|
136
|
+
//
|
|
137
|
+
// const { rows } = await runQueryForStatement(queryStatement)
|
|
138
|
+
//
|
|
139
|
+
// const propertiesDataOld: PropertyData[] = []
|
|
140
|
+
//
|
|
141
|
+
// for (const row of rows) {
|
|
142
|
+
// propertiesDataOld.push({
|
|
143
|
+
// localId: row[0],
|
|
144
|
+
// uid: row[1],
|
|
145
|
+
// schemaUid: row[2],
|
|
146
|
+
// propertyName: row[3],
|
|
147
|
+
// propertyValue: row[4],
|
|
148
|
+
// versionsCount: row[5],
|
|
149
|
+
// itemModelName: row[6],
|
|
150
|
+
// attestationCreatedAt: row[7],
|
|
151
|
+
// metadataLocalId: row[8],
|
|
152
|
+
// metadataUid: row[9],
|
|
153
|
+
// metadataAttestationCreatedAt: row[10],
|
|
154
|
+
// refSeedType: row[11],
|
|
155
|
+
// refValueType: row[12],
|
|
156
|
+
// seedLocalId: row[13],
|
|
157
|
+
// seedUid: row[14],
|
|
158
|
+
// createdAt: row[15],
|
|
159
|
+
// updatedAt: row[16],
|
|
160
|
+
// versionUid: row[17],
|
|
161
|
+
// })
|
|
162
|
+
// }
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { ItemData } from '@/types'
|
|
2
|
+
import {
|
|
3
|
+
and,
|
|
4
|
+
count,
|
|
5
|
+
desc,
|
|
6
|
+
eq,
|
|
7
|
+
isNotNull,
|
|
8
|
+
isNull,
|
|
9
|
+
max,
|
|
10
|
+
or,
|
|
11
|
+
SQL,
|
|
12
|
+
sql,
|
|
13
|
+
} from 'drizzle-orm'
|
|
14
|
+
import { seeds, versions } from '@/shared/seedSchema'
|
|
15
|
+
import { getAppDb } from '@/browser/db/sqlWasmClient'
|
|
16
|
+
|
|
17
|
+
type GetItemsDataProps = {
|
|
18
|
+
modelName?: string
|
|
19
|
+
deleted?: boolean
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
type GetItemsData = (props: GetItemsDataProps) => Promise<ItemData[]>
|
|
23
|
+
|
|
24
|
+
export const getItemsData: GetItemsData = async ({
|
|
25
|
+
modelName,
|
|
26
|
+
deleted,
|
|
27
|
+
}): Promise<ItemData[]> => {
|
|
28
|
+
const appDb = getAppDb()
|
|
29
|
+
|
|
30
|
+
const conditions: SQL[] = []
|
|
31
|
+
|
|
32
|
+
if (modelName) {
|
|
33
|
+
conditions.push(eq(seeds.type, modelName.toLowerCase()))
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
if (deleted) {
|
|
37
|
+
conditions.push(
|
|
38
|
+
or(
|
|
39
|
+
isNotNull(seeds._markedForDeletion),
|
|
40
|
+
eq(seeds._markedForDeletion, 1),
|
|
41
|
+
) as SQL,
|
|
42
|
+
)
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
if (!deleted) {
|
|
46
|
+
conditions.push(
|
|
47
|
+
or(
|
|
48
|
+
isNull(seeds._markedForDeletion),
|
|
49
|
+
eq(seeds._markedForDeletion, 0),
|
|
50
|
+
) as SQL,
|
|
51
|
+
)
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
let query = appDb
|
|
55
|
+
.select({
|
|
56
|
+
seedLocalId: seeds.localId,
|
|
57
|
+
seedUid: seeds.uid,
|
|
58
|
+
schemaUid: seeds.schemaUid,
|
|
59
|
+
modelName: sql<string>`${modelName}`,
|
|
60
|
+
attestationCreatedAt: seeds.attestationCreatedAt,
|
|
61
|
+
versionsCount: count(versions.localId),
|
|
62
|
+
lastVersionPublishedAt: max(versions.attestationCreatedAt),
|
|
63
|
+
lastLocalUpdateAt: max(versions.createdAt),
|
|
64
|
+
createdAt: seeds.createdAt,
|
|
65
|
+
})
|
|
66
|
+
.from(seeds)
|
|
67
|
+
.leftJoin(versions, eq(seeds.localId, versions.seedLocalId))
|
|
68
|
+
.where(and(...conditions))
|
|
69
|
+
.orderBy(desc(seeds.createdAt))
|
|
70
|
+
.groupBy(seeds.localId)
|
|
71
|
+
|
|
72
|
+
const itemsData = (await query) as ItemData[]
|
|
73
|
+
|
|
74
|
+
return itemsData
|
|
75
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { getAppDb } from '@/browser/db'
|
|
2
|
+
import { and, eq, SQL } from 'drizzle-orm'
|
|
3
|
+
import { metadata, MetadataType } from '@/shared/seedSchema'
|
|
4
|
+
|
|
5
|
+
type GetMetadataProps =
|
|
6
|
+
| {
|
|
7
|
+
storageTransactionId?: string
|
|
8
|
+
}
|
|
9
|
+
| undefined
|
|
10
|
+
|
|
11
|
+
type GetMetadata = (
|
|
12
|
+
props: GetMetadataProps,
|
|
13
|
+
) => Promise<MetadataType | undefined>
|
|
14
|
+
|
|
15
|
+
export const getMetadata: GetMetadata = async (props) => {
|
|
16
|
+
const appDb = getAppDb()
|
|
17
|
+
|
|
18
|
+
let storageTransactionId: string | undefined
|
|
19
|
+
if (props) {
|
|
20
|
+
storageTransactionId = props.storageTransactionId
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
const whereClauses: SQL[] = []
|
|
24
|
+
|
|
25
|
+
if (storageTransactionId) {
|
|
26
|
+
whereClauses.push(eq(metadata.propertyName, 'storageTransactionId'))
|
|
27
|
+
whereClauses.push(eq(metadata.propertyValue, storageTransactionId))
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const metadataRows = await appDb
|
|
31
|
+
.select()
|
|
32
|
+
.from(metadata)
|
|
33
|
+
.where(and(...whereClauses))
|
|
34
|
+
|
|
35
|
+
if (!metadataRows || metadataRows.length === 0) {
|
|
36
|
+
return
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
return metadataRows[0]
|
|
40
|
+
}
|