Skip to content

Commit

Permalink
New model API
Browse files Browse the repository at this point in the history
  • Loading branch information
serefyarar committed Jul 4, 2024
1 parent a701203 commit 65043c4
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 39 deletions.
13 changes: 9 additions & 4 deletions api/src/controllers/model.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,31 @@
import { createClient } from "redis";
import { indexNewModel, stopIndexingModels } from "../libs/composedb.js";
import { createNewModel, indexNewModel, stopIndexingModels } from "../libs/composedb.js";

export const info = async (req, res) => {
const runtimeDefinition = req.app.get("runtimeDefinition");
const modelFragments = req.app.get("modelFragments");

res.json({
runtimeDefinition,
modelFragments,
});
};

export const create = async (req, res, next) => {
const indexResult = await createNewModel(
req.body.schema
);
res.json(indexResult);
};

export const deploy = async (req, res, next) => {

const pubClient = createClient({
url: process.env.REDIS_CONNECTION_STRING,
});
await pubClient.connect();
const indexResult = await indexNewModel(
req.app,
req.params.id,
req.headers.authorization,
);
if (indexResult) {
pubClient.publish("newModel", req.params.id);
Expand All @@ -35,7 +41,6 @@ export const remove = async (req, res, next) => {
const indexResult = await stopIndexingModels(
req.app,
req.params.id,
req.headers.authorization,
);
if (indexResult) {
pubClient.publish("newModel", req.params.id);
Expand Down
64 changes: 31 additions & 33 deletions api/src/libs/composedb.js
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,7 @@ let defaultRuntime = {
},
};


export const jsonSchemaToGraphQLFragment = (schema, prefix = false) => {
function resolveRef(ref, defs) {
const refPath = ref.replace(/^#\/\$defs\//, "");
Expand Down Expand Up @@ -488,56 +489,53 @@ export const jsonSchemaToGraphQLFragment = (schema, prefix = false) => {
return `... on ${schema.name} {\n${finalFragment}\n}`;
};

export const indexNewModel = async (app, modelId, ceramicAdminPrivateKey) => {
const indexerCeramic = new CeramicClient(process.env.CERAMIC_HOST);
if (!ceramicAdminPrivateKey) {
return false;
export const createNewModel = async (graphQLSchema) => {
await authenticateAdmin()
try {
const response = await Composite.create({ ceramic, schema: graphQLSchema, index: false })
return {status: true, models: response.modelIDs}
} catch (e) {
return {status: false, error: e.message}
}
const key = fromString(ceramicAdminPrivateKey, "base16");
const did = new DID({
resolver: getResolver(),
provider: new Ed25519Provider(key),
});
await did.authenticate();
if (!did.authenticated) {
return false;
}

export const indexNewModel = async (app, modelId) => {

const modelName = await ceramic.loadStream(modelId);
const protectedModelNames = Object.keys(defaultRuntime.models)
if (protectedModelNames.includes(modelName)) {
console.log(`Model name is protected`)
return false
}
const indexedModelList =await ceramic.admin.getIndexedModels()
if (indexedModelList.includes(modelId)) {
console.log(`Model is already indexed`)
return false
}
indexerCeramic.did = did;

await authenticateAdmin()
await Composite.fromModels({
ceramic: indexerCeramic,
ceramic,
models: [modelId],
index: true,
});
await setIndexedModelParams(app);

return true;
};

export const stopIndexingModels = async (
app,
modelId,
ceramicAdminPrivateKey,
) => {
const indexerCeramic = new CeramicClient(process.env.CERAMIC_HOST);
if (!ceramicAdminPrivateKey) {
return false;
await authenticateAdmin()
const modelName = await ceramic.loadStream(modelId);
const protectedModelNames = Object.keys(defaultRuntime.models)
if (protectedModelNames.includes(modelName)) {
console.log(`Model name is protected`)
return false
}
const key = fromString(ceramicAdminPrivateKey, "base16");
const did = new DID({
resolver: getResolver(),
provider: new Ed25519Provider(key),
});
await did.authenticate();
if (!did.authenticated) {
return false;
}
indexerCeramic.did = did;

const models = await indexerCeramic.admin.stopIndexingModels([modelId]);

const models = await ceramic.admin.stopIndexingModels([modelId]);
await setIndexedModelParams(app);

return models;
};
export const setIndexedModelParams = async (app) => {
Expand Down
12 changes: 10 additions & 2 deletions api/src/packages/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -659,22 +659,30 @@ app.post(

app.get("/model/info", modelController.info);
app.post(
"/model/index/:id",
"/model/:id",
validator.params(
Joi.object({
id: Joi.custom(isStreamID, "Model ID").required(),
}),
),
authCheckMiddleware,
modelController.deploy,
);

app.post(
"/model",
authCheckMiddleware,
modelController.create,
);

app.delete(
"/model/index/:id",
"/model/:id",
validator.params(
Joi.object({
id: Joi.custom(isStreamID, "Model ID").required(),
}),
),
authCheckMiddleware,
modelController.remove,
);

Expand Down

0 comments on commit 65043c4

Please sign in to comment.