Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
Realm.copyBundledRealmFiles = function () {
let sourceDir = path.join(__dirname, '../data');
let destinationDir = path.dirname(Realm.defaultPath);
for (let filename of fs.readdirSync(sourceDir)) {
let src = path.join(sourceDir, filename);
let dest = path.join(destinationDir, filename);
// If the destination file already exists, then don't overwrite it.
try {
fs.accessSync(dest);
continue;
} catch (e) { }
fs.writeFileSync(dest, fs.readFileSync(src));
}
};
function configSchema() {
console.log('初始化 ================> ')
// && DeviceInfo.isEmulator()
if (__DEV__ && Platform.OS === 'ios') {
Realm.defaultPath = '/Users/Shared/data/data.realm'
}
console.log('Realm.defaultPath:', Realm.defaultPath);
var next = Realm.schemaVersion(Realm.defaultPath);
if (next > 0) {
while (next < schemas.length) {
const migratedSchema = schemas[next++];
const migratedRealm = new Realm(migratedSchema);
migratedRealm.close();
}
}
const getNewSchemas = schemas[schemas.length - 1];
const realm = new Realm(getNewSchemas);
// realm.close();
}
testRealmSchemaVersion: function() {
TestCase.assertEqual(Realm.schemaVersion(Realm.defaultPath), -1);
let realm = new Realm({schema: []});
TestCase.assertEqual(realm.schemaVersion, 0);
TestCase.assertEqual(Realm.schemaVersion(Realm.defaultPath), 0);
realm = new Realm({schema: [], schemaVersion: 2, path: 'another.realm'});
TestCase.assertEqual(realm.schemaVersion, 2);
TestCase.assertEqual(Realm.schemaVersion('another.realm'), 2);
},
testDefaultPath: function() {
const defaultPath = Realm.defaultPath;
let defaultRealm = new Realm({schema: []});
TestCase.assertEqual(defaultRealm.path, Realm.defaultPath);
try {
const newPath = `${Realm.defaultPath.substring(0, defaultPath.lastIndexOf(pathSeparator) + 1)}default2.realm`;
Realm.defaultPath = newPath;
defaultRealm = new Realm({schema: []});
TestCase.assertEqual(defaultRealm.path, newPath, "should use updated default realm path");
TestCase.assertEqual(Realm.defaultPath, newPath, "defaultPath should have been updated");
} finally {
Realm.defaultPath = defaultPath;
}
},
exports.realmPathForFile = function(str) {
var path = Realm.defaultPath;
return path.substring(0, path.lastIndexOf("/") + 1) + str;
};
function createRealmAccess(path = Realm.defaultPath) {
let __realm = null;
return async function accessRealm() {
if (!__realm) {
try {
__realm = await Realm.open({
schema: [ITEM_SCHEMA],
path
});
} catch (error) {
throw error;
}
}
return __realm;
};
}
function configureRealm() {
var schema = new Schema();
var next = Realm.schemaVersion(Realm.defaultPath);
if (next > 0) {
while (next < schema.schemas.length) {
var migratedSchema = schema.schemas[next++];
var migratedRealm = new Realm(migratedSchema);
migratedRealm.close();
}
}
var current = schema.current();
var realm = new Realm(current);
realm.close();
}