3.2.0 • Published 4 years ago

@pgr4567/reapi v3.2.0

Weekly downloads
-
License
MIT
Repository
github
Last release
4 years ago

ReAPI

npm GitHub Lines of code npm bundle size npm type definitions npm GitHub top language

My personal API for webservers that use a database. You probably don't want to use this xd

Summary

This projects enables me(and you) to create applications that use databases more easily. You simply have to populate ReAPI with your database schema and let it generate endpoints for all collections. These endpoints include authorization and authentication so you do not have to write the same backend every time you need a database. Just tell ReAPI that all users can create a Task, but only the Task owner can edit and delete it. ReAPI handles the rest and the client can simply call the endpoints, almost as if the client connected to the database directly.

Documentation

Setup

To use ReAPI, start by importing the module and creating a new ReMongo instance. Then call the connect function and add your collections. Lastly, call create and see your endpoints in action!

Example

import { CollectionDocument, ReMongo } from "@pgr4567/reapi";

const remongo = new ReMongo("connection_string", "database_name", 8000, "0.0.0.0");

async function run(): Promise<void> {
    await remongo.connect();

    remongo.setCollection("Tasks", {
        "name": "Tasks",
        "fields": {
            "owner_id": {
                "type": "string",
                "required": false,
                "unique": false,
                "readonly": true,
                "secret": false,
                "preInsert": (_: string, user?: CollectionDocument) => {
                    return user!["id"];
                }
            },
            "name": {
                "type": "string",
                "required": true,
                "unique": false,
                "readonly": false,
                "secret": false
            },
            "coolness": {
                "type": "number",
                "required": false,
                "unique": false,
                "readonly": false,
                "secret": false
            }
        },
        "access": {
            "read": "$owner_id=&id",
            "delete": "$owner_id=&id",
            "edit": "$owner_id=&id",
            "info": "@everyuser",
            "insert": "@everyuser"
        }
    });
    
    remongo.setUserCollection({
        "name": "",
        "fields": {},
        "access": {
            "read": "",
            "delete": "",
            "edit": "",
            "info": "",
            "insert": ""
        }
    });

    remongo.create();
}

run();

Creation of collections

When creating a collection, the required access fields read, edit, insert, info, delete have the following format:

  • The only supported operant is =.
  • $ references the fields of the document.
  • & references the fields of the user document.
  • Additional special values are @everyone, @noone and @everyuser. Use these rules to create a condition that enables said access right when true.

Making requests to endpoints

  • Every request is a POST request that must have all data in the body.
  • Content-Type header must be application/json.
  • username and user_token is required in every request.
  • query is required when you want to target existing documents. The format is:
    • Separate clauses must be separated by &.
    • Clause statements must be separated by |.
  • document_data is required when you want to insert a new or edit an existing document.

TODO

  • Add more database types.
  • Add more authentication methods.
  • Improve documentation:
    • id field is reserved on collection level and always gets populated with a random id,
    • id fields in deeper levels are not populated
    • if a parent element sets a field, the child elements inherit it.
3.2.0

4 years ago

3.1.0

4 years ago

2.1.0

4 years ago

3.0.0

4 years ago

2.0.19

4 years ago

2.0.17

4 years ago

2.0.18

4 years ago

2.0.15

4 years ago

2.0.3

4 years ago

2.0.16

4 years ago

2.0.2

4 years ago

2.0.13

4 years ago

2.0.5

4 years ago

2.0.14

4 years ago

2.0.4

4 years ago

2.0.7

4 years ago

2.0.12

4 years ago

2.0.6

4 years ago

2.0.20

4 years ago

2.0.9

4 years ago

2.0.10

4 years ago

2.0.8

4 years ago

2.0.1

4 years ago

2.0.0

4 years ago

1.0.0

4 years ago