sync: make collection & db name usage more obvious

This commit is contained in:
Abdullah Atta committed 2023-09-09 20:28:46 +05:00
1 parent 8bbb4d0b9e
commit 55a7e9fd1c
12 files changed
+46 -79

No files matched your search

-2
View File
@@ -22,11 +22,9 @@ using System.Runtime.Serialization;
using System.Text.Json.Serialization; using System.Text.Json.Serialization;
using MongoDB.Bson; using MongoDB.Bson;
using MongoDB.Bson.Serialization.Attributes; using MongoDB.Bson.Serialization.Attributes;
using Streetwriters.Data.Attributes;
namespace Notesnook.API.Models namespace Notesnook.API.Models
{ {
[BsonCollection("notesnook", "announcements")]
public class Announcement public class Announcement
{ {
public Announcement() public Announcement()
-2
View File
@@ -21,11 +21,9 @@ using System.Text.Json.Serialization;
using MongoDB.Bson; using MongoDB.Bson;
using MongoDB.Bson.Serialization.Attributes; using MongoDB.Bson.Serialization.Attributes;
using Notesnook.API.Interfaces; using Notesnook.API.Interfaces;
using Streetwriters.Data.Attributes;
namespace Notesnook.API.Models namespace Notesnook.API.Models
{ {
[BsonCollection("notesnook", "monographs")]
public class Monograph : IMonograph public class Monograph : IMonograph
{ {
public Monograph() public Monograph()
+20 -11
View File
@@ -23,13 +23,14 @@ using System.Text.Json.Serialization;
using MongoDB.Bson; using MongoDB.Bson;
using MongoDB.Bson.Serialization.Attributes; using MongoDB.Bson.Serialization.Attributes;
using Notesnook.API.Interfaces; using Notesnook.API.Interfaces;
using Streetwriters.Data.Attributes;
namespace Notesnook.API.Models namespace Notesnook.API.Models
{ {
[MessagePack.MessagePackObject]
public class SyncItem : ISyncItem public class SyncItem : ISyncItem
{ {
[IgnoreDataMember] [IgnoreDataMember]
[MessagePack.IgnoreMember]
[JsonPropertyName("dateSynced")] [JsonPropertyName("dateSynced")]
public long DateSynced public long DateSynced
{ {
@@ -38,6 +39,7 @@ namespace Notesnook.API.Models
[DataMember(Name = "userId")] [DataMember(Name = "userId")]
[JsonPropertyName("userId")] [JsonPropertyName("userId")]
[MessagePack.Key("userId")]
public string UserId public string UserId
{ {
get; set; get; set;
@@ -45,6 +47,7 @@ namespace Notesnook.API.Models
[JsonPropertyName("iv")] [JsonPropertyName("iv")]
[DataMember(Name = "iv")] [DataMember(Name = "iv")]
[MessagePack.Key("iv")]
[Required] [Required]
public string IV public string IV
{ {
@@ -54,6 +57,7 @@ namespace Notesnook.API.Models
[JsonPropertyName("cipher")] [JsonPropertyName("cipher")]
[DataMember(Name = "cipher")] [DataMember(Name = "cipher")]
[MessagePack.Key("cipher")]
[Required] [Required]
public string Cipher public string Cipher
{ {
@@ -62,6 +66,7 @@ namespace Notesnook.API.Models
[DataMember(Name = "id")] [DataMember(Name = "id")]
[JsonPropertyName("id")] [JsonPropertyName("id")]
[MessagePack.Key("id")]
public string ItemId public string ItemId
{ {
get; set; get; set;
@@ -71,6 +76,7 @@ namespace Notesnook.API.Models
[BsonIgnoreIfDefault] [BsonIgnoreIfDefault]
[BsonRepresentation(BsonType.ObjectId)] [BsonRepresentation(BsonType.ObjectId)]
[JsonIgnore] [JsonIgnore]
[MessagePack.IgnoreMember]
public ObjectId Id public ObjectId Id
{ {
get; set; get; set;
@@ -78,6 +84,7 @@ namespace Notesnook.API.Models
[JsonPropertyName("length")] [JsonPropertyName("length")]
[DataMember(Name = "length")] [DataMember(Name = "length")]
[MessagePack.Key("length")]
[Required] [Required]
public long Length public long Length
{ {
@@ -86,6 +93,7 @@ namespace Notesnook.API.Models
[JsonPropertyName("v")] [JsonPropertyName("v")]
[DataMember(Name = "v")] [DataMember(Name = "v")]
[MessagePack.Key("v")]
[Required] [Required]
public double Version public double Version
{ {
@@ -94,6 +102,7 @@ namespace Notesnook.API.Models
[JsonPropertyName("alg")] [JsonPropertyName("alg")]
[DataMember(Name = "alg")] [DataMember(Name = "alg")]
[MessagePack.Key("alg")]
[Required] [Required]
public string Algorithm public string Algorithm
{ {
@@ -101,33 +110,33 @@ namespace Notesnook.API.Models
} = Algorithms.Default; } = Algorithms.Default;
} }
[BsonCollection("notesnook", "attachments")] [MessagePack.MessagePackObject]
public class Attachment : SyncItem { } public class Attachment : SyncItem { }
[BsonCollection("notesnook", "content")] [MessagePack.MessagePackObject]
public class Content : SyncItem { } public class Content : SyncItem { }
[BsonCollection("notesnook", "notes")] [MessagePack.MessagePackObject]
public class Note : SyncItem { } public class Note : SyncItem { }
[BsonCollection("notesnook", "notebooks")] [MessagePack.MessagePackObject]
public class Notebook : SyncItem { } public class Notebook : SyncItem { }
[BsonCollection("notesnook", "relations")] [MessagePack.MessagePackObject]
public class Relation : SyncItem { } public class Relation : SyncItem { }
[BsonCollection("notesnook", "reminders")] [MessagePack.MessagePackObject]
public class Reminder : SyncItem { } public class Reminder : SyncItem { }
[BsonCollection("notesnook", "settings")] [MessagePack.MessagePackObject]
public class Setting : SyncItem { } public class Setting : SyncItem { }
[BsonCollection("notesnook", "shortcuts")] [MessagePack.MessagePackObject]
public class Shortcut : SyncItem { } public class Shortcut : SyncItem { }
[BsonCollection("notesnook", "tags")] [MessagePack.MessagePackObject]
public class Tag : SyncItem { } public class Tag : SyncItem { }
[BsonCollection("notesnook", "colors")] [MessagePack.MessagePackObject]
public class Color : SyncItem { } public class Color : SyncItem { }
} }
-2
View File
@@ -20,11 +20,9 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
using MongoDB.Bson; using MongoDB.Bson;
using MongoDB.Bson.Serialization.Attributes; using MongoDB.Bson.Serialization.Attributes;
using Notesnook.API.Interfaces; using Notesnook.API.Interfaces;
using Streetwriters.Data.Attributes;
namespace Notesnook.API.Models namespace Notesnook.API.Models
{ {
[BsonCollection("notesnook", "user_settings")]
public class UserSettings : IUserSettings public class UserSettings : IUserSettings
{ {
public UserSettings() public UserSettings()
@@ -35,13 +35,13 @@ using Streetwriters.Data.Repositories;
namespace Notesnook.API.Repositories namespace Notesnook.API.Repositories
{ {
public class SyncItemsRepository<T> : Repository<T> where T : SyncItem public class SyncItemsRepository<T> : Repository<SyncItem> where T : SyncItem
{ {
public SyncItemsRepository(IDbContext dbContext) : base(dbContext) public SyncItemsRepository(IDbContext dbContext, string databaseName, string collectionName) : base(dbContext, databaseName, collectionName)
{ {
Collection.Indexes.CreateOne(new CreateIndexModel<T>(Builders<T>.IndexKeys.Ascending(i => i.UserId).Descending(i => i.DateSynced))); Collection.Indexes.CreateOne(new CreateIndexModel<SyncItem>(Builders<SyncItem>.IndexKeys.Ascending(i => i.UserId).Descending(i => i.DateSynced)));
Collection.Indexes.CreateOne(new CreateIndexModel<T>(Builders<T>.IndexKeys.Ascending(i => i.UserId).Ascending((i) => i.ItemId))); Collection.Indexes.CreateOne(new CreateIndexModel<SyncItem>(Builders<SyncItem>.IndexKeys.Ascending(i => i.UserId).Ascending((i) => i.ItemId)));
Collection.Indexes.CreateOne(new CreateIndexModel<T>(Builders<T>.IndexKeys.Ascending(i => i.UserId))); Collection.Indexes.CreateOne(new CreateIndexModel<SyncItem>(Builders<SyncItem>.IndexKeys.Ascending(i => i.UserId)));
} }
private readonly List<string> ALGORITHMS = new List<string> { Algorithms.Default }; private readonly List<string> ALGORITHMS = new List<string> { Algorithms.Default };
@@ -85,7 +85,7 @@ namespace Notesnook.API.Repositories
await base.UpsertAsync(item, (x) => (x.ItemId == item.ItemId) && x.UserId == userId); await base.UpsertAsync(item, (x) => (x.ItemId == item.ItemId) && x.UserId == userId);
} }
public void Upsert(T item, string userId, long dateSynced) public void Upsert(SyncItem item, string userId, long dateSynced)
{ {
if (item.Length > 15 * 1024 * 1024) if (item.Length > 15 * 1024 * 1024)
+16 -3
View File
@@ -179,8 +179,20 @@ namespace Notesnook.API
services.AddScoped<IDbContext, MongoDbContext>(); services.AddScoped<IDbContext, MongoDbContext>();
services.AddScoped<IUnitOfWork, UnitOfWork>(); services.AddScoped<IUnitOfWork, UnitOfWork>();
services.AddScoped(typeof(Repository<>));
services.AddScoped(typeof(SyncItemsRepository<>)); services.AddScoped((provider) => new Repository<UserSettings>(provider.GetRequiredService<IDbContext>(), "notesnook", "user_settings"));
services.AddScoped((provider) => new Repository<Monograph>(provider.GetRequiredService<IDbContext>(), "notesnook", "monographs"));
services.AddScoped((provider) => new Repository<Announcement>(provider.GetRequiredService<IDbContext>(), "notesnook", "announcements"));
services.AddScoped((provider) => new SyncItemsRepository<Attachment>(provider.GetRequiredService<IDbContext>(), "notesnook", "attachments"));
services.AddScoped((provider) => new SyncItemsRepository<Content>(provider.GetRequiredService<IDbContext>(), "notesnook", "content"));
services.AddScoped((provider) => new SyncItemsRepository<Note>(provider.GetRequiredService<IDbContext>(), "notesnook", "notes"));
services.AddScoped((provider) => new SyncItemsRepository<Notebook>(provider.GetRequiredService<IDbContext>(), "notesnook", "notebooks"));
services.AddScoped((provider) => new SyncItemsRepository<Relation>(provider.GetRequiredService<IDbContext>(), "notesnook", "relations"));
services.AddScoped((provider) => new SyncItemsRepository<Reminder>(provider.GetRequiredService<IDbContext>(), "notesnook", "reminders"));
services.AddScoped((provider) => new SyncItemsRepository<Setting>(provider.GetRequiredService<IDbContext>(), "notesnook", "settings"));
services.AddScoped((provider) => new SyncItemsRepository<Shortcut>(provider.GetRequiredService<IDbContext>(), "notesnook", "shortcuts"));
services.AddScoped((provider) => new SyncItemsRepository<Tag>(provider.GetRequiredService<IDbContext>(), "notesnook", "tags"));
services.AddScoped((provider) => new SyncItemsRepository<Color>(provider.GetRequiredService<IDbContext>(), "notesnook", "colors"));
services.TryAddTransient<ISyncItemsRepositoryAccessor, SyncItemsRepositoryAccessor>(); services.TryAddTransient<ISyncItemsRepositoryAccessor, SyncItemsRepositoryAccessor>();
services.TryAddTransient<IUserService, UserService>(); services.TryAddTransient<IUserService, UserService>();
@@ -192,8 +204,9 @@ namespace Notesnook.API
services.AddSignalR((hub) => services.AddSignalR((hub) =>
{ {
hub.MaximumReceiveMessageSize = 100 * 1024 * 1024; hub.MaximumReceiveMessageSize = 100 * 1024 * 1024;
hub.ClientTimeoutInterval = TimeSpan.FromMinutes(10);
hub.EnableDetailedErrors = true; hub.EnableDetailedErrors = true;
}).AddMessagePackProtocol(); }).AddMessagePackProtocol().AddJsonProtocol();
services.AddResponseCompression(options => services.AddResponseCompression(options =>
{ {
-2
View File
@@ -26,11 +26,9 @@ using MongoDB.Bson;
using MongoDB.Bson.Serialization.Attributes; using MongoDB.Bson.Serialization.Attributes;
using Streetwriters.Common.Enums; using Streetwriters.Common.Enums;
using Streetwriters.Common.Interfaces; using Streetwriters.Common.Interfaces;
using Streetwriters.Data.Attributes;
namespace Streetwriters.Common.Models namespace Streetwriters.Common.Models
{ {
[BsonCollection("subscriptions", "offers")]
public class Offer : IOffer public class Offer : IOffer
{ {
public Offer() public Offer()
-2
View File
@@ -20,11 +20,9 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
using AspNetCore.Identity.Mongo.Model; using AspNetCore.Identity.Mongo.Model;
using Streetwriters.Data.Attributes;
namespace Streetwriters.Common.Models namespace Streetwriters.Common.Models
{ {
[BsonCollection("identity", "roles")]
public class Role : MongoRole public class Role : MongoRole
{ {
// [DataMember(Name = "email")] // [DataMember(Name = "email")]
@@ -24,11 +24,9 @@ using MongoDB.Bson;
using MongoDB.Bson.Serialization.Attributes; using MongoDB.Bson.Serialization.Attributes;
using Streetwriters.Common.Enums; using Streetwriters.Common.Enums;
using Streetwriters.Common.Interfaces; using Streetwriters.Common.Interfaces;
using Streetwriters.Data.Attributes;
namespace Streetwriters.Common.Models namespace Streetwriters.Common.Models
{ {
[BsonCollection("subscriptions", "subscriptions")]
public class Subscription : ISubscription public class Subscription : ISubscription
{ {
public Subscription() public Subscription()
-2
View File
@@ -20,11 +20,9 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
using AspNetCore.Identity.Mongo.Model; using AspNetCore.Identity.Mongo.Model;
using Streetwriters.Data.Attributes;
namespace Streetwriters.Common.Models namespace Streetwriters.Common.Models
{ {
[BsonCollection("identity", "users")]
public class User : MongoUser public class User : MongoUser
{ {
} }
@@ -1,36 +0,0 @@
/*
This file is part of the Notesnook Sync Server project (https://notesnook.com/)
Copyright (C) 2023 Streetwriters (Private) Limited
This program is free software: you can redistribute it and/or modify
it under the terms of the Affero GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Affero GNU General Public License for more details.
You should have received a copy of the Affero GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
using System;
namespace Streetwriters.Data.Attributes
{
[AttributeUsage(AttributeTargets.Class, Inherited = false)]
public class BsonCollectionAttribute : Attribute
{
public string CollectionName { get; }
public string DatabaseName { get; }
public BsonCollectionAttribute(string databaseName, string collectionName)
{
CollectionName = collectionName;
DatabaseName = databaseName;
}
}
}
@@ -24,7 +24,6 @@ using System.Linq.Expressions;
using System.Threading.Tasks; using System.Threading.Tasks;
using MongoDB.Bson; using MongoDB.Bson;
using MongoDB.Driver; using MongoDB.Driver;
using Streetwriters.Data.Attributes;
using Streetwriters.Data.Interfaces; using Streetwriters.Data.Interfaces;
namespace Streetwriters.Data.Repositories namespace Streetwriters.Data.Repositories
@@ -34,19 +33,15 @@ namespace Streetwriters.Data.Repositories
protected readonly IDbContext dbContext; protected readonly IDbContext dbContext;
protected IMongoCollection<TEntity> Collection { get; set; } protected IMongoCollection<TEntity> Collection { get; set; }
public Repository(IDbContext _dbContext) public Repository(IDbContext _dbContext, string databaseName, string collectionName)
{ {
dbContext = _dbContext; dbContext = _dbContext;
Collection = GetCollection(); Collection = GetCollection(databaseName, collectionName);
} }
private protected IMongoCollection<TEntity> GetCollection() private protected IMongoCollection<TEntity> GetCollection(string databaseName, string collectionName)
{ {
var attribute = (BsonCollectionAttribute)typeof(TEntity).GetCustomAttributes( return dbContext.GetCollection<TEntity>(databaseName, collectionName);
typeof(BsonCollectionAttribute),
true).FirstOrDefault();
if (string.IsNullOrEmpty(attribute.CollectionName) || string.IsNullOrEmpty(attribute.DatabaseName)) throw new Exception("Could not get a valid collection or database name.");
return dbContext.GetCollection<TEntity>(attribute.DatabaseName, attribute.CollectionName);
} }