Skip to content

Fun with DataTypes

Pamblam edited this page Dec 11, 2016 · 4 revisions

Javascript Query Language supports a handful of built-in data types for all sorts of data, strings, numbers, dates, even executable functions. The default dataType, for tables that do not specify datatypes for their columns, is "AMBI", which stores strings, numbers, functions and Date objects, anything else is converted to a string.

jSQL does provide an API for adding your own data type definitions, which is useful for storing instances of your own custom objects.

Back to Top


Supported Types

  • NUMERIC
    • Aliases: NUMBER, DECIMAL, FLOAT
    • Stores types as numbers
    • Will store 0 if a non number is stored.
    • The current version of jSQL ignores all arguments for this data type.
  • FUNCTION
    • Stores functions.
    • Will throw an error is non functions are stored.
  • BOOLEAN
    • Alias: BOOL
    • Store boolean values
  • INT
    • Store an integer
    • The current version of jSQL ignores all arguments for this data type.
  • CHAR
    • Aliases: VARCHAR, LONGTEXT, MEDIUMTEXT
    • Stores a string
    • The current version of jSQL ignores all arguments for this data type.
  • DATE
    • Stores a Javascript Date object
    • The current version of jSQL ignores all arguments for this data type.
  • AMBI
    • Stores functions and JS Date objects, everything else is converted to a string.

Back to Top


Creating custom DataTypes

Custom datatypes are useful for storing instances of custom objects, or otherwise filtering your data in your columns.

To create a new data type, use the jSQL.types.add() method to add a new dataType. It takes an object with 4 properties:

  • type: The datatype name
  • aliases: An array of aliases (alternative names for this datatype)
  • serialize: A function that receives the value to be stored, and an array containing any arguments passed into the table definition. This function must return a string serialization of the object.
  • unserialize: A function that takes the serialized value to be returned and any arguments passed from the table definition and should return the unserialized version of the stored object.

When using custom data types, it is important to defin them before the database is loaded into memory (before jSQL.load() is called), so that any items stored in memory can be properly interpreted by the API.

Example
jSQL.types.add({
	type: "ENUM",
	serialize: function(value, args){
    	if(args.indexOf(value) < -1) value = args[0];
	    return value;
	},
	unserialize: function(value, args){
		return value;
	}
});

Back to Top


Clone this wiki locally