als-mongoose-form

1.0.0 • Public • Published

als-mongoose-form

What is als-mongoose-form?

als-mongoose-form is a lightweight utility designed to transform Mongoose schemas into JSON data structures suitable for generating forms. It automates the process of creating form elements by analyzing the schema definition, including properties like types, defaults, and constraints.

Key Features

  • Converts Mongoose schema definitions to JSON form descriptors.
  • Supports field types (String, Number, Boolean, Date, etc.) and additional Mongoose options (e.g., enum, default, min, max).
  • Handles array fields, subdocuments, and references (ref).
  • Automatically includes timestamps and _id fields based on schema options.

Installation

Install the library via npm:

npm install als-mongoose-form

Basic Usage

Here’s how to use the library to generate form data from a Mongoose schema:

const mongoose = require('mongoose');
const SchemaToForm = require('als-mongoose-form');
// Or
const {schemaToForm} = require('als-mongoose-form');

// Define a Mongoose schema
const userSchema = new mongoose.Schema({
    name: { type: String, required: true, label: 'Full Name' },
    age: { type: Number, min: 0, max: 120, label: 'Age' },
    gender: { type: String, enum: ['Male', 'Female', 'Other'], label: 'Gender' },
    email: { type: String, required: true, label: 'Email', match: /^[^@\s]+@[^@\s]+\.[^@\s]+$/ },
    isAdmin: { type: Boolean, default: false, label: 'Administrator' },
    address: new mongoose.Schema({
      street: { type: String, label: 'Street' },
      city: { type: String, label: 'City' },
      zipCode: { type: String, label: 'Zip Code', match: /^\d{5}$/ },
    },{_id:false,title:'Address'}),
    hobbies: [{ type: String, label: 'Hobbies' }], // Array of strings
    tasks: [ // Array of objects
      new mongoose.Schema({
          title: { type: String, required: true, label: 'Task Title' },
          completed: { type: Boolean, default: false, label: 'Completed' },
      })
    ], 
}, {
    timestamps: true,
    title: 'User Form',
    description: 'Form to manage user details',
});


// Convert schema to form descriptor
const form = new SchemaToForm(userSchema).result;
// Or
const form = schemaToForm(userSchema);

Output Example

{
   "title": "User Form",
   "description": "Form to manage user details",
   "items": [
      {
         "tag": "input",
         "type": "hidden",
         "key": "id"
      },
      {
         "tag": "text",
         "key": "createdAt"
      },
      {
         "tag": "text",
         "key": "updatedAt"
      },
      {
         "key": "name",
         "tag": "input",
         "type": "text",
         "required": true,
         "label": "Full Name"
      },
      {
         "key": "age",
         "tag": "input",
         "type": "number",
         "min": 0,
         "max": 120,
         "label": "Age"
      },
      {
         "key": "gender",
         "tag": "select",
         "type": "text",
         "options": [
            {
               "value": "Male",
               "text": "Male"
            },
            {
               "value": "Female",
               "text": "Female"
            },
            {
               "value": "Other",
               "text": "Other"
            }
         ],
         "label": "Gender"
      },
      {
         "key": "email",
         "tag": "input",
         "type": "text",
         "required": true,
         "label": "Email",
         "pattern": "^[^@[ \\t\\r\\n\\f]]+@[^@[ \\t\\r\\n\\f]]+\\.[^@[ \\t\\r\\n\\f]]+$"
      },
      {
         "key": "isAdmin",
         "tag": "input",
         "type": "checkbox",
         "checked": false,
         "label": "Administrator"
      },
      {
         "key": "address",
         "tag": "input",
         "form": {
            "title": "Address",
            "items": [
               {
                  "key": "street",
                  "tag": "input",
                  "type": "text",
                  "label": "Street"
               },
               {
                  "key": "city",
                  "tag": "input",
                  "type": "text",
                  "label": "City"
               },
               {
                  "key": "zipCode",
                  "tag": "input",
                  "type": "text",
                  "label": "Zip Code",
                  "pattern": "^[0-9]{5}$"
               }
            ]
         }
      },
      {
         "key": "hobbies",
         "tag": "input",
         "isArray": true,
         "type": "text",
         "label": "Hobbies"
      },
      {
         "key": "tasks",
         "tag": "input",
         "isArray": true,
         "form": {
            "items": [
               {
                  "tag": "input",
                  "type": "hidden",
                  "key": "id"
               },
               {
                  "key": "title",
                  "tag": "input",
                  "type": "text",
                  "required": true,
                  "label": "Task Title"
               },
               {
                  "key": "completed",
                  "tag": "input",
                  "type": "checkbox",
                  "checked": false,
                  "label": "Completed"
               }
            ]
         }
      }
   ]
}

Package Sidebar

Install

npm i als-mongoose-form

Weekly Downloads

9

Version

1.0.0

License

ISC

Unpacked Size

14.8 kB

Total Files

6

Last publish

Collaborators

  • alexsorkin