Skip to content

Instantly share code, notes, and snippets.

@wtoalabi
Created January 10, 2020 14:04
Show Gist options
  • Save wtoalabi/9272f1937b931d469d5a60a41b3c310c to your computer and use it in GitHub Desktop.
Save wtoalabi/9272f1937b931d469d5a60a41b3c310c to your computer and use it in GitHub Desktop.
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