-
Notifications
You must be signed in to change notification settings - Fork 7
Fun with DataTypes
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.
- 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.
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.
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;
}
});
jSQLTable.name
jSQLTable.columns
jSQLTable.data
jSQLTable.colmap
jSQLTable.renameColumn
jSQLTable.addColumn
jSQLTable.loadData
jSQLTable.insertRow
jSQLQuery.ifNotExists
jSQLQuery.ignore
jSQLQuery.execute
jSQLQuery.fetch
jSQLQuery.fetchAll
jSQLQuery.values
jSQLQuery.set
jSQLQuery.where
jSQLQuery.from
jSQLQuery.limit
jSQLQuery.orderBy
jSQLQuery.asc
jSQLQuery.desc
jSQLQuery.distinct
jSQLWhereClause.where
jSQLWhereClause.equals
jSQLWhereClause.preparedLike
jSQLWhereClause.doesNotEqual
jSQLWhereClause.lessThan
jSQLWhereClause.contains
jSQLWhereClause.endsWith
jSQLWhereClause.beginsWith
jSQLWhereClause.and
jSQLWhereClause.or
jSQLWhereClause.limit
jSQLWhereClause.orderBy
jSQLWhereClause.asc
jSQLWhereClause.desc
jSQLWhereClause.execute