Research
Security News
Malicious npm Packages Inject SSH Backdoors via Typosquatted Libraries
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
api-data-tools
Advanced tools
Bundle of utilities for generating migration files, syncing schema with database, and wiring up a REST API for a vanilla node backend (or integrated with any framework), all based on a schema.json file that it will keep in sync.
This is a discrete package of tools to help with various data operations based on a single schema.json file.
Each feature can be used independently from the others.
npm i --save api-data-tools
Three tools you can use:
import http from 'http';
import { RestAPI } from 'api-data-tools';
let api = new RestAPI();
const apiServer = http.createServer((req, res) => {
api.handle(req, res);
});
apiServer.listen(8080);
import { DataMapper } from 'api-data-tools';
let data = { someData: "update" };
DataMapper.save('modelName', data);
(public interface incomplete, but works) The way to do this right now is by making these npm scripts, ie:
"schema": "node node_modules/api-data-tools/build/generateMigrations.js --config=config --schema-file=config/schema.json --migrations-dir=scripts/migrations",
"migrate": "npm run schema && db-migrate up --migrations-dir=scripts/migrations"
Then run:
npm run migrate
anytime you make a change to schema.json.
...and more to come (see todo.md)
Add a config directory to the project. Inside this, add a config file with some parameters for whichever featues you'd like to use.
At least specify the database connection and the folder where you'd like the migrations generated:
{
"database": {
"driver": "mysql",
"host": "127.0.0.1",
"user": "user",
"password": "password",
"database": "yourdatabase",
"multipleStatements": true
},
"migrationPath": "scripts/migrations",
}
You can also add options for the API generation:
"apiEnabled": true,
"autoRegisterRoutes": true,
Then, also add a schema.json file to this directory, defining your schema. See examples folder for example schemas. This uses the node-db-migrate property types, and the standard JSON sp ecification, ie:
{
"users": {
"properties": {
"id": {
"type": "int" ,
"primaryKey": true,
"autoIncrement": true
},
"name": "string",
"email": {
"type": "email",
"isIndex": true
},
"cityId": {
"type": "int",
"foreignKey": {
"name": "users_city_city_id_fk",
"table": "cities",
"rules": {
"onDelete": "CASCADE",
"onUpdate": "RESTRICT"
},
"mapping": "id"
}
},
"registered": "datetime",
"role": {
"enum": ["user", "superuser", "admin"]
},
},
"required": [
"name", "email", "registered", "role"
],
"api": {
"generate": true
}
}
}
Notice there is an additional "api" property to specify whether you want to automatically generate the API for a specific type, if that feature is used.
For reference: https://db-migrate.readthedocs.io/en/latest/API/SQL/ Data types: https://github.com/db-migrate/shared/blob/master/data_type.js
To automatically generate a REST API in your application, ie. vanilla node:
import http from 'http';
import { RestAPI } from 'api-data-tools';
let api = new RestAPI();
function requestHandler(req, res) {
api.handle(req, res);
}
const apiServer = http.createServer(requestHandler);
apiServer.listen(8080);
This will automatically create CRUD endpoints per your object schema defined in schema.json. For instance, if you have a user object, you will have a CRUD endpoint at which accepts a JSON body according to the schema:
GET,POST,PUT,DELETE http://localhost:8080/user/:id
Or within an Express-like application, just pass the API handler as a middleware, ie:
import * as express from 'express';
import { RestAPI } from 'api-data-tools';
let app = express();
let api = new RestAPI();
app.use(api.handler);
This module will allow you to serialize objects to the database according to the project's schema file.
Example usage:
import { DataMapper } from 'api-data-tools'
// Get an object of type user:
let user = await DataMapper.get('users', { id: 21 });
// Create or update an object of type user:
let user = await DataMapper.save('users', { ...userProperties });
This all works, but the commands need some aliases into the public interface. For now, you can work around it as described below.
You can copy the built scripts/generateMigrations.js file, or refer to it manually on your global or project-based node-modules/api-tools/build/generateMigrations.js.
Run this command to generate new migrations files from the current schema.json file. This command will make a backup file calleed '.curr.schema.json' to compare any new schema changes to:
"CONFIG_PATH=config/config.dev.json node scripts/generateMigrations.js",
"migrate": "db-migrate up --config=config/database.json --migrations-dir=scripts/migrations"
(Note: Currently only MySQL-based databases will work, but is extensible and easily adaptable to others)
db-migrate up --config=config/database.json --migrations-dir=scripts/migrations
FAQs
Bundle of utilities for generating migration files, syncing schema with database, and wiring up a REST API for a vanilla node backend (or integrated with any framework), all based on a schema.json file that it will keep in sync.
The npm package api-data-tools receives a total of 4 weekly downloads. As such, api-data-tools popularity was classified as not popular.
We found that api-data-tools demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
Security News
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.