Skip to content

Function Count

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'
});
ReferenceLooks LikeValue
await factory.NewQuery().where('email', 'linbolen@gradii.com').doesntExist()exactly matchfalse
await factory.NewQuery().where('email', 'mohamed@laravel.com').doesntExist()exactly matchtrue
ReferenceLooks LikeValue
model.emailexactly match'linbolen@gradii.com'
model.email !== undefinedexactly matchtrue
ReferenceLooks LikeValue
friends !== undefinedexactly matchtrue
friendsmatch[]
ReferenceLooks LikeValue
modelinstance type exactly matchFedacoTestUser
model.idmatch1
ReferenceLooks LikeValue
modelinstance type exactly matchFedacoTestUser
model.idmatch2
ReferenceLooks LikeValue
missingexactly matchUndefined();
ReferenceLooks LikeValue
isArray(collection)exactly matchtrue
collection.lengthexactly match0
ReferenceLooks LikeValue
isArray(collection)exactly matchtrue
collection.lengthexactly match2
typescript
// .cursor();
for (const m of models) {
  expect(m.id).toEqual(1);
  expect(m.getConnectionName()).toBe('default');
}

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
);
ReferenceLooks LikeValue
user2._existsexactly matchtrue
user1.getConnectionName()exactly match'second_connection'
user2.getConnectionName()exactly match'second_connection'
typescript
user2 = await FedacoTestUser.useConnection('second_connection').firstOrNew({
  email: 'tony.stark@gradii.com'
});
ReferenceLooks LikeValue
user2._existsexactly matchtrue
user1.getConnectionName()exactly match'second_connection'
user2.getConnectionName()exactly match'second_connection'
await FedacoTestUser.useConnection('second_connection').count()match1
typescript
user2 = await FedacoTestUser.useConnection('second_connection').firstOrCreate({
  email: 'tony.stark@gradii.com'
});
ReferenceLooks LikeValue
user2.getConnectionName()exactly match'second_connection'
await FedacoTestUser.useConnection('second_connection').count()match2

see also prerequisites

count for pagination with grouping and sub selects

typescript
const user1 = await FedacoTestUser.createQuery().create({
  id: 1,
  email: 'linbolen@gradii.com'
});
await FedacoTestUser.createQuery().create({
  id: 2,
  email: 'xsilen@gradii.com'
});
await FedacoTestUser.createQuery().create({
  id: 3,
  email: 'foo@gmail.com'
});
await FedacoTestUser.createQuery().create({
  id: 4,
  email: 'foo@gmail.com'
});
const friendsRelation = user1.NewRelation('friends');
await friendsRelation.create({
  id: 5,
  email: 'friend@gmail.com'
});
const query = await FedacoTestUser.createQuery()
  .select({
    0: 'id',
    friends_count: await FedacoTestUser.createQuery()
      .whereColumn('friend_id', 'user_id')
      .count()
  })
  .groupBy('email')
  .getQuery();

see also prerequisites

multi inserts with different values

typescript
const date = '1970-01-01';
const result = await FedacoTestPost.createQuery().insert([
  {
    user_id: 1,
    name: 'Post',
    created_at: date,
    updated_at: date
  },
  {
    user_id: 2,
    name: 'Post',
    created_at: date,
    updated_at: date
  }
]);
ReferenceLooks LikeValue
await FedacoTestPost.createQuery().count()match2

see also prerequisites

multi inserts with same values

typescript
const date = '1970-01-01';
const result = await FedacoTestPost.createQuery().insert([
  {
    user_id: 1,
    name: 'Post',
    created_at: date,
    updated_at: date
  },
  {
    user_id: 1,
    name: 'Post',
    created_at: date,
    updated_at: date
  }
]);
ReferenceLooks LikeValue
await FedacoTestPost.createQuery().count()match2

see also prerequisites

save or fail

typescript
const date = '1970-01-01';
const post = FedacoTestPost.initAttributes({
  user_id: 1,
  name: 'Post',
  created_at: date,
  updated_at: date
});
ReferenceLooks LikeValue
await FedacoTestPost.createQuery().count()match1

see also prerequisites

update or create on different connection

typescript
await FedacoTestUser.createQuery().create({
  email: 'linbolen@gradii.com'
});
await FedacoTestUser.useConnection('second_connection').updateOrCreate(
  {
    email: 'linbolen@gradii.com'
  },
  {
    name: 'Taylor Otwell'
  }
);
await FedacoTestUser.useConnection('second_connection').updateOrCreate(
  {
    email: 'tony.stark@gradii.com'
  },
  {
    name: 'Mohamed Said'
  }
);
ReferenceLooks LikeValue
await FedacoTestUser.useConnection('second_connection').count()exactly match2

see also prerequisites

update or create

typescript
const user1 = await FedacoTestUser.createQuery().create({
  email: 'linbolen@gradii.com'
});
const user2 = await FedacoTestUser.createQuery().updateOrCreate(
  {
    email: 'linbolen@gradii.com'
  },
  {
    name: 'Taylor Otwell'
  }
);
ReferenceLooks LikeValue
user2.emailexactly match'linbolen@gradii.com'
user2.nameexactly match'Taylor Otwell'
ReferenceLooks LikeValue
user3.nameexactly match'Mohamed Said'
await FedacoTestUser.createQuery().count()exactly match2

see also prerequisites

Released under the MIT License.