1.0.21 • Published 3 years ago

@timekadel/lazy v1.0.21

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

1. Lazy API Framework

:warning: These instructions are incomplete. Documentation process is still in progress.

A Node.js API framework made by lazy developers for lazy developers. Easy setup and migration of MySQL table models, automated and intuitive API endpoint generation, straightforward API integration tests setup and much more.

Table of contents

2. Installation

Lazy is free from native bindings and can be installed on Linux, Mac OS or Windows without any issues.

npm install --save @timekadel/lazy

3. Setup

3.1. Configuration

const {Lazy} = require('@timekadel/lazy'); //Requiring Lazy

//Setting up Lazy
Lazy.config({
    searchPath: String,                 //Root path to lazys configuration files (search is recursive)
    db:{                                //DB configuration
        host: String,                   //DB Host
        db: String,                     //DB name
        user: String,                   //DB user
        password: String                //DB password
    },
    server:{                            //API server configuration
        port: Number,                   //API running port. (Default: 4000)
        allowedOrigins: Array<String>,
        auth: Function                  //API server Authentication middleware function
    },
    io:{                                //Socket.io configuration (Optional)
        onConnect: Function,            //On connection function
        auth: Function,                 //Socket.io authentication middleware function 
        cookie: Boolean,                //Enable Cookies ?
        pingInterval: Number,           //Socket.io Ping interval
        pingTimeout: Number             //Socket.io Ping timeout
    }
});

Lazy recursively searches for *.lazy.js files within a provided search path from which MySQL tables and API endpoints will be generated. Lazy's API server may be configured to run over any given port.

3.1.1. Database options

The following configuration options are mandatory in order to establish a connection with the database:

  • host: The hostname of the database you are connecting to. (Default: localhost)
  • port: The port number to connect to. (Default: 3306) and port are ignored.
  • user: The MySQL user to authenticate as.
  • password: The password of that MySQL user.
  • database: Name of the database to use.

3.1.2. Server Options

If a list of allowed origins is provided, API requests will be restricted to the provided list of origins and responded with a status-code 403 otherwise.

It is up to developers to setup an authentication middleware function. If none is provided, each described route should be configured with their protected parameter set to false. (see Describing Lazys).

  • port: The port number to connect to. (Default: 4000)
  • allowedOrigins: Access control - Allowed origins list.
  • auth: API server Authentication middleware function.

3.1.3. IO Options

As a way to broadcast live messages, Lazy comes pre-packaged with socket.io. You may opt to use it within your project by providing an io configuration section to Lazy's configuration object as follows:

4. Describing Lazys

Lazy's modules called "Lazys" must be placed anywhere under the provided searchPath (see Setup section). Lazys may be used to describe models and/or API endpoints. Valid Lazys must be described as follows:

/** <appRoot>/<searchPath>/<lazyname>.lazy.js */

module.exports = {
    name: String,       //Each lazy name must be unique !.
    model: Object,      //Optional: Model configuration. (You may create virtual endpoints)
    endpoint: Object    //Optional: Endpoint configuration. (You may choose not to register endpoints)
}

To give a better overall understanding of Lazy's syntax and operation, the following sections will be based around a very simple example described below:

4.1. Describing Models

model:{
    table: String,
    schema: Object
}

4.1.1. Describing Schemas

schema:{
    column_1_name: Object,
    //...
    column_n_name: Object
}

4.1.1.1. Describing Columns

column_name:{
    type: String,
    size: Number,
    default: String,
    pk: Boolean,
    fk: Object
}
4.1.1.1.1. Foreign Key Configuration
fk:{
    join: Object<Lazy>,
    on: String,
    as: String,
    delete: String,
    update: String,
}

4.1.2. Model Declaration Examples

To give a better overall understanding of the Lazy's model features, Basic examples based aroud the implementation of the following class diagram are described within the next section.

:warning: WIP, This section is incomplete.

4.1.2.1. Simple User Model

