Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug: Change metadata consistency #394

Merged
merged 2 commits into from
Dec 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions packages/api/src/graphql/metadata.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { gql } from "@apollo/client";
import {
TMetadataCollection,
TMetadataCollectionRequest,
TMetadataDocument,
TMetadataDocumentRequest,
TCollectionMetadata,
TCollectionMetadataRequest,
TDocumentMetadata,
TDocumentMetadataRequest,
} from "@zkdb/common";
import { createQueryFunction, TApolloClient } from "./common";

Expand All @@ -14,13 +14,13 @@ const METADATA_COLLECTION = gql``;

export const metadata = <T>(client: TApolloClient<T>) => ({
document: createQueryFunction<
TMetadataDocument,
TMetadataDocumentRequest,
{ getMetadataDocument: TMetadataDocument }
TDocumentMetadata,
TDocumentMetadataRequest,
{ documentMetadata: TDocumentMetadata }
>(client, METADATA_DOCUMENT, (data) => data),
collection: createQueryFunction<
TMetadataCollection,
TMetadataCollectionRequest,
{ getMetadataCollection: TMetadataCollection }
TCollectionMetadata,
TCollectionMetadataRequest,
{ collectionMetadata: TCollectionMetadata }
>(client, METADATA_COLLECTION, (data) => data),
});
4 changes: 2 additions & 2 deletions packages/common/src/types/collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { OwnershipAndPermission } from '@zkdb/permission';
import { IndexDirection } from 'mongodb';
import { TPickAlter } from './common.js';
import { TDatabaseRequest } from './database.js';
import { TMetadataCollection } from './metadata.js';
import { TCollectionMetadata } from './metadata.js';
import { TSchemaFieldDefinition } from './schema.js';

/**
Expand Down Expand Up @@ -82,7 +82,7 @@ export type TCollectionCreateResponse = boolean;
// Collection list
export type TCollectionListRequest = TDatabaseRequest;

export type TCollectionListResponse = TMetadataCollection[];
export type TCollectionListResponse = TCollectionMetadata[];

// Collection exist
export type TCollectionExistRequest = TCollectionRequest;
Expand Down
7 changes: 0 additions & 7 deletions packages/common/src/types/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,4 @@ export type TDatabaseFindByIndexRequest = TDatabaseRequest & {
index: number;
};

// Database change owner
export type TDatabaseChangeOwnerRequest = TDatabaseRequest & {
newOwner: string;
};

export type TDatabaseChangeOwnerResponse = boolean;

export type TDatabaseExistResponse = boolean;
4 changes: 2 additions & 2 deletions packages/common/src/types/document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { ObjectId } from 'mongodb';
import { TContractSchemaField } from '../schema.js';
import { TDbRecord, TNullable } from './common.js';
import { TMerkleProof } from './merkle-tree.js';
import { TMetadataDocument } from './metadata.js';
import { TDocumentMetadata } from './metadata.js';
import { TPagination, TPaginationReturn } from './pagination.js';
import { EProofStatusDocument } from './proof.js';

Expand Down Expand Up @@ -42,7 +42,7 @@ export type TDocumentFindRequest = TDocumentNamespace & {
export type TDocumentFindResponse = TPaginationReturn<
Array<
TDocumentResponse & {
metadata?: TMetadataDocument;
metadata?: TDocumentMetadata;
proofStatus?: EProofStatusDocument;
}
>
Expand Down
24 changes: 14 additions & 10 deletions packages/common/src/types/metadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,26 @@ import { TDocument } from './document.js';
export type TMetadataBasic = OwnershipAndPermission;

// Document metadata
export type TMetadataDocument = TMetadataBasic & {
export type TDocumentMetadata = TMetadataBasic & {
collectionName: string;
docId: string;
merkleIndex: string;
};

export type TMetadataDocumentRecord = TDbRecord<TMetadataDocument>;
export type TDocumentMetadataRecord = TDbRecord<TDocumentMetadata>;

// Metadata from mongodb
export type TMetadataCollectionMongo = {
export type TCollectionMetadataMongo = {
sizeOnDisk: number;
};

// Collection metadata
export type TMetadataCollection = TMetadataDetail<
TCollection & Partial<TMetadataCollectionMongo>,
export type TCollectionMetadata = TMetadataDetail<
TCollection & Partial<TCollectionMetadataMongo>,
TMetadataBasic
>;

export type TMetadataCollectionRecord = TDbRecord<TMetadataCollection>;
export type TCollectionMetadataRecord = TDbRecord<TCollectionMetadata>;

/**
* Not sure what this is for?
Expand All @@ -37,13 +37,17 @@ export type TMetadataCollectionRecord = TDbRecord<TMetadataCollection>;
*/
export type TMetadataDetail<T, M> = T & { metadata: M };

export type TMetadataDetailDocument<T> = TMetadataDetail<T, TMetadataDocument>;
export type TMetadataDetailDocument<T> = TMetadataDetail<T, TDocumentMetadata>;

export type TMetadataDocumentRequest = TDatabaseRequest &
export type TDocumentMetadataRequest = TDatabaseRequest &
Pick<TCollection, 'collectionName'> &
Pick<TDocument, 'docId'>;

export type TMetadataCollectionRequest = Omit<
TMetadataDocumentRequest,
export type TDocumentMetadataResponse = TDocumentMetadata;

export type TCollectionMetadataRequest = Omit<
TDocumentMetadataRequest,
'docId'
>;

export type TCollectionMetadataResponse = TCollectionMetadata;
39 changes: 35 additions & 4 deletions packages/serverless/src/apollo/app/collection.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Collection } from '@domain';
import { GROUP_DEFAULT_ADMIN } from '@common';
import { Collection, Metadata } from '@domain';
import { gql } from '@helper';
import {
collectionName,
Expand All @@ -13,13 +14,14 @@ import {
TCollectionExistResponse,
TCollectionListRequest,
TCollectionListResponse,
TCollectionMetadataRequest,
TCollectionMetadataResponse,
} from '@zkdb/common';
import { Permission } from '@zkdb/permission';
import { withTransaction } from '@zkdb/storage';
import GraphQLJSON from 'graphql-type-json';
import Joi from 'joi';
import { authorizeWrapper, publicWrapper } from '../validation';
import { GROUP_DEFAULT_ADMIN } from '@common';

export const schemaField = Joi.object({
name: Joi.string()
Expand Down Expand Up @@ -58,7 +60,12 @@ export const typeDefsCollection = gql`
extend type Query {
collectionList(databaseName: String!): [MetadataCollection]!

collectionExist(databaseName: String!, collectionName: String!): Boolean
collectionMetadata(
databaseName: String!
collectionName: String!
): MetadataCollection!

collectionExist(databaseName: String!, collectionName: String!): Boolean!
}

extend type Mutation {
Expand All @@ -68,7 +75,7 @@ export const typeDefsCollection = gql`
schema: [SchemaFieldInput!]!
group: String
permission: Int
): Boolean
): Boolean!
}
`;

Expand Down Expand Up @@ -124,11 +131,35 @@ const collectionCreate = authorizeWrapper<
)
);

const collectionMetadata = authorizeWrapper<
TCollectionMetadataRequest,
TCollectionMetadataResponse
>(
Joi.object({
databaseName,
collectionName,
}),
async (_root, { databaseName, collectionName }, ctx) => {
const collectionMetadata = await Metadata.collection({
databaseName,
collectionName,
actor: ctx.userName,
});

if (!collectionMetadata) {
throw new Error(`Can't find metadata collection: ${collectionName}`);
}

return collectionMetadata;
}
);

export const resolversCollection = {
JSON: GraphQLJSON,
Query: {
collectionList,
collectionExist,
collectionMetadata,
},
Mutation: {
collectionCreate,
Expand Down
26 changes: 2 additions & 24 deletions packages/serverless/src/apollo/app/database.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Database } from '@domain';
import { config, gql } from '@helper';
import {
TDatabaseChangeOwnerRequest,
TDatabaseChangeOwnerResponse,
TDatabaseCreateRequest,
TDatabaseCreateResponse,
TDatabaseExistResponse,
Expand All @@ -14,7 +14,6 @@ import {
merkleHeight,
pagination,
publicKey,
userName,
} from '@zkdb/common';
import {
ModelDatabase,
Expand All @@ -24,8 +23,6 @@ import {
import GraphQLJSON from 'graphql-type-json';
import Joi from 'joi';
import { Document, ObjectId } from 'mongodb';
import { Database } from '@domain';
import { config, gql } from '@helper';
import { authorizeWrapper, publicWrapper } from '../validation';

export const typeDefsDatabase = gql`
Expand Down Expand Up @@ -69,7 +66,6 @@ export const typeDefsDatabase = gql`

extend type Mutation {
dbCreate(databaseName: String!, merkleHeight: Int!): Boolean
dbTransferOwner(databaseName: String!, newOwner: String!): Boolean
dbDeploy(databaseName: String!, appPublicKey: String!): Boolean
}
`;
Expand Down Expand Up @@ -107,12 +103,6 @@ export const JOI_DATABASE_UPDATE_DEPLOY =
appPublicKey: publicKey,
});

export const JOI_DATABASE_TRANSFER_OWNER =
Joi.object<TDatabaseChangeOwnerRequest>({
databaseName,
newOwner: userName,
});

// Query
const dbStats = publicWrapper<TDatabaseRequest, Document>(
Joi.object({
Expand Down Expand Up @@ -178,17 +168,6 @@ const dbCreate = authorizeWrapper<
)
);

const dbTransferOwner = authorizeWrapper<
TDatabaseChangeOwnerRequest,
TDatabaseChangeOwnerResponse
>(JOI_DATABASE_TRANSFER_OWNER, async (_root, { databaseName, newOwner }, ctx) =>
Database.transferOwnership({
databaseName,
newOwner,
databaseOwner: ctx.userName,
})
);

const dbExist = publicWrapper<TDatabaseRequest, TDatabaseExistResponse>(
Joi.object({
databaseName,
Expand Down Expand Up @@ -224,7 +203,6 @@ export const resolversDatabase = {
},
Mutation: {
dbCreate,
dbTransferOwner,
dbDeploy,
},
};
45 changes: 44 additions & 1 deletion packages/serverless/src/apollo/app/document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import { withCompoundTransaction, withTransaction } from '@zkdb/storage';
import GraphQLJSON from 'graphql-type-json';
import Joi from 'joi';
import { Document } from '@domain';
import { Document, Metadata } from '@domain';
import { gql } from '@helper';
import { authorizeWrapper } from '../validation';
import {
Expand All @@ -24,6 +24,8 @@ import {
TDocumentDropRequest,
TDocumentDropResponse,
docId,
TDocumentMetadataRequest,
TDocumentMetadataResponse,
} from '@zkdb/common';

import { Permission } from '@zkdb/permission';
Expand Down Expand Up @@ -121,6 +123,15 @@ export const typeDefsDocument = gql`
offset: Int!
}

type DocumentMetadataResponse {
owner: String!
group: String!
permission: Int!
collectionName: String!
docId: String!
merkleIndex: String!
}

extend type Query {
documentFind(
databaseName: String!
Expand All @@ -129,6 +140,12 @@ export const typeDefsDocument = gql`
pagination: PaginationInput
): DocumentFindResponse

documentMetadata(
databaseName: String!
collectionName: String!
docId: String!
): DocumentMetadataResponse

documentHistoryFind(
databaseName: String!
collectionName: String!
Expand Down Expand Up @@ -299,11 +316,37 @@ const documentHistoryFind = authorizeWrapper<
}
);

const documentMetadata = authorizeWrapper<
TDocumentMetadataRequest,
TDocumentMetadataResponse
>(
Joi.object({
databaseName,
collectionName,
docId,
}),
async (_root, { databaseName, collectionName, docId }, ctx) => {
const documentMetadata = await Metadata.document({
databaseName,
collectionName,
docId,
actor: ctx.userName,
});

if (!documentMetadata) {
throw new Error(`Can't find metadata document: ${docId}`);
}

return documentMetadata;
}
);

export const resolversDocument = {
JSON: GraphQLJSON,
Query: {
documentFind,
documentHistoryFind,
documentMetadata,
},
Mutation: {
documentCreate,
Expand Down
1 change: 0 additions & 1 deletion packages/serverless/src/apollo/app/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ export * from './database';
export * from './document';
export * from './group';
export * from './merkle-tree';
export * from './metadata';
export * from './proof';
export * from './rollup';
export * from './transaction';
Expand Down
Loading