forked from pubkey/rxdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
initializeDb.js
42 lines (39 loc) · 1.18 KB
/
initializeDb.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import schema from './src/Schema';
import { createRxDatabase, getRxStoragePouch } from 'rxdb';
const syncURL = 'http://localhost:10102/'; // Replace localhost with a public ip address!
const dbName = 'heroesreactdatabase1';
const initialize = async () => {
let db;
try {
console.log('Initializing database...');
db = await createRxDatabase({
name: dbName,
storage: getRxStoragePouch('asyncstorage'),
password: 'myLongAndStupidPassword',
multiInstance: false,
ignoreDuplicate: true,
});
console.log('Database initialized!');
} catch (err) {
console.log('ERROR CREATING DATABASE', err);
}
console.log('Adding hero collection...');
try {
const heroCollection = await db.addCollections({
heroes: {
schema: schema,
},
});
heroCollection.sync({
remote: syncURL + dbName + '/',
options: {
live: true,
retry: true,
},
});
} catch (err) {
console.log('ERROR CREATING COLLECTION', err);
}
return db;
};
export default initialize;