All files / myorm decorators.ts

92.85% Statements 65/70
89.65% Branches 52/58
100% Functions 13/13
92.75% Lines 64/69

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294              10x                                                                                                                   60x 60x   60x 1x           59x 15x                 59x 10x         59x                                             9x 9x                         1x                         54x 9x   45x 25x   20x 18x   2x 2x                                       14x 14x       14x 8x 8x   14x   14x           14x   14x     14x 2x     2x   3x                 2x   2x                 2x 2x         14x     12x   12x                       14x   14x       14x 1x     13x   13x     13x 13x         13x     13x     13x   60x 14x 14x 1x       13x   46x 9x 37x 4x   33x         12x 12x    
import { InvalidArgumentError, InvalidEntityError } from "@/utils/ErrorTypes";
import logger from "@/utils/logger";
import "reflect-metadata";
import BaseEntity from "./base-entity";
import * as SQLite from "expo-sqlite";
import { promisify } from "util";
 
const DB_NAME = "sk-vaccine-app.db";
 
export interface ColumnOptions {
  /** The name of the row in the db */
  name?: string;
  /**
   * The sql row type, either `tsType` or `type` must be set.
   *  `type` takes presedence over `tsType`
   */
  type?: string;
  /** Set to store a list of elements as a json*/
  isList?: boolean;
  /** Is it the primary key column */
  isPrimary?: boolean;
  /** Can the row hold the value of null, `false` by default */
  isNullable?: boolean;
  /** Must every value in this column be unique, `false` by default */
  isUnique?: boolean;
  /**
   * typescript type, either `tsType` or `type` must be set.
   * `type` takes presedence over `tsType`
   */
  tsType?: any;
}
 
export interface ColumnMetadata extends ColumnOptions {
  /** The attribute name */
  propertyKey: string;
}
 
export interface EntityConstructor extends Function {
  // constructor signature so type can be retrieved
  prototype: EntityPrototype;
}
 
export interface EntityPrototype {
  _columns?: ColumnMetadata[];
  _primaryKey?: string;
  _tableName?: string;
}
 
/**
 * Decorator for creating a column in the database.
 * @precondition This decorator must be used on a class that extends `BaseEntity`.
 *  The class must have a `Entity` on it and exactly one of the attributes must be set
 *  as a primary column.
 * @param options Properties of the row in the database see {@link ColumnOptions},
 *      `options.tsType` or `options.type` must be set
 * @returns Returns a decorator function that will be applied to an attribute.
 * @throws {InvalidEntityError} If the type of the attribute is not valid
 */
export function Column(options?: ColumnOptions) {
  /**
   * Returns a decorator function that will be applied to an attribute.
   * @param target The prototype of the class.
   * @param propertyKey The name of the property being decorated.
   * @throws {InvalidEntityError} If the type of the object is not valid
   */
  return function (target: any, propertyKey: string) {
    const prototype = target.constructor.prototype as EntityPrototype;
 
    if (!options?.tsType && !options?.type) {
      throw new InvalidEntityError(
        "options.tsType or options.type must be set"
      );
    }
 
    // initialize metadata storage on the prototype
    if (!prototype._columns) {
      prototype._columns = [] as ColumnMetadata[];
    }
 
    // get the type of the attribute
    // "design:type" is the type the attribute is declared as
    // DOES NOT WORK. SOMETHING IS WRONG WITH THE COMPILER
    // const designType = Reflect.getMetadata("design:type", target, propertyKey);
 
    // set primary key
    if (options?.isPrimary) {
      prototype._primaryKey = propertyKey;
    }
 
    // add a new metadata object into the "_columns" array
    // it stores data about the column
    prototype._columns.push({
      propertyKey: propertyKey,
      name: options?.name || propertyKey,
      type: options?.type || mapTsTypeToSql(options?.tsType, options?.isList),
      isUnique: options?.isUnique || false,
      isList: options?.isList || false,
      isPrimary: options?.isPrimary || false,
      isNullable: options?.isNullable || false,
    } as ColumnMetadata);
  };
}
 
/**
 * Decorator for marking a property as the primary key.
 * @precondition This decorator must be used on a class that extends `BaseEntity`.
 *  The class must have a `Entity` on it and there can only be one primary column.
 * @param options Properties of the row in the database see {@link ColumnOptions}.
 *  must define `options.tsType` or `options.type`.
 * @returns Returns a decorator function that will be applied to an attribute.
 */
export function PrimaryGeneratedColumn(
  options?: Omit<ColumnOptions, "isPrimary">
) {
  return function (target: any, propertyKey: string) {
    Column({ ...options, isPrimary: true, type: options?.type || "INTEGER" })(
      target,
      propertyKey
    );
  };
}
 
/**
 * Decorator for lists. The value is stored as a json but returned as a list.
 * @param options Properties of the row in the database see {@link ColumnOptions}
 * @returns Returns a decorator function that will be applied to an attribute.
 */
export function List(options?: Omit<ColumnOptions, "isList">) {
  return Column({ ...options, type: "TEXT", isList: true });
}
 
/**
 * Maps types to SQL types for the types:
 * String, Number, Boolean and Array, otherwise the value is stored as text
 *
 * @param type The type to map, must be String, Number, Boolean and Array
 * @param isList If the data is a list
 * @returns The sql type as a string
 * @throws {InvalidEntityError} if the type is invalid.
 */
export function mapTsTypeToSql(jsType: any, isList?: boolean): string {
  if (isList) {
    return "TEXT";
  }
  if (jsType === String) {
    return "TEXT";
  }
  if (jsType === Number) {
    return "REAL";
  }
  Eif (jsType === Boolean) {
    return "INTEGER";
  }
  if (jsType === Array) {
    return "TEXT";
  }
  throw new InvalidEntityError(`${jsType} is not a valid column type`);
}
 
/**
 * Class decorator that creates a table for the class.
 * @precondition This must be on a table that extends BaseEntity and have exactly one
 *  primary column
 * @param options.tableName The name of the table. Maximum 64 characters. Can contain
 *  capital and lower case letters, underscores, and `$`.
 * @param options.immutable If the columns change should the table be cleared amd
 *      rebuilt. There is currently no way to add database migrations to the table
 *      because it is not needed.
 * @throws {InvalidEntityError} If there is no primary column
 */
export function Entity(options?: { tableName?: string; immutable?: boolean }) {
  options = options || {};
  return function (constructor: Function) {
 
    let db: SQLite.SQLiteDatabase;
    
    if (BaseEntity.db == undefined) {
      BaseEntity.db = SQLite.openDatabaseSync(DB_NAME);
      logger.debug("Database initialized successfully:");
    }
    db = BaseEntity.db;
 
    logger.info(
      "Entity initialization starting, should run after db initialization unless this is a test"
    );
 
    let sql: string;
 
    const prototype = constructor.prototype;
    // store table name in the prototype (default is lowercase class name)
    prototype._tableName = options.tableName || constructor.name.toLowerCase();
 
    // if immutable remove table if the fields have changed
    if (options.immutable) {
      const result = db.getAllSync(
        `PRAGMA table_info('${prototype._tableName}');`
      );
      const tableColumns = result
        .map((col: any) => {
          return {
            name: col.name,
            type: col.type,
            isNullable: !col.notnull,
            primaryKey: col.pk,
          };
        })
        .sort();
 
      const entityColumns = prototype._columns
        ?.map((col: any) => {
          return {
            name: col.name,
            type: col.type,
            isNullable: col.isNullable,
            isPrimary: col.isPrimary,
          };
        })
        .sort();
 
      Eif (JSON.stringify(tableColumns) != JSON.stringify(entityColumns)) {
        db.execSync(`DROP TABLE IF EXISTS ${prototype._tableName};`);
      }
    }
    // generate sql table.
 
    sql = createTable(constructor.prototype);
    //logger.debug(sql);
 
    db.execSync(sql);
 
    logger.info(`created table for entity ${constructor.name}:`);
  };
}
 
/**
 * Creates a table for an entity if it does not exist.
 * @param prototype The metadata for the table. All of the attributes must
 *  be defined.
 * @returns An sql statement that builds a table
 * @throws {InvalidEntityError} If the prototype has undefined values
 */
function createTable(prototype: EntityPrototype): string {
  const tableName = prototype._tableName;
 
  Iif (tableName == undefined) {
    throw new InvalidEntityError(`No tablename defined in table: ${tableName}`);
  }
 
  if (prototype._columns == undefined || prototype._columns.length == 0) {
    throw new InvalidEntityError(`No columns defined in table: ${tableName}`);
  }
 
  const columns = prototype._columns;
 
  let sql = `CREATE TABLE IF NOT EXISTS ${tableName} (\n`;
 
  // create primary key column
  const pkColumn = columns.find((col: ColumnMetadata) => col.isPrimary);
  Iif (!pkColumn) {
    throw new InvalidEntityError(
      `No primary key defined in table: ${tableName}`
    );
  }
  sql += `  ${pkColumn.name} INTEGER PRIMARY KEY AUTOINCREMENT,\n`;
 
  // make sure there is only one primary key
  let primaryCount = 0;
 
  // add other columns
  columns.forEach((col: ColumnMetadata) => {
    // skip primary key (already added)
    if (col.isPrimary) {
      primaryCount++;
      if (primaryCount > 1) {
        throw new InvalidEntityError(
          `Multiple primary keys defined in table: ${tableName}`
        );
      }
      return;
    }
    if (col.isNullable) {
      sql += `  ${col.name} ${col.type} NULL,\n`;
    } else if (col.isUnique) {
      sql += `  ${col.name} ${col.type} UNIQUE, \n`;
    } else {
      sql += `  ${col.name} ${col.type} NOT NULL,\n`;
    }
  });
 
  // end the sql statement
  sql = sql.slice(0, -2) + "\n);";
  return sql;
}