back

by freakynit·1y ago·view on hn ↗
(1.) Since this is a javascript environment, the types are pretty minimal and just one of each top level type (unlike languages like java or C, which, for example, clould have int, float, double, etc. for just one `number` top-level type). These, here, in javascript, are: number, string, boolean, object, null/undefined.

(2.) Then I use `typeof` operator for each field to determine appropriate javascript type.

(3.) Then, I map these javascript types to sqlite types using this mapping:::

const typeMapping = { 'number': 'REAL', 'string': 'TEXT', 'boolean': 'INTEGER', 'object': 'TEXT', 'undefined': 'TEXT' };

(4.) Then, I generate create table sql's fields declaration part. This also handles an edge case where if first 1000 rows (used for schema generation) of a particular field are all null/undefined, I default to using sqlite's TEXT type using this:::

const columns = fields.map(field => `'${field}' ${typeMapping[columnTypes[field]] || 'TEXT'}`);

(5.) Finally, I create sqlite table:::

const createTableSQL = `CREATE TABLE data (${columns.join(', ')});`;

db = new SQL.Database();

db.run(createTableSQL);

Thanks..

1 comments
Nice explainer, thanks!
Welcome :)