Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
_setUpSchema(): void {
logger.log('[DB][Worker] Setting up schema')
// Add collections
values(this.schema.tables).forEach(tableSchema => {
this._addCollection(tableSchema)
})
this.loki.addCollection('local_storage', {
unique: ['key'],
indices: [],
disableMeta: true,
})
// Set database version
this._databaseVersion = this.schema.version
logger.log('[DB][Worker] Database collections set up')
}
// This is called with `{}` when making a new record, so we need to set a new ID, status
// Also: If an existing has one of those fields broken, we're screwed. Safest to treat it as a
// new record (so that it gets synced)
const raw = {}
if (typeof id === 'string') {
raw.id = id
raw._status = isValidStatus(_status) ? _status : 'created'
raw._changed = typeof _changed === 'string' ? _changed : ''
} else {
raw.id = randomId()
raw._status = 'created'
raw._changed = ''
}
values(tableSchema.columns).forEach(columnSchema => {
const key = (columnSchema.name: string)
const value = dirtyRaw[key]
_setRaw(raw, key, value, columnSchema)
})
return raw
}
_unsafeClearCaches(): void {
values(this.collections.map).forEach(collection => {
collection.unsafeClearCache()
})
}
export default function encodeInsert(model: Model): SQLiteQuery {
const { _raw: raw, table } = model
const sql = `insert into ${table} (${columnNames(raw)}) values (${valuePlaceholders(raw)})`
const args = values(raw)
return [sql, args]
}
export const encodeSchema: AppSchema => SQL = ({ tables }) =>
values(tables)
.map(encodeTable)
.join('')
const recordStatesEqual = (left: RecordState, right: RecordState): boolean =>
identicalArrays(values(left), values(right))