react-native-flipper-databases
Version:
Flipper Databases plugin for React Native
82 lines (68 loc) • 2.67 kB
JavaScript
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.RealmDriver = void 0;
exports.getCellValue = getCellValue;
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
exports.RealmDriver = RealmDriver;
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) ?? {};
}
function getCellValue(row, columnName) {
const cellValue = row[columnName];
return typeof cellValue === 'object' ? JSON.stringify(cellValue, null, 2) : cellValue;
}
//# sourceMappingURL=realm.js.map
;