Function Find
basic model retrieval
typescript
const factory = new FedacoTestUser();
await factory.NewQuery().create({
id: 1,
email: 'linbolen@gradii.com'
});
await factory.NewQuery().create({
id: 2,
email: 'xsilen@gradii.com'
});
Reference Looks Like Value await factory.NewQuery().where('email', 'linbolen@gradii.com').doesntExist()exactly match falseawait factory.NewQuery().where('email', 'mohamed@laravel.com').doesntExist()exactly match true
Reference Looks Like Value model.emailexactly match 'linbolen@gradii.com'model.email !== undefinedexactly match true
Reference Looks Like Value friends !== undefinedexactly match truefriendsmatch []
Reference Looks Like Value modelinstance type exactly match FedacoTestUsermodel.idmatch 1
Reference Looks Like Value modelinstance type exactly match FedacoTestUsermodel.idmatch 2
Reference Looks Like Value missingexactly match Undefined();
Reference Looks Like Value isArray(collection)exactly match truecollection.lengthexactly match 0
Reference Looks Like Value isArray(collection)exactly match truecollection.lengthexactly match 2
typescript
// .cursor();
for (const m of models) {
expect(m.id).toEqual(1);
expect(m.getConnectionName()).toBe('default');
}see also prerequisites
belongs to many custom pivot
typescript
const john = await FedacoTestUserWithCustomFriendPivot.createQuery().create({
id: 1,
name: 'John Doe',
email: 'johndoe@example.com'
});
const jane = await FedacoTestUserWithCustomFriendPivot.createQuery().create({
id: 2,
name: 'Jane Doe',
email: 'janedoe@example.com'
});
const jack = await FedacoTestUserWithCustomFriendPivot.createQuery().create({
id: 3,
name: 'Jack Doe',
email: 'jackdoe@example.com'
});
const jule = await FedacoTestUserWithCustomFriendPivot.createQuery().create({
id: 4,
name: 'Jule Doe',
email: 'juledoe@example.com'
});
await FedacoTestFriendLevel.createQuery().create({
id: 1,
level: 'acquaintance'
});
await FedacoTestFriendLevel.createQuery().create({
id: 2,
level: 'friend'
});
await FedacoTestFriendLevel.createQuery().create({
id: 3,
level: 'bff'
});
await john.NewRelation('friends').attach(jane, {
friend_level_id: 1
});
await john.NewRelation('friends').attach(jack, {
friend_level_id: 2
});
await john.NewRelation('friends').attach(jule, {
friend_level_id: 3
});
const johnWithFriends = await FedacoTestUserWithCustomFriendPivot.createQuery()
.with('friends')
.find(1);
Reference Looks Like Value await (await johnWithFriends.friends.find(it => it.id === 3).getAttribute( 'pivot').level).levelexactly match 'friend'(await johnWithFriends.friends.find(it => it.id === 4).getAttribute( 'pivot').friend).nameexactly match 'Jule Doe'
see also prerequisites
check and create methods on multi connections
typescript
await FedacoTestUser.createQuery().create({
id: 1,
email: 'linbolen@gradii.com'
});
await FedacoTestUser.useConnection('second_connection').find(
FedacoTestUser.useConnection('second_connection').insert({
id: 2,
email: 'tony.stark@gradii.com'
})
);
let user1 = await FedacoTestUser.useConnection('second_connection').findOrNew(
1
);
let user2 = await FedacoTestUser.useConnection('second_connection').findOrNew(
2
);
Reference Looks Like Value user2._existsexactly match trueuser1.getConnectionName()exactly match 'second_connection'user2.getConnectionName()exactly match 'second_connection'
typescript
user2 = await FedacoTestUser.useConnection('second_connection').firstOrNew({
email: 'tony.stark@gradii.com'
});
Reference Looks Like Value user2._existsexactly match trueuser1.getConnectionName()exactly match 'second_connection'user2.getConnectionName()exactly match 'second_connection'await FedacoTestUser.useConnection('second_connection').count()match 1
typescript
user2 = await FedacoTestUser.useConnection('second_connection').firstOrCreate({
email: 'tony.stark@gradii.com'
});
Reference Looks Like Value user2.getConnectionName()exactly match 'second_connection'await FedacoTestUser.useConnection('second_connection').count()match 2
see also prerequisites
is after retrieving the same model
typescript
const saved = await FedacoTestUser.createQuery().create({
id: 1,
email: 'linbolen@gradii.com'
});
const retrieved = await FedacoTestUser.createQuery().find(1);see also prerequisites