UNPKG

sqlite3orm

Version:

ORM for sqlite3 and TypeScript/JavaScript

165 lines 5.25 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Field = void 0; const dbcatalog_1 = require("../dbcatalog"); const utils_1 = require("../utils"); const PropertyType_1 = require("./PropertyType"); const Schema_1 = require("./Schema"); /** * Class holding a field definition * * @export * @class Field */ class Field { /** * The name of the column */ name; /** * The quoted field name */ get quotedName() { return (0, utils_1.backtickQuoteSimpleIdentifier)(this.name); } _dbDefaultType; get dbDefaultType() { return this._dbDefaultType; } set dbDefaultType(dbType) { this._dbDefaultType = dbType; if (!this._dbtype) { this._dbTypeInfo = Field.parseDbType(this._dbDefaultType); } } /** * The type of the table column */ _dbtype; _dbTypeInfo; get dbtype() { return this._dbtype ? this._dbtype : this.dbDefaultType; } set dbtype(dbType) { this._dbtype = dbType; this._dbTypeInfo = Field.parseDbType(this._dbtype); } get isDbTypeDefined() { return this._dbtype ? true : false; } get dbTypeInfo() { return this._dbTypeInfo; } /** * If this property should be serialized/deserialized to the database as Json data */ _isJson; get isJson() { return this._isJson == undefined ? false : this._isJson; } set isJson(isJson) { this._isJson = isJson; } get isIsJsonDefined() { return this._isJson == undefined ? false : true; } _dateInMilliSeconds; get dateInMilliSeconds() { return this._dateInMilliSeconds == undefined ? (0, Schema_1.schema)().dateInMilliSeconds : this._dateInMilliSeconds; } set dateInMilliSeconds(val) { this._dateInMilliSeconds = val; } get isDateInMilliSecondsDefined() { return this._dateInMilliSeconds == undefined ? false : true; } /** * Flag if this field is part of the primary key */ isIdentity; /** * Creates an instance of Field. */ constructor(name, isIdentity, opts, propertyType) { this.name = name; this.isIdentity = !!isIdentity; this.setDbDefaultType(propertyType, opts); if (opts) { if (opts.dbtype) { this.dbtype = opts.dbtype; } if (opts.isJson != undefined) { this._isJson = opts.isJson; } if (opts.dateInMilliSeconds != undefined) { this._dateInMilliSeconds = opts.dateInMilliSeconds; } } } setDbDefaultType(propertyType, opts) { switch (propertyType) { case PropertyType_1.PropertyType.BOOLEAN: case PropertyType_1.PropertyType.DATE: if (opts && opts.notNull) { this.dbDefaultType = 'INTEGER NOT NULL'; } else { this.dbDefaultType = 'INTEGER'; } break; case PropertyType_1.PropertyType.NUMBER: if (this.isIdentity) { this.dbDefaultType = 'INTEGER NOT NULL'; } else { if (opts && opts.notNull) { this.dbDefaultType = 'REAL NOT NULL'; } else { this.dbDefaultType = 'REAL'; } } break; default: // otherwise 'TEXT' will be used as default if (opts && opts.notNull) { this.dbDefaultType = 'TEXT NOT NULL'; } else { this.dbDefaultType = 'TEXT'; } break; } } static parseDbType(dbtype) { const typeDefMatches = /^\s*((\w+)(\s*\(\s*\d+\s*(,\s*\d+\s*)?\))?)(.*)$/.exec(dbtype); /* istanbul ignore if */ if (!typeDefMatches) { throw new Error(`failed to parse '${dbtype}'`); } const typeAffinity = dbcatalog_1.DbCatalogDAO.getTypeAffinity(typeDefMatches[2]); const rest = typeDefMatches[5]; const notNull = /\bNOT\s+NULL\b/i.exec(rest) ? true : false; let defaultValue; const defaultNumberMatches = /\bDEFAULT\s+([+-]?\d+(\.\d*)?)/i.exec(rest); if (defaultNumberMatches) { defaultValue = defaultNumberMatches[1]; } const defaultLiteralMatches = /\bDEFAULT\s+(('[^']*')+)/i.exec(rest); if (defaultLiteralMatches) { defaultValue = defaultLiteralMatches[1]; defaultValue.replace(/''/g, "'"); } const defaultExprMatches = /\bDEFAULT\s*\(([^)]*)\)/i.exec(rest); if (defaultExprMatches) { defaultValue = defaultExprMatches[1]; } // debug(`dbtype='${dbtype}'`); // debug(`type='${typeName}'`); // debug(`notNull='${notNull}'`); // debug(`default='${defaultValue}'`); return { typeAffinity, notNull, defaultValue }; } } exports.Field = Field; //# sourceMappingURL=Field.js.map