Giter Site home page Giter Site logo

db-mock's Introduction

db-mock

What's better in this life than having a database mocking, for our api-mocking! db-mock, is an engine that is perfect for simulating a database, with a very simple setup, you can have a db-like and an api-mocks in matter of minutes. If you mock you api using Node and Express, db-mock will make your life 10x easier!

Features

  • handy data types (string, number, bool, date, array, object)
  • data type constraints (required, min, max)
  • one-to-one and one-to-many relations ($has, and $hasMany)S
  • injecting related resources optionally
  • handy resource apis (add, get, update, delete, list and query)
  • validations are everywhere to help you spot any conflicts (validating schema, and operations)
  • configurations for almost everything
  • colorful and informative logging
  • It can run in three different modes
    • as a node module, using: require('db-mock')
    • as a standalone module that communicates on sockets (still under development)
    • as a web-console that makes you manage everything visually, edit schema and browse the data (still under development)

Install

npm install db-mock --save-dev

Examples

Seed data

./node_modules/.bin/db-mock seed

seeding data

Clean data

./node_modules/.bin/db-mock clean

cleaning data

Schema example

// profile.json
{
  "address": "string",
  "phone": "string"
}
// course.json
{
  "name": "string",
  "description": "string",
  "$has": {
    "many": [{
      "relationWith": "student",
      "inject": true
    }]
  }
}
// student.json
{
  "name": {
    "type": "string",
    "required": true
  },
  "age": {
    "type": "number",
    "min": 0,
    "max": 100
  },
  "dateOfBirth": "date",
  "married": "bool",
  "sons": {
    "type": "array",
    "required": true
  },
  "$has": {
    "one": [{
      "relationWith": "profile",
      "required": true,
      "inject": true
    }],
    "many": [
      "course"
    ]
  }
}

Seed example

// seed.js
var _ = require('lodash');

module.exports = function(db) {
  _.times(10, (i) => {
    let studentProfile = db.profile.add({
      address: 'address ' + i
    });

    db.student.add({
      name: 'student name ' + i,
      dateOfBirth: new Date(),
      profile_ID: studentProfile.ID
    });
  });
}

Getting started

If you know NodeJS and Express, then let's do few steps and have our api-mocks running just like real ones. I've created sample application for you to check and follow Here it is!

Data types

  • string
  • date
  • number
  • bool
  • array
  • object

Configurations

All optional! Create .dbmockrc on the root to configure any option.

Option Default Description
socketsPort 6789 The port of engine when it runs in sockets mode
webConsolePort 6790 The port of the web-console
data 'db-mock/data/' The directory of data (should be git ignored)
schema 'db-mock/schema/' Directory of the schema
seed 'db-mock/seed.js' File location of seed script
IDProperty 'ID' ID property
foreignIDSuffix '_ID' How should foreign IDs be. (classID, classId, class_id, or classID)
enableCreatedAtProperty true Auto insert 'created at' field
createdAtProperty '_createdAt' Name of 'created at' property if enabled
enableUpdatedAtProperty true Auto insert 'updated at' field
updatedAtProperty '_updatedAt' Name of 'updated at' property
injectResourceName true Whether to inject the resource name at retrieval, (student resource will has '$name'='student' property)
resourceNameProperty '_name' resource name property

Sample

{
  "data": "db-mock-data/data/",
  "schema": "db-mock-data/schema/",
  "seed": "db-mock-data/seed.js",
  "foreignIDSuffix": "ID"
}

License

MIT

db-mock's People

Contributors

amrabdulrahman avatar

Watchers

James Cloos avatar zee avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.