Learning Center

Learning Center

Learn Menu

JavaScript Engine

This engine conforms to the ESLint standard style guide except for semicolons. Since the code is generated, we feel it's safer to include the semicolons.

Data Types

Standard

Design JavaScript Default Value
bool boolean false
int number 0
int8 number 0
int16 number 0
int32 number 0
int64 number 0
uint number 0
uint8 number 0
uint16 number 0
uint32 number 0
uint64 number 0
float number 0
double number 0
string string ''
char string ''
byte number 0
bytea Uint8Array null
date Date null
datetime Date null

Complex

Name Default Value
Array []
Object (Map) {}

Config

There are currently no javascript config options available for this engine. It's likely to change in the future based on user feedback.

ESLint Config

If you're just starting a new project and looking for a compatible .eslintrc.js config, you can use the one below.

  module.exports = {
  env: {
    browser: true,
    es2021: true
  },
  extends: [
    'standard'
  ],
  parser: '@typescript-eslint/parser',
  parserOptions: {
    ecmaVersion: 'latest',
    sourceType: 'module'
  },
  plugins: [
    '@typescript-eslint'
  ]
}

Package.json dependencies

You'll also need to add the following devDependencies to your package.json file. Just make sure to update the versions as they may be outdated by the time you read this.

  "devDependencies": {
    "eslint": "^8.16.0",
    "eslint-config-standard": "^17.0.0",
    "eslint-plugin-import": "^2.26.0",
    "eslint-plugin-n": "^15.2.0",
    "eslint-plugin-promise": "^6.0.0"
  }

Enum

Since JavaScript doesn't support true enums, we decided to define an enum as a frozen object. See the example below.

  export const Color = Object.freeze({
  GREEN: 'Green',
  BLUE: 'Blue',
  BROWN: 'Brown'
});

const myEyeColor = Color.BROWN;
console.log(myEyeColor); // prints out 'Brown'

Class

Every class is generated with a param-less constructor that initializes all the properties. See an example User class below.

  export class User {
  constructor () {
    this.email = '';
    this.password = '';
    this.age = 0;
  }
}

Serialization

It may seem that JavaScript serialization to/from JSON is perfect out of the box, but it gets tricky when you can't just work with plain objects returned by your API.

That's why OneGen generates serialization (toPlain) and deserialization (fromPlain) methods for every class.

Let's consider the following example. We have two models, User and Company (which is referenced from the User model). Take a look at the generated serialization methods and how they work.

  export class Company {
  constructor () {
    this.name = '';
  }

  static fromPlain (plain) {
    const company = new Company();
    company.name = plain.name;
    return company;
  }

  toPlain () {
    const plain = {};
    plain.name = this.name;
    return plain;
  }
}

export class User {
  constructor () {
    this.email = '';
    this.password = '';
    this.isSubscribed = false;
    this.created = null;
    this.company = null;
  }

  static fromPlain (plain) {
    const user = new User();
    user.email = plain.email;
    user.password = plain.password;
    user.isSubscribed = plain.isSubscribed;
    user.created = plain.created ? new Date(plain.created) : null;
    user.company = plain.company ? Company.fromPlain(plain.company) : null;
    return user;
  }

  toPlain () {
    const plain = {};
    plain.email = this.email;
    plain.password = this.password;
    plain.isSubscribed = this.isSubscribed;
    plain.created = this.created ? this.created.toISOString() : null;
    plain.company = this.company ? this.company.toPlain() : null;
    return plain;
  }
}

fromPlain (deserialize)

This static method takes a plain JavaScript object and returns a new instance of the associated model. It's very easy to use, let's say we want to create a User object from a mocked API response.

  // Imagine the following object is returned from your API
const userAPIResponse = {
  email: 'joe@doe.com',
  password: '',
  isSubscribed: true,
  created: '2022-05-30T21:26:49+00:00',
  company: { name: 'Friendly Corp' } // Notice the plain Company object here
};
const user = User.fromPlain(userAPIResponse);
console.log(user.email); // prints out 'joe@doe.com'
console.log(user.company); // it's an instance of Company now
console.log(user.created); // it's an instance of Date now

toPlain (serialize)

This instance method doesn't accept any parameters and returns a new plain javascript object. Let's use the previous example to serialize the User instance into a new plain object.

  const newPlainUser = user.toPlain();
console.log(newPlainUser.company); // it's a plain object now
console.log(newPlainUser.created); // it's an ISO 8601 string now.

Copy/Clone

If you've already noticed, congratulations! Because our serialization methods safely copy data from/to plain objects, we can actually use them to create a deep clone/copy of any model. Check out the example below.

  let user = new User();
user.email = 'john@doe.com';
user.company = Company.fromPlain({ name: 'Cat Cafe, LLC.' });

let clone = User.fromPlain(user.toPlain());
clone.company.name = 'Dog Cafe, LLC.'; // we can just rename the company and leave the rest
console.log(user.company.name); // prints out 'Cat Cafe, LLC.'
console.log(clone.company.name); // prints out 'Dog Cafe, LLC.'

Date format

We expect plain objects to have the date in the ISO 8601 string format. For instance, January 5th, 2022 at 11:45 am would be 2022-01-05T11:45:00.000Z

Nested Entities

As you know, OneGen supports both nested classes and enums in the design module. Unfortunately, JavaScript doesn't support nested classes (or enums), so we had to work around it to generate models you can still comfortably work with.

We basically pluck out the nested entities to the file level and prefix their names. It's best to see it in action - imagine we have a class Person and a nested enum Pet which can be either a dog or a cat.

  export const PersonPet = Object.freeze({
  DOG: 'dog',
  CAT: 'cat'
});

export class Person {
  // ...
}

We use cookies to track activity using Google Analytics & reCAPTCHA. It helps us understand what our visitors like about our product and how they interact with our website. For more information, check out our Privacy Policy.