module.exports = {
    name: "LazyUser",
    model:{
        table: "users",
        schema:{
            id:{
                type: "VARCHAR",
                size: 50,
                pk: true
            },
            fname:{
                type: "VARCHAR",
                size: 20,
                default: "Fname"
            },
            lname:{
                type: "VARCHAR",
                size: 20,
                default: "Lname"
            },
        },
    },
    //...Endpoint declaration
}

4.1.2.2. "Complex" Friend Model (with foreign key constraints)

const {Lazy} = require('@timekadel/lazy');
const LazyUser = Lazy.require('LazyUser');

module.exports = {
    name: "LazyFriend",
    model:{
        table: "friends",
        schema:{
            user_id:{
                type: "VARCHAR",
                size: 50,
                fk: {
                    join: LazyUser,
                    on: "id",
                    delete: "CASCADE",
                    update:  "CASCADE"
                }
            },
            friend_id:{
                type: "VARCHAR",
                size: 50,
                fk: {
                    join: LazyUser,
                    on: "id",
                    as: "friend",
                    delete: "CASCADE",
                    update:  "CASCADE"
                }
            },
            timestamp:{
                type: "TIMESTAMP",
                size: 3,
                default: "CURRENT_TIMESTAMP"
            }
        }
    },
    //...Endpoint declaration

4.2. Configuring Endpoints

endpoint:{
    root: String,
    protected: Boolean,
    methods: Object
}

:warning: WIP, This section is incomplete.

5. Changelog

  • 1.0.21:
    • Implemented direction for ORDER BY clause
    • Patched lazy.controller.js to avoid automatically fetching resources for tables without private key
  • 1.0.20:
    • Improved instructions sections organisation/numbering
  • 1.0.19:
    • Created git repository
    • Updated links to git repository
    • Updated Model declaration examples
  • 1.0.18:
    • Patched lazy.model.js to multiple join queries on same foreign table.
    • Numbered instruction sections to make things clearer
  • 1.0.17:
    • Patched lazy.model.js to multiple join queries on same foreign table.
    • Patched lazy.controller to avoid throwing errors when no tests are beign described.
    • Patched lazy.js to avoid throwing error when no auth functions are provided.
    • Patched lazy.js to avoid throwing error if io configuration does not exist
    • Updated Instructions

6. Appendix

6.1. Supported Column Types

:warning: WIP, This section is incomplete/invalid.

Type StringIs TypedAdditional Parameters
CHARyesnone
VARCHARyesnone
BINARYyesnone
VARBINARYyesnone
TINYBLOByesnone
TINYTEXTyesnone
TEXTyesnone
BLOByesnone
MEDIUMTEXTyesnone
MEDIUMBLOByesnone
LONGTEXTyesnone
LONGBLOByesnone
ENUMyesnone
CHARyesnone
VARCHARyesnone
BINARYyesnone
VARBINARYyesnone
TINYBLOByesnone
TINYTEXTyesnone
TEXTyesnone
BLOByesnone
MEDIUMTEXTyesnone
MEDIUMBLOByesnone
LONGTEXTyesnone
LONGBLOByesnone
ENUMyesnone
SETyesnone
BITyesnone
TINYINTyesnone
BOOLyesnone
BOOLEANyesnone
SMALLINTyesnone
MEDIUMINTyesnone
INTyesnone
INTEGERyesnone
BIGINTyesnone
FLOATyesnone
DOUBLEyesnone
DOUBLE PRECISIONyesnone
DECIMALyesnone
DECyesnone
DATEyesnone
DATETIMEyesnone
TIMESTAMPyesnone
TIMEyesnone
YEARyesnone
1.0.21

3 years ago

1.0.20

3 years ago

1.0.19

3 years ago

1.0.18

3 years ago

1.0.17

3 years ago

1.0.16

3 years ago

1.0.15

3 years ago

1.0.14

3 years ago

1.0.13

3 years ago

1.0.12

3 years ago

1.0.11

3 years ago

1.0.10

3 years ago

1.0.9

3 years ago

1.0.8

3 years ago

1.0.7

3 years ago

1.0.6

3 years ago

1.0.5

3 years ago

1.0.4

3 years ago

1.0.3

3 years ago

1.0.2

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago