scam

0.1.6 • Public • Published

Scam

Simple schema-based REST API on node and SQLite, with fully functional CRUD flows.

Scam is not suitable for production use as the name implies! Scam is a prototyping tool intended for designing REST APIs and supporting frontend development.

Scam is still quite early in development.

Usage

npm install express scam --save
const path = require('path')
const express = require('express')
const scam = require('scam')
const app = express()
app.set('port', (process.env.PORT || 3333))
 
scam.init(app, {
  data: { ... },
  schema: { ... },
  databasePath: path.resolve(__dirname, './db.sql')
})
 
app.listen(app.get('port'), function () {
  scam.log()
})

See the demo project for a better example.

Schema

{
  posts: {
    singular: 'post',
    plural: 'posts',
    fields: [
      name: {
        type: 'string'
      },
      body: {
        type: 'string'
      },
      flagged: {
        type: 'boolean',
        default: false
      },
      user: {
        type: 'user' // Note singular
      },
      comments: {
        type: 'comments' // Note plural
      }
    ]
  },
  comments: {
    ...
  },
  users: {
    ...
  }
}

Data (optional)

{
  posts: [
    {
      name: 'Some Name',
      body: 'Body text body text body text',
      user: 1,
      comments: [1, 2, 3]
    },
 
    ...
 
  ]
}

Options

{
  // Set Scam to debug mode (prints internal error messages on 500)
  debug: debug,
 
  // Default cache time (can be overwritten per resource in schema)
  cache: 0,
 
  // Dummy data as an object, or path to `require` it from
  data: require('./data'),
 
  // API schema as an object, or path to `require` it from
  schema: require('./schema'),
 
  // Path of the db file to create/remove/update (in-memory DB not yet supported)
  databasePath: path.resolve(__dirname, 'db/db.sql')
}

API

let app = require('express')()
let scam = require('scam')
 
// Prepare a new Scam instance
scam.init(app, options)
 
// Delete DB if it exists
scam.clearDatabase()
 
// Init new DB based on schema
scam.setupDatabase()
 
// Load dummy data
scam.loadData()
 
// Print instructions and endpoints
scam.log()
 
// Access props
scam.data
scam.dbPath
scam.endpoints
scam.schema

Deployment

You can deploy a Scam API just like any other express app. A quick way to try it out is to host your repo on GitHub and connect it to Heroku.

Schema: Field types

The following raw types are supported:

  • (integer)
  • string
  • float
  • boolean
  • array (will be stored as string (of JSON) in DB)
  • date
  • time
  • timestamp

Default is 'integer', and it doesn't need to be specified.

You can also use the key of another resource type as the type. In this case the values are integers and are treated as IDs pointing to resources of the given type.

Contributing

Scam is still early in development, and pretty hacky. See todo.md for the next steps.

Feel free to file issues or PRs though!

/scam/

    Package Sidebar

    Install

    npm i scam

    Weekly Downloads

    0

    Version

    0.1.6

    License

    none

    Last publish

    Collaborators

    • eiskis