Skip to content

Commit 5976c90

Browse files
committed
fix saving into db
1 parent b797efa commit 5976c90

5 files changed

Lines changed: 53 additions & 11 deletions

File tree

‎apps/server/src/modules/tldraw/entities/tldraw-drawing.entity.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export class TldrawDrawing {
3030
version: string;
3131

3232
@Property({ nullable: false })
33-
value: string;
33+
value: Buffer;
3434

3535
@Property({ nullable: true })
3636
clock?: number;
@@ -48,6 +48,6 @@ export interface TldrawDrawingProps {
4848
version: string;
4949
clock?: number;
5050
action?: string;
51-
value: string;
51+
value: Buffer;
5252
part?: number;
5353
}

‎apps/server/src/modules/tldraw/factory/tldraw.factory.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export const tldrawEntityFactory = BaseFactory.define<TldrawDrawing, TldrawDrawi
77
return {
88
_id: 'test-id',
99
docName: 'test-name',
10-
value: 'test-value',
10+
value: Buffer.from('test'),
1111
version: `test-version-${sequence}`,
1212
};
1313
}

‎apps/server/src/modules/tldraw/repo/mongodb-adapter.ts‎

Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,49 @@ export class MongodbAdapter {
1616
this.collectionName = this.configService.get<string>('TLDRAW_DB_COLLECTION_NAME') ?? 'drawings';
1717
}
1818

19+
/**
20+
* Apply a $query and get one document from MongoDB.
21+
* @param {object} query
22+
* @returns {Promise<TldrawDrawing | null>}
23+
*/
1924
get(query: object): Promise<TldrawDrawing | null> {
2025
const collection = this.getCollection();
2126
return collection.findOne<TldrawDrawing>(query, { allowDiskUse: true });
2227
}
2328

24-
async put(query: object, values: object) {
29+
/**
30+
* Store one document in MongoDB.
31+
* @param {object} query
32+
* @param {object} values
33+
* @returns {Promise<object>} Stored document
34+
*/
35+
async put(query: object, values: object): Promise<TldrawDrawing | null> {
2536
const collection = this.getCollection();
2637
await collection.updateOne(query, { $set: values }, { upsert: true });
38+
return this.get(query);
2739
}
2840

29-
async del(query: object) {
41+
/**
42+
* Removes all documents that fit the $query
43+
* @param {object} query
44+
* @returns {Promise<object>} Contains status of the operation
45+
*/
46+
del(query: object): Promise<object> {
3047
const collection = this.getCollection();
31-
await collection.deleteMany(query);
48+
const bulk = collection.initializeOrderedBulkOp();
49+
bulk.find(query).delete();
50+
return bulk.execute();
3251
}
3352

34-
readAsCursor(query: object, opts: object = {}) {
53+
/**
54+
* Get all or at least $opts.limit documents that fit the $query.
55+
* @param {object} query
56+
* @param {object} [opts]
57+
* @param {number} [opts.limit]
58+
* @param {boolean} [opts.reverse]
59+
* @returns {Promise<Array<object>>}
60+
*/
61+
readAsCursor(query: object, opts: object = {}): Promise<TldrawDrawing[]> {
3562
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
3663
// @ts-ignore
3764
const { limit = 0, reverse = false } = opts;
@@ -44,6 +71,21 @@ export class MongodbAdapter {
4471
return curs.toArray();
4572
}
4673

74+
/**
75+
* Close connection to MongoDB instance.
76+
*/
77+
async close() {
78+
await this.orm.close();
79+
}
80+
81+
/**
82+
* Delete database
83+
*/
84+
async flush() {
85+
await this.getCollection().drop();
86+
await this.close();
87+
}
88+
4789
getCollection() {
4890
return this.em.getCollection(this.collectionName);
4991
}

‎apps/server/src/modules/tldraw/repo/utils.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,10 +97,10 @@ const convertMongoUpdates = (docs: TldrawDrawing[]) => {
9797
if (!doc.part) {
9898
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
9999
// @ts-ignore
100-
updates.push(doc.value);
100+
updates.push(doc.value.buffer);
101101
} else if (doc.part === 1) {
102102
// merge the docs together that got split because of mongodb size limits
103-
const parts = [Buffer.from(doc.value)];
103+
const parts = [Buffer.from(doc.value.buffer)];
104104
let j;
105105
let currentPartId: number | undefined = doc.part;
106106
// eslint-disable-next-line no-plusplus
@@ -113,7 +113,7 @@ const convertMongoUpdates = (docs: TldrawDrawing[]) => {
113113
if (currentPartId !== part.part - 1) {
114114
throw new Error('Couldnt merge updates together because a part is missing!');
115115
}
116-
parts.push(Buffer.from(part.value));
116+
parts.push(Buffer.from(part.value.buffer));
117117
currentPartId = part.part;
118118
} else {
119119
break;

‎apps/server/src/modules/tldraw/repo/y-mongodb.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ export class YMongodb {
6565
};
6666
}
6767

68-
public async createIndex(): Promise<void> {
68+
async createIndex(): Promise<void> {
6969
const collection = this.adapter.getCollection();
7070
await collection.createIndex({
7171
version: 1,

0 commit comments

Comments
 (0)