npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

node-data-editor

v0.0.18

Published

a simple data editor for node, based on json schema with a customziable data adapter structure.

Downloads

5

Readme

node-data-editor

A customizable data editor for node, using JSON schema. By using custom Data Adapters, it can be connetced to a REST API or any other data provider.

interface

Demo: https://repl.it/@lutzer/Node-Data-Editor-Example

Install

npm install node-data-editor

Usage

Import

import * as DataEditor from 'node-data-editor'
// or
const DataEditor = require('node-data-editor')

Basic example

const DataEditor = require('node-data-editor')

const initialData = [
  {
    id: '1',
    name: 'Peter',
    age: 20,
    siblings: ['Maria', 'Lea']
  }
]

DataEditor.start({
  models: [new DataEditor.DataModel({
    schema: {
      $id: 'Persons',
      properties: {
        id: { type: 'string', minLength: 1 },
        name: { type: 'string', minLength: 3 },
        address: { type: 'string', maxLength: 256 },
        siblings: { type: 'array' },
        married: { type: 'boolean', default: false },
        age: { type: 'number', default: 0 },
      },
      primaryKey: 'id',
      required: ['id'],
      titleTemplate: '<%= name %>:<%= age %>'
    },
    adapter: new DataEditor.MemoryAdapter(initialData, 'id')
  })],
  port: 3000
}).then((server) => { console.log('Editor is available on localhost:3000') })

Authentification

Adding a credential object to the editor will password protect its data and editing functions

DataEditor.start({
  models: [...],
  port: 3000,
  credentials: {
    login: 'admin',
    password: 'password'
  }
}).then()

Schema Description

The Schma definitions follow JSON Schema. It needs to contain a primaryKey property of type string, itentified by the 'primaryKey' field. The Editor currently does not support nested properties, just the base Datatypes. It will validate nested properties though.

// type DataType = 'string' | 'number' | 'boolean' | 'object' | 'array' | 'null'
{
  $id: string,
  properties: { [key : string] : {
    type : DataType
    readonly? : boolean
    default? : any
  }}
  primaryKey: string,
  required? : string[],
  titleTemplate? : string,
  links? : { model : string, key : string, foreignKey : string }[]
}

Schema Example

var schema = {
  "$id": "Book",
  "properties": {
    "id": {
      "type": "string"
    },
    "author": {
      "type": "string",
      "default" : "John Doe"
    },
    "title": {
      "type": "string",
      "default" : "untitled"
    }
  },
  "primaryKey": "id",
  "required": [ "title" ]
}

Custom Title for data entry

By defining titleTemplate a custom title is displayed for each data entry in the data model using lodash templates.

// example using titleTemplate. can be of style <%- name %> or ${name}
var schema = {
  $id: 'projects',
  properties: {
    id : { type: 'string' },
    name : { type: 'string' }
  },
  primaryKey: 'id',
  required : ['name', 'description', 'password'],
  titleTemplate: '${name}:${id}'
}

Linking Models

By defining a link property in the schema, an entry can be linked to other models by specifing a key and foreign Key to match entries:

var schema1 = {
  $id : 'foo',
  properties: {
    id: { type: 'string' },
    text: { type : 'string' }
  },
  primaryKey : 'id',
  links : [ { model: 'bar', key: 'id', foreignKey: 'fooId' } ]
}
var schema2 = {
  $id : 'bar',
  properties: {
    id: { type: 'string' },
    fooId : { type : 'string' },
    text: { type : 'string' }
  },
  primaryKey : 'id',
}

Data Adapter

The connection to a dataset/database works through the Adapter Interface. There are two implementations:

RestAdapter

// connects to a standard REST api, using these endpoints: GET '/', GET '/:id', PUT '/:id', POST '/', DELETE '/:id'. The Options object is passed to the axios http calls.
const adapter = new RestAdapter(apiAddress, options)

MemoryAdapter

// saves data in an simple array. does not persist data. first argument is the initial data array, second argument is the primary Key of the entries.
const adapter = new MemoryAdapter([], 'id')

Custom Adapter

The custom adapter needs to implement 5 Methods. See src/adapter.ts MemoryAdapter or RestAdapter for example implementations. 'id' is the defined primaryKey of the schema.

class CustomAdapter implements DataEditor.Adapter {
  list(schema: DataSchema): Promise<any[]> {
    // list all data entries of this resource
  }
  read(id: string, schema: DataSchema): Promise<any|undefined> {
    // list one entry with the specified id
  }
  update(id: string, data: any, schema: DataSchema): Promise<any> {
    // updates a single entry, specified by id
  }
  delete(id: string, schema: DataSchema): Promise<void> {
    // deletes the specified entry
  }
  create(data: any, schema: DataSchema): Promise<any> {
    // creates a new entry
  }
}

Development

  • run npm install
  • run npm run dev to start test server
  • run npm run build to start a full production build

Unit Tests

  • run npm run test

Frontend Development

  • see /frontend/ dir
  • run cd frontend; npm start to run frontend dev server

Troubleshooting

  • node-sass currently requires a node version < 15