-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathdatabase.service.ts
More file actions
executable file
·154 lines (129 loc) · 3.87 KB
/
database.service.ts
File metadata and controls
executable file
·154 lines (129 loc) · 3.87 KB
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import { Injectable, Injector, inject } from '@angular/core';
import { createReactivityFactory } from 'rxdb/plugins/reactivity-angular';
import type { AngularSignalReactivityLambda } from 'rxdb/plugins/reactivity-angular';
// import typings
import {
RxHeroDocument,
RxHeroesDatabase,
RxHeroesCollections
} from './../RxDB.d';
import {
environment
} from '../../environments/environment';
import {
createRxDatabase
} from 'rxdb/plugins/core';
import {
HERO_COLLECTION_NAME,
DATABASE_NAME
} from '../../shared';
import {
HERO_SCHEMA,
RxHeroDocumentType
} from '../schemas/hero.schema';
import { startSync } from './replication';
const collectionSettings = {
[HERO_COLLECTION_NAME]: {
schema: HERO_SCHEMA,
methods: {
hpPercent(this: RxHeroDocument): number {
return this.hp / 100 * 100;
}
},
sync: true
}
};
console.log('syncURL: ' + environment.rxdbSyncUrl);
function doSync(): boolean {
if (environment.isServerSideRendering) {
return false;
}
if (typeof window !== 'undefined' && window.location.hash == '#nosync') {
return false;
}
return true;
}
/**
* creates the database
*/
async function _create(): Promise<RxHeroesDatabase> {
environment.addRxDBPlugins();
console.log('DatabaseService: creating database..');
const db = await createRxDatabase<RxHeroesCollections, unknown, unknown, AngularSignalReactivityLambda>({
name: DATABASE_NAME,
storage: environment.getRxStorage(),
multiInstance: environment.multiInstance,
reactivity: createReactivityFactory(inject(Injector)),
ignoreDuplicate: !environment.production && environment.isServerSideRendering,
// password: 'myLongAndStupidPassword' // no password needed
}) as RxHeroesDatabase;
console.log('DatabaseService: created database');
if (!environment.isServerSideRendering) {
// write to window for debugging
(window as any)['db'] = db;
}
// show leadership in title
if (environment.multiInstance) {
db.waitForLeadership()
.then(() => {
console.log('isLeader now');
if (typeof document !== 'undefined') {
document.title = '♛ ' + document.title;
}
});
}
// create collections
console.log('DatabaseService: create collections');
await db.addCollections(collectionSettings);
// hooks
console.log('DatabaseService: add hooks');
db.collections.hero.preInsert(function (docObj: RxHeroDocumentType) {
const color = docObj.color;
return db.collections.hero
.findOne({
selector: {
color
}
})
.exec()
.then((has: RxHeroDocument | null) => {
if (has != null) {
alert('another hero already has the color ' + color);
throw new Error('color already there');
}
return db;
});
}, false);
// sync with server
if (doSync()) {
await startSync(
db,
environment
);
}
console.log('DatabaseService: created');
return db as any;
}
let initState: null | Promise<any> = null;;
let DB_INSTANCE: RxHeroesDatabase;
/**
* This is run via APP_INITIALIZER in app.module.ts
* to ensure the database exists before the angular-app starts up
*/
export async function initDatabase() {
/**
* When server side rendering is used,
* The database might already be there
*/
if (!initState) {
console.log('initDatabase()');
initState = _create().then(db => DB_INSTANCE = db);
}
await initState;
}
@Injectable()
export class DatabaseService {
get db(): RxHeroesDatabase {
return DB_INSTANCE;
}
}