UNPKG

react-native-flipper-databases

Version:

Flipper Databases plugin for React Native

71 lines (61 loc) 2.5 kB
export class RealmDriver { constructor(name, realm) { this.name = name; this.realm = realm; } async getDatabases() { return [{ name: this.name }]; } async getTableNames(_databaseDescriptor) { // Return all models excluding embedded ones return this.realm.schema.filter(schema => !schema.embedded).map(schema => schema.name); } async getTableStructure(_databaseDescriptor, schema) { const schemaProps = getSchemaProperties(this.realm, schema); const schemaColumns = Object.keys(schemaProps); const columnsDef = schemaColumns.map(k => [k, schemaProps[k].type, schemaProps[k].default, schemaProps[k].indexed, schemaProps[k].optional, schemaProps[k].mapTo]); return { structureColumns: ['name', 'type', 'defaultValue', 'isIndexed', 'isOptional', 'mapTo'], structureValues: columnsDef, indexesColumns: ['name', 'type'], indexesValues: schemaColumns.filter(k => schemaProps[k].indexed).map(k => [k, schemaProps[k].type]) }; } async getTableData(_databaseDescriptor, schema, order, reverse, start, count) { const schemaProperties = getSchemaProperties(this.realm, schema); const allColumns = Object.keys(schemaProperties); const dataSnapshot = this.realm.objects(schema).snapshot(); const sorted = order ? dataSnapshot.sorted(order, reverse) : dataSnapshot; const data = sorted.slice(start, start + count); return { columns: allColumns, values: data.map(row => { const rowJSON = row.toJSON(); return allColumns.map(colName => getCellValue(rowJSON, colName)); }), start, count: data.length, total: dataSnapshot.length }; } async getTableInfo(_databaseDescriptor, table) { const tableSchema = this.realm.schema.find(schema => schema.name === table); return { definition: JSON.stringify(tableSchema, null, 2) }; } async executeSql(_databaseDescriptor, _query) { return Promise.reject('Unsupported method'); } } // UTILITY FUNCTIONS function getSchemaProperties(realm, schemaName) { var _realm$schema$find; return ((_realm$schema$find = realm.schema.find(schema => schema.name === schemaName)) === null || _realm$schema$find === void 0 ? void 0 : _realm$schema$find.properties) ?? {}; } export function getCellValue(row, columnName) { const cellValue = row[columnName]; return typeof cellValue === 'object' ? JSON.stringify(cellValue, null, 2) : cellValue; } //# sourceMappingURL=realm.js.map