forked from pubkey/rxdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
initializeDb.js
101 lines (84 loc) · 3.17 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import { addRxPlugin, createRxDatabase } from 'rxdb';
import { addPouchPlugin, getRxStoragePouch } from 'rxdb/plugins/pouchdb';
// import { getRxStorageMemory } from 'rxdb/plugins/memory';
import PouchdbAdapterMemory from 'pouchdb-adapter-memory';
import PouchdbAdapterHttp from 'pouchdb-adapter-http';
import PouchdbReplication from 'pouchdb-replication';
// import PouchdbAdapterAsync from 'pouchdb-adapter-asyncstorage';
import { RxDBDevModePlugin } from 'rxdb/plugins/dev-mode';
import { RxDBMigrationPlugin } from 'rxdb/plugins/migration'
import { RxDBUpdatePlugin } from 'rxdb/plugins/update'
import { RxDBQueryBuilderPlugin } from 'rxdb/plugins/query-builder'
import { RxDBReplicationCouchDBPlugin } from 'rxdb/plugins/replication-couchdb'
addPouchPlugin(PouchdbAdapterMemory);
addPouchPlugin(PouchdbAdapterHttp);
addPouchPlugin(PouchdbReplication);
// addPouchPlugin(PouchdbAdapterAsync);
addRxPlugin(RxDBMigrationPlugin)
addRxPlugin(RxDBUpdatePlugin)
addRxPlugin(RxDBQueryBuilderPlugin)
addRxPlugin(RxDBReplicationCouchDBPlugin)
import schema from './src/Schema';
const syncURL = 'http://admin:mysecret1@localhost:5984'; // Replace with you couchdb instance
const dbName = 'heroesreactdatabase1';
export const HeroesCollectionName = 'heroes';
const isDevelopment = process.env.NODE_ENV !== 'production' || process.env.DEBUG_PROD === 'true';
const initialize = async () => {
if (isDevelopment) {
await addRxPlugin(RxDBDevModePlugin);
}
let db;
try {
console.log('Initializing database...');
db = await createRxDatabase({
name: dbName,
// storage: getRxStorageMemory(), // RxStorageMemory
storage: getRxStoragePouch('memory'), // PouchDbAdapted
// storage: getRxStoragePouch('asyncstorage'), // PouchDbAdapted
multiInstance: false,
ignoreDuplicate: true,
});
console.log('Database initialized!');
} catch (err) {
console.log('ERROR CREATING DATABASE', err);
}
try {
console.log('Adding hero collection...');
await db.addCollections({
[HeroesCollectionName]: {
schema: schema,
},
});
console.log('Collection added!');
} catch (err) {
console.log('ERROR CREATING COLLECTION', err);
}
try {
console.log('Start sync...');
const rxReplicationState = db[HeroesCollectionName].syncCouchDB({
remote: `${syncURL}/${HeroesCollectionName}/`,
options: {
live: true,
retry: true,
},
waitForLeadership: false,
direction: {
push: true,
pull: true,
}
});
rxReplicationState.change$.subscribe((v) => {
console.log('Replication change$:', v)
})
rxReplicationState.complete$.subscribe((v) => {
console.log('Replication complete$:', v)
})
rxReplicationState.error$.subscribe(async error => {
console.error('Replication error$:',error)
})
} catch (err) {
console.log('Error initialize sync', err);
}
return db;
};
export default initialize;