Coder Social home page Coder Social logo

techuila / node-loopie Goto Github PK

View Code? Open in Web Editor NEW
6.0 2.0 0.0 25 KB

Reduce your code with node-loopie. Routes declaration, models, and etc... node-loopie will shorten it for you. Converts your nodejs files in a folder to an object.

License: ISC License

JavaScript 100.00%
code-optimization code-simplification declarative-programming express model-generation nodejs automatic-routing file-to-object

node-loopie's Introduction

node-loopie NPM version NPM monthly downloads NPM total downloads

Reduce your code with node-loopie. Routes declaration, models, and etc... node-loopie will shorten it for you. Converts your nodejs files in a folder to an object.

  • Declarative: node-loopie helps you shorten your code, you don't have to declare multiple properties to export your object or even initialize function(s). It makes recognition of objects easier with just looking at your file names.

Installation

Install with npm:

$ npm install --save node-loopie

Usage

Arguments

nl(folder_path, callback, ignore_file_names, extension_name)

  • folder_path (required) : [String] folder path of the files you want to loop.
  • callback : [Function] function to be executed. passes 3 arguments: file [String], filename [String], file [File].
  • ignore_file_names : [Array (String)] array of file names to be ignored in a folder.
  • extension_name extension name of the files you want to loop from.

Examples

Sequelize (index.js)

const Loopie = require('node-loopie');
const path = require('path');
const Sequelize = require('sequelize');
const env = NODE_ENV || 'localhost';
const config = require('/../config/config.js')[env];
const db = {};

let sequelize;
if (config.use_env_variable) {
	sequelize = new Sequelize(process.env[config.use_env_variable], config);
} else {
	sequelize = new Sequelize(
		config.database,
		config.username,
		config.password,
		config
	);
}

// Get all the filename inside the models folder
Loopie(__dirname, (file) => {
	const model = require(path.join(__dirname, file))(
		sequelize,
		Sequelize.DataTypes
	);
	db[model.name] = model;
});

Object.keys(db).forEach((modelName) => {
	if (db[modelName].associate) {
		db[modelName].associate(db);
	}
});

db.sequelize = sequelize;
db.Sequelize = Sequelize;

module.exports = db;

Express Routes (index.js)

const express = require('express');
const Loopie = require('node-loopie');
const app = express();

Loopie(__dirname, (file, fileName) => {
	app.use(`/api/${fileName}`, require(path.join(__dirname, file)));
});

Author

Axl Cuyugan

License

Released under the ISC License.


node-loopie's People

Contributors

techuila avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 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.