Package: cornflow-core
Cornflow-core is a library that contains modules to help you create REST APIs in an easier and faster way.
It includes a set of modules that can be used to start the creation of your flask REST API and some command line
interface commands that let you create a full REST API from a JSONSchema file that represent your data or
create a JSONSchema file representing your REST API.Command line interfacegenerate_from_schemaThe cligenerate_from_schemaallows you to automatically generate models, endpoints and schemas
from a JSONSchema. The generated files can then be added to your flask (RestFul) REST API.How to use?To start, you need to have a json file containing the schema of the tables you want to create.
Let’s assume that this file is stored on your computer asC:/Users/User/instance.jsonOpen the terminal, then run:generate_from_schema -p C:/Users/User/instance.json -a application_nameThe argumentapplication_namewill be the prefix of the name used for the generated files, classes
and tables. It is an optional argument
This command will create a newoutput/directory in the folder where is executed, containing three
directoriesmodels/,schemas/andendpoints/, in which the new files will be added.Optional argumentsOutput pathUse the-oor--output-pathoptions to set an output path for the files. The
directoriesmodels/,endpoint/andschemas/will be created directly in that
direction instead of the./output/directory.Example:generate_from_schema -p C:/Users/User/instance.json -a application_name --output-path C:/Users/User/output_filesRemove methodsBy default, two endpoints are created:A global endpoint, with three methods:get(), that returns all the element of the table.post(**kwargs), that adds a new row to the table.A detail endpoint, with three methods:get(idx), that returns the entry with the given id.put(idx, **kwargs), that updates the entry with the given id with the given data.patch(idx, **kwargs)that patches the entry with the given id with the given oatch.delete(idx), that deletes the entry with the given id.If one or several of those methods are not necessary, the option--remove-methodsor-rallows to not
generate some of those methods.Example:generate_from_schema -p C:/Users/User/instance.json -a application_name --remove-methods get-list -r delete-detailIn that example, for each table, the detail endpoint will not contain thedelete()method and
the list endpoint will not contain theget()method. The choices for this method areget-list,post-list,get-detail,put-detail,delete-detailandpatch-detail.One tableBy default, the module accepts schemas that contain several tables at once (see for example the
instance schema for the rostering application, incornflow-dags). If you only need to create one table,
the schema can also have the following format:{
  "type": "array",
  "description": "Table with the employee master information",
  "items": {
    "type": "object",
    "properties": {
      "id": {
        "description": "The unique identifier for each employee.",
        "type": "integer",
      },
      "name": {
        "description": "The name of each employee.",
        "type": "string",
      },
    },
    "required": [
      "id",
      "name",
    ]
  }
}that is, the schema is simply the description of the table. In that case, you can use
the--oneoption to indicate the name of the table. If not, the generated table will be called{application_name}_databy default.Example:generate_from_schema -p C:/Users/User/instance.json -a application_name --one table_nameIn that case, only one table will be created.NotesPrimary keysIf your table contains a field namedid, this field will automatically be considered the
primary key of the table. If it doesn’t, an autoincrementing columnidwill be added to the
table andidwill be set as the primary key of the table.Foreign keysIf a field is a foreign key to another table, this can be indicated in the schema.
You only need to add the propertyforeign_keyin the information about the property.
Its value must have the formattable_name.key,table_namebeing the name of the table
the attributes refers to, andkeybeing the name of the foreign key in the original table.
For example, if the table employee has aid_jobproperty that is a foreign_key referring to
the propertyidof the tablejobs, then the propertyid_jobcan be described
as follows:{
    ...,
    "id_job": {
        "type": "integer",
        "description": "The id. of the job",
        "foreign_key": "jobs.id"
    },
    ...
}If the propertyforeign_keyis left empty, it is assumed that the key is not a foreign key.Moduleschema_from_modelsThe clischema_from_modelsallows you to automatically generate a JSONSchema based on
a set of models.How to use?To start, you need to have a directory containing the SQLAlchemy models.
Let’s assume that this directory is stored on your computer asC:/Users/User/modelsOpen the terminal and run:schema_from_models -p C:/Users/User/modelsThis command will create a newoutput_schema.jsondirectory in the directory from where it was executed,
containing the generated schema.Optional argumentsOutput pathSpecify an output path using the argument-oor--output_path.Ignore filesBy default, all the python files that do not contain models will be ignored. However, if you
need to specify that some model files need to be ignored, you can use the-ior--ignore-filesoption. This option takes as arguments the name of the files to ignore
with their extension. Example:schema_from_models -p C:/Users/User/models --ignore-files instance.py -i execution.py