src/Adapters/Storage/Postgres/PostgresStorageAdapter.js
// Just create a table, do not insert in schema
Just create a table, do not insert in schema
Just create a table do not insert in schema
createTable(className, schema) {
    debug('createTable', className, schema);
    let valuesArray = [];
    let patternsArray = [];
    let fields = Object.assign({}, schema.fields);
    if (className === '_User') {
      fields._email_verify_token_expires_at = {type: 'Date'};
      fields._email_verify_token = {type: 'String'};
      fields._account_lockout_expires_at = {type: 'Date'};
      fields._failed_login_count = {type: 'Number'};
      fields._perishable_token = {type: 'String'};
    }
    let index = 2;
    let relations = [];
    Object.keys(fields).forEach((fieldName) => {
      let parseType = fields[fieldName];
      if (parseType.type === 'Relation') {
        relations.push(fieldName)
        return;
      }
      if (['_rperm', '_wperm'].indexOf(fieldName) >= 0) {
        parseType.contents = { type: 'String' };
      }
      valuesArray.push(fieldName);
      valuesArray.push(parseTypeToPostgresType(parseType));
      patternsArray.push(`$${index}:name $${index+1}:raw`);
      if (fieldName === 'objectId') {
        patternsArray.push(`PRIMARY KEY ($${index}:name)`)
      }
      index = index+2;
    });
    const qs = `CREATE TABLE $1:name (${patternsArray.join(',')})`;
    const values = [className, ...valuesArray];
    return this._ensureSchemaCollectionExists()
    .then(() => this._client.none(qs, values))
    .catch(error => {
      if (error.code === PostgresDuplicateRelationError) {
      } else {
        throw error;
      }
    }).then(() => {
      return Promise.all(relations.map((fieldName) => {
        return this._client.none('CREATE TABLE IF NOT EXISTS $<joinTable:name> ("relatedId" varChar(120), "owningId" varChar(120), PRIMARY KEY("relatedId", "owningId") )', {joinTable: `_Join:${fieldName}:${className}`});
      }));
    });
  }
createTable ( className , schema ) { debug ( 'createTable' , className , schema ) ; let valuesArray = [ ] ; let patternsArray = [ ] ; let fields = Object . assign ( { } , schema . fields ) ; if ( className === '_User' ) { fields . _email_verify_token_expires_at = { type : 'Date' } ; fields . _email_verify_token = { type : 'String' } ; fields . _account_lockout_expires_at = { type : 'Date' } ; fields . _failed_login_count = { type : 'Number' } ; fields . _perishable_token = { type : 'String' } ; } let index = 2 ; let relations = [ ] ; Object . keys ( fields ) . forEach ( ( fieldName ) => { let parseType = fields [ fieldName ] ; if ( parseType . type === 'Relation' ) { relations . push ( fieldName ) return ; } if ( [ '_rperm' , '_wperm' ] . indexOf ( fieldName ) >= 0 ) { parseType . contents = { type : 'String' } ; } valuesArray . push ( fieldName ) ; valuesArray . push ( parseTypeToPostgresType ( parseType ) ) ; patternsArray . push ( ` ${ index } ${ index + 1 } ` ) ; if ( fieldName === 'objectId' ) { patternsArray . push ( ` ${ index } ` ) } index = index + 2 ; } ) ; const qs = ` ${ patternsArray . join ( ',' ) } ` ; const values = [ className , ... valuesArray ] ; return this . _ensureSchemaCollectionExists ( ) . then ( ( ) => this . _client . none ( qs , values ) ) . catch ( error => { if ( error . code === PostgresDuplicateRelationError ) { } else { throw error ; } } ) . then ( ( ) => return Promise . all ( relations . map ( ( fieldName ) => return this . _client . none ( 'CREATE TABLE IF NOT EXISTS $<joinTable:name> ("relatedId" varChar(120), "owningId" varChar(120), PRIMARY KEY("relatedId", "owningId") )' , { joinTable : ` ${ fieldName } ${ className } ` } ) ; } ) ) ; } ) ; }
Just create a table, do not insert in schema
Just create a table do not insert in schema
// Skip when it's a relation // We'll create the tables later // Table already exists, must have been created by a different request. Ignore error. // Create the relation tables