namespace repositories { export var concept_core : helpers.repository_sqltable = { "setup": function () { return helpers.database.query_do_named("concept.setup-core"); }, "teardown": function () { throw (new Error("not implemented")); }, "create": function (row) { return helpers.database.query_put_free ( "INSERT INTO concepts(type_id, description) VALUES (:type_id, :description);", { "type_id": row["type_id"], "description": row["description"], } ); }, "update": function (concept_id, row) { return helpers.database.query_do_free ( "UPDATE concepts SET type_id = :type_id, description = :description WHERE (id = :id)", { "id": concept_id, "type_id": row["type_id"], "description": row["description"], } ); }, "delete": function (concept_id) { return helpers.database.query_do_free ( "DELETE FROM concepts WHERE (id = :id)", { "id": concept_id, } ); }, "read": function (concept_id) { return ( helpers.database.query_get_free ( "SELECT type_id,description FROM concepts WHERE (id = :id)", { "id": concept_id, } ) .then ( (rows) => ( (rows.length === 1) ? Promise.resolve(rows[0]) : Promise.reject(new Error("not found")) ) ) ); }, "purge": async function (column, value) { throw (new Error("not implemented")); }, "take": async function (column, value) { throw (new Error("not implemented")); }, }; export var concept_tags : helpers.repository_sqltable = { "setup": function () { return helpers.database.query_do_named("concept.setup-tags"); }, "teardown": function () { throw (new Error("not implemented")); }, "create": function (row) { return helpers.database.query_put_free ( "INSERT INTO concept_tags(concept_id, tag_id) VALUES (:concept_id, :tag_id);", { "concept_id": row["concept_id"], "tag_id": row["tag_id"], } ); }, "update": function (concept_tag_id, row) { return helpers.database.query_do_free ( "UPDATE concept_tags SET concept_id = :concept_id, tag_id = :tag_id WHERE (id = :id)", { "id": concept_tag_id, "concept_id": row["concept_id"], "tag_id": row["tag_id"], } ); }, "delete": function (concept_tag_id) { return helpers.database.query_do_free ( "DELETE FROM concept_tags WHERE (id = :id)", { "id": concept_tag_id, } ); }, "read": async function (concept_tag_id) { return ( helpers.database.query_get_free ( "SELECT concept_id,tag_id FROM concept_tags WHERE (id = :id)", { "id": concept_tag_id, } ) .then ( (rows) => ( (rows.length === 1) ? Promise.resolve(rows[0]) : Promise.reject(new Error("not found")) ) ) ); }, "purge": async function (column, value) { return helpers.database.query_do_free ( "DELETE FROM concept_tags WHERE (" + column + " = :value)", { "value": value, } ); }, "take": async function (column, value) { return await helpers.database.query_get_free ( "SELECT concept_id,tag_id FROM concept_tags WHERE (" + column + " = :value)", { "value": value, } ); }, }; /** */ export var concept_expressions : helpers.repository_sqltable = { "setup": function () { return helpers.database.query_do_named("concept.setup-expressions"); }, "teardown": function () { throw (new Error("not implemented")); }, "create": function (row) { return helpers.database.query_put_free ( "INSERT INTO concept_expressions(concept_id, expression_id) VALUES (:concept_id, :expression_id);", { "concept_id": row["concept_id"], "expression_id": row["expression_id"], } ); }, "update": function (concept_expression_id, row) { return helpers.database.query_do_free ( "UPDATE concept_expressions SET concept_id = :concept_id, expression_id = :expression_id WHERE (id = :id)", { "id": concept_expression_id, "concept_id": row["concept_id"], "expression_id": row["expression_id"], } ); }, "delete": function (concept_expression_id) { return helpers.database.query_do_free ( "DELETE FROM concept_expressions WHERE (id = :id)", { "id": concept_expression_id, } ); }, "read": async function (concept_expression_id) { return ( helpers.database.query_get_free ( "SELECT concept_id,expression_id FROM concept_expressions WHERE (id = :id)", { "id": concept_expression_id, } ) .then ( (rows) => ( (rows.length === 1) ? Promise.resolve(rows[0]) : Promise.reject(new Error("not found")) ) ) ); }, "purge": async function (column, value) { return helpers.database.query_do_free ( "DELETE FROM concept_expressions WHERE (" + column + " = :value)", { "value": value, } ); }, "take": async function (column, value) { return await helpers.database.query_get_free ( "SELECT concept_id,expression_id FROM concept_expressions WHERE (" + column + " = :value)", { "value": value, } ); }, }; export var concept : ( helpers.repository & { get_translations : (language_from : string, language_to : string, part : string)=>Promise>; search : (part : string)=>Promise>; export : ()=>Promise>; } ) = { "setup": async function () { await concept_core.setup(); await concept_tags.setup(); await concept_expressions.setup(); return Promise.resolve(undefined); }, "teardown": async function () { await concept_expressions.teardown(); await concept_tags.teardown(); await concept_core.teardown(); return Promise.resolve(undefined); }, "create": async function (concept_entity) { const row_core : type_row = {"type_id": concept_entity.type_id, "description": concept_entity.description}; const concept_id : int = await concept_core.create(row_core); for await (let tag_id of concept_entity.tags) { const row_tag : type_row = {"concept_id": concept_id, "tag_id": tag_id}; const concept_tag_id : int = await concept_tags.create(row_tag); } for await (let expression_id of concept_entity.expressions) { const row_expressions : type_row = {"concept_id": concept_id, "expression_id": expression_id}; const concept_expressions_id : int = await concept_expressions.create(row_expressions); } return Promise.resolve(concept_id); }, "update": function (concept_id, concept_entity) { throw (new Error("not implemented")); }, "delete": async function (concept_id) { await concept_expressions.purge("concept_id", concept_id); await concept_tags.purge("concept_id", concept_id); await concept_core.delete(concept_id); return Promise.resolve(undefined); }, "read": async function (concept_id) { const row_core : type_row = await concept_core.read(concept_id);; const rows_tags : Array = await concept_tags.take("concept_id", concept_id); const rows_expressions : Array = await concept_expressions.take("concept_id", concept_id); const concept_entity : entities.concept = { "type_id": row_core["type_id"], "description": row_core["description"], "tags": rows_tags.map(row_tag => row_tag["tag_id"]), "expressions": rows_expressions.map(row_expression => row_expression["expression_id"]), }; return Promise.resolve(concept_entity); }, "get_translations": function (language_from, language_to, part) { return helpers.database.query_get_named ( "concept.get_translations", { "language_from": language_from, "language_to": language_to, "part": part.replace(new RegExp("-", "g"), "%"), } ); }, "search": function (part) : Promise> { return ( helpers.database.query_get_named("concept.search", {"part": part.replace(new RegExp("-", "g"), "%")}) .then> ( (rows) => Promise.resolve>(rows.map(row => row["id"])) ) ); }, "export": function () : Promise> { return helpers.database.query_get_named("concept.dump", {"part": null}); }, }; }