Created
January 10, 2020 14:04
-
-
Save wtoalabi/9272f1937b931d469d5a60a41b3c310c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Init: | |
--------------------------------------------------------------- | |
let collection = await db.collection({ | |
name: 'contacts', | |
schema: contactsMeta.schema, | |
migrationStrategies: contactsMeta.migrations, | |
statics: contactsMeta.statics, | |
methods: contactsMeta.methods, | |
}); | |
middleware.preCreate(collection); | |
await store.commit("contactsInitialCollection", collection); | |
schema: | |
-------------------------------------- | |
schema: { | |
title: "contacts", | |
version: 0, | |
type: "object", | |
keyCompression: true, | |
properties: { | |
id: { | |
type: "string", | |
primary: true | |
}, | |
name: { | |
type: "string", | |
}, | |
email: { | |
type: "string", | |
}, | |
phones: { | |
type: "array", | |
items: { | |
type: "object" | |
} | |
}, | |
groups: { | |
type: "array", | |
items: { | |
type: "object" | |
} | |
}, | |
created_at: { | |
type: "string" | |
} | |
}, | |
}, | |
preCreate/insert (middleware): | |
------------------------------------------------------------- | |
collection.preInsert(function(data){ | |
data.created_at = Date.now().toString(); | |
data.id = uuid() | |
}, false); | |
collection.preSave(function(data){ | |
}, false); | |
insert: | |
---------------------------------------- | |
return collection.insert(data).then(response =>{ | |
if(response){ | |
return response.resource(); | |
} | |
}).catch(error=>{ | |
throw error; | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment