How to use @nozbe/watermelondb - 10 common examples

To help you get started, we’ve selected a few @nozbe/watermelondb examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github RocketChat / Rocket.Chat.ReactNative / app / lib / methods / updateMessages.js View on Github external
return db.action(async() => {
			const subCollection = db.collections.get('subscriptions');
			let sub;
			try {
				sub = await subCollection.find(rid);
			} catch (error) {
				sub = { id: rid };
				console.log('updateMessages: subscription not found');
			}

			const messagesIds = [...update.map(m => m._id), ...remove.map(m => m._id)];
			const msgCollection = db.collections.get('messages');
			const threadCollection = db.collections.get('threads');
			const threadMessagesCollection = db.collections.get('thread_messages');
			const allMessagesRecords = await msgCollection
				.query(Q.where('rid', rid), Q.where('id', Q.oneOf(messagesIds)))
				.fetch();
			const allThreadsRecords = await threadCollection
				.query(Q.where('rid', rid), Q.where('id', Q.oneOf(messagesIds)))
				.fetch();
			const allThreadMessagesRecords = await threadMessagesCollection
				.query(Q.where('subscription_id', rid), Q.where('id', Q.oneOf(messagesIds)))
				.fetch();

			update = update.map(m => buildMessage(m));

			// filter messages
			let msgsToCreate = update.filter(i1 => !allMessagesRecords.find(i2 => i1._id === i2.id));
			let msgsToUpdate = allMessagesRecords.filter(i1 => update.find(i2 => i1.id === i2._id));

			// filter threads
			const allThreads = update.filter(m => m.tlm);
github RocketChat / Rocket.Chat.ReactNative / app / lib / methods / updateMessages.js View on Github external
sub = { id: rid };
				console.log('updateMessages: subscription not found');
			}

			const messagesIds = [...update.map(m => m._id), ...remove.map(m => m._id)];
			const msgCollection = db.collections.get('messages');
			const threadCollection = db.collections.get('threads');
			const threadMessagesCollection = db.collections.get('thread_messages');
			const allMessagesRecords = await msgCollection
				.query(Q.where('rid', rid), Q.where('id', Q.oneOf(messagesIds)))
				.fetch();
			const allThreadsRecords = await threadCollection
				.query(Q.where('rid', rid), Q.where('id', Q.oneOf(messagesIds)))
				.fetch();
			const allThreadMessagesRecords = await threadMessagesCollection
				.query(Q.where('subscription_id', rid), Q.where('id', Q.oneOf(messagesIds)))
				.fetch();

			update = update.map(m => buildMessage(m));

			// filter messages
			let msgsToCreate = update.filter(i1 => !allMessagesRecords.find(i2 => i1._id === i2.id));
			let msgsToUpdate = allMessagesRecords.filter(i1 => update.find(i2 => i1.id === i2._id));

			// filter threads
			const allThreads = update.filter(m => m.tlm);
			let threadsToCreate = allThreads.filter(i1 => !allThreadsRecords.find(i2 => i1._id === i2.id));
			let threadsToUpdate = allThreadsRecords.filter(i1 => allThreads.find(i2 => i1.id === i2._id));

			// filter thread messages
			const allThreadMessages = update.filter(m => m.tmid);
			let threadMessagesToCreate = allThreadMessages.filter(i1 => !allThreadMessagesRecords.find(i2 => i1._id === i2.id));
github RocketChat / Rocket.Chat.ReactNative / app / lib / rocketchat.js View on Github external
let roomRoles = [];
		try {
			// get the room from database
			const room = await subsCollection.find(rid);
			// get room roles
			roomRoles = room.roles;
		} catch (error) {
			console.log('hasPermission -> Room not found');
			return permissions.reduce((result, permission) => {
				result[permission] = false;
				return result;
			}, {});
		}
		// get permissions from database
		try {
			const permissionsFiltered = await permissionsCollection.query(Q.where('id', Q.oneOf(permissions))).fetch();
			// get user roles on the server from redux
			const userRoles = (reduxStore.getState().login.user && reduxStore.getState().login.user.roles) || [];
			// merge both roles
			const mergedRoles = [...new Set([...roomRoles, ...userRoles])];

			// return permissions in object format
			// e.g. { 'edit-room': true, 'set-readonly': false }
			return permissions.reduce((result, permission) => {
				result[permission] = false;
				const permissionFound = permissionsFiltered.find(p => p.id === permission);
				if (permissionFound) {
					result[permission] = returnAnArray(permissionFound.roles).some(r => mergedRoles.includes(r));
				}
				return result;
			}, {});
		} catch (e) {
github RocketChat / Rocket.Chat.ReactNative / app / lib / methods / updateMessages.js View on Github external
try {
				sub = await subCollection.find(rid);
			} catch (error) {
				sub = { id: rid };
				console.log('updateMessages: subscription not found');
			}

			const messagesIds = [...update.map(m => m._id), ...remove.map(m => m._id)];
			const msgCollection = db.collections.get('messages');
			const threadCollection = db.collections.get('threads');
			const threadMessagesCollection = db.collections.get('thread_messages');
			const allMessagesRecords = await msgCollection
				.query(Q.where('rid', rid), Q.where('id', Q.oneOf(messagesIds)))
				.fetch();
			const allThreadsRecords = await threadCollection
				.query(Q.where('rid', rid), Q.where('id', Q.oneOf(messagesIds)))
				.fetch();
			const allThreadMessagesRecords = await threadMessagesCollection
				.query(Q.where('subscription_id', rid), Q.where('id', Q.oneOf(messagesIds)))
				.fetch();

			update = update.map(m => buildMessage(m));

			// filter messages
			let msgsToCreate = update.filter(i1 => !allMessagesRecords.find(i2 => i1._id === i2.id));
			let msgsToUpdate = allMessagesRecords.filter(i1 => update.find(i2 => i1.id === i2._id));

			// filter threads
			const allThreads = update.filter(m => m.tlm);
			let threadsToCreate = allThreads.filter(i1 => !allThreadsRecords.find(i2 => i1._id === i2.id));
			let threadsToUpdate = allThreadsRecords.filter(i1 => allThreads.find(i2 => i1.id === i2._id));
github RocketChat / Rocket.Chat.ReactNative / app / lib / database / models / Subscription.js View on Github external
@field('alert') alert

	@field('unread') unread

	@field('user_mentions') userMentions

	@field('ro') ro

	@date('last_open') lastOpen

	@field('description') description

	@field('announcement') announcement

	@field('topic') topic

	@field('blocked') blocked

	@field('blocker') blocker

	@field('react_when_read_only') reactWhenReadOnly

	@field('archived') archived

	@field('join_code_required') joinCodeRequired

	@field('notifications') notifications

	@field('broadcast') broadcast

	// // muted: { type: 'list', objectType: 'usersMuted' },
github RocketChat / Rocket.Chat.ReactNative / app / lib / database / models / Subscription.js View on Github external
@field('announcement') announcement

	@field('topic') topic

	@field('blocked') blocked

	@field('blocker') blocker

	@field('react_when_read_only') reactWhenReadOnly

	@field('archived') archived

	@field('join_code_required') joinCodeRequired

	@field('notifications') notifications

	@field('broadcast') broadcast

	// // muted: { type: 'list', objectType: 'usersMuted' },
	@date('room_updated_at') roomUpdatedAt
	// // lastMessage: { type: 'messages', optional: true },

	@lazy
	roles = this.collections
		.get('roles')
		.query(Q.on('subscriptions_roles', 'subscription_id', this.id));

	@children('subscriptions_roles') subscriptions_roles

	@action deleteRoles() {
		this.subscriptions_roles.destroyAllPermanently();
github RocketChat / Rocket.Chat.ReactNative / app / lib / database / models / Subscription.js View on Github external
@field('join_code_required') joinCodeRequired

	@field('notifications') notifications

	@field('broadcast') broadcast

	// // muted: { type: 'list', objectType: 'usersMuted' },
	@date('room_updated_at') roomUpdatedAt
	// // lastMessage: { type: 'messages', optional: true },

	@lazy
	roles = this.collections
		.get('roles')
		.query(Q.on('subscriptions_roles', 'subscription_id', this.id));

	@children('subscriptions_roles') subscriptions_roles

	@action deleteRoles() {
		this.subscriptions_roles.destroyAllPermanently();
	}

	@action addRole(roleId) {
		return this.collections.get('subscriptions_roles').create((sr) => {
			sr.subscriptionId = this.id;
			sr.roleId = roleId;
		});
	}
}
github AliAllaf / firemelon / src / utils / schema.ts View on Github external
export default function newDatabase() {
    const adapter = new LokiJSAdapter({
        schema,
    });
    const database = new Database({
        actionsEnabled: true,
        adapter,
        // @ts-ignore
        modelClasses: [Todo, User],
    });

    return database;
}
github RocketChat / Rocket.Chat.ReactNative / app / lib / database / helpers / messages.js View on Github external
await messageRecord.update((m) => {
				m._raw = sanitizedRaw({
					...m._raw,
					...message
				}, messagesCollection.schema);
				m.ts = message.ts;
			});
			// await action.subAction(() => messageRecord.deleteRoles());
github RocketChat / Rocket.Chat.ReactNative / app / lib / database / models / Subscription.js View on Github external
@field('archived') archived

	@field('join_code_required') joinCodeRequired

	@field('notifications') notifications

	@field('broadcast') broadcast

	// // muted: { type: 'list', objectType: 'usersMuted' },
	@date('room_updated_at') roomUpdatedAt
	// // lastMessage: { type: 'messages', optional: true },

	@lazy
	roles = this.collections
		.get('roles')
		.query(Q.on('subscriptions_roles', 'subscription_id', this.id));

	@children('subscriptions_roles') subscriptions_roles

	@action deleteRoles() {
		this.subscriptions_roles.destroyAllPermanently();
	}

	@action addRole(roleId) {
		return this.collections.get('subscriptions_roles').create((sr) => {
			sr.subscriptionId = this.id;
			sr.roleId = roleId;
		});
	}
